switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / DefaultBundle / ListOptionTypeParser.cti
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
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
10 //
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.
14 //
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.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
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.
24 //
25 // Contributor(s):
26 //   Philipp Batroff <pug@berlios.de>
27
28 /** \file
29     \brief ListOptionTypeParser inline template implementation */
30
31 #include "ListOptionTypeParser.ih"
32
33 // Custom includes
34
35 #define prefix_ inline
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39 // senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>
40
41 template <class ElementParser, class AuxPolicy>
42 prefix_
43 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::
44 ListOptionTypeParser_Policy()
45 {}
46
47 template <class ElementParser, class AuxPolicy>
48 template <class Arg>
49 prefix_
50 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::
51 ListOptionTypeParser_Policy(Arg const & arg)
52     : AuxPolicy(arg)
53 {}
54
55 template <class ElementParser, class AuxPolicy>
56 prefix_ typename senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::size_type
57 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::bytes(data_iterator i,
58                                                                           state_type s)
59     const
60 {
61     return ((AuxPolicy::aux(i, s) * 8 + 6)  + AuxPolicy::aux_bytes);
62 }
63
64 template <class ElementParser, class AuxPolicy>
65 prefix_ typename senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::size_type
66 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::size(data_iterator i,
67                                                                          state_type s)
68     const
69 {
70     parser_type p(*this, i, s);
71     container_type c(p);
72     return std::distance(c.begin(), c.end());
73 }
74
75 template <class ElementParser, class AuxPolicy>
76 prefix_ void
77 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::init(data_iterator i,
78                                                                          state_type s)
79     const
80 {
81     i[0] = 1u;
82     i[1] = 4u;
83     for (unsigned int n = 2;n < 6; ++n)
84         i[n] = 0u;
85     AuxPolicy::aux(0, i, s);
86 }
87
88 //-/////////////////////////////////////////////////////////////////////////////////////////////////
89 // senf::detail::ListOptionTypeParser_Policy<ElementParser,BytesParser>
90
91 //constructor
92 template <class ElementParser, class AuxPolicy>
93 prefix_
94 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
95 container_policy(parser_policy const & p)
96     : AuxPolicy(p)
97 {}
98
99 //destructor
100 template <class ElementParser, class AuxPolicy>
101 prefix_
102 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
103 ~container_policy()
104 {}
105
106 //construct method
107 template <class ElementParser, class AuxPolicy>
108 prefix_ void
109 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
110 construct(container_type & c)
111 {
112     safe_data_iterator i (c.data(), c.i()) ;
113     realAux_ = (AuxPolicy::aux(i, c.state()) * 8) + 6;
114     safe_data_iterator e = i + realAux_;
115     for (n_ = 0; i != e;) {
116         size_type elByte;
117         if (i[0] == 0u)
118             elByte = 1;
119         else
120             elByte = senf::bytes(ElementParser(i, c.state()));
121         if (((i + elByte) == e) && (i[0] == 0u || i[0] == 1u)) { //check weather last element is padding or not
122             realAux_ -= std::distance(i, e);
123             c.data().erase(i, e); //delete padding
124             e = i; //set end iterator
125         } else{
126             ++n_;
127             std::advance(i, elByte);
128         }
129     }
130     container_size_ = c.data().size(); //set actual size without padding
131 }
132
133 //destruct method
134 template <class ElementParser, class AuxPolicy>
135 prefix_ void
136 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
137 destruct(container_type & c)
138 {
139     //data_iterator i (AuxPolicy::adjust(parser_type::get(p).i(), parser_type::get(p).state()));
140     safe_data_iterator i (c.data(), c.i()) ;
141     safe_data_iterator e = i + realAux_;
142     unsigned int padBytes = 0;
143     if (realAux_ == 0)      //if list is empty, 6 padding bytes required!
144       padBytes = 6;
145     else{
146       padBytes = ( (realAux_+2) % 8);
147       if (padBytes != 0)
148           padBytes = 8 - padBytes;
149     }
150     if (padBytes > 0) {
151         c.data().insert(e, padBytes, 0u);
152         if (padBytes > 1) {
153             e[0] = 1u;
154             e[1] = padBytes - 2;
155         } else
156             e[0] = 0;
157         container_size_ += padBytes;
158         realAux_ += padBytes;
159         ++n_;
160     }
161     AuxPolicy::aux(( (realAux_ + 2)/ 8 - 1), i, c.state());
162 }
163
164 //bytes()
165 template <class ElementParser, class AuxPolicy>
166 prefix_ typename senf::detail::ListOptionTypeParser_Policy<
167     ElementParser,AuxPolicy>::container_policy::size_type
168 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
169 bytes(data_iterator i, state_type s)
170     const
171 {
172     return (realAux_ );
173 }
174
175 //size()
176 template <class ElementParser, class AuxPolicy>
177 prefix_ typename senf::detail::ListOptionTypeParser_Policy<
178     ElementParser,AuxPolicy>::container_policy::size_type
179 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
180 size(data_iterator i, state_type s)
181     const
182 {
183     return n_;
184 }
185
186 //init()
187 template <class ElementParser, class AuxPolicy>
188 prefix_ void
189 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
190 init(data_iterator i, state_type s)
191 {
192     realAux_ = 0;
193     n_ = 0;
194     container_size_ = s->size();
195     AuxPolicy::aux(0, i, s);
196 }
197
198 //erase()
199 template <class ElementParser, class AuxPolicy>
200 prefix_ void
201 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
202 erase(container_type & c, data_iterator p)
203 {
204     size_type b(senf::bytes(ElementParser(p, c.state())));
205     realAux_ -= b;
206     --n_;
207     container_size_ = c.data().size() - b;
208 }
209
210 //insert()
211 template <class ElementParser, class AuxPolicy>
212 prefix_ void
213 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
214 insert(container_type & c, data_iterator p)
215 {
216     size_type b(senf::bytes(ElementParser(p, c.state())));
217     realAux_ += b;
218     ++n_;
219     container_size_ = c.data().size();
220 }
221
222 //update()
223 template <class ElementParser, class AuxPolicy>
224 prefix_ void
225 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
226 update(container_type const & c)
227     const
228 {
229     if (container_size_ == c.data().size())
230         return;
231     data_iterator i(AuxPolicy::adjust(c.i(), c.state()));
232     data_iterator j(i);
233     for (size_type n(n_); n; --n, std::advance(j, senf::bytes(ElementParser(j,c.state())))) { ; }
234     realAux_ = std::distance(i, j);
235     container_size_ = c.data().size();
236 }
237
238 //setbegin()
239 template <class ElementParser, class AuxPolicy>
240 prefix_ typename senf::detail::ListOptionTypeParser_Policy<
241     ElementParser,AuxPolicy>::container_policy::data_iterator
242 senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::
243 setBegin(container_type const & c, iterator_data & d)
244     const
245 {
246     return c.i();
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 {
257     return boost::next(c.i(),realAux_);
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 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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: