# PacketFactory

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

# PacketFactory

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

class PacketFactory
```

[Defined in src/base/include/icy/packetfactory.h:95](https://github.com/auscaster/icey/blob/main/src/base/include/icy/packetfactory.h#L95)

Priority-ordered factory that creates typed packets from raw buffers using registered strategies.

## Public Methods

| Return | Name | Description |
|--------|------|-------------|
|  | [`PacketFactory`](#packetfactory-1)  | Defaulted constructor. |
|  | [`PacketFactory`](#packetfactory-2)  | Deleted constructor. |
| [`PacketFactory`](#packetfactory) & | [`operator=`](#operator-67)  | Deleted assignment operator. |
|  | [`PacketFactory`](#packetfactory-3)  | Defaulted constructor. |
| [`PacketFactory`](#packetfactory) & | [`operator=`](#operator-68)  | Defaulted assignment operator. |
|  | [`~PacketFactory`](#packetfactory-4) `virtual` | Defaulted destructor. |
| `void` | [`registerPacketType`](#registerpackettype) `inline` | Registers a `PacketCreationStrategy<PacketT>` at the given priority. Any previously registered strategy for `PacketT` is replaced. |
| `void` | [`unregisterPacketType`](#unregisterpackettype) `inline` | Removes the `PacketCreationStrategy<PacketT>` from the factory, if present. |
| `void` | [`registerStrategy`](#registerstrategy) `inline` | Registers an arbitrary [`IPacketCreationStrategy`](https://auscaster.com/icey/docs/api-reference/icy-IPacketCreationStrategy.md#ipacketcreationstrategy) subclass at the given priority. Any previously registered instance of the same type is replaced. |
| `void` | [`unregisterStrategy`](#unregisterstrategy) `inline` | Removes the `StrategyT` instance from the factory, if present. |
| [`PacketCreationStrategyList`](https://auscaster.com/icey/docs/api-reference/base.md#packetcreationstrategylist) & | [`types`](#types-1) `inline` |  |
| const [`PacketCreationStrategyList`](https://auscaster.com/icey/docs/api-reference/base.md#packetcreationstrategylist) & | [`types`](#types-2) `const` `inline` |  |
| `bool` | [`onPacketCreated`](#onpacketcreated) `virtual` `inline` | Called after a packet is successfully created by a strategy. Override to apply filtering; return false to reject the packet (it will be deleted). |
| [`IPacket`](https://auscaster.com/icey/docs/api-reference/icy-IPacket.md#ipacket) * | [`createPacket`](#createpacket) `virtual` `inline` | Iterates registered strategies in priority order and returns the first successfully created packet. |

---

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

### PacketFactory

```cpp
PacketFactory() = default
```

[Defined in src/base/include/icy/packetfactory.h:98](https://github.com/auscaster/icey/blob/main/src/base/include/icy/packetfactory.h#L98)

Defaulted constructor.

---

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

### PacketFactory

```cpp
PacketFactory(const PacketFactory &) = delete
```

[Defined in src/base/include/icy/packetfactory.h:99](https://github.com/auscaster/icey/blob/main/src/base/include/icy/packetfactory.h#L99)

Deleted constructor.

---

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

### operator=

```cpp
PacketFactory & operator=(const PacketFactory &) = delete
```

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

Deleted assignment operator.

---

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

### PacketFactory

```cpp
PacketFactory(PacketFactory &&) = default
```

[Defined in src/base/include/icy/packetfactory.h:101](https://github.com/auscaster/icey/blob/main/src/base/include/icy/packetfactory.h#L101)

Defaulted constructor.

---

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

### operator=

```cpp
PacketFactory & operator=(PacketFactory &&) = default
```

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

Defaulted assignment operator.

---

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

### ~PacketFactory

`virtual`

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

[Defined in src/base/include/icy/packetfactory.h:103](https://github.com/auscaster/icey/blob/main/src/base/include/icy/packetfactory.h#L103)

Defaulted destructor.

---

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

### registerPacketType

`inline`

```cpp
template<class PacketT> inline void registerPacketType(int priority)
```

[Defined in src/base/include/icy/packetfactory.h:110](https://github.com/auscaster/icey/blob/main/src/base/include/icy/packetfactory.h#L110)

Registers a `PacketCreationStrategy<PacketT>` at the given priority. Any previously registered strategy for `PacketT` is replaced.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `priority` | `int` | Dispatch priority (0–100; higher runs first). |

---

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

### unregisterPacketType

`inline`

```cpp
template<class PacketT> inline void unregisterPacketType()
```

[Defined in src/base/include/icy/packetfactory.h:120](https://github.com/auscaster/icey/blob/main/src/base/include/icy/packetfactory.h#L120)

Removes the `PacketCreationStrategy<PacketT>` from the factory, if present.

---

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

### registerStrategy

`inline`

```cpp
template<class StrategyT> inline void registerStrategy(int priority)
```

[Defined in src/base/include/icy/packetfactory.h:134](https://github.com/auscaster/icey/blob/main/src/base/include/icy/packetfactory.h#L134)

Registers an arbitrary [`IPacketCreationStrategy`](https://auscaster.com/icey/docs/api-reference/icy-IPacketCreationStrategy.md#ipacketcreationstrategy) subclass at the given priority. Any previously registered instance of the same type is replaced.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `priority` | `int` | Dispatch priority (0–100; higher runs first). |

---

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

### unregisterStrategy

`inline`

```cpp
template<class StrategyT> inline void unregisterStrategy()
```

[Defined in src/base/include/icy/packetfactory.h:144](https://github.com/auscaster/icey/blob/main/src/base/include/icy/packetfactory.h#L144)

Removes the `StrategyT` instance from the factory, if present.

---

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

### types

`inline`

```cpp
inline PacketCreationStrategyList & types()
```

[Defined in src/base/include/icy/packetfactory.h:154](https://github.com/auscaster/icey/blob/main/src/base/include/icy/packetfactory.h#L154)

#### Returns
Mutable reference to the ordered list of registered strategies.

---

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

### types

`const` `inline`

```cpp
inline const PacketCreationStrategyList & types() const
```

[Defined in src/base/include/icy/packetfactory.h:160](https://github.com/auscaster/icey/blob/main/src/base/include/icy/packetfactory.h#L160)

#### Returns
Const reference to the ordered list of registered strategies.

---

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

### onPacketCreated

`virtual` `inline`

```cpp
virtual inline bool onPacketCreated(IPacket * packet)
```

[Defined in src/base/include/icy/packetfactory.h:169](https://github.com/auscaster/icey/blob/main/src/base/include/icy/packetfactory.h#L169)

Called after a packet is successfully created by a strategy. Override to apply filtering; return false to reject the packet (it will be deleted). 
#### Returns
True to accept the packet, false to discard it and try the next strategy.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `packet` | [`IPacket`](https://auscaster.com/icey/docs/api-reference/icy-IPacket.md#ipacket) * | Newly created packet. |

---

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

### createPacket

`virtual` `inline`

```cpp
virtual inline IPacket * createPacket(const ConstBuffer & buffer, size_t & nread)
```

[Defined in src/base/include/icy/packetfactory.h:181](https://github.com/auscaster/icey/blob/main/src/base/include/icy/packetfactory.h#L181)

Iterates registered strategies in priority order and returns the first successfully created packet. 
#### Returns
Newly allocated packet, or nullptr if no strategy matched.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `buffer` | const [`ConstBuffer`](https://auscaster.com/icey/docs/api-reference/icy-ConstBuffer.md#constbuffer-6) & | Raw input data. |
| `nread` | `size_t &` | Set to the number of bytes consumed on success. |

#### Exceptions

| Exception | Description |
|-----------|-------------|
| `std::logic_error` | if no packet types have been registered. |

## Protected Attributes

| Return | Name | Description |
|--------|------|-------------|
| [`PacketCreationStrategyList`](https://auscaster.com/icey/docs/api-reference/base.md#packetcreationstrategylist) | [`_types`](#_types-1)  |  |

---

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

### _types

```cpp
PacketCreationStrategyList _types
```

Type: [`PacketCreationStrategyList`](https://auscaster.com/icey/docs/api-reference/base.md#packetcreationstrategylist)

[Defined in src/base/include/icy/packetfactory.h:200](https://github.com/auscaster/icey/blob/main/src/base/include/icy/packetfactory.h#L200)

## Private Methods

| Return | Name | Description |
|--------|------|-------------|
| `void` | [`sortTypes`](#sorttypes) `inline` |  |

---

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

### sortTypes

`inline`

```cpp
inline void sortTypes()
```

[Defined in src/base/include/icy/packetfactory.h:203](https://github.com/auscaster/icey/blob/main/src/base/include/icy/packetfactory.h#L203)
