4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <stefan.bund@fokus.fraunhofer.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 ParseVec public header */
27 #define HH_ParseVec_ 1
30 #include <boost/iterator/iterator_facade.hpp>
31 #include <boost/utility.hpp>
32 #include <boost/range.hpp>
33 #include <boost/type_traits.hpp>
34 #include "PacketParser.hh"
35 #include "ParseArray.hh" // for Parse_Array_iterator
37 //#include "ParseVec.mpp"
38 ///////////////////////////////hh.p////////////////////////////////////////
42 template <class ElementParser, class Sizer> class Parse_Vector_Container;
44 /** \brief Collection of fixed-size elements
46 A Vector is a collection of fixed-size elements of which the size of the collection can be
47 determined directly (that is without traversing the collection). This allows very efficient
48 random access to the elements of the collection.
50 A vector is a model of an STL random-access sequence. The parser only provides a reduced
51 interface, the container wrapper however completes this interface.
53 Parse_Vector makes use of a policy template argument, \a Sizer, to customize the way the
54 containers size is obtained. You will normally not instantiate Parser_Vector directly, you
55 will use one of the 'template typedefs' (which are templated structures since C++ does not
56 provide real template typedefs) provided with the policy implementations.
58 \todo Make the sizer a private base-class to profit from the empty-base-class optimization
60 \see ExampleVectorPolicy
61 \ingroup parsecollection
63 template <class ElementParser, class Sizer>
64 struct Parse_Vector : public PacketParserBase
66 Parse_Vector(data_iterator i, state_type s);
67 Parse_Vector(Sizer sizer, data_iterator i, state_type s);
68 ///< Additional sizer specific constructor
69 /**< This constructor may be used, if the sizer needs
70 additional parameters. */
72 size_type bytes() const;
75 static const size_type init_bytes = Sizer::init_bytes;
77 ///////////////////////////////////////////////////////////////////////////
78 // Container interface
80 typedef ElementParser value_type;
81 typedef detail::Parse_Array_iterator<value_type> iterator;
82 typedef iterator const_iterator;
83 typedef Parse_Vector_Container<ElementParser,Sizer> container;
85 size_type size() const;
88 iterator begin() const;
91 value_type operator[](difference_type i) const;
92 value_type front() const;
93 value_type back() const;
97 // The mutators provided here are those which don't take an iterator argument.
98 // If you need to pass an iterator it is much simpler and cleaner to use the
99 // 'container' wrapper
101 template <class Value> void push_back (Value value, size_type n=1) const;
102 void push_back_space (size_type n=1) const;
103 template <class Value> void push_front (Value value, size_type n=1) const;
104 void push_front_space (size_type n=1) const;
105 void resize (size_type n) const;
106 template <class Value> void resize (size_type n, Value value) const;
111 friend class Parse_Vector_Container<ElementParser,Sizer>;
114 namespace detail { template <class SizeParser> class Parse_VectorN_Sizer; }
116 /** \brief Vector with prefix sizing
118 This is a 'template typedef'. It defines a vector with a <em>directly preceding</em> size
119 field holding the number of vector elements. The size field is considered part of the
122 // Define MyVector as a vector of 16bit unsigned elements with a directly preceding
123 // 8bit unsigned size field
124 typedef senf::Parse_VectorN<senf::Parse_UInt16, senf::Parse_UInt8>::parser MyVector;
127 \param ElementParser \e fixed-size parser for parsing the vector elements
128 \param SizeParser parser for parsing the vector size (number of elements)
131 \ingroup parsecollection
133 template <class ElementParser, class SizeParser>
136 typedef Parse_Vector< ElementParser,
137 detail::Parse_VectorN_Sizer<SizeParser> > parser;
140 /** \brief Define Parse_VectorN field
142 This macro is a special helper to define a senf::Parse_VectorN type field, a vector of
143 elements of type \a elt_type (a parser) directly preceded by a numeric size field of type \a
144 size_type (another parser).
146 \param[in] name field name
147 \param[in] elt_type vector element type
148 \param[in] size_type size type
150 \ingroup packetparsermacros
152 # define SENF_PARSER_VEC_N(name, elt_type, size_type) \
153 typedef senf::Parse_VectorN<elt_type, size_type>::parser BOOST_PP_CAT(name, _vec_t); \
154 SENF_PARSER_FIELD( name, BOOST_PP_CAT(name, _vec_t) )
156 /** \brief Parse_Vector container wrapper
158 This is the container wrapper used for vector parsers. The container wrapper will stay valid
159 after changing the collection. However the container still depends on the packet and will be
160 invalidated if the Packet is deallocated or if the packet size is changed from without the
161 container wrapper (more precisely, it is invalided if the insertion/deletion happens before
162 the vector in the packet data).
164 The vector container wrapper provides a complete STL random-access sequence interface.
168 SomePacket::aVectorCollection_t::container c (p->aVectorCollection());
169 c.insert(c.begin(), ... );
174 template <class ElementParser, class Sizer>
175 class Parse_Vector_Container
178 ///////////////////////////////////////////////////////////////////////////
181 typedef Parse_Vector<ElementParser,Sizer> parser_type;
182 typedef PacketParserBase::data_iterator data_iterator;
183 typedef PacketParserBase::size_type size_type;
184 typedef PacketParserBase::difference_type difference_type;
185 typedef ElementParser value_type;
186 typedef detail::Parse_Array_iterator<value_type> iterator;
187 typedef iterator const_iterator;
188 typedef PacketParserBase::state_type state_type;
190 ///////////////////////////////////////////////////////////////////////////
191 ///\name Structors and default members
194 // no default constructor
196 // default destructor
197 // conversion constructors
199 Parse_Vector_Container(parser_type const & vector);
202 ///////////////////////////////////////////////////////////////////////////
207 size_type size() const;
210 iterator begin() const;
211 iterator end() const;
213 value_type operator[](difference_type i) const;
214 value_type front() const;
215 value_type back() const;
221 iterator shift(iterator pos, size_type n=1);
222 template <class Value>
223 void insert(iterator pos, Value const & t);
224 template <class Value>
225 void insert(iterator pos, size_type n, Value const & t);
227 template <class ForwardIterator>
228 void insert(iterator pos, ForwardIterator f, ForwardIterator l,
229 typename boost::disable_if< boost::is_convertible<ForwardIterator,size_type> >::type * = 0);
231 template <class ForwardIterator>
232 void insert(iterator pos, ForwardIterator f, ForwardIterator l);
235 void erase(iterator pos, size_type n=1);
236 void erase(iterator f, iterator l);
239 template <class Value> void push_back (Value value, size_type n=1);
240 void push_back_space (size_type n=1);
241 template <class Value> void push_front (Value value, size_type n=1);
242 void push_front_space (size_type n=1);
243 void resize (size_type n);
244 template <class Value> void resize (size_type n, Value value);
248 ///\name Parser interface
251 parser_type parser() const;
252 data_iterator i() const;
253 state_type state() const;
254 PacketData & data() const;
256 size_type bytes() const;
264 void setSize(size_type value);
273 ///////////////////////////////hh.e////////////////////////////////////////
275 #if !defined(HH_Packets__decls_) && !defined(HH_ParseVec_i_)
276 #define HH_ParseVec_i_
277 //#include "ParseVec.cci"
278 #include "ParseVec.ct"
279 #include "ParseVec.cti"
286 // c-file-style: "senf"
287 // indent-tabs-mode: nil
288 // ispell-local-dictionary: "american"
289 // compile-command: "scons -u test"
290 // comment-column: 40