4c8bc04f31e5acab3d1cdadeed97c1d1aa6289f1
[senf.git] / senf / Packets / PacketImpl.cci
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 PacketImpl inline non-template implementation */
25
26 // Custom includes
27
28 #define prefix_ inline
29 //-/////////////////////////////////////////////////////////////////////////////////////////////////
30
31 //-/////////////////////////////////////////////////////////////////////////////////////////////////
32 // senf::detail::AnnotationRegistry
33
34 prefix_ std::string senf::detail::AnnotationRegistry::name(key_type key)
35     const
36 {
37     Registry::const_iterator i (registry_.find(key));
38     return i == registry_.end() ? "" : i->second->v_name();
39 }
40
41 prefix_ bool senf::detail::AnnotationRegistry::isComplex(key_type key)
42     const
43 {
44     Registry::const_iterator i (registry_.find(key));
45     return i != registry_.end() && i->second->v_isComplex();
46 }
47
48 prefix_ unsigned senf::detail::AnnotationRegistry::size(key_type key)
49     const
50 {
51     Registry::const_iterator i (registry_.find(key));
52     return i == registry_.end() ? 0 : i->second->v_size();
53 }
54
55 prefix_ senf::detail::AnnotationRegistry::iterator senf::detail::AnnotationRegistry::begin()
56     const
57 {
58     return boost::make_transform_iterator(index_.begin(),
59                                           __gnu_cxx::select2nd<Index::value_type>());
60 }
61
62 prefix_ senf::detail::AnnotationRegistry::iterator senf::detail::AnnotationRegistry::end()
63     const
64 {
65     return boost::make_transform_iterator(index_.end(),
66                                           __gnu_cxx::select2nd<Index::value_type>());
67 }
68
69 prefix_ senf::detail::AnnotationRegistry::AnnotationRegistry()
70     : simpleAnnotationCount_ (0), complexAnnotationCount_ (0)
71 {}
72
73 //-/////////////////////////////////////////////////////////////////////////////////////////////////
74
75 // Memory management:
76 //
77 // * The PacketImpl destructor will *explicitly* clean-up the interpreters_ list by removing
78 //   each element from the list and deleting it if it's (intrusive) refcount is 0
79 // * The PacketInterpreters use safe hooks -> they know whether they are part of a list or not
80 // * PacketHandle has an intrusive_ptr to PacketInterpreterBase. The intrusive_ptr_add_ref
81 //   will refcount both the PacketImpl as well as the PacketInterpreterBase
82 // * intrusive_ptr_remove will only delete the object if it's not in a container
83 // * removing an object from the list will decrement the PacketImpl refcount accordingly
84 // * inserting an object into the list will increment the PacketImpl refcount accordingly
85 // * each PacketInterpreterBase instance holds a *raw* pointer to the PacketImpl
86 //
87 // The following operations change refcounts:
88 //
89 // * intrusive_ptr_add_ref(PacketInterpreterBase *);
90 // * intrusive_ptr_remove(PacketInterpreterBase *);
91 // * PacketImpl::appendInterpreter();
92 // * PacketImpl::prependInterpreter();
93 // * PacketImpl::truncateInterpreters();
94 //
95 // The last three also modify the impl_ member accordingly by calling
96 // PacketInterpreterBase::assign/release
97
98 //-/////////////////////////////////////////////////////////////////////////////////////////////////
99 // senf::detail::PacketImpl
100
101 prefix_ senf::detail::PacketImpl::PacketImpl()
102     : refcount_(0)
103 {
104     ::memset(simpleAnnotations_, 0, sizeof(simpleAnnotations_));
105 }
106
107 prefix_ senf::detail::PacketImpl::PacketImpl(size_type size, byte initValue)
108     : refcount_(0), data_(size,initValue)
109 {
110     ::memset(simpleAnnotations_, 0, sizeof(simpleAnnotations_));
111 }
112
113 // reference/memory management
114
115 prefix_ void senf::detail::PacketImpl::add_ref()
116 {
117     ++ refcount_;
118 }
119
120 prefix_ senf::detail::PacketImpl::refcount_t senf::detail::PacketImpl::refcount()
121     const
122 {
123     return refcount_;
124 }
125
126 // Interpreter chain
127
128 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::first()
129 {
130     return interpreters_.empty() ? 0 : & interpreters_.front();
131 }
132
133 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::last()
134 {
135     return interpreters_.empty() ? 0 : & interpreters_.back();
136 }
137
138 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::next(PacketInterpreterBase * p)
139 {
140     interpreter_list::iterator i (interpreter_list::current(*p));
141     return (++i == interpreters_.end()) ? 0 : &*i;
142 }
143
144 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::prev(PacketInterpreterBase * p)
145 {
146     interpreter_list::iterator i (interpreter_list::current(*p));
147     return (i == interpreters_.begin()) ? 0 : &*(--i);
148 }
149
150 prefix_ void senf::detail::PacketImpl::truncateInterpreters(PacketInterpreterBase * p)
151 {
152     Guard guard (this);
153     eraseInterpreters(interpreter_list::current(*p),interpreters_.end());
154 }
155
156 prefix_ void senf::detail::PacketImpl::truncateInterpretersBackwards(PacketInterpreterBase * p)
157 {
158     Guard guard (this);
159     eraseInterpreters(interpreters_.begin(),boost::next(interpreter_list::current(*p)));
160 }
161
162 // Data container
163
164 prefix_ senf::detail::PacketImpl::iterator senf::detail::PacketImpl::begin()
165 {
166     return data_.begin();
167 }
168
169 prefix_ senf::detail::PacketImpl::iterator senf::detail::PacketImpl::end()
170 {
171     return data_.end();
172 }
173
174 prefix_ senf::detail::PacketImpl::size_type senf::detail::PacketImpl::size()
175 {
176     return data_.size();
177 }
178
179 prefix_ void senf::detail::PacketImpl::insert(PacketData * self, iterator pos, byte v)
180 {
181     difference_type ix (std::distance(begin(),pos));
182     data_.insert(pos,v);
183     updateIterators(self,ix,1);
184 }
185
186 prefix_ void senf::detail::PacketImpl::insert(PacketData * self, iterator pos, size_type n,
187                                               byte v)
188 {
189     difference_type ix (std::distance(begin(),pos));
190     data_.insert(pos,n,v);
191     updateIterators(self,ix,n);
192 }
193
194 prefix_ void senf::detail::PacketImpl::erase(PacketData * self, iterator pos)
195 {
196     difference_type ix (std::distance(begin(),pos));
197     data_.erase(pos);
198     updateIterators(self,ix,-1);
199 }
200
201 prefix_ void senf::detail::PacketImpl::erase(PacketData * self, iterator first, iterator last)
202 {
203     difference_type ix (std::distance(begin(),first));
204     difference_type delta (std::distance(first,last));
205     data_.erase(first,last);
206     updateIterators(self,ix,-delta);
207 }
208
209 prefix_ void senf::detail::PacketImpl::reserve(size_type n)
210 {
211     data_.reserve(n);
212 }
213
214 prefix_ senf::detail::PacketImpl::size_type senf::detail::PacketImpl::capacity()
215     const
216 {
217     return data_.capacity();
218 }
219
220 // Annotations
221
222 prefix_ void * senf::detail::PacketImpl::annotation(AnnotationRegistry::key_type key)
223 {
224     return key >= 0 ? & simpleAnnotations_[key] : complexAnnotation(key);
225 }
226
227 //-/////////////////////////////////////////////////////////////////////////////////////////////////
228 // senf::detail::PacketImpl::Guard
229
230 prefix_ senf::detail::PacketImpl::Guard::Guard(PacketImpl * impl)
231     : p (impl)
232 {
233     p->add_ref();
234 }
235
236 prefix_ senf::detail::PacketImpl::Guard::~Guard()
237 {
238     p->release();
239 }
240
241 //-/////////////////////////////////////////////////////////////////////////////////////////////////
242 #undef prefix_
243
244 \f
245 // Local Variables:
246 // mode: c++
247 // fill-column: 100
248 // c-file-style: "senf"
249 // indent-tabs-mode: nil
250 // ispell-local-dictionary: "american"
251 // compile-command: "scons -u test"
252 // comment-column: 40
253 // End: