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