X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=senf%2FPackets%2FGenericTLV.hh;h=6f89e827c5a642cd119cbf662243ccd8c21748c5;hb=3fe57f6d48f9061fa70628ac832537de30d24b92;hp=894d728c026557f20f6120be71afbc72c0ff98f0;hpb=a79c5e98760ea8232c13d8266eb7ca0ac5cdefd3;p=senf.git diff --git a/senf/Packets/GenericTLV.hh b/senf/Packets/GenericTLV.hh index 894d728..6f89e82 100644 --- a/senf/Packets/GenericTLV.hh +++ b/senf/Packets/GenericTLV.hh @@ -35,6 +35,97 @@ namespace senf { + /** \brief Base class for generic TLV parsers + + This abstract base class can be used to define generic TLV parsers. The following + class structure is assumed: + \image html GenericTLV.png + + Your TLVParser base class has to define a \c type and a \c length field: + \code + struct MyTLVParserBase : public senf::PacketParserBase + { + # include SENF_PARSER() + SENF_PARSER_FIELD ( type, senf::UInt8Parser ); + SENF_PARSER_FIELD_RO ( length, senf::UInt8Parser ); + SENF_PARSER_FINALIZE ( MyTLVParserBase ); + }; + \endcode + + Your concrete TLV parsers will inherit from this base class and have to define a specific + value field and a \c TYPEID member: + \code + struct MyConcreteTLVParser : public MyTLVParserBase + { + # include SENF_PARSER() + SENF_PARSER_INHERIT ( MyTLVParserBase ); + SENF_PARSER_FIELD ( myValue, senf::UInt32Parser ); + SENF_PARSER_FINALIZE ( MyConcreteTLVParser ); + + SENF_PARSER_INIT() { + type() = TYPEID; + length_() = 4; + } + static const type_t::value_type TYPEID = 0x42; + }; + \endcode + + With GenericTLVParserBase you can define a generic parser class which provides + members to access the value data and and to cast the parser to a concrete tlv + parser: + \code + struct MyGenericTLVParser : public senf::GenericTLVParserBase + { + typedef senf::GenericTLVParserBase base; + MyGenericTLVParser(data_iterator i, state_type s) : base(i,s) {} + + // members for your generic TLV parser... + }; + \endcode + + If your generic TLV parser just inherits from GenericTLVParserBase and doesn't + add any additional functionality you can use a simple \c typedef as well: + \code + typedef senf::GenericTLVParserBase MyGenericTLVParser; + \endcode + + This generiv tlv parser can now be used for example in a list: + \code + class MyTestPacketParser : public senf::PacketParserBase + { + # include SENF_PARSER() + SENF_PARSER_FIELD_RO ( list_length, senf::UInt8Parser ); + SENF_PARSER_LIST ( tlv_list, list_length, MyGenericTLVParser ); + SENF_PARSER_FINALIZE ( MyTestPacketParser ); + }; + \endcode + + Now, you can access the TLV parsers in the list in a generic way or you + can cast the parsers to some concrete tlv parser: + \code + MyTestPacket p (... + typedef MyTestPacket::Parser::tlv_list_t::container container_t; + container_t tlvContainer (p->tlv_list() ); + optContainer_t::iterator listIter (tlvContainer.begin()); + + // listIter points to a MyGenericTLVParser, so you have generic access: + listIter->type() = 0x42; + listIter->value( someRangeOfValueData); + + // cast to an instance of MyConcreteTLVParser: + if (listIter->is()) { + MyConcreteTLVParser concreteTLVParser ( listIter->as()); + concreteTLVParser.myValue() = 0xabababab; + } + + // add a MyConcreteTLV to the list: + MyConcreteTLVParser tlv ( tlvContainer.push_back_space().init()); + tlv.myValue() = 0xffff; + \endcode + + \see + IPv6GenericOptionTLVParser, WLANGenericInfoElementParser, MIHGenericTLVParser + */ template class GenericTLVParserBase : public Base { @@ -49,13 +140,14 @@ namespace senf { Parser init(); template - Parser as(); + Parser as() const; template - bool is(); + bool is() const; senf::PacketInterpreterBase::range value() const; +#ifndef DOXYGEN template void value(ForwardReadableRange const & val, typename boost::disable_if >::type * = 0); @@ -67,10 +159,20 @@ namespace senf { template void value(std::pair const & val, typename boost::enable_if >::type * = 0); - +#else + template + void value(ForwardReadableRange const & val); + + template + void value(std::pair const & val); +#endif + private: template void value_(ForwardReadableRange const &range); + + Base & self(); + Base const & self() const; }; }