# State

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

# State

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

class State
```

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

> **Subclassed by:** [`DiagnosticState`](https://auscaster.com/icey/docs/api-reference/icy-DiagnosticState.md#diagnosticstate), [`PacketStreamState`](https://auscaster.com/icey/docs/api-reference/icy-PacketStreamState.md#packetstreamstate), [`TransactionState`](https://auscaster.com/icey/docs/api-reference/icy-TransactionState.md#transactionstate), [`EncoderState`](https://auscaster.com/icey/docs/api-reference/icy-av-EncoderState.md#encoderstate), [`InstallationState`](https://auscaster.com/icey/docs/api-reference/icy-pacm-InstallationState.md#installationstate), [`ClientState`](https://auscaster.com/icey/docs/api-reference/icy-smpl-ClientState.md#clientstate-1), [`ClientState`](https://auscaster.com/icey/docs/api-reference/icy-turn-ClientState.md#clientstate)

[State](#state) class for state machines.

This class defines the state for a state machine, and should be extended and passed to implementations of the [`Stateful`](https://auscaster.com/icey/docs/api-reference/icy-Stateful.md#stateful) template.

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

## Friends

| Name | Description |
|------|-------------|
| [`operator<<`](#operator-91) `inline` |  |

---

<a id="operator-91"></a>

### operator<<

`inline`

```cpp
friend inline std::ostream & operator<<(std::ostream & os, const State & state)
```

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

## Public Methods

| Return | Name | Description |
|--------|------|-------------|
|  | [`State`](#state-1)  |  |
|  | [`State`](#state-2)  | Copy constructor. |
| [`State`](#state) & | [`operator=`](#operator-92)  | Copy assignment. |
|  | [`~State`](#state-3) `virtual` | Defaulted destructor. |
| [`ID`](#id-1) | [`id`](#id) `const` | Returns the current state ID. |
| `void` | [`set`](#set-2)  | Sets the state ID. |
| `std::string` | [`str`](#str-4) `virtual` `const` | Returns a human-readable string for the given state ID. Override in derived classes to provide meaningful names. |
| `std::string` | [`toString`](#tostring-2) `virtual` `const` | Returns a human-readable string for the current state ID. |
| `bool` | [`equals`](#equals) `const` | Returns true if the current state ID equals the given ID. |
| `bool` | [`between`](#between) `const` | Returns true if the current state ID is in the inclusive range [lid, rid]. |
| `bool` | [`operator==`](#operator-93) `const` `inline` |  |
| `bool` | [`operator==`](#operator-94) `const` `inline` |  |

---

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

### State

```cpp
State(ID id = 0)
```

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

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `id` | [`ID`](#id-1) | Initial state ID. Defaults to 0. |

---

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

### State

```cpp
State(const State & that)
```

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

Copy constructor.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `that` | const [`State`](#state) & | [State](#state) to copy from. |

---

<a id="operator-92"></a>

### operator=

```cpp
State & operator=(const State & that)
```

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

Copy assignment.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `that` | const [`State`](#state) & | [State](#state) to assign from. |

---

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

### ~State

`virtual`

```cpp
virtual ~State() = default
```

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

Defaulted destructor.

---

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

### id

`const`

```cpp
ID id() const
```

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

Returns the current state ID. 
#### Returns
Atomic state ID value.

---

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

### set

```cpp
void set(ID id)
```

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

Sets the state ID.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `id` | [`ID`](#id-1) | New state ID to assign. |

---

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

### str

`virtual` `const`

```cpp
virtual std::string str(ID id) const
```

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

Returns a human-readable string for the given state ID. Override in derived classes to provide meaningful names. 
#### Returns
String representation of the state, or "undefined" by default.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `id` | [`ID`](#id-1) | [State](#state) ID to convert. |

---

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

### toString

`virtual` `const`

```cpp
virtual std::string toString() const
```

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

Returns a human-readable string for the current state ID. 
#### Returns
Result of `str(id())`.

#### Reimplemented by

- [`ClientState::toString`](https://auscaster.com/icey/docs/api-reference/icy-turn-ClientState.md#tostring-9)

---

<a id="equals"></a>

### equals

`const`

```cpp
bool equals(ID id) const
```

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

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

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `id` | [`ID`](#id-1) | [State](#state) ID to compare against. |

---

<a id="between"></a>

### between

`const`

```cpp
bool between(ID lid, ID rid) const
```

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

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

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `lid` | [`ID`](#id-1) | Lower bound state ID. |
| `rid` | [`ID`](#id-1) | Upper bound state ID. |

---

<a id="operator-93"></a>

### operator==

`const` `inline`

```cpp
inline bool operator==(const State & that) const
```

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

---

<a id="operator-94"></a>

### operator==

`const` `inline`

```cpp
inline bool operator==(const State::ID & that) const
```

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

## Protected Attributes

| Return | Name | Description |
|--------|------|-------------|
| std::atomic< [`ID`](#id-1) > | [`_id`](#_id)  |  |

---

<a id="_id"></a>

### _id

```cpp
std::atomic< ID > _id
```

Type: std::atomic< [`ID`](#id-1) >

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

## Public Types

| Name | Description |
|------|-------------|
| [`ID`](#id-1)  |  |

---

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

### ID

```cpp
using ID = uint32_t
```

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