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