4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief PacketImpl inline non-template implementation */
27 #include <senf/Utils/senfassert.hh>
28 // #include "PacketInterpreter.hh"
30 #define prefix_ inline
31 ///////////////////////////////cci.p///////////////////////////////////////
33 ///////////////////////////////////////////////////////////////////////////
34 // senf::detail::AnnotationRegistry
36 prefix_ void senf::detail::AnnotationRegistry::dump(key_t key, std::ostream & os,
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);
48 prefix_ std::string senf::detail::AnnotationRegistry::name(key_t key)
51 Registry::const_iterator i (registry_.find(key));
52 return i == registry_.end() ? "" : i->second->v_name();
55 prefix_ bool senf::detail::AnnotationRegistry::isComplex(key_t key)
58 Registry::const_iterator i (registry_.find(key));
59 return i != registry_.end() && i->second->v_isComplex();
62 prefix_ unsigned senf::detail::AnnotationRegistry::size(key_t key)
65 Registry::const_iterator i (registry_.find(key));
66 return i == registry_.end() ? 0 : i->second->v_size();
69 prefix_ senf::detail::AnnotationRegistry::AnnotationRegistry()
70 : simpleAnnotationCount_ (0), complexAnnotationCount_ (0)
73 ///////////////////////////////////////////////////////////////////////////
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
87 // The following operations change refcounts:
89 // * intrusive_ptr_add_ref(PacketInterpreterBase *);
90 // * intrusive_ptr_remove(PacketInterpreterBase *);
91 // * PacketImpl::appendInterpreter();
92 // * PacketImpl::prependInterpreter();
93 // * PacketImpl::truncateInterpreters();
95 // The last three also modify the impl_ member accordingly by calling
96 // PacketInterpreterBase::assign/release
98 ///////////////////////////////////////////////////////////////////////////
99 // senf::detail::PacketImpl
101 prefix_ senf::detail::PacketImpl::PacketImpl()
104 ::memset(simpleAnnotations_, 0, sizeof(simpleAnnotations_));
107 prefix_ senf::detail::PacketImpl::PacketImpl(size_type size, byte initValue)
108 : refcount_(0), data_(size,initValue)
110 ::memset(simpleAnnotations_, 0, sizeof(simpleAnnotations_));
113 // reference/memory management
115 prefix_ void senf::detail::PacketImpl::add_ref(refcount_t n)
120 prefix_ senf::detail::PacketImpl::refcount_t senf::detail::PacketImpl::refcount()
128 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::first()
130 return interpreters_.empty() ? 0 : & interpreters_.front();
133 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::last()
135 return interpreters_.empty() ? 0 : & interpreters_.back();
138 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::next(PacketInterpreterBase * p)
140 interpreter_list::iterator i (interpreter_list::current(*p));
141 return (++i == interpreters_.end()) ? 0 : &*i;
144 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::prev(PacketInterpreterBase * p)
146 interpreter_list::iterator i (interpreter_list::current(*p));
147 return (i == interpreters_.begin()) ? 0 : &*(--i);
150 prefix_ void senf::detail::PacketImpl::truncateInterpreters(PacketInterpreterBase * p)
153 eraseInterpreters(interpreter_list::current(*p),interpreters_.end());
156 prefix_ void senf::detail::PacketImpl::truncateInterpretersBackwards(PacketInterpreterBase * p)
159 eraseInterpreters(interpreters_.begin(),boost::next(interpreter_list::current(*p)));
164 prefix_ senf::detail::PacketImpl::iterator senf::detail::PacketImpl::begin()
166 return data_.begin();
169 prefix_ senf::detail::PacketImpl::iterator senf::detail::PacketImpl::end()
174 prefix_ senf::detail::PacketImpl::size_type senf::detail::PacketImpl::size()
179 prefix_ void senf::detail::PacketImpl::insert(PacketData * self, iterator pos, byte v)
181 difference_type ix (std::distance(begin(),pos));
183 updateIterators(self,ix,1);
186 prefix_ void senf::detail::PacketImpl::insert(PacketData * self, iterator pos, size_type n,
189 difference_type ix (std::distance(begin(),pos));
190 data_.insert(pos,n,v);
191 updateIterators(self,ix,n);
194 prefix_ void senf::detail::PacketImpl::erase(PacketData * self, iterator pos)
196 difference_type ix (std::distance(begin(),pos));
198 updateIterators(self,ix,-1);
201 prefix_ void senf::detail::PacketImpl::erase(PacketData * self, iterator first, iterator last)
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);
209 prefix_ void senf::detail::PacketImpl::reserve(size_type n)
214 prefix_ senf::detail::PacketImpl::size_type senf::detail::PacketImpl::capacity()
217 return data_.capacity();
220 // This function has a problem being inlined. Somehow, often when calling this, the size of the
221 // resulting inlined code would be huge?
223 prefix_ void senf::detail::PacketImpl::release(refcount_t n)
225 SENF_ASSERT(refcount_ >= n, "Internal failure: Releasing dead PacketImpl ??");
226 // uah ... we need to be extremely careful here. If refcount_ is n, we want to commit suicide,
227 // however the destructor will remove all PacketInterpreters from the list and will thereby
228 // decrement refcount -> only decrement refcount_ when *not* calling delete
237 prefix_ void * senf::detail::PacketImpl::annotation(AnnotationRegistry::key_t key)
239 return key >= 0 ? & simpleAnnotations_[key] : complexAnnotation(key);
242 ///////////////////////////////////////////////////////////////////////////
243 // senf::detail::PacketImpl::Guard
245 prefix_ senf::detail::PacketImpl::Guard::Guard(PacketImpl * impl)
251 prefix_ senf::detail::PacketImpl::Guard::~Guard()
256 ///////////////////////////////cci.e///////////////////////////////////////
263 // c-file-style: "senf"
264 // indent-tabs-mode: nil
265 // ispell-local-dictionary: "american"
266 // compile-command: "scons -u test"
267 // comment-column: 40