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