Reapply changes missed during the previous merge (duh ...)
[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 template <class ListPolicy>
103 template <class ForwardIterator>
104 prefix_ void senf::Parse_List_Container<ListPolicy>::
105 insert(iterator pos, ForwardIterator f, ForwardIterator l,
106        typename boost::disable_if< boost::is_convertible<ForwardIterator,size_type> >::type *)
107 {
108     ListPolicy::update(i(),state());
109     safe_data_iterator sp (data(),pos.raw());
110     safe_data_iterator si (data(),i());
111     for (; f!=l; ++f) {
112         data().insert(sp,senf::init_bytes<value_type>::value,0);
113         value_type(sp,state()).init();
114         value_type(sp,state()) << *f;
115         ListPolicy::insert(si,state(),sp);
116         sp += senf::bytes(value_type(sp,state()));
117     }
118 }
119
120 template <class ListPolicy>
121 prefix_ void senf::Parse_List_Container<ListPolicy>::erase(iterator pos,
122                                                            size_type n)
123 {
124     ListPolicy::update(i(),state());
125     safe_data_iterator si (data(),i());
126     safe_data_iterator sp (data(),pos.raw());
127     for (; n>0; --n) {
128         ListPolicy::erase(si,state(),sp);
129         data().erase(sp,boost::next(sp,senf::bytes(value_type(sp,state()))));
130     }
131 }
132
133 template <class ListPolicy>
134 prefix_ void senf::Parse_List_Container<ListPolicy>::clear()
135 {
136     size_type sz (bytes());
137     if (sz > ListPolicy::init_bytes)
138         data().erase(boost::next(i(),ListPolicy::init_bytes),boost::next(i(),sz));
139     else
140         data().insert(boost::next(i(),sz), ListPolicy::init_bytes-sz, 0u);
141     std::fill(i(),boost::next(i(),ListPolicy::init_bytes), 0u);
142     ListPolicy::init(i(),state());
143 }
144
145 template <class ListPolicy>
146 prefix_ void senf::Parse_List_Container<ListPolicy>::resize(size_type n)
147 {
148     size_type sz (size());
149     if (sz>n)
150         erase(boost::next(begin(),n),end());
151     else
152         push_back_space(n-sz);
153 }
154
155 template <class ListPolicy>
156 template <class Value>
157 prefix_ void senf::Parse_List_Container<ListPolicy>::resize(size_type n,
158                                                             Value value)
159 {
160     size_type sz (size());
161     if (sz>n)
162         erase(boost::next(begin(),n),end());
163     else
164         push_back(value,n-sz);
165 }
166
167 ///////////////////////////////ct.e////////////////////////////////////////
168 #undef prefix_
169
170 \f
171 // Local Variables:
172 // mode: c++
173 // fill-column: 100
174 // comment-column: 40
175 // c-file-style: "senf"
176 // indent-tabs-mode: nil
177 // ispell-local-dictionary: "american"
178 // compile-command: "scons -u test"
179 // End: