switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / PacketImpl.cci
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief PacketImpl inline non-template implementation */
30
31 // Custom includes
32
33 #define prefix_ inline
34 //-/////////////////////////////////////////////////////////////////////////////////////////////////
35
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37 // senf::detail::AnnotationRegistry
38
39 prefix_ std::string senf::detail::AnnotationRegistry::name(key_type key)
40     const
41 {
42     Registry::const_iterator i (registry_.find(key));
43     return i == registry_.end() ? "" : i->second->v_name();
44 }
45
46 prefix_ bool senf::detail::AnnotationRegistry::isComplex(key_type key)
47     const
48 {
49     Registry::const_iterator i (registry_.find(key));
50     return i != registry_.end() && i->second->v_isComplex();
51 }
52
53 prefix_ unsigned senf::detail::AnnotationRegistry::size(key_type key)
54     const
55 {
56     Registry::const_iterator i (registry_.find(key));
57     return i == registry_.end() ? 0 : i->second->v_size();
58 }
59
60 prefix_ senf::detail::AnnotationRegistry::iterator senf::detail::AnnotationRegistry::begin()
61     const
62 {
63     return boost::make_transform_iterator(index_.begin(),
64                                           __gnu_cxx::select2nd<Index::value_type>());
65 }
66
67 prefix_ senf::detail::AnnotationRegistry::iterator senf::detail::AnnotationRegistry::end()
68     const
69 {
70     return boost::make_transform_iterator(index_.end(),
71                                           __gnu_cxx::select2nd<Index::value_type>());
72 }
73
74 prefix_ senf::detail::AnnotationRegistry::AnnotationRegistry()
75     : simpleAnnotationCount_ (0), complexAnnotationCount_ (0)
76 {}
77
78 //-/////////////////////////////////////////////////////////////////////////////////////////////////
79
80 // Memory management:
81 //
82 // * The PacketImpl destructor will *explicitly* clean-up the interpreters_ list by removing
83 //   each element from the list and deleting it if it's (intrusive) refcount is 0
84 // * The PacketInterpreters use safe hooks -> they know whether they are part of a list or not
85 // * PacketHandle has an intrusive_ptr to PacketInterpreterBase. The intrusive_ptr_add_ref
86 //   will refcount both the PacketImpl as well as the PacketInterpreterBase
87 // * intrusive_ptr_remove will only delete the object if it's not in a container
88 // * removing an object from the list will decrement the PacketImpl refcount accordingly
89 // * inserting an object into the list will increment the PacketImpl refcount accordingly
90 // * each PacketInterpreterBase instance holds a *raw* pointer to the PacketImpl
91 //
92 // The following operations change refcounts:
93 //
94 // * intrusive_ptr_add_ref(PacketInterpreterBase *);
95 // * intrusive_ptr_remove(PacketInterpreterBase *);
96 // * PacketImpl::appendInterpreter();
97 // * PacketImpl::prependInterpreter();
98 // * PacketImpl::truncateInterpreters();
99 //
100 // The last three also modify the impl_ member accordingly by calling
101 // PacketInterpreterBase::assign/release
102
103 //-/////////////////////////////////////////////////////////////////////////////////////////////////
104 // senf::detail::PacketImpl
105
106 prefix_ senf::detail::PacketImpl::PacketImpl()
107     : refcount_(0)
108 {
109     ::memset(simpleAnnotations_, 0, sizeof(simpleAnnotations_));
110 }
111
112 prefix_ senf::detail::PacketImpl::PacketImpl(size_type size, byte initValue)
113     : refcount_(0), data_(size,initValue)
114 {
115     ::memset(simpleAnnotations_, 0, sizeof(simpleAnnotations_));
116 }
117
118 // reference/memory management
119
120 prefix_ void senf::detail::PacketImpl::add_ref()
121 {
122     ++ refcount_;
123 }
124
125 prefix_ senf::detail::PacketImpl::refcount_t senf::detail::PacketImpl::refcount()
126     const
127 {
128     return refcount_;
129 }
130
131 // Interpreter chain
132
133 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::first()
134 {
135     return interpreters_.empty() ? 0 : & interpreters_.front();
136 }
137
138 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::last()
139 {
140     return interpreters_.empty() ? 0 : & interpreters_.back();
141 }
142
143 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::next(PacketInterpreterBase * p)
144 {
145     interpreter_list::iterator i (interpreter_list::current(*p));
146     return (++i == interpreters_.end()) ? 0 : &*i;
147 }
148
149 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::prev(PacketInterpreterBase * p)
150 {
151     interpreter_list::iterator i (interpreter_list::current(*p));
152     return (i == interpreters_.begin()) ? 0 : &*(--i);
153 }
154
155 prefix_ void senf::detail::PacketImpl::truncateInterpreters(PacketInterpreterBase * p)
156 {
157     Guard guard (this);
158     eraseInterpreters(interpreter_list::current(*p),interpreters_.end());
159 }
160
161 prefix_ void senf::detail::PacketImpl::truncateInterpretersBackwards(PacketInterpreterBase * p)
162 {
163     Guard guard (this);
164     eraseInterpreters(interpreters_.begin(),boost::next(interpreter_list::current(*p)));
165 }
166
167 // Data container
168
169 prefix_ senf::detail::PacketImpl::iterator senf::detail::PacketImpl::begin()
170 {
171     return data_.begin();
172 }
173
174 prefix_ senf::detail::PacketImpl::iterator senf::detail::PacketImpl::end()
175 {
176     return data_.end();
177 }
178
179 prefix_ senf::detail::PacketImpl::size_type senf::detail::PacketImpl::size()
180 {
181     return data_.size();
182 }
183
184 prefix_ void senf::detail::PacketImpl::insert(PacketData * self, iterator pos, byte v)
185 {
186     difference_type ix (std::distance(begin(),pos));
187     data_.insert(pos,v);
188     updateIterators(self,ix,1);
189 }
190
191 prefix_ void senf::detail::PacketImpl::insert(PacketData * self, iterator pos, size_type n,
192                                               byte v)
193 {
194     difference_type ix (std::distance(begin(),pos));
195     data_.insert(pos,n,v);
196     updateIterators(self,ix,n);
197 }
198
199 prefix_ void senf::detail::PacketImpl::erase(PacketData * self, iterator pos)
200 {
201     difference_type ix (std::distance(begin(),pos));
202     data_.erase(pos);
203     updateIterators(self,ix,-1);
204 }
205
206 prefix_ void senf::detail::PacketImpl::erase(PacketData * self, iterator first, iterator last)
207 {
208     difference_type ix (std::distance(begin(),first));
209     difference_type delta (std::distance(first,last));
210     data_.erase(first,last);
211     updateIterators(self,ix,-delta);
212 }
213
214 prefix_ void senf::detail::PacketImpl::reserve(size_type n)
215 {
216     data_.reserve(n);
217 }
218
219 prefix_ senf::detail::PacketImpl::size_type senf::detail::PacketImpl::capacity()
220     const
221 {
222     return data_.capacity();
223 }
224
225 // Annotations
226
227 prefix_ void * senf::detail::PacketImpl::annotation(AnnotationRegistry::key_type key)
228 {
229     return key >= 0 ? & simpleAnnotations_[key] : complexAnnotation(key);
230 }
231
232 //-/////////////////////////////////////////////////////////////////////////////////////////////////
233 // senf::detail::PacketImpl::Guard
234
235 prefix_ senf::detail::PacketImpl::Guard::Guard(PacketImpl * impl)
236     : p (impl)
237 {
238     p->add_ref();
239 }
240
241 prefix_ senf::detail::PacketImpl::Guard::~Guard()
242 {
243     p->release();
244 }
245
246 //-/////////////////////////////////////////////////////////////////////////////////////////////////
247 #undef prefix_
248
249 \f
250 // Local Variables:
251 // mode: c++
252 // fill-column: 100
253 // c-file-style: "senf"
254 // indent-tabs-mode: nil
255 // ispell-local-dictionary: "american"
256 // compile-command: "scons -u test"
257 // comment-column: 40
258 // End: