Whitespce cleanup: Remove whitespace at end-on-line, remove tabs, wrap
[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::AnnotationIndexerBase
35
36 prefix_ senf::detail::AnnotationIndexerBase::~AnnotationIndexerBase()
37 {}
38
39 prefix_ std::vector<bool> & senf::detail::AnnotationIndexerBase::small()
40 {
41     static std::vector<bool> smalls;
42     return smalls;
43 }
44
45 prefix_ std::vector<senf::detail::AnnotationIndexerBase*> &
46 senf::detail::AnnotationIndexerBase::registry()
47 {
48     static std::vector<AnnotationIndexerBase*> reg;
49     return reg;
50 }
51
52 ///////////////////////////////////////////////////////////////////////////
53 // senf::detail::AnnotationP
54
55 prefix_ senf::detail::AnnotationP::~AnnotationP()
56 {}
57
58 // Memory management:
59 //
60 // * The PacketImpl destructor will *explicitly* clean-up the interpreters_ list by removing
61 //   each element from the list and deleting it if it's (intrusive) refcount is 0
62 // * The PacketInterpreters use safe hooks -> they know wether they are part of a list or not
63 // * PacketHandle has an intrusive_ptr to PacketInterpreterBase. The intrusive_ptr_add_ref
64 //   will refcount both the PacketImpl as well as the PacketInterpreterBase
65 // * intrusive_ptr_remove will only delete the object if it's not in a container
66 // * removing an object from the list will decrement the PacketImpl refcount accordingly
67 // * inserting an object into the list will incroment the PacketImpl refcount accordingly
68 // * each PacketInterpreterBase instance holds a *raw* pointer to the PacketImpl
69 //
70 // The following operations change refcounts:
71 //
72 // * intrusive_ptr_add_ref(PacketInterpreterBase *);
73 // * intrusive_ptr_remove(PacketInterpreterBase *);
74 // * PacketImpl::appendInterpreter();
75 // * PacketImpl::prependInterpreter();
76 // * PacketImpl::truncateInterpreters();
77 //
78 // The last three also modify the impl_ member accordingly by calling
79 // PacketInterpreterBase::assign/release
80
81 ///////////////////////////////////////////////////////////////////////////
82 // senf::detail::PacketImpl
83
84 prefix_ senf::detail::PacketImpl::PacketImpl()
85     : refcount_(0), annotations_(AnnotationIndexerBase::maxAnnotations)
86 {}
87
88 prefix_ senf::detail::PacketImpl::PacketImpl(size_type size, byte initValue)
89     : refcount_(0), data_(size,initValue), annotations_(AnnotationIndexerBase::maxAnnotations)
90 {}
91
92 // rerference/memory management
93
94 prefix_ void senf::detail::PacketImpl::add_ref(refcount_t n)
95 {
96     refcount_ += n;
97 }
98
99 prefix_ senf::detail::PacketImpl::refcount_t senf::detail::PacketImpl::refcount()
100     const
101 {
102     return refcount_;
103 }
104
105 // Interpreter chain
106
107 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::first()
108 {
109     return interpreters_.empty() ? 0 : & interpreters_.front();
110 }
111
112 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::last()
113 {
114     return interpreters_.empty() ? 0 : & interpreters_.back();
115 }
116
117 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::next(PacketInterpreterBase * p)
118 {
119     interpreter_list::iterator i (interpreter_list::current(*p));
120     return (++i == interpreters_.end()) ? 0 : &*i;
121 }
122
123 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::prev(PacketInterpreterBase * p)
124 {
125     interpreter_list::iterator i (interpreter_list::current(*p));
126     return (i == interpreters_.begin()) ? 0 : &*(--i);
127 }
128
129 prefix_ void senf::detail::PacketImpl::truncateInterpreters(PacketInterpreterBase * p)
130 {
131     Guard guard (this);
132     eraseInterpreters(interpreter_list::current(*p),interpreters_.end());
133 }
134
135 prefix_ void senf::detail::PacketImpl::truncateInterpretersBackwards(PacketInterpreterBase * p)
136 {
137     Guard guard (this);
138     eraseInterpreters(interpreters_.begin(),boost::next(interpreter_list::current(*p)));
139 }
140
141 // Data container
142
143 prefix_ senf::detail::PacketImpl::iterator senf::detail::PacketImpl::begin()
144 {
145     return data_.begin();
146 }
147
148 prefix_ senf::detail::PacketImpl::iterator senf::detail::PacketImpl::end()
149 {
150     return data_.end();
151 }
152
153 prefix_ senf::detail::PacketImpl::size_type senf::detail::PacketImpl::size()
154 {
155     return data_.size();
156 }
157
158 prefix_ void senf::detail::PacketImpl::insert(PacketData * self, iterator pos, byte v)
159 {
160     difference_type ix (std::distance(begin(),pos));
161     data_.insert(pos,v);
162     updateIterators(self,ix,1);
163 }
164
165 prefix_ void senf::detail::PacketImpl::insert(PacketData * self, iterator pos, size_type n,
166                                               byte v)
167 {
168     difference_type ix (std::distance(begin(),pos));
169     data_.insert(pos,n,v);
170     updateIterators(self,ix,n);
171 }
172
173 prefix_ void senf::detail::PacketImpl::erase(PacketData * self, iterator pos)
174 {
175     difference_type ix (std::distance(begin(),pos));
176     data_.erase(pos);
177     updateIterators(self,ix,-1);
178 }
179
180 prefix_ void senf::detail::PacketImpl::erase(PacketData * self, iterator first, iterator last)
181 {
182     difference_type ix (std::distance(begin(),first));
183     difference_type delta (std::distance(first,last));
184     data_.erase(first,last);
185     updateIterators(self,ix,-delta);
186 }
187
188 prefix_ void senf::detail::PacketImpl::reserve(size_type n)
189 {
190     data_.reserve(n);
191 }
192
193 prefix_ senf::detail::PacketImpl::size_type senf::detail::PacketImpl::capacity()
194     const
195 {
196     return data_.capacity();
197 }
198
199 prefix_ void senf::detail::PacketImpl::dumpAnnotations(std::ostream & os)
200 {
201     AnnotationIndexerBase::dump(this, os);
202 }
203
204 // This function has a problem being inlined. Somehow, often when calling this, the size of the
205 // resulting inlined code would be huge?
206
207 prefix_ void senf::detail::PacketImpl::release(refcount_t n)
208 {
209     SENF_ASSERT(refcount_ >= n);
210     // uah ... we need to be extremely careful here. If refcount_ is n, we want to commit suicide,
211     // however the destructor will remove all PacketInterpreters from the list and will thereby
212     // decrement refcount -> only decrenebt refcount_ when *not* caling delete
213     if (refcount_ == n)
214         delete this;
215     else
216         refcount_ -= n;
217 }
218
219
220 ///////////////////////////////////////////////////////////////////////////
221 // senf::detail::PacketImpl::Guard
222
223 prefix_ senf::detail::PacketImpl::Guard::Guard(PacketImpl * impl)
224     : p (impl)
225 {
226     p->add_ref();
227 }
228
229 prefix_ senf::detail::PacketImpl::Guard::~Guard()
230 {
231     p->release();
232 }
233
234 ///////////////////////////////cci.e///////////////////////////////////////
235 #undef prefix_
236
237 \f
238 // Local Variables:
239 // mode: c++
240 // fill-column: 100
241 // c-file-style: "senf"
242 // indent-tabs-mode: nil
243 // ispell-local-dictionary: "american"
244 // compile-command: "scons -u test"
245 // comment-column: 40
246 // End: