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