4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief VectorParser public header */
31 #ifndef HH_SENF_Packets_VectorParser_
32 #define HH_SENF_Packets_VectorParser_ 1
35 #include <boost/type_traits.hpp>
36 #include "PacketParser.hh"
37 #include "ArrayParser.hh" // for ArrayParser_iterator
38 #include "AuxParser.hh" // for the AuxPolicies
40 //#include "VectorParser.mpp"
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45 template <class ElementParser, class AuxPolicy> class VectorParser_Container;
47 /** \brief Collection of fixed-size elements
49 A Vector is a collection of fixed-size elements of which the size of the collection can be
50 determined directly (that is without traversing the collection). This allows very efficient
51 random access to the elements of the collection.
53 A vector is a model of an STL random-access sequence. The parser only provides a reduced
54 interface, the container wrapper however completes this interface.
56 VectorParser makes use of a policy template argument, \a AuxPolicy, to customize the way the
57 containers size is obtained. You will normally not instantiate VectorParser directly, you
58 will use the \ref SENF_PARSER_VECTOR() helper macro.
60 Some basic vector access methods are defined as parser members. To access the complete list
61 API however you will need to instantiate a container wrapper for the vector. See \ref
62 packet_usage_fields_collection.
65 \ref How to access \ref packet_usage_fields_collection \n
66 SENF_PARSER_VECTOR() macro used to define vector fields \n
67 VectorParser_Container vector container wrapper API
69 \ingroup parsecollection
71 template <class ElementParser, class AuxPolicy>
73 : public PacketParserBase,
76 VectorParser(data_iterator i, state_type s);
77 VectorParser(AuxPolicy policy, data_iterator i, state_type s);
78 ///< Additional sizer specific constructor
79 /**< This constructor may be used, if the sizer needs
80 additional parameters. */
82 size_type bytes() const;
85 static const size_type init_bytes = AuxPolicy::aux_bytes;
87 //-////////////////////////////////////////////////////////////////////////
88 // Container interface
90 typedef ElementParser value_type;
91 typedef detail::ArrayParser_iterator<value_type> iterator;
92 typedef iterator const_iterator;
93 typedef VectorParser_Container<ElementParser,AuxPolicy> container;
95 size_type size() const;
98 iterator begin() const;
101 value_type operator[](difference_type i) const;
102 value_type front() const;
103 value_type back() const;
107 // The mutators provided here are those which don't take an iterator argument.
108 // If you need to pass an iterator it is much simpler and cleaner to use the
109 // 'container' wrapper
111 template <class Value> void push_back (Value const & value, size_type n=1) const;
112 value_type push_back_space (size_type n=1) const;
113 template <class Value> void push_front (Value const & value, size_type n=1) const;
114 value_type push_front_space (size_type n=1) const;
115 void resize (size_type n) const;
116 template <class Value> void resize (size_type n, Value value) const;
120 friend class VectorParser_Container<ElementParser,AuxPolicy>;
123 /** \brief VectorParser container wrapper
125 This is the container wrapper used for vector parsers. The container wrapper will stay valid
126 after changing the collection. However the container still depends on the packet and will be
127 invalidated if the Packet is deallocated or if the packet size is changed from without the
128 container wrapper (more precisely, it is invalidated if the insertion/deletion happens before
129 the vector in the packet data).
131 The vector container wrapper provides a complete STL random-access sequence interface.
135 SomePacket::aVectorCollection_t::container c (p->aVectorCollection());
136 c.insert(c.begin(), ... );
141 template <class ElementParser, class AuxPolicy>
142 class VectorParser_Container
143 : private AuxPolicy::WrapperPolicy
146 //-////////////////////////////////////////////////////////////////////////
149 typedef VectorParser<ElementParser,AuxPolicy> parser_type;
150 typedef PacketParserBase::data_iterator data_iterator;
151 typedef PacketParserBase::size_type size_type;
152 typedef PacketParserBase::difference_type difference_type;
153 typedef ElementParser value_type;
154 typedef detail::ArrayParser_iterator<value_type> iterator;
155 typedef iterator const_iterator;
156 typedef PacketParserBase::state_type state_type;
158 //-////////////////////////////////////////////////////////////////////////
159 ///\name Structors and default members
162 // no default constructor
164 // default destructor
165 // conversion constructors
167 VectorParser_Container(parser_type const & vector);
170 //-////////////////////////////////////////////////////////////////////////
175 size_type size() const;
178 iterator begin() const;
179 iterator end() const;
181 value_type operator[](difference_type i) const;
182 value_type front() const;
183 value_type back() const;
189 iterator shift(iterator pos, size_type n=1);
190 template <class Value>
191 void insert(iterator pos, Value const & t);
192 template <class Value>
193 void insert(iterator pos, size_type n, Value const & t);
195 template <class ForwardIterator>
196 void insert(iterator pos, ForwardIterator f, ForwardIterator l,
197 typename boost::disable_if< boost::is_convertible<ForwardIterator,size_type> >::type * = 0);
199 template <class ForwardIterator>
200 void insert(iterator pos, ForwardIterator f, ForwardIterator l);
203 void erase(iterator pos, size_type n=1);
204 void erase(iterator f, iterator l);
207 template <class Value> void push_back (Value const & value, size_type n=1);
208 value_type push_back_space (size_type n=1);
209 template <class Value> void push_front (Value const & value, size_type n=1);
210 value_type push_front_space (size_type n=1);
211 void resize (size_type n);
212 template <class Value> void resize (size_type n, Value value);
216 ///\name Parser interface
219 parser_type parser() const;
220 data_iterator i() const;
221 state_type state() const;
222 PacketData & data() const;
224 size_type bytes() const;
232 void setSize(size_type value);
238 /** \brief Define VectorParser field
240 This macro is a special helper to define a senf::VectorParser type field, a vector of
241 elements of type \a elt_type (a parser) which size is given by the \a size field.
244 // The size field should be declared private (size is accessible via the vector)
245 SENF_PARSER_PRIVATE_FIELD ( vec_size_, senf::UInt16Parser );
246 // Define the vector, here it has 32bit unsigned integer elements
247 SENF_PARSER_VECTOR ( vec, vec_size_, senf::UInt32Parser );
250 \warning Realize, that the \a size field is controlled by the vector parser. This field
251 should therefore be declared either read-only or private and must be changed only via
254 Further additional tags are supported which modify the way, the \a size field is
257 <table class="senf fixedcolumn">
258 <tr><td>\c bytes(\a size)</td><td>\a size gives the size of the vector in bytes not the
259 number of contained elements</td></tr>
261 <tr><td>\c packetSize()</td><td>Use the size of the packet to get the vector size. The
262 vector will occupy all space up to the end of the packet.</td></tr>
264 <tr><td>\c transform(\a transform, \a size)</td><td>The \a transform is applied to the \a
265 size value, the value is not used directly</td>
268 The optional \a transform is a class with the following layout
273 typedef ... value_type;
274 static value_type get(other_type v);
275 static other_type set(value_type v);
277 \endcode \c other_type is the \a size ::\c value_type where as the \c value_type typedef is
278 the arbitrary return type of the transform.
280 The tags are applied to the \a size parameter:
282 SENF_PARSER_VECTOR ( vec, transform(MyTransform, vec_size_), senf::UInt32Parser );
283 SENF_PARSER_VECTOR ( vec, packetSize(), senf::UInt32Parser );
286 \param[in] name field name
287 \param[in] size name of field giving the vector size
288 \param[in] elt_type vector element type
291 How to use \ref packet_usage_fields_collection \n
292 senf::VectorParser the vector parser API for vector field access
293 senf::VectorParser_Container the vector parser container API for vector field access
296 \ingroup packetparsermacros
298 # define SENF_PARSER_VECTOR(name, size, elt_type) \
299 SENF_PARSER_VECTOR_I(public, name, size, elt_type)
301 /** \brief Define private VectorParser field
303 \see \ref SENF_PARSER_VECTOR()
306 \ingroup packetparsermacros
308 # define SENF_PARSER_PRIVATE_VECTOR(name, size, elt_type) \
309 SENF_PARSER_VECTOR_I(protected, name, size, elt_type)
312 //-/////////////////////////////////////////////////////////////////////////////////////////////////
314 #if !defined(HH_SENF_Packets_Packets__decls_) && !defined(HH_SENF_Packets_VectorParser_i_)
315 #define HH_SENF_Packets_VectorParser_i_
316 //#include "VectorParser.cci"
317 #include "VectorParser.ct"
318 #include "VectorParser.cti"
325 // c-file-style: "senf"
326 // indent-tabs-mode: nil
327 // ispell-local-dictionary: "american"
328 // compile-command: "scons -u test"
329 // comment-column: 40