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