PPI: Checkin of first compiling (yet not working) version
[senf.git] / Packets / ParseList.ct
1 // Copyright (C) 2007 
2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 //     Stefan Bund <g0dil@berlios.de>
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
21 /** \file
22     \brief ParseList non-inline template implementation  */
23
24 #include "ParseList.ih"
25
26 // Custom includes
27
28 #define prefix_
29 ///////////////////////////////ct.p////////////////////////////////////////
30
31 ///////////////////////////////////////////////////////////////////////////
32 // senf::Parse_List<ElementParser,ListPolicy>
33
34 template <class ListPolicy>
35 prefix_ void senf::Parse_List<ListPolicy>::init()
36     const
37 {
38     ListPolicy::init(i(),state());
39     iterator i (begin());
40     iterator const e (end());
41     for(; i!=e; ++i)
42         i->init();
43 }
44
45 template <class ListPolicy>
46 prefix_ typename senf::Parse_List<ListPolicy>::value_type
47 senf::Parse_List<ListPolicy>::back()
48     const
49 {
50     BOOST_ASSERT( ! empty() );
51     iterator i (begin()), j;
52     iterator const e (end());
53     for (j=i; i!=e; j=i, ++i) ;
54     return *j;
55 }
56
57 ///////////////////////////////////////////////////////////////////////////
58 // senf::Parse_List_Container<ListPolicy>
59
60 template <class ListPolicy>
61 prefix_ typename senf::Parse_List_Container<ListPolicy>::value_type
62 senf::Parse_List_Container<ListPolicy>::back()
63     const
64 {
65     BOOST_ASSERT( ! empty() );
66     iterator i (begin()), j;
67     iterator const e (end());
68     for (j=i; i!=e; j=i, ++i) ;
69     return *j;
70 }
71
72 template <class ListPolicy>
73 prefix_ void senf::Parse_List_Container<ListPolicy>::shift(iterator pos, size_type n)
74 {
75     ListPolicy::update(i(),state());
76     safe_data_iterator sp (data(),pos.raw());
77     safe_data_iterator si (data(),i());
78     for (; n>0; --n) {
79         data().insert(sp,senf::init_bytes<value_type>::value,0);
80         value_type(sp,state()).init();
81         ListPolicy::insert(si,state(),sp);
82     }
83 }
84
85 template <class ListPolicy>
86 template <class Value>
87 prefix_ void senf::Parse_List_Container<ListPolicy>::insert(iterator pos,
88                                                             size_type n,
89                                                             Value const & t)
90 {
91     ListPolicy::update(i(),state());
92     safe_data_iterator sp (data(),pos.raw());
93     safe_data_iterator si (data(),i());
94     for (; n>0; --n) {
95         data().insert(sp,senf::init_bytes<value_type>::value,0);
96         value_type(sp,state()).init();
97         value_type(sp,state()) << t;
98         ListPolicy::insert(si,state(),sp);
99     }
100 }
101
102 #ifndef DOXYGEN
103 template <class ListPolicy>
104 template <class ForwardIterator>
105 prefix_ void senf::Parse_List_Container<ListPolicy>::
106 insert(iterator pos, ForwardIterator f, ForwardIterator l,
107        typename boost::disable_if< boost::is_convertible<ForwardIterator,size_type> >::type *)
108 {
109     ListPolicy::update(i(),state());
110     safe_data_iterator sp (data(),pos.raw());
111     safe_data_iterator si (data(),i());
112     for (; f!=l; ++f) {
113         data().insert(sp,senf::init_bytes<value_type>::value,0);
114         value_type(sp,state()).init();
115         value_type(sp,state()) << *f;
116         ListPolicy::insert(si,state(),sp);
117         sp += senf::bytes(value_type(sp,state()));
118     }
119 }
120 #else
121 template <class ListPolicy>
122 template <class ForwardIterator>
123 prefix_ void senf::Parse_List_Container<ListPolicy>::
124 insert(iterator pos, ForwardIterator f, ForwardIterator l)
125 {}
126 #endif
127
128 template <class ListPolicy>
129 prefix_ void senf::Parse_List_Container<ListPolicy>::erase(iterator pos,
130                                                            size_type n)
131 {
132     ListPolicy::update(i(),state());
133     safe_data_iterator si (data(),i());
134     safe_data_iterator sp (data(),pos.raw());
135     for (; n>0; --n) {
136         ListPolicy::erase(si,state(),sp);
137         data().erase(sp,boost::next(sp,senf::bytes(value_type(sp,state()))));
138     }
139 }
140
141 template <class ListPolicy>
142 prefix_ void senf::Parse_List_Container<ListPolicy>::clear()
143 {
144     size_type sz (bytes());
145     if (sz > ListPolicy::init_bytes)
146         data().erase(boost::next(i(),ListPolicy::init_bytes),boost::next(i(),sz));
147     else
148         data().insert(boost::next(i(),sz), ListPolicy::init_bytes-sz, 0u);
149     std::fill(i(),boost::next(i(),ListPolicy::init_bytes), 0u);
150     ListPolicy::init(i(),state());
151 }
152
153 template <class ListPolicy>
154 prefix_ void senf::Parse_List_Container<ListPolicy>::resize(size_type n)
155 {
156     size_type sz (size());
157     if (sz>n)
158         erase(boost::next(begin(),n),end());
159     else
160         push_back_space(n-sz);
161 }
162
163 template <class ListPolicy>
164 template <class Value>
165 prefix_ void senf::Parse_List_Container<ListPolicy>::resize(size_type n,
166                                                             Value value)
167 {
168     size_type sz (size());
169     if (sz>n)
170         erase(boost::next(begin(),n),end());
171     else
172         push_back(value,n-sz);
173 }
174
175 ///////////////////////////////ct.e////////////////////////////////////////
176 #undef prefix_
177
178 \f
179 // Local Variables:
180 // mode: c++
181 // fill-column: 100
182 // comment-column: 40
183 // c-file-style: "senf"
184 // indent-tabs-mode: nil
185 // ispell-local-dictionary: "american"
186 // compile-command: "scons -u test"
187 // End: