125921dee60132bcf3113fc94b136e9a10166f66
[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_ senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::ListOptionTypeParser_Policy() {
38 }
39
40 template<class ElementParser, class AuxPolicy>
41 template<class Arg>
42 prefix_ senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::ListOptionTypeParser_Policy(Arg const & arg) :AuxPolicy(arg) {
43 }
44
45 template<class ElementParser, class AuxPolicy>
46 prefix_ typename senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::size_type
47 senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::bytes(data_iterator i, state_type s) const {
48     return AuxPolicy::aux(i, s) + AuxPolicy::aux_bytes;
49 }
50
51 template<class ElementParser, class AuxPolicy>
52 prefix_ typename senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::size_type
53 senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::size(data_iterator i, state_type s) const {
54     parser_type p(*this, i, s);
55     container_type c(p);
56     return std::distance(c.begin(), c.end());
57 }
58
59 template<class ElementParser, class AuxPolicy>
60 prefix_ void
61 senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::init(data_iterator i, state_type s) const {
62     AuxPolicy::aux(0, i, s);
63 }
64
65 ///////////////////////////////////////////////////////////////////////////
66 // senf::detail::ListOptionTypeParser_Policy<ElementParser,BytesParser>
67
68 //constructor
69 template<class ElementParser, class AuxPolicy>
70 prefix_ senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::container_policy::container_policy(parser_policy const & p) :
71     AuxPolicy(p) {
72 }
73
74 //destructor
75 template<class ElementParser, class AuxPolicy>
76 prefix_ senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::container_policy::~container_policy()
77 {}
78
79 //construct method
80 template<class ElementParser, class AuxPolicy>
81 prefix_ void
82 senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::container_policy::construct(container_type & c) {
83     data_iterator i = c.i();
84     realAux_ = (AuxPolicy::aux(i, c.state()) * 8) + 6;
85     data_iterator e = i + realAux_;
86     for (n_ = 0; i != e; ++n_) {
87         unsigned int elByte = senf::bytes(ElementParser(i, c.state()));
88         if (((i + elByte) == e) && (i[0] == 0u || i[0] == 1u)) { //check wether last element is padding or not
89             realAux_ -= std::distance(i, e);
90             c.data().erase(i, e); //delete padding
91             e = i; //set end iterator
92         } else
93             std::advance(i, elByte);
94     }
95     //    container_size_ = std::distance(i,e);
96     container_size_ = c.data().size(); //set actual size without padding
97 }
98
99 //destruct method
100 template<class ElementParser, class AuxPolicy>
101 prefix_ void
102 senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::container_policy::destruct(container_type & c) {
103     //  data_iterator i (AuxPolicy::adjust(parser_type::get(p).i(), parser_type::get(p).state()));
104     data_iterator i = c.i();
105     data_iterator const e = i + realAux_;
106     unsigned int padBytes = (realAux_ % 8);
107     c.data().insert(e, padBytes, 0u);
108     if (padBytes > 0) {
109         if (padBytes > 1) {
110             e[0] = 1;
111             e[1] = padBytes - 2;
112         } else
113             e[0] = 0;
114         container_size_ += padBytes;
115         ++n_;
116     }
117     AuxPolicy::aux((realAux_ / 8 - 1), i, c.state());
118 }
119
120 //bytes()
121 template<class ElementParser, class AuxPolicy>
122 prefix_ typename senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::size_type
123 senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::container_policy::bytes(data_iterator i,state_type s) const {
124     return (realAux_ + 2);
125 }
126
127 //size()
128 template<class ElementParser, class AuxPolicy>
129 prefix_ typename senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::size_type
130 senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::container_policy::size(data_iterator i,state_type s) const {
131     return n_;
132 }
133
134 //init()
135 template<class ElementParser, class AuxPolicy>
136 prefix_ void
137 senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::container_policy::init(data_iterator i, state_type s) {
138     n_ = 0;
139     container_size_ = s->size();
140     AuxPolicy::aux(0, i, s);
141 }
142
143 //erase()
144 template<class ElementParser, class AuxPolicy>
145 prefix_ void
146 senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::container_policy::erase(container_type & c, data_iterator p) {
147     size_type b(senf::bytes(ElementParser(p, c.state()))); //length of parser
148     //    AuxPolicy::aux(
149     //                  AuxPolicy::aux( c.i(), c.state()) -b,
150     //                  c.i(),
151     //                  c.state());
152     realAux_ -= b;
153     --n_;
154     // The container will be reduced by b bytes directly after this call
155     container_size_ = c.data().size() - b;
156 }
157
158 //insert()
159 template<class ElementParser, class AuxPolicy>
160 prefix_ void
161 senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::container_policy::insert(container_type & c, data_iterator p) {
162     size_type b(senf::bytes(ElementParser(p, c.state())));
163     //    AuxPolicy::aux( AuxPolicy::aux(c.i(), c.state())+b, c.i(), c.state());
164     realAux_ += b;
165     ++n_;
166     container_size_ = c.data().size();
167 }
168
169 //update()
170 template<class ElementParser, class AuxPolicy>
171 prefix_ void
172 senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::container_policy::update(container_type const & c) const {
173     if (container_size_ == c.data().size())
174         return;
175     data_iterator i(AuxPolicy::adjust(c.i(), c.state()));
176     data_iterator j(i);
177     for (size_type n(n_); n; --n, std::advance(j, senf::bytes(ElementParser(j,c.state()))));
178     realAux_ = std::distance(i, j);
179     //    aux( std::distance(i,j), c.i(), c.state() );
180     container_size_ = c.data().size();
181 }
182
183 //setbegin()
184 template<class ElementParser, class AuxPolicy>
185 prefix_ typename senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::data_iterator
186 senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::container_policy::setBegin(container_type const & c, iterator_data & d) const {
187     return c.i();
188     //    return AuxPolicy::adjust(c.i(), c.state());
189 }
190
191 //setEnd()
192 template<class ElementParser, class AuxPolicy>
193 prefix_ typename senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::data_iterator
194 senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::container_policy::setEnd(container_type const & c, iterator_data & d) const { //wtf??
195     return boost::next(AuxPolicy::adjust(c.i(), c.state()), aux(c.i(),c.state()));
196 }
197
198 //setFromPosition()
199 template<class ElementParser, class AuxPolicy>
200 prefix_ void
201 senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::container_policy::setFromPosition(container_type const & c, iterator_data & d, data_iterator p) const {
202 }
203
204 //next()
205 template<class ElementParser, class AuxPolicy>
206 prefix_ typename senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::data_iterator
207 senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::container_policy::next(container_type const & c, iterator_data & d) const {
208     return boost::next(container_type::iterator::get(d).i(), senf::bytes(ElementParser(container_type::iterator::get(d).i(), c.state())));
209 }
210
211 //raw()
212 template<class ElementParser, class AuxPolicy>
213 prefix_ typename senf::detail::ListOptionTypeParser_Policy<ElementParser,AuxPolicy>::container_policy::data_iterator
214 senf::detail::ListOptionTypeParser_Policy<ElementParser, AuxPolicy>::container_policy::raw(container_type const & c, iterator_data const & d) const {
215     return container_type::iterator::get(d).i();
216 }
217
218 ///////////////////////////////cti.e///////////////////////////////////////
219 #undef prefix_
220
221 \f
222 // Local Variables:
223 // mode: c++
224 // fill-column: 100
225 // comment-column: 40
226 // c-file-style: "senf"
227 // indent-tabs-mode: nil
228 // ispell-local-dictionary: "american"
229 // compile-command: "scons -u test"
230 // End: