c4acd4d581ddbe6a74c14133946b2d75f0ff83e4
[senf.git] / 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 "../Utils/senfassert.hh"
28 #include "PacketInterpreter.hh"
29
30 #define prefix_ inline
31 ///////////////////////////////cci.p///////////////////////////////////////
32
33 // Memory management:
34 // 
35 // * The PacketImpl destructor will *explicitly* clean-up the interpreters_ list by removing
36 //   each element from the list and deleting it if it's (intrusive) refcount is 0
37 // * The PacketInterpreters use safe hooks -> they know wether they are part of a list or not
38 // * PacketHandle has an intrusive_ptr to PacketInterpreterBase. The intrusive_ptr_add_ref
39 //   will refcount both the PacketImpl as well as the PacketInterpreterBase
40 // * intrusive_ptr_remove will only delete the object if it's not in a container
41 // * removing an object from the list will decrement the PacketImpl refcount accordingly
42 // * inserting an object into the list will incroment the PacketImpl refcount accordingly
43 // * each PacketInterpreterBase instance holds a *raw* pointer to the PacketImpl
44 //
45 // The following operations change refcounts:
46 //
47 // * intrusive_ptr_add_ref(PacketInterpreterBase *);
48 // * intrusive_ptr_remove(PacketInterpreterBase *);
49 // * PacketImpl::appendInterpreter();
50 // * PacketImpl::prependInterpreter();
51 // * PacketImpl::truncateInterpreters();
52 //
53 // The last three also modify the impl_ member accordingly by calling
54 // PacketInterpreterBase::assign/release
55
56 ///////////////////////////////////////////////////////////////////////////
57 // senf::detail::PacketImpl
58
59 prefix_ senf::detail::PacketImpl::PacketImpl()
60     : refcount_(0)
61 {}
62
63 prefix_ senf::detail::PacketImpl::PacketImpl(size_type size, byte initValue)
64     : refcount_(0), data_(size,initValue)
65 {}
66
67 prefix_ senf::detail::PacketImpl::~PacketImpl()
68 {
69     // We increment refcount_ to ensure, release() won't call delete again
70     ++refcount_;
71     eraseInterpreters(interpreters_.begin(), interpreters_.end());
72 }
73
74 // rerference/memory management
75
76 prefix_ void senf::detail::PacketImpl::add_ref(refcount_t n)
77 {
78     refcount_ += n;
79 }
80
81 prefix_ void senf::detail::PacketImpl::release(refcount_t n)
82 {
83     SENF_ASSERT(refcount_ >= n);
84     // uah ... we need to be extremely careful here. If refcount_ is n, we want to commit suicide,
85     // however the destructor will remove all PacketInterpreters from the list and will thereby
86     // decrement refcount -> only decrenebt refcount_ when *not* caling delete
87     if (refcount_ == n)
88         delete this;
89     else
90         refcount_ -= n;
91 }
92
93 prefix_ senf::detail::PacketImpl::refcount_t senf::detail::PacketImpl::refcount()
94     const
95 {
96     return refcount_;
97 }
98
99 // Interpreter chain
100
101 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::first()
102 {
103     return interpreters_.empty() ? 0 : & interpreters_.front();
104 }
105
106 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::last()
107 {
108     return interpreters_.empty() ? 0 : & interpreters_.back();
109 }
110
111 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::next(PacketInterpreterBase * p)
112 {
113     interpreter_list::iterator i (interpreter_list::current(*p));
114     return (++i == interpreters_.end()) ? 0 : &*i;
115 }
116
117 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::prev(PacketInterpreterBase * p)
118
119     interpreter_list::iterator i (interpreter_list::current(*p));
120     return (i == interpreters_.begin()) ? 0 : &*(--i);
121 }
122
123 prefix_ void senf::detail::PacketImpl::truncateInterpreters(PacketInterpreterBase * p)
124 {
125     Guard guard (this);
126     eraseInterpreters(interpreter_list::current(*p),interpreters_.end());
127 }
128
129 prefix_ void senf::detail::PacketImpl::truncateInterpretersBackwards(PacketInterpreterBase * p)
130 {
131     Guard guard (this);
132     eraseInterpreters(interpreters_.begin(),boost::next(interpreter_list::current(*p)));
133 }
134
135 // Data container
136
137 prefix_ senf::detail::PacketImpl::iterator senf::detail::PacketImpl::begin()
138 {
139     return data_.begin();
140 }
141
142 prefix_ senf::detail::PacketImpl::iterator senf::detail::PacketImpl::end()
143 {
144     return data_.end();
145 }
146
147 prefix_ senf::detail::PacketImpl::size_type senf::detail::PacketImpl::size()
148 {
149     return data_.size();
150 }
151
152 prefix_ void senf::detail::PacketImpl::insert(PacketData * self, iterator pos, byte v)
153 {
154     difference_type ix (std::distance(begin(),pos));
155     data_.insert(pos,v);
156     updateIterators(self,ix,1);
157 }
158
159 prefix_ void senf::detail::PacketImpl::insert(PacketData * self, iterator pos, size_type n,
160                                               byte v)
161 {
162     difference_type ix (std::distance(begin(),pos));
163     data_.insert(pos,n,v);
164     updateIterators(self,ix,n);
165 }
166
167 prefix_ void senf::detail::PacketImpl::erase(PacketData * self, iterator pos)
168 {
169     difference_type ix (std::distance(begin(),pos));
170     data_.erase(pos);
171     updateIterators(self,ix,-1);
172 }
173
174 prefix_ void senf::detail::PacketImpl::erase(PacketData * self, iterator first, iterator last)
175 {
176     difference_type ix (std::distance(begin(),first));
177     difference_type delta (std::distance(first,last));
178     data_.erase(first,last);
179     updateIterators(self,ix,-delta);
180 }
181
182 ///////////////////////////////////////////////////////////////////////////
183 // senf::detail::PacketImpl::Guard
184
185 prefix_ senf::detail::PacketImpl::Guard::Guard(PacketImpl * impl)
186     : p (impl)
187 {
188     p->add_ref();
189 }
190
191 prefix_ senf::detail::PacketImpl::Guard::~Guard()
192 {
193     p->release();
194 }
195
196 ///////////////////////////////cci.e///////////////////////////////////////
197 #undef prefix_
198
199 \f
200 // Local Variables:
201 // mode: c++
202 // fill-column: 100
203 // c-file-style: "senf"
204 // indent-tabs-mode: nil
205 // ispell-local-dictionary: "american"
206 // compile-command: "scons -u test"
207 // comment-column: 40
208 // End: