30 #ifndef TINS_PDU_ALLOCATOR_H 31 #define TINS_PDU_ALLOCATOR_H 49 template<
typename PDUType>
50 PDU* default_allocator(
const uint8_t* buffer, uint32_t size) {
51 return new PDUType(buffer, size);
54 template<
typename Tag>
57 typedef typename Tag::identifier_type id_type;
58 typedef PDU *(*allocator_type)(
const uint8_t *, uint32_t);
60 template<
typename PDUType>
62 allocators[identifier] = &default_allocator<PDUType>;
63 pdu_types[PDUType::pdu_flag] = identifier;
66 static PDU* allocate(id_type identifier,
const uint8_t* buffer, uint32_t size) {
67 typename allocators_type::const_iterator it = allocators.find(identifier);
68 return (it == allocators.end()) ? 0 : (*it->second)(buffer, size);
72 return pdu_types.count(type) != 0;
76 typename pdu_map_types::const_iterator it = pdu_types.find(type);
80 typedef std::map<id_type, allocator_type> allocators_type;
81 typedef std::map<PDU::PDUType, id_type> pdu_map_types;
83 static allocators_type allocators;
84 static pdu_map_types pdu_types;
87 template<
typename Tag>
88 typename PDUAllocator<Tag>::allocators_type PDUAllocator<Tag>::allocators;
90 template<
typename Tag>
91 typename PDUAllocator<Tag>::pdu_map_types PDUAllocator<Tag>::pdu_types;
93 template<
typename IDType>
95 typedef IDType identifier_type;
98 template<
typename PDUType>
99 struct pdu_tag_mapper;
101 #define TINS_GENERATE_TAG_MAPPER(pdu, id_type) \ 103 struct pdu_tag_mapper<pdu> { \ 104 typedef pdu_tag<id_type> type; \ 107 TINS_GENERATE_TAG_MAPPER(EthernetII, uint16_t)
108 TINS_GENERATE_TAG_MAPPER(SNAP, uint16_t)
109 TINS_GENERATE_TAG_MAPPER(SLL, uint16_t)
110 TINS_GENERATE_TAG_MAPPER(Dot1Q, uint16_t)
111 TINS_GENERATE_TAG_MAPPER(IP, uint8_t)
112 TINS_GENERATE_TAG_MAPPER(IPv6, uint8_t)
114 #undef TINS_GENERATE_TAG_MAPPER 116 template<
typename PDUType>
117 PDU* allocate(
typename pdu_tag_mapper<PDUType>::type::identifier_type
id,
118 const uint8_t* buffer,
120 return PDUAllocator<typename pdu_tag_mapper<PDUType>::type>::allocate(
id, buffer, size);
123 template<
typename PDUType>
125 return PDUAllocator<typename pdu_tag_mapper<PDUType>::type>::pdu_type_registered(type);
128 template<
typename PDUType>
129 typename pdu_tag_mapper<PDUType>::type::identifier_type pdu_type_to_id(
PDU::PDUType type) {
130 return PDUAllocator<typename pdu_tag_mapper<PDUType>::type>::pdu_type_to_id(type);
141 namespace Allocators {
165 template<
typename PDUType,
typename AllocatedType>
167 Internals::PDUAllocator<
168 typename Internals::pdu_tag_mapper<PDUType>::type
169 >::template register_allocator<AllocatedType>(id);
175 #endif // TINS_PDU_ALLOCATOR_H PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:127
The Tins namespace.
Definition: address_range.h:38
void register_allocator(typename Internals::pdu_tag_mapper< PDUType >::type::identifier_type id)
Registers an allocator for the provided PDU type.
Definition: pdu_allocator.h:166