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 VectorParser public header */
26 #ifndef HH_VectorParser_
27 #define HH_VectorParser_ 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 "ArrayParser.hh" // for ArrayParser_iterator
36 #include "AuxParser.hh" // for the AuxPolicies
38 //#include "VectorParser.mpp"
39 ///////////////////////////////hh.p////////////////////////////////////////
43 template <class ElementParser, class AuxPolicy> class VectorParser_Container;
45 /** \brief Collection of fixed-size elements
47 A Vector is a collection of fixed-size elements of which the size of the collection can be
48 determined directly (that is without traversing the collection). This allows very efficient
49 random access to the elements of the collection.
51 A vector is a model of an STL random-access sequence. The parser only provides a reduced
52 interface, the container wrapper however completes this interface.
54 VectorParser makes use of a policy template argument, \a Sizer, to customize the way the
55 containers size is obtained. You will normally not instantiate Parser_Vector directly, you
56 will use one of the 'template typedefs' (which are templated structures since C++ does not
57 provide real template typedefs) provided with the policy implementations.
59 \todo Make the sizer a private base-class to profit from the empty-base-class optimization
61 \see ExampleVectorPolicy
62 \ingroup parsecollection
64 template <class ElementParser, class AuxPolicy>
66 : public PacketParserBase,
69 VectorParser(data_iterator i, state_type s);
70 VectorParser(AuxPolicy policy, data_iterator i, state_type s);
71 ///< Additional sizer specific constructor
72 /**< This constructor may be used, if the sizer needs
73 additional parameters. */
75 size_type bytes() const;
78 static const size_type init_bytes = AuxPolicy::aux_bytes;
80 ///////////////////////////////////////////////////////////////////////////
81 // Container interface
83 typedef ElementParser value_type;
84 typedef detail::ArrayParser_iterator<value_type> iterator;
85 typedef iterator const_iterator;
86 typedef VectorParser_Container<ElementParser,AuxPolicy> container;
88 size_type size() const;
91 iterator begin() const;
94 value_type operator[](difference_type i) const;
95 value_type front() const;
96 value_type back() const;
100 // The mutators provided here are those which don't take an iterator argument.
101 // If you need to pass an iterator it is much simpler and cleaner to use the
102 // 'container' wrapper
104 template <class Value> void push_back (Value value, size_type n=1) const;
105 void push_back_space (size_type n=1) const;
106 template <class Value> void push_front (Value value, size_type n=1) const;
107 void push_front_space (size_type n=1) const;
108 void resize (size_type n) const;
109 template <class Value> void resize (size_type n, Value value) const;
113 friend class VectorParser_Container<ElementParser,AuxPolicy>;
116 /** \brief Define VectorNParser field
118 This macro is a special helper to define a senf::VectorNParser type field, a vector of
119 elements of type \a elt_type (a parser) which size is given by the \a size field.
122 // The size field should be declared private (size is accessible via the vector)
123 SENF_PARSER_PRIVATE_FIELD ( vec_size_, senf::UInt16Parser );
124 // Define the vector, here it has 32bit unsigned integer elements
125 SENF_PARSER_VEC_N ( vec, vec_size_, senf::UInt32Parser );
128 \param[in] name field name
129 \param[in] size name of field giving the vector size
130 \param[in] elt_type vector element type
132 \ingroup packetparsermacros
135 /** \brief Define VectorNParser field
137 \see \ref SENF_PARSER_VEC_N()
139 \ingroup packetparsermacros
142 # define SENF_PARSER_VECTOR(name, size, elt_type) \
143 SENF_PARSER_VECTOR_I(public, name, size, elt_type)
145 # define SENF_PARSER_PRIVATE_VECTOR(name, size, elt_type) \
146 SENF_PARSER_VECTOR_I(private, name, size, elt_type)
150 /** \brief VectorParser container wrapper
152 This is the container wrapper used for vector parsers. The container wrapper will stay valid
153 after changing the collection. However the container still depends on the packet and will be
154 invalidated if the Packet is deallocated or if the packet size is changed from without the
155 container wrapper (more precisely, it is invalided if the insertion/deletion happens before
156 the vector in the packet data).
158 The vector container wrapper provides a complete STL random-access sequence interface.
162 SomePacket::aVectorCollection_t::container c (p->aVectorCollection());
163 c.insert(c.begin(), ... );
168 template <class ElementParser, class AuxPolicy>
169 class VectorParser_Container
173 ///////////////////////////////////////////////////////////////////////////
176 typedef VectorParser<ElementParser,AuxPolicy> parser_type;
177 typedef PacketParserBase::data_iterator data_iterator;
178 typedef PacketParserBase::size_type size_type;
179 typedef PacketParserBase::difference_type difference_type;
180 typedef ElementParser value_type;
181 typedef detail::ArrayParser_iterator<value_type> iterator;
182 typedef iterator const_iterator;
183 typedef PacketParserBase::state_type state_type;
185 ///////////////////////////////////////////////////////////////////////////
186 ///\name Structors and default members
189 // no default constructor
191 // default destructor
192 // conversion constructors
194 VectorParser_Container(parser_type const & vector);
197 ///////////////////////////////////////////////////////////////////////////
202 size_type size() const;
205 iterator begin() const;
206 iterator end() const;
208 value_type operator[](difference_type i) const;
209 value_type front() const;
210 value_type back() const;
216 iterator shift(iterator pos, size_type n=1);
217 template <class Value>
218 void insert(iterator pos, Value const & t);
219 template <class Value>
220 void insert(iterator pos, size_type n, Value const & t);
222 template <class ForwardIterator>
223 void insert(iterator pos, ForwardIterator f, ForwardIterator l,
224 typename boost::disable_if< boost::is_convertible<ForwardIterator,size_type> >::type * = 0);
226 template <class ForwardIterator>
227 void insert(iterator pos, ForwardIterator f, ForwardIterator l);
230 void erase(iterator pos, size_type n=1);
231 void erase(iterator f, iterator l);
234 template <class Value> void push_back (Value value, size_type n=1);
235 void push_back_space (size_type n=1);
236 template <class Value> void push_front (Value value, size_type n=1);
237 void push_front_space (size_type n=1);
238 void resize (size_type n);
239 template <class Value> void resize (size_type n, Value value);
243 ///\name Parser interface
246 parser_type parser() const;
247 data_iterator i() const;
248 state_type state() const;
249 PacketData & data() const;
251 size_type bytes() const;
259 void setSize(size_type value);
267 ///////////////////////////////hh.e////////////////////////////////////////
269 #if !defined(HH_Packets__decls_) && !defined(HH_VectorParser_i_)
270 #define HH_VectorParser_i_
271 //#include "VectorParser.cci"
272 #include "VectorParser.ct"
273 #include "VectorParser.cti"
280 // c-file-style: "senf"
281 // indent-tabs-mode: nil
282 // ispell-local-dictionary: "american"
283 // compile-command: "scons -u test"
284 // comment-column: 40