ListParser.cti
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
14 /** \file
15  \brief ListParser inline template implementation */
16 
17 #include "ListParser.ih"
18 
19 // Custom includes
20 #include <senf/Utils/senfassert.hh>
21 
22 #define prefix_ inline
23 //-/////////////////////////////////////////////////////////////////////////////////////////////////
24 
25 //-/////////////////////////////////////////////////////////////////////////////////////////////////
26 // senf::ListParser<ListPolicy>
27 
28 template <class ListPolicy>
29 prefix_ senf::ListParser<ListPolicy>::ListParser(data_iterator i, state_type s)
30  : PacketParserBase(i,s), ListPolicy()
31 {}
32 
33 template <class ListPolicy>
34 prefix_ senf::ListParser<ListPolicy>::ListParser(ListPolicy policy,
35  data_iterator i, state_type s)
36  : PacketParserBase(i,s), ListPolicy(policy)
37 {}
38 
39 template <class ListPolicy>
40 prefix_ senf::PacketParserBase::size_type
41 senf::ListParser<ListPolicy>::bytes()
42  const
43 {
44  return ListPolicy::bytes(i(),state());
45 }
46 
47 template <class ListPolicy, class OutputIterator>
48 prefix_ void senf::copy(ListParser_Container<ListPolicy> container, OutputIterator result)
49 {
50  for (typename ListParser_Container<ListPolicy>::iterator i (container.begin()); i != container.end(); ++i) {
51  *result++ = *i++;
52  }
53 }
54 
55 //-/////////////////////////////////////////////////////////////////////////////////////////////////
56 // Container interface
57 
58 template <class ListPolicy>
59 prefix_ typename senf::ListParser<ListPolicy>::container_type
60 senf::ListParser<ListPolicy>::container()
61  const
62 {
63  return container_type(*this);
64 }
65 
66 template <class ListPolicy>
67 prefix_ senf::PacketParserBase::size_type
68 senf::ListParser<ListPolicy>::size()
69  const
70 {
71  return ListPolicy::size(i(),state());
72 }
73 
74 template <class ListPolicy>
75 prefix_ bool senf::ListParser<ListPolicy>::empty()
76  const
77 {
78  container_type c (*this);
79  return c.empty();
80 }
81 
82 template <class ListPolicy>
83 template <class InputIterator>
84 prefix_ void senf::ListParser<ListPolicy>::assign(InputIterator first, InputIterator last)
85 {
86  container_type c (*this);
87  c.clear();
88  while (first != last)
89  c.push_back( *first++);
90 }
91 
92 template <class ListPolicy>
93 template <class Range>
94 prefix_ void senf::ListParser<ListPolicy>::assign(Range const & range)
95 {
96  assign(boost::begin(range), boost::end(range));
97 }
98 
99 template <class ListPolicy>
100 prefix_ typename senf::ListParser<ListPolicy>::value_type
101 senf::ListParser<ListPolicy>::front()
102  const
103 {
104  SENF_ASSERT( ! empty(), "front() called on empty list" );
105  container_type c (*this);
106  return *(c.begin());
107 }
108 
109 template <class ListPolicy>
110 template <class Value>
111 prefix_ void senf::ListParser<ListPolicy>::push_back(Value const & value, size_type n)
112  const
113 {
114  container_type c (*this);
115  c.push_back(value,n);
116 }
117 
118 template <class ListPolicy>
119 prefix_ typename senf::ListParser<ListPolicy>::value_type
120 senf::ListParser<ListPolicy>::push_back_space(size_type n)
121  const
122 {
123  container_type c (*this);
124  return c.push_back_space(n);
125 }
126 
127 template <class ListPolicy>
128 template <class Value>
129 prefix_ void senf::ListParser<ListPolicy>::push_front(Value const & value, size_type n)
130  const
131 {
132  container_type c (*this);
133  c.push_front(value,n);
134 }
135 
136 template <class ListPolicy>
137 prefix_ typename senf::ListParser<ListPolicy>::value_type
138 senf::ListParser<ListPolicy>::push_front_space(size_type n)
139  const
140 {
141  container_type c (*this);
142  return c.push_front_space(n);
143 }
144 
145 template <class ListPolicy>
146 prefix_ void senf::ListParser<ListPolicy>::resize(size_type n)
147  const
148 {
149  container_type c (*this);
150  c.resize(n);
151 }
152 
153 template <class ListPolicy>
154 template <class Value>
155 prefix_ void senf::ListParser<ListPolicy>::resize(size_type n, Value value)
156  const
157 {
158  container_type c (*this);
159  c.resize(n,value);
160 }
161 
162 template <class ListPolicy>
163 prefix_ typename senf::ListParser<ListPolicy> &
164 senf::ListParser<ListPolicy>::get(ListPolicy & p)
165 {
166  return static_cast<ListParser &>(p);
167 }
168 
169 template <class ListPolicy>
170 prefix_ typename senf::ListParser<ListPolicy> const &
171 senf::ListParser<ListPolicy>::get(ListPolicy const & p)
172 {
173  return static_cast<ListParser const &>(p);
174 }
175 
176 //-/////////////////////////////////////////////////////////////////////////////////////////////////
177 // senf::detail::ListParser_Iterator<ElementParser,IteratorPolicy>
178 
179 template <class Container>
180 prefix_ senf::detail::ListParser_Iterator<Container>::ListParser_Iterator()
181 {}
182 
183 template <class Container>
184 prefix_ senf::detail::ListParser_Iterator<Container>::ListParser_Iterator(Container const & c)
185  : c_ (&c)
186 {}
187 
188 template <class Container>
189 prefix_ senf::PacketParserBase::data_iterator
190 senf::detail::ListParser_Iterator<Container>::raw()
191  const
192 {
193  return c_->Container::policy::raw(*c_, *this);
194 }
195 
196 template <class Container>
197 prefix_ senf::PacketParserBase::data_iterator
198 senf::detail::ListParser_Iterator<Container>::i()
199  const
200 {
201  return i_;
202 }
203 
204 template <class Container>
205 prefix_ typename senf::detail::ListParser_Iterator<Container> &
206 senf::detail::ListParser_Iterator<Container>::get(typename Container::policy::iterator_data & d)
207 {
208  return static_cast<ListParser_Iterator &>(d);
209 }
210 
211 template <class Container>
212 prefix_ typename senf::detail::ListParser_Iterator<Container> const &
213 senf::detail::ListParser_Iterator<Container>::
214 get(typename Container::policy::iterator_data const & d)
215 {
216  return static_cast<ListParser_Iterator const &>(d);
217 }
218 
219 template <class Container>
220 prefix_ typename senf::detail::ListParser_Iterator<Container>::value_type
221 senf::detail::ListParser_Iterator<Container>::dereference()
222  const
223 {
224  return value_type(i_,c_->state());
225 }
226 
227 template <class Container>
228 prefix_ bool
229 senf::detail::ListParser_Iterator<Container>::equal(ListParser_Iterator const & other)
230  const
231 {
232  return i_ == other.i_;
233 }
234 
235 template <class Container>
236 prefix_ void senf::detail::ListParser_Iterator<Container>::increment()
237 {
238  i_ = c_->Container::policy::next(*c_, *this);
239 }
240 
241 //-/////////////////////////////////////////////////////////////////////////////////////////////////
242 // senf::ListParser_Container<ListPolicy>
243 
244 // Structors and default members
245 
246 template <class ListPolicy>
247 prefix_ senf::ListParser_Container<ListPolicy>::
248 ListParser_Container(parser_type const & list)
249  : ListPolicy(static_cast<typename parser_type::policy const &>(list)),
250  state_(list.state()), i_(std::distance(data().begin(),list.i()))
251 {
252  ListPolicy::construct(*this);
253 }
254 
255 template <class ListPolicy>
256 prefix_ senf::ListParser_Container<ListPolicy>::~ListParser_Container()
257 {
258  ListPolicy::update(*this);
259  ListPolicy::destruct(*this);
260 }
261 
262 // Accessors
263 
264 template <class ListPolicy>
265 prefix_ typename senf::ListParser_Container<ListPolicy>::size_type
266 senf::ListParser_Container<ListPolicy>::size()
267  const
268 {
269  ListPolicy::update(*this);
270  return ListPolicy::size(i(),state());
271 }
272 
273 template <class ListPolicy>
274 prefix_ bool senf::ListParser_Container<ListPolicy>::empty()
275  const
276 {
277  ListPolicy::update(*this);
278  return begin() == end();
279 }
280 
281 template <class ListPolicy>
282 prefix_ typename senf::ListParser_Container<ListPolicy>::iterator
283 senf::ListParser_Container<ListPolicy>::begin()
284  const
285 {
286  ListPolicy::update(*this);
287  iterator i (*this);
288  i.i_ = ListPolicy::setBegin(*this, i);
289  return i;
290 }
291 
292 template <class ListPolicy>
293 prefix_ typename senf::ListParser_Container<ListPolicy>::iterator
294 senf::ListParser_Container<ListPolicy>::end()
295  const
296 {
297  ListPolicy::update(*this);
298  iterator i (*this);
299  i.i_ = ListPolicy::setEnd(*this, i);
300  return i;
301 }
302 
303 template <class ListPolicy>
304 prefix_ typename senf::ListParser_Container<ListPolicy>::value_type
305 senf::ListParser_Container<ListPolicy>::front()
306  const
307 {
308  SENF_ASSERT( ! empty(), "front() called on empty list" );
309  return *begin();
310 }
311 
312 // Mutators
313 
314 template <class ListPolicy>
315 template <class Value>
316 prefix_ void senf::ListParser_Container<ListPolicy>::insert(iterator pos,
317  Value const & t)
318 {
319  insert(pos,1,t);
320 }
321 
322 template <class ListPolicy>
323 prefix_ void senf::ListParser_Container<ListPolicy>::erase(iterator f, iterator l)
324 {
325  erase(f,std::distance(f,l));
326 }
327 
328 template <class ListPolicy>
329 template <class Value>
330 prefix_ void senf::ListParser_Container<ListPolicy>::push_back(Value const & value,
331  size_type n)
332 {
333  insert(end(),n,value);
334 }
335 
336 template <class ListPolicy>
337 prefix_ typename senf::ListParser_Container<ListPolicy>::value_type
338 senf::ListParser_Container<ListPolicy>::push_back_space(size_type n)
339 {
340  return shift(end(),n);
341 }
342 
343 template <class ListPolicy>
344 template <class Value>
345 prefix_ void senf::ListParser_Container<ListPolicy>::push_front(Value const & value,
346  size_type n)
347 {
348  insert(begin(),n,value);
349 }
350 
351 template <class ListPolicy>
352 prefix_ typename senf::ListParser_Container<ListPolicy>::value_type
353 senf::ListParser_Container<ListPolicy>::push_front_space(size_type n)
354 {
355  return shift(begin(),n);
356 }
357 
358 // Parser interface
359 
360 template <class ListPolicy>
361 prefix_ typename senf::ListParser_Container<ListPolicy>::parser_type
362 senf::ListParser_Container<ListPolicy>::parser()
363  const
364 {
365  ListPolicy::update(*this);
366  return parser_type(i(),state());
367 }
368 
369 template <class ListPolicy>
370 prefix_ typename senf::ListParser_Container<ListPolicy>::data_iterator
371 senf::ListParser_Container<ListPolicy>::i()
372  const
373 {
374  return boost::next(data().begin(),i_);
375 }
376 
377 template <class ListPolicy>
378 prefix_ typename senf::ListParser_Container<ListPolicy>::state_type
379 senf::ListParser_Container<ListPolicy>::state()
380  const
381 {
382  return state_;
383 }
384 
385 template <class ListPolicy>
386 prefix_ senf::PacketData & senf::ListParser_Container<ListPolicy>::data()
387  const
388 {
389  return *state_;
390 }
391 
392 template <class ListPolicy>
393 prefix_ typename senf::ListParser_Container<ListPolicy>::size_type
394 senf::ListParser_Container<ListPolicy>::bytes()
395  const
396 {
397  ListPolicy::update(*this);
398  return ListPolicy::bytes(i(),state());
399 }
400 
401 template <class ListPolicy>
402 prefix_ void senf::ListParser_Container<ListPolicy>::init()
403  const
404 {
405  parser().init();
406 }
407 
408 //-/////////////////////////////////////////////////////////////////////////////////////////////////
409 #undef prefix_
410 
411 
412 // Local Variables:
413 // mode: c++
414 // fill-column: 100
415 // comment-column: 40
416 // c-file-style: "senf"
417 // indent-tabs-mode: nil
418 // ispell-local-dictionary: "american"
419 // compile-command: "scons -u test"
420 // End: