4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Thorsten Horstmann <tho@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief GenericTLV public header */
26 #ifndef HH_SENF_Packets_GenericTLV_
27 #define HH_SENF_Packets_GenericTLV_ 1
30 #include <boost/ptr_container/ptr_map.hpp>
31 #include <senf/Packets/Packets.hh>
32 #include <senf/Utils/type_traits.hh>
33 #include <senf/Utils/singleton.hh>
35 //#include "GenericTLV.hh.mpp"
36 ///////////////////////////////hh.p////////////////////////////////////////
40 /** \brief Base class for generic TLV parsers
42 This abstract base class can be used to define generic TLV parsers. The following
43 class structure is assumed:
44 \image html GenericTLV.png
46 Your TLVParser base class has to define a \c type and a \c length field:
48 struct MyTLVParserBase : public senf::PacketParserBase
50 # include SENF_PARSER()
51 SENF_PARSER_FIELD ( type, senf::UInt8Parser );
52 SENF_PARSER_FIELD_RO ( length, senf::UInt8Parser );
53 SENF_PARSER_FINALIZE ( MyTLVParserBase );
57 Your concrete TLV parsers will inherit from this base class and have to define a specific
58 value field and a \c typeId member:
60 struct MyConcreteTLVParser : public MyTLVParserBase
62 # include SENF_PARSER()
63 SENF_PARSER_INHERIT ( MyTLVParserBase );
64 SENF_PARSER_FIELD ( myValue, senf::UInt32Parser );
65 SENF_PARSER_FINALIZE ( MyConcreteTLVParser );
71 static const type_t::value_type typeId = 0x42;
75 With GenericTLVParserBase you can define a generic parser class which provides
76 members to access the value data and and to cast the parser to a concrete tlv
79 struct MyGenericTLVParser : public senf::GenericTLVParserBase<MyTLVParserBase>
81 typedef senf::GenericTLVParserBase<MyTLVParserBase> base;
82 MyGenericTLVParser(data_iterator i, state_type s) : base(i,s) {}
84 // members for your generic TLV parser...
88 If your generic TLV parser just inherits from GenericTLVParserBase and doesn't
89 add any additional functionality you can use a simple \c typedef as well:
91 typedef senf::GenericTLVParserBase<MyTLVParserBase> MyGenericTLVParser;
94 This generic tlv parser can now be used for example in a list:
96 class MyTestPacketParser : public senf::PacketParserBase
98 # include SENF_PARSER()
99 SENF_PARSER_FIELD_RO ( list_length, senf::UInt8Parser );
100 SENF_PARSER_LIST ( tlv_list, list_length, MyGenericTLVParser );
101 SENF_PARSER_FINALIZE ( MyTestPacketParser );
105 Now, you can access the TLV parsers in the list in a generic way or you
106 can cast the parsers to some concrete tlv parser:
109 typedef MyTestPacket::Parser::tlv_list_t::container container_t;
110 container_t tlvContainer (p->tlv_list() );
111 optContainer_t::iterator listIter (tlvContainer.begin());
113 // listIter points to a MyGenericTLVParser, so you have generic access:
114 listIter->type() = 0x42;
115 listIter->value( someRangeOfValueData);
117 // cast to an instance of MyConcreteTLVParser:
118 if (listIter->is<MyConcreteTLVParser>()) {
119 MyConcreteTLVParser concreteTLVParser ( listIter->as<MyConcreteTLVParser>());
120 concreteTLVParser.myValue() = 0xabababab;
123 // add a MyConcreteTLV to the list:
124 MyConcreteTLVParser tlv ( tlvContainer.push_back_space().init<MyConcreteTLVParser>());
125 tlv.myValue() = 0xffff;
129 IPv6GenericOptionTLVParser, WLANGenericInfoElementParser, MIHGenericTLVParser
131 template <class Base>
132 class GenericTLVParserBase : public Base
135 GenericTLVParserBase(senf::PacketParserBase::data_iterator i, senf::PacketParserBase::state_type s)
138 senf::PacketParserBase::size_type bytes();
141 template <class Parser>
144 template <class Parser>
147 template <class Parser>
150 senf::PacketInterpreterBase::range value() const;
152 void dump(std::ostream & os) const;
155 template<class ForwardReadableRange>
156 void value(ForwardReadableRange const & val,
157 typename boost::disable_if<senf::is_pair<ForwardReadableRange> >::type * = 0);
159 template<class First, class Second>
160 void value(std::pair<First, Second> const & val,
161 typename boost::disable_if<boost::is_convertible<First, typename Base::type_t::value_type> >::type * = 0);
163 template <class Type, class ForwardReadableRange>
164 void value(std::pair<Type, ForwardReadableRange> const & val,
165 typename boost::enable_if<boost::is_convertible<Type, typename Base::type_t::value_type> >::type * = 0);
167 template<class ForwardReadableRange>
168 void value(ForwardReadableRange const & val);
170 template <class ForwardReadableRange>
171 void value(std::pair<typename Base::type_t::value_type, ForwardReadableRange> const & val);
175 template<class ForwardReadableRange>
176 void value_(ForwardReadableRange const &range);
179 Base const & self() const;
184 template <class BaseParser>
185 struct GenericTLVParserRegistry_EntryBase {
186 virtual void dump(GenericTLVParserBase<BaseParser> const & parser, std::ostream & os) = 0;
189 template <class BaseParser, class Parser>
190 struct GenericTLVParserRegistry_Entry
191 : GenericTLVParserRegistry_EntryBase<BaseParser>
193 virtual void dump(GenericTLVParserBase<BaseParser> const & parser, std::ostream & os);
197 template <class BaseParser>
198 class GenericTLVParserRegistry
199 : public senf::singleton<GenericTLVParserRegistry<BaseParser> >
201 typedef boost::ptr_map<
202 typename BaseParser::type_t::value_type,
203 detail::GenericTLVParserRegistry_EntryBase<BaseParser> > Map;
206 GenericTLVParserRegistry() {};
208 using senf::singleton<GenericTLVParserRegistry<BaseParser> >::instance;
209 friend class senf::singleton<GenericTLVParserRegistry<BaseParser> >;
211 template <class PacketParser>
212 struct RegistrationProxy {
216 template <typename Parser>
217 void registerParser();
219 void dump(GenericTLVParserBase<BaseParser> const & parser, std::ostream & os);
222 # define SENF_PACKET_TLV_REGISTRY_REGISTER( ConreteTLVParser ) \
224 ConreteTLVParser::Registry::RegistrationProxy<ConreteTLVParser> \
225 BOOST_PP_CAT(tlvparserRegistration_, __LINE__); \
230 ///////////////////////////////hh.e////////////////////////////////////////
231 //#include "GenericTLV.cci"
232 #include "GenericTLV.ct"
233 #include "GenericTLV.cti"
240 // comment-column: 40
241 // c-file-style: "senf"
242 // indent-tabs-mode: nil
243 // ispell-local-dictionary: "american"
244 // compile-command: "scons -u test"