# Stateful

<a id="stateful"></a>

# Stateful

```cpp
#include <icy/stateful.h>

template<typename T>
class Stateful
```

[Defined in src/base/include/icy/stateful.h:97](https://github.com/auscaster/icey/blob/main/src/base/include/icy/stateful.h#L97)

[State](https://auscaster.com/icey/docs/api-reference/icy-State.md#state) machine implementation.

For an example **See also**: [PacketStream](https://auscaster.com/icey/docs/api-reference/icy-PacketStream.md#packetstream)

#### Template Parameters
* `The` derived [`State`](https://auscaster.com/icey/docs/api-reference/icy-State.md#state) type.

## Public Attributes

| Return | Name | Description |
|--------|------|-------------|
| [`Signal`](https://auscaster.com/icey/docs/api-reference/icy-Signal.md#signal)< void(void *, T &, const T &)> | [`StateChange`](#statechange)  | Signals when the state changes. |

---

<a id="statechange"></a>

### StateChange

```cpp
Signal< void(void *, T &, const T &)> StateChange
```

Type: [`Signal`](https://auscaster.com/icey/docs/api-reference/icy-Signal.md#signal)< void(void *, T &, const T &)>

[Defined in src/base/include/icy/stateful.h:130](https://github.com/auscaster/icey/blob/main/src/base/include/icy/stateful.h#L130)

Signals when the state changes.

## Public Methods

| Return | Name | Description |
|--------|------|-------------|
|  | [`Stateful`](#stateful-1) `inline` |  |
|  | [`~Stateful`](#stateful-2) `virtual` `inline` |  |
| `bool` | [`stateEquals`](#stateequals) `virtual` `const` `inline` | Returns true if the current state ID equals the given ID. |
| `bool` | [`stateBetween`](#statebetween) `virtual` `const` `inline` | Returns true if the current state ID is in the inclusive range [lid, rid]. |
| `T &` | [`state`](#state-4) `virtual` `inline` | Returns a mutable reference to the current state. |
| `const T` | [`state`](#state-5) `virtual` `const` `inline` | Returns a copy of the current state. |

---

<a id="stateful-1"></a>

### Stateful

`inline`

```cpp
inline Stateful()
```

[Defined in src/base/include/icy/stateful.h:100](https://github.com/auscaster/icey/blob/main/src/base/include/icy/stateful.h#L100)

---

<a id="stateful-2"></a>

### ~Stateful

`virtual` `inline`

```cpp
virtual inline ~Stateful()
```

[Defined in src/base/include/icy/stateful.h:102](https://github.com/auscaster/icey/blob/main/src/base/include/icy/stateful.h#L102)

---

<a id="stateequals"></a>

### stateEquals

`virtual` `const` `inline`

```cpp
virtual inline bool stateEquals(typename T::ID id) const
```

[Defined in src/base/include/icy/stateful.h:107](https://github.com/auscaster/icey/blob/main/src/base/include/icy/stateful.h#L107)

Returns true if the current state ID equals the given ID. 
#### Returns
True if the current state matches.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `id` | `typename T::ID` | [State](https://auscaster.com/icey/docs/api-reference/icy-State.md#state) ID to compare against. |

---

<a id="statebetween"></a>

### stateBetween

`virtual` `const` `inline`

```cpp
virtual inline bool stateBetween(typename T::ID lid, typename T::ID rid) const
```

[Defined in src/base/include/icy/stateful.h:116](https://github.com/auscaster/icey/blob/main/src/base/include/icy/stateful.h#L116)

Returns true if the current state ID is in the inclusive range [lid, rid]. 
#### Returns
True if lid <= state.id() <= rid.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `lid` | `typename T::ID` | Lower bound state ID. |
| `rid` | `typename T::ID` | Upper bound state ID. |

---

<a id="state-4"></a>

### state

`virtual` `inline`

```cpp
virtual inline T & state()
```

[Defined in src/base/include/icy/stateful.h:123](https://github.com/auscaster/icey/blob/main/src/base/include/icy/stateful.h#L123)

Returns a mutable reference to the current state. 
#### Returns
Reference to the internal state object.

---

<a id="state-5"></a>

### state

`virtual` `const` `inline`

```cpp
virtual inline const T state() const
```

[Defined in src/base/include/icy/stateful.h:127](https://github.com/auscaster/icey/blob/main/src/base/include/icy/stateful.h#L127)

Returns a copy of the current state. 
#### Returns
Current state value.

## Protected Attributes

| Return | Name | Description |
|--------|------|-------------|
| `T` | [`_state`](#_state)  |  |

---

<a id="_state"></a>

### _state

```cpp
T _state
```

[Defined in src/base/include/icy/stateful.h:170](https://github.com/auscaster/icey/blob/main/src/base/include/icy/stateful.h#L170)

## Protected Methods

| Return | Name | Description |
|--------|------|-------------|
| `bool` | [`beforeStateChange`](#beforestatechange) `virtual` `inline` | Override to handle pre state change logic. Return false to prevent state change. |
| `void` | [`onStateChange`](#onstatechange-2) `virtual` `inline` | Override to handle post state change logic. |
| `bool` | [`setState`](#setstate) `virtual` `inline` | Sets the state and sends the state signal if the state change was successful. |
| `bool` | [`setState`](#setstate-1) `virtual` `inline` | Sets the state and sends the state signal if the state change was successful. |

---

<a id="beforestatechange"></a>

### beforeStateChange

`virtual` `inline`

```cpp
virtual inline bool beforeStateChange(const T & state)
```

[Defined in src/base/include/icy/stateful.h:135](https://github.com/auscaster/icey/blob/main/src/base/include/icy/stateful.h#L135)

Override to handle pre state change logic. Return false to prevent state change.

---

<a id="onstatechange-2"></a>

### onStateChange

`virtual` `inline`

```cpp
virtual inline void onStateChange(T &, const T &)
```

[Defined in src/base/include/icy/stateful.h:143](https://github.com/auscaster/icey/blob/main/src/base/include/icy/stateful.h#L143)

Override to handle post state change logic.

#### Reimplemented by

- [`PacketStream::onStateChange`](https://auscaster.com/icey/docs/api-reference/icy-PacketStream.md#onstatechange)
- [`PacketTransaction::onStateChange`](https://auscaster.com/icey/docs/api-reference/icy-PacketTransaction.md#onstatechange-1)
- [`PacketTransaction::onStateChange`](https://auscaster.com/icey/docs/api-reference/icy-PacketTransaction.md#onstatechange-1)

---

<a id="setstate"></a>

### setState

`virtual` `inline`

```cpp
virtual inline bool setState(void * sender, typename T::ID id)
```

[Defined in src/base/include/icy/stateful.h:147](https://github.com/auscaster/icey/blob/main/src/base/include/icy/stateful.h#L147)

Sets the state and sends the state signal if the state change was successful.

---

<a id="setstate-1"></a>

### setState

`virtual` `inline`

```cpp
virtual inline bool setState(void * sender, const T & state)
```

[Defined in src/base/include/icy/stateful.h:156](https://github.com/auscaster/icey/blob/main/src/base/include/icy/stateful.h#L156)

Sets the state and sends the state signal if the state change was successful.
