Move sourcecode into 'senf/' directory
[senf.git] / senf / 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     container c (*this);
43     typename container::iterator i (c.begin());
44     typename container::iterator const e (c.end());
45     for(; i!=e; ++i)
46         i->init();
47 }
48
49 template <class ListPolicy>
50 prefix_ typename senf::ListParser<ListPolicy>::value_type
51 senf::ListParser<ListPolicy>::back()
52     const
53 {
54     SENF_ASSERT( ! empty() );
55     container c(*this);
56     typename container::iterator i (c.begin()), j;
57     typename container::iterator const e (c.end());
58     for (j=i; i!=e; j=i, ++i) ;
59     return *j;
60 }
61
62 ///////////////////////////////////////////////////////////////////////////
63 // senf::ListParser_Container<ListPolicy>
64
65 template <class ListPolicy>
66 prefix_ typename senf::ListParser_Container<ListPolicy>::value_type
67 senf::ListParser_Container<ListPolicy>::back()
68     const
69 {
70     SENF_ASSERT( ! empty() );
71     iterator i (begin()), j;
72     iterator const e (end());
73     for (j=i; i!=e; j=i, ++i) ;
74     return *j;
75 }
76
77 template <class ListPolicy>
78 prefix_ void senf::ListParser_Container<ListPolicy>::shift(iterator pos, size_type n)
79 {
80     ListPolicy::update(*this);
81     safe_data_iterator sp (data(),pos.raw());
82     for (; n>0; --n) {
83         data().insert(sp,senf::init_bytes<value_type>::value,0);
84         value_type(sp,state()).init();
85         ListPolicy::insert(*this,sp);
86     }
87 }
88
89 template <class ListPolicy>
90 template <class Value>
91 prefix_ void senf::ListParser_Container<ListPolicy>::insert(iterator pos,
92                                                             size_type n,
93                                                             Value const & t)
94 {
95     ListPolicy::update(*this);
96     safe_data_iterator sp (data(),pos.raw());
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(*this,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(*this);
113     safe_data_iterator sp (data(),pos.raw());
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(*this,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(*this);
135     safe_data_iterator sp (data(),pos.raw());
136     for (; n>0; --n) {
137         ListPolicy::erase(*this,sp);
138         data().erase(sp,boost::next(sp,senf::bytes(value_type(sp,state()))));
139     }
140 }
141
142 template <class ListPolicy>
143 prefix_ void senf::ListParser_Container<ListPolicy>::clear()
144 {
145     size_type sz (bytes());
146     if (sz > ListPolicy::init_bytes)
147         data().erase(boost::next(i(),ListPolicy::init_bytes),boost::next(i(),sz));
148     else
149         data().insert(boost::next(i(),sz), ListPolicy::init_bytes-sz, 0u);
150     std::fill(i(),boost::next(i(),ListPolicy::init_bytes), 0u);
151     ListPolicy::init(i(),state());
152 }
153
154 template <class ListPolicy>
155 prefix_ void senf::ListParser_Container<ListPolicy>::resize(size_type n)
156 {
157     size_type sz (size());
158     if (sz>n)
159         erase(boost::next(begin(),n),end());
160     else
161         push_back_space(n-sz);
162 }
163
164 template <class ListPolicy>
165 template <class Value>
166 prefix_ void senf::ListParser_Container<ListPolicy>::resize(size_type n,
167                                                             Value value)
168 {
169     size_type sz (size());
170     if (sz>n)
171         erase(boost::next(begin(),n),end());
172     else
173         push_back(value,n-sz);
174 }
175
176 ///////////////////////////////ct.e////////////////////////////////////////
177 #undef prefix_
178
179 \f
180 // Local Variables:
181 // mode: c++
182 // fill-column: 100
183 // comment-column: 40
184 // c-file-style: "senf"
185 // indent-tabs-mode: nil
186 // ispell-local-dictionary: "american"
187 // compile-command: "scons -u test"
188 // End: