ListParser.ct
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 non-inline template implementation */
16 
17 #include "ListParser.ih"
18 
19 // Custom includes
20 #include <senf/Utils/senfassert.hh>
21 
22 #define prefix_
23 //-/////////////////////////////////////////////////////////////////////////////////////////////////
24 
25 //-/////////////////////////////////////////////////////////////////////////////////////////////////
26 // senf::ListParser<ElementParser,ListPolicy>
27 
28 template <class ListPolicy>
29 prefix_ void senf::ListParser<ListPolicy>::init()
30  const
31 {
32  ListPolicy::init(i(),state());
33  container_type c (*this);
34  typename container_type::iterator i (c.begin());
35  typename container_type::iterator const e (c.end());
36  for (; i!=e; ++i)
37  i->init();
38 }
39 
40 template <class ListPolicy>
41 prefix_ typename senf::ListParser<ListPolicy>::value_type
42 senf::ListParser<ListPolicy>::back()
43  const
44 {
45  SENF_ASSERT( ! empty(), "back() called on empty list" );
46  container_type c (*this);
47  typename container_type::iterator i (c.begin()), j;
48  typename container_type::iterator const e (c.end());
49  for (j=i; i!=e; j=i, ++i) ;
50  return *j;
51 }
52 
53 //-/////////////////////////////////////////////////////////////////////////////////////////////////
54 // senf::ListParser_Container<ListPolicy>
55 
56 template <class ListPolicy>
57 prefix_ typename senf::ListParser_Container<ListPolicy>::value_type
58 senf::ListParser_Container<ListPolicy>::back()
59  const
60 {
61  SENF_ASSERT( ! empty(), "back() called on empty list" );
62  iterator i (begin()), j;
63  iterator const e (end());
64  for (j=i; i!=e; j=i, ++i) ;
65  return *j;
66 }
67 
68 template <class ListPolicy>
69 prefix_ typename senf::ListParser_Container<ListPolicy>::value_type
70 senf::ListParser_Container<ListPolicy>::shift(iterator pos, size_type n)
71 {
72  ListPolicy::update(*this);
73  safe_data_iterator sp (data(),pos.raw());
74  for (; n>0; --n) {
75  data().insert(sp,senf::init_bytes<value_type>::value,0);
76  value_type(sp,state()).init();
77  ListPolicy::insert(*this,sp);
78  }
79  return value_type(sp,state());
80 }
81 
82 template <class ListPolicy>
83 template <class Value>
84 prefix_ void senf::ListParser_Container<ListPolicy>::insert(iterator pos,
85  size_type n,
86  Value const & t)
87 {
88  ListPolicy::update(*this);
89  safe_data_iterator sp (data(),pos.raw());
90  for (; n>0; --n) {
91  data().insert(sp,senf::init_bytes<value_type>::value,0);
92  value_type(sp,state()).init();
93  value_type(sp,state()) << t;
94  ListPolicy::insert(*this,sp);
95  }
96 }
97 
98 #ifndef DOXYGEN
99 template <class ListPolicy>
100 template <class ForwardIterator>
101 prefix_ void senf::ListParser_Container<ListPolicy>::
102 insert(iterator pos, ForwardIterator f, ForwardIterator l,
103  typename boost::disable_if< boost::is_convertible<ForwardIterator,size_type> >::type *)
104 {
105  ListPolicy::update(*this);
106  safe_data_iterator sp (data(),pos.raw());
107  for (; f!=l; ++f) {
108  data().insert(sp,senf::init_bytes<value_type>::value,0);
109  value_type(sp,state()).init();
110  value_type(sp,state()) << *f;
111  ListPolicy::insert(*this,sp);
112  sp += senf::bytes(value_type(sp,state()));
113  }
114 }
115 #else
116 template <class ListPolicy>
117 template <class ForwardIterator>
118 prefix_ void senf::ListParser_Container<ListPolicy>::
119 insert(iterator pos, ForwardIterator f, ForwardIterator l)
120 {}
121 #endif
122 
123 template <class ListPolicy>
124 prefix_ void senf::ListParser_Container<ListPolicy>::erase(iterator pos,
125  size_type n)
126 {
127  ListPolicy::update(*this);
128  safe_data_iterator sp (data(),pos.raw());
129  for (; n>0; --n) {
130  ListPolicy::erase(*this,sp);
131  data().erase(sp,boost::next(sp,senf::bytes(value_type(sp,state()))));
132  }
133 }
134 
135 template <class ListPolicy>
136 prefix_ void senf::ListParser_Container<ListPolicy>::clear()
137 {
138  size_type sz (bytes());
139  if (sz > ListPolicy::init_bytes)
140  data().erase(boost::next(i(),ListPolicy::init_bytes),boost::next(i(),sz));
141  else
142  data().insert(boost::next(i(),sz), ListPolicy::init_bytes-sz, 0u);
143  std::fill(i(),boost::next(i(),ListPolicy::init_bytes), 0u);
144  ListPolicy::init(i(),state());
145 }
146 
147 template <class ListPolicy>
148 prefix_ void senf::ListParser_Container<ListPolicy>::resize(size_type n)
149 {
150  size_type sz (size());
151  if (sz>n)
152  erase(boost::next(begin(),n),end());
153  else
154  push_back_space(n-sz);
155 }
156 
157 template <class ListPolicy>
158 template <class Value>
159 prefix_ void senf::ListParser_Container<ListPolicy>::resize(size_type n,
160  Value value)
161 {
162  size_type sz (size());
163  if (sz>n)
164  erase(boost::next(begin(),n),end());
165  else
166  push_back(value,n-sz);
167 }
168 
169 //-/////////////////////////////////////////////////////////////////////////////////////////////////
170 #undef prefix_
171 
172 
173 // Local Variables:
174 // mode: c++
175 // fill-column: 100
176 // comment-column: 40
177 // c-file-style: "senf"
178 // indent-tabs-mode: nil
179 // ispell-local-dictionary: "american"
180 // compile-command: "scons -u test"
181 // End: