updated ListOptionTypeParser and unittests, fixed error in ICMP packet
[senf.git] / senf / Packets / ListOptionTypeParser.cti
1 // $Id: ListOptionTypeParser.cti 869 2008-06-09 13:57:27Z pug $
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Philipp.Batroff@fokus.fraunhofer.de
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24  \brief ListOptionTypeParser inline template implementation */
25
26 #include "ListOptionTypeParser.ih"
27
28 // Custom includes
29
30 #define prefix_ inline
31 ///////////////////////////////cti.p///////////////////////////////////////
32
33 ///////////////////////////////////////////////////////////////////////////
34 // senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>
35
36 template <class ElementParser, class AuxPolicy>
37 prefix_
38 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::
39 ListOptionTypeParser_Policy()
40 {}
41
42 template <class ElementParser, class AuxPolicy>
43 template <class Arg>
44 prefix_
45 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::
46 ListOptionTypeParser_Policy(Arg const & arg)
47     : AuxPolicy(arg) 
48 {}
49
50 template <class ElementParser, class AuxPolicy>
51 prefix_ typename senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::size_type
52 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::bytes(data_iterator i,
53                                                                           state_type s)
54     const
55 {
56     return ((AuxPolicy::aux(i, s) * 8 + 6)  + AuxPolicy::aux_bytes);
57 }
58
59 template <class ElementParser, class AuxPolicy>
60 prefix_ typename senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::size_type
61 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::size(data_iterator i,
62                                                                          state_type s)
63     const
64 {
65     parser_type p(*this, i, s);
66     container_type c(p);
67     return std::distance(c.begin(), c.end());
68 }
69
70 template <class ElementParser, class AuxPolicy>
71 prefix_ void
72 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::init(data_iterator i,
73                                                                          state_type s)
74     const
75 {
76     i[0] = 1u;
77     i[1] = 4u;
78     for (unsigned int n = 2;n < 6; ++n)
79         i[n] = 0u;
80     AuxPolicy::aux(0, i, s);
81 }
82
83 ///////////////////////////////////////////////////////////////////////////
84 // senf::detail::ListOptionTypeParser_Policy<ElementParser,BytesParser>
85
86 //constructor
87 template <class ElementParser, class AuxPolicy>
88 prefix_
89 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
90 container_policy(parser_policy const & p)
91     : AuxPolicy(p) 
92 {}
93
94 //destructor
95 template <class ElementParser, class AuxPolicy>
96 prefix_
97 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
98 ~container_policy()
99 {}
100
101 //construct method
102 template <class ElementParser, class AuxPolicy>
103 prefix_ void
104 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
105 construct(container_type & c)
106 {
107     safe_data_iterator i (c.data(), c.i()) ;
108     realAux_ = (AuxPolicy::aux(i, c.state()) * 8) + 6;
109     safe_data_iterator e = i + realAux_;
110     for (n_ = 0; i != e;) {
111         unsigned int elByte = senf::bytes(ElementParser(i, c.state()));
112         if (((i + elByte) == e) && (i[0] == 0u || i[0] == 1u)) { //check wether last element is padding or not
113             realAux_ -= std::distance(i, e);
114             container_size_ -= std::distance(i, e);
115             c.data().erase(i, e); //delete padding
116             e = i; //set end iterator
117         } else{
118             ++n_;
119             std::advance(i, elByte);
120         }
121     }
122     //    container_size_ = std::distance(i,e);
123     container_size_ = c.data().size(); //set actual size without padding
124 }
125
126 //destruct method
127 template <class ElementParser, class AuxPolicy>
128 prefix_ void
129 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
130 destruct(container_type & c)
131 {
132     //  data_iterator i (AuxPolicy::adjust(parser_type::get(p).i(), parser_type::get(p).state()));
133     safe_data_iterator i (c.data(), c.i()) ;
134     safe_data_iterator const e = i + realAux_;
135     unsigned int padBytes = 0;
136     if (realAux_ == 0)      //if list is empty, 6 padding bytes required!
137       padBytes = 6;
138     else
139       padBytes = ( (realAux_+2) % 8);
140     if (padBytes > 0) {
141         c.data().insert(e, padBytes, 0u);
142         if (padBytes > 1) {
143             e[0] = 1u;
144             e[1] = padBytes - 2;
145         } else
146             e[0] = 0;
147         container_size_ += padBytes;
148         realAux_ += padBytes;
149         ++n_;
150     }
151     AuxPolicy::aux(( (realAux_ + 2)/ 8 - 1), i, c.state());
152 }
153
154 //bytes()
155 template <class ElementParser, class AuxPolicy>
156 prefix_ typename senf::detail::ListOptionTypeParser_Policy<
157     ElementParser,AuxPolicy>::container_policy::size_type
158 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
159 bytes(data_iterator i, state_type s)
160     const
161 {
162     return (realAux_ );
163 }
164
165 //size()
166 template <class ElementParser, class AuxPolicy>
167 prefix_ typename senf::detail::ListOptionTypeParser_Policy<
168     ElementParser,AuxPolicy>::container_policy::size_type
169 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
170 size(data_iterator i, state_type s)
171     const
172 {
173     return n_;
174 }
175
176 //init()
177 template <class ElementParser, class AuxPolicy>
178 prefix_ void
179 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
180 init(data_iterator i, state_type s)
181 {
182 //    i[0] = 1u;
183     //    for (unsigned int n = 1;n < 6; ++n)
184 //        i[n] = 0u;
185     n_ = 0;
186     container_size_ = s->size();
187     AuxPolicy::aux(0, i, s);
188 }
189
190 //erase()
191 template <class ElementParser, class AuxPolicy>
192 prefix_ void
193 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
194 erase(container_type & c, data_iterator p)
195 {
196     size_type b(senf::bytes(ElementParser(p, c.state()))); //length of parser
197     //    AuxPolicy::aux(
198     //                  AuxPolicy::aux( c.i(), c.state()) -b,
199     //                  c.i(),
200     //                  c.state());
201     realAux_ -= b;
202     --n_;
203     // The container will be reduced by b bytes directly after this call
204     container_size_ -= b;
205 }
206
207 //insert()
208 template <class ElementParser, class AuxPolicy>
209 prefix_ void
210 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
211 insert(container_type & c, data_iterator p)
212 {
213     size_type b(senf::bytes(ElementParser(p, c.state())));
214     //    AuxPolicy::aux( AuxPolicy::aux(c.i(), c.state())+b, c.i(), c.state());
215     realAux_ += b;
216     ++n_;
217     container_size_ = c.data().size();
218 }
219
220 //update()
221 template <class ElementParser, class AuxPolicy>
222 prefix_ void
223 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
224 update(container_type const & c)
225     const
226 {
227     if (container_size_ == c.data().size())
228         return;
229     data_iterator i(AuxPolicy::adjust(c.i(), c.state()));
230     data_iterator j(i);
231     for (size_type n(n_); n; --n, std::advance(j, senf::bytes(ElementParser(j,c.state()))));
232     realAux_ = std::distance(i, j);
233     //    aux( std::distance(i,j), c.i(), c.state() );
234     container_size_ = c.data().size();
235 }
236
237 //setbegin()
238 template <class ElementParser, class AuxPolicy>
239 prefix_ typename senf::detail::ListOptionTypeParser_Policy<
240     ElementParser,AuxPolicy>::container_policy::data_iterator
241 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
242 setBegin(container_type const & c, iterator_data & d)
243     const
244 {
245     return c.i();
246     //    return AuxPolicy::adjust(c.i(), c.state());
247 }
248
249 //setEnd()
250 template <class ElementParser, class AuxPolicy>
251 prefix_ typename senf::detail::ListOptionTypeParser_Policy<
252     ElementParser,AuxPolicy>::container_policy::data_iterator
253 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
254 setEnd(container_type const & c, iterator_data & d)
255     const
256 { //wtf??
257     return boost::next(AuxPolicy::adjust(c.i(), c.state()), aux(c.i(),c.state()));
258 }
259
260 //setFromPosition()
261 template <class ElementParser, class AuxPolicy>
262 prefix_ void
263 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
264 setFromPosition(container_type const & c, iterator_data & d, data_iterator p)
265     const
266 {}
267
268 //next()
269 template <class ElementParser, class AuxPolicy>
270 prefix_ typename senf::detail::ListOptionTypeParser_Policy<
271     ElementParser,AuxPolicy>::container_policy::data_iterator
272 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
273 next(container_type const & c, iterator_data & d)
274     const
275 {
276     return boost::next(container_type::iterator::get(d).i(), senf::bytes(ElementParser(container_type::iterator::get(d).i(), c.state())));
277 }
278
279 //raw()
280 template <class ElementParser, class AuxPolicy>
281 prefix_ typename senf::detail::ListOptionTypeParser_Policy<
282     ElementParser,AuxPolicy>::container_policy::data_iterator
283 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
284 raw(container_type const & c, iterator_data const & d)
285     const
286 {
287     return container_type::iterator::get(d).i();
288 }
289
290 ///////////////////////////////cti.e///////////////////////////////////////
291 #undef prefix_
292
293 \f
294 // Local Variables:
295 // mode: c++
296 // fill-column: 100
297 // comment-column: 40
298 // c-file-style: "senf"
299 // indent-tabs-mode: nil
300 // ispell-local-dictionary: "american"
301 // compile-command: "scons -u test"
302 // End: