Mixin to provide standard implementations for nextPacketRange and nextPacketType.
More...
template<class Self, class Registry = void>
class senf::PacketTypeMixin< Self, Registry >
Mixin to provide standard implementations for nextPacketRange and nextPacketType.
This mixin class simplifies the definition of simple packets:
- The packets consist of three sections: The header, the payload and an optional trailer.
- If the packet has a trailer, both the header and the trailer must have a fixed size.
This mixin provides the nextPacketRange() member as well as initSize() and init(). If you additionally provide the optional Registry argument, PacketTypeMixin provides a simple implementation of nextPacketType().
When using the PacketTypeMixin, the implementation of a packet is simplified to:
struct SimplePacketType
{
typedef SomePacketParser parser;
using mixin::nextPacketRange;
using mixin::nextPacketType;
using mixin::initSize;
using mixin::init;
static key_t nextPacketKey(packet p)
{
return p->typeField();
}
static void finalize(packet p)
{
}
static void dump(packet p, std::ostream & os)
{
}
{
return header_size;
}
};
Most of the members are optional, which reduces the minimal implementation of a packet to:
struct SimplePacketType
{
typedef SomePacketParser parser;
using mixin::nextPacketRange;
using mixin::nextPacketType;
using mixin::initSize;
using mixin::init;
static key_t nextPacketKey(packet p) {
return p->typeField(); }
static void finalize(packet p) { p->typeField() <<
key(p.next(
senf::nothrow)); }
static void dump(packet p) {
}
};
Definition at line 300 of file PacketType.hh.
template<class Self, class Registry = void>
Find key of packet from registry.
key() will query the registry to find the key of the given packet. Whereas nextPacketKey()
as implemented by the mixin user will provide the registry key of the next packet from information stored in the current packets header, the key() member will look up the type of packet p in the registry and return it's key.
If either p is an in - valid() packet or the packet type is not found in the registry, the returned optional value will be empty.