4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@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 VariantParser public header */
26 #ifndef HH_VariantParser_
27 #define HH_VariantParser_ 1
30 #error "Don't include 'VariantParser.hh' directly, include 'Packets.hh'"
34 #include <boost/mpl/vector.hpp>
35 #include <boost/mpl/at.hpp>
36 #include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
37 #include <boost/preprocessor/repetition/enum_params.hpp>
38 #include <boost/preprocessor/seq/for_each.hpp>
39 #include "PacketParser.hh"
41 //#include "VariantParser.mpp"
42 ///////////////////////////////hh.p////////////////////////////////////////
46 # ifndef SENF_LIMIT_PARSE_VARIANT
47 /** \brief Maximum number of senf::VariantParser sub-parsers.
49 This number defines the maximum number of parser arguments senf::VariantParser takes.
51 # define SENF_LIMIT_PARSE_VARIANT 6
54 /** \brief Variant parser
56 This is not really a collection parser (it does not provide a collection
57 interface). However, it is not a composite parser or value parser either.
59 A variant parser will parse any number of sub-parsers discriminated by an arbitrary, policy
60 defined condition. This is the parser to use, if the type and/or number of fields of a
61 packet change depending on some condition.
63 typedef senf::VariantParser<
65 senf::mpl::vector<senf::VoidPacketParser, TypeAParser, TypeBParser> > MyVariantParser;
67 This typedef defines a variant parser choosing one of three sub
68 parsers. senf::VoidPacketParser is an empty parser, it effectively makes this parser
71 When creating a new packet containing a variant parser, the variant parser will always be
72 initialized to the first sub-parser.
75 ExampleAuxPolicy on how to implement the \a AuxPolicy \n
76 \ref SENF_PARSER_VARIANT() on how to integrate the parser into another parser
77 \ingroup parsecollection
79 template <class AuxPolicy, class Parsers>
81 : public PacketParserBase, private AuxPolicy
83 typedef Parsers parsers;
86 ///\name Parser interface
89 VariantParser(data_iterator i, state_type s);
90 VariantParser(AuxPolicy policy, data_iterator i, state_type s);
92 size_type bytes() const;
95 static const size_type init_bytes = senf::init_bytes<
96 typename boost::mpl::at<parsers, boost::mpl::int_<0> >::type>::value
97 + AuxPolicy::aux_bytes;
100 ///////////////////////////////////////////////////////////////////////////
102 unsigned variant() const; ///< Get current variant
103 /**< Get the currently selected variant index. The returned
104 number directly indexes the list of sub parsers.
105 \returns Index of currently selected variant. Integer
106 in the range from 0 to (number-of-sub-parsers - 1)
109 template <unsigned N>
110 typename boost::mpl::at< parsers, boost::mpl::int_<N> >::type get() const;
111 ///< Access sub-parser
112 /**< This call will return the sub-parser at index \a
113 N. This call will fail, if the currently active
114 variant() is not \a N.
115 \pre variant() == \a N
116 \returns sub-parser at index \a N */
118 template <unsigned N>
119 void init(); ///< Re-initialize field
120 /**< This will reinitialize the field to the variant
121 sub-parser at index \a N changing the currently
123 \post variant() == \a N */
126 /** \brief Define VariantParser field
128 This macro is a special helper to define a senf::DirectVariantParser type field. This is a
129 variant field which chooses the sub-type by directly taking the value of some other field.
132 struct SomeParser : public PacketParserBase
134 # include SENF_PARSER()
136 SENF_PARSER_PRIVATE_FIELD( type, senf::UInt8Parser );
137 SENF_PARSER_PRIVATE_VARIANT( content, type,
138 (senf::VoidPacketParser)
142 (senf::UInt32Parser) );
144 senf::UInt8Parser uint8() const { return content().get<1>(); }
145 senf::UInt16Parser uint16() const { return content().get<2>(); }
146 senf::UInt24Parser uint24() const { return content().get<3>(); }
147 senf::UInt32Parser uint32() const { return content().get<4>(); }
149 void disable() const { content().init<0>(); }
150 void set_uint8() const { content().init<1>(); }
151 void set_uint16() const { content().init<2>(); }
152 void set_uint24) const { content().init<3>(); }
153 void set_uint23() const { content().init<4>(); }
155 SENF_PARSER_FINALIZE(SomeParser);
159 The variant \c content chooses one of the sub parsers depending on the \c type field. If \c
160 type is 0, senf::VoidPacketParser is selected, if it is 1, senf::UInt8Parser and so on.
162 It is customary, to hide the variant parser (by defining it private) and provide more
163 conveniently named accessors.
165 Further additional tags are supported which modify the way, the \a chooser field is
168 <table class="senf fixedcolumn">
169 <tr><td>\c transform(\a transform, \a size)</td><td>The \a transform is applied to the \a
170 chooser value, the value is not used directly</td>
173 The optional \a transform is a class with the following layout
178 typedef ... value_type;
179 static value_type get(other_type v);
180 static other_type set(value_type v);
182 \endcode \c other_type is the \a chooser ::\c value_type where as the \c value_type typedef
183 is the arbitrary return type of the transform.
185 The tags are applied to the \a chooser parameter:
187 SENF_PARSER_VARIANT ( content, transform(MyTransform, type_),
188 (senf::VoidPacketParser)
192 (senf::UInt32Parser) );
195 \param[in] name name of the field
196 \param[in] chooser name of the field choosing the variant to use
197 \param[in] types a Boost.Preprocessor style sequence of sub-parser types
200 senf::VariantParser \n
201 \ref SENF_PARSER_PRIVATE_VARIANT()
203 \ingroup packetparsermacros
205 # define SENF_PARSER_VARIANT(name, chooser, types) \
206 SENF_PARSER_VARIANT_I(public, name, chooser, types)
208 /** \brief Define private VariantParser field
210 \see \ref SENF_PARSER_VARIANT()
212 \ingroup packetparsermacros
214 # define SENF_PARSER_PRIVATE_VARIANT(name, chooser, types) \
215 SENF_PARSER_VARIANT_I(private, name, chooser, types)
218 ///////////////////////////////hh.e////////////////////////////////////////
220 #if !defined(HH_Packets__decls_) && !defined(HH_VariantParser_i_)
221 #define HH_VariantParser_i_
222 //#include "VariantParser.cci"
223 #include "VariantParser.ct"
224 #include "VariantParser.cti"
231 // comment-column: 40
232 // c-file-style: "senf"
233 // indent-tabs-mode: nil
234 // ispell-local-dictionary: "american"
235 // compile-command: "scons -u test"