Better SENF configuration support (local_config.hh)
[senf.git] / Packets / PacketImpl.cci
1 // Copyright (C) 2007 
2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 //     Stefan Bund <g0dil@berlios.de>
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
21 /** \file
22     \brief PacketImpl inline non-template implementation */
23
24 // Custom includes
25 #include "PacketInterpreter.hh"
26
27 #define prefix_ inline
28 ///////////////////////////////cci.p///////////////////////////////////////
29
30 // Memory management:
31 // 
32 // * The PacketImpl destructor will *explicitly* clean-up the interpreters_ list by removing
33 //   each element from the list and deleting it if it's (intrusive) refcount is 0
34 // * The PacketInterpreters use safe hooks -> they know wether they are part of a list or not
35 // * PacketHandle has an intrusive_ptr to PacketInterpreterBase. The intrusive_ptr_add_ref
36 //   will refcount both the PacketImpl as well as the PacketInterpreterBase
37 // * intrusive_ptr_remove will only delete the object if it's not in a container
38 // * removing an object from the list will decrement the PacketImpl refcount accordingly
39 // * inserting an object into the list will incroment the PacketImpl refcount accordingly
40 // * each PacketInterpreterBase instance holds a *raw* pointer to the PacketImpl
41 //
42 // The following operations change refcounts:
43 //
44 // * intrusive_ptr_add_ref(PacketInterpreterBase *);
45 // * intrusive_ptr_remove(PacketInterpreterBase *);
46 // * PacketImpl::appendInterpreter();
47 // * PacketImpl::prependInterpreter();
48 // * PacketImpl::truncateInterpreters();
49 //
50 // The last three also modify the impl_ member accordingly by calling
51 // PacketInterpreterBase::assign/release
52
53 ///////////////////////////////////////////////////////////////////////////
54 // senf::detail::PacketImpl
55
56 prefix_ senf::detail::PacketImpl::PacketImpl()
57     : refcount_(0)
58 {}
59
60 prefix_ senf::detail::PacketImpl::PacketImpl(size_type size, byte initValue)
61     : refcount_(0), data_(size,initValue)
62 {}
63
64 prefix_ senf::detail::PacketImpl::~PacketImpl()
65 {
66     // We increment refcount_ to ensure, release() won't call delete again
67     ++refcount_;
68     eraseInterpreters(interpreters_.begin(), interpreters_.end());
69 }
70
71 // rerference/memory management
72
73 prefix_ void senf::detail::PacketImpl::add_ref(refcount_t n)
74 {
75     refcount_ += n;
76 }
77
78 prefix_ void senf::detail::PacketImpl::release(refcount_t n)
79 {
80     BOOST_ASSERT(refcount_ >= n);
81     // uah ... we need to be extremely careful here. If refcount_ is n, we want to commit suicide,
82     // however the destructor will remove all PacketInterpreters from the list and will thereby
83     // decrement refcount -> only decrenebt refcount_ when *not* caling delete
84     if (refcount_ == n)
85         delete this;
86     else
87         refcount_ -= n;
88 }
89
90 prefix_ senf::detail::PacketImpl::refcount_t senf::detail::PacketImpl::refcount()
91     const
92 {
93     return refcount_;
94 }
95
96 // Interpreter chain
97
98 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::first()
99 {
100     return interpreters_.empty() ? 0 : & interpreters_.front();
101 }
102
103 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::last()
104 {
105     return interpreters_.empty() ? 0 : & interpreters_.back();
106 }
107
108 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::next(PacketInterpreterBase * p)
109 {
110     interpreter_list::iterator i (interpreter_list::current(*p));
111     return (++i == interpreters_.end()) ? 0 : &*i;
112 }
113
114 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::prev(PacketInterpreterBase * p)
115
116     interpreter_list::iterator i (interpreter_list::current(*p));
117     return (i == interpreters_.begin()) ? 0 : &*(--i);
118 }
119
120 prefix_ void senf::detail::PacketImpl::truncateInterpreters(PacketInterpreterBase * p)
121 {
122     Guard guard (this);
123     eraseInterpreters(interpreter_list::current(*p),interpreters_.end());
124 }
125
126 prefix_ void senf::detail::PacketImpl::truncateInterpretersBackwards(PacketInterpreterBase * p)
127 {
128     Guard guard (this);
129     eraseInterpreters(interpreters_.begin(),boost::next(interpreter_list::current(*p)));
130 }
131
132 // Data container
133
134 prefix_ senf::detail::PacketImpl::iterator senf::detail::PacketImpl::begin()
135 {
136     return data_.begin();
137 }
138
139 prefix_ senf::detail::PacketImpl::iterator senf::detail::PacketImpl::end()
140 {
141     return data_.end();
142 }
143
144 prefix_ senf::detail::PacketImpl::size_type senf::detail::PacketImpl::size()
145 {
146     return data_.size();
147 }
148
149 prefix_ void senf::detail::PacketImpl::insert(PacketData * self, iterator pos, byte v)
150 {
151     difference_type ix (std::distance(begin(),pos));
152     data_.insert(pos,v);
153     updateIterators(self,ix,1);
154 }
155
156 prefix_ void senf::detail::PacketImpl::insert(PacketData * self, iterator pos, size_type n,
157                                               byte v)
158 {
159     difference_type ix (std::distance(begin(),pos));
160     data_.insert(pos,n,v);
161     updateIterators(self,ix,n);
162 }
163
164 prefix_ void senf::detail::PacketImpl::erase(PacketData * self, iterator pos)
165 {
166     difference_type ix (std::distance(begin(),pos));
167     data_.erase(pos);
168     updateIterators(self,ix,-1);
169 }
170
171 prefix_ void senf::detail::PacketImpl::erase(PacketData * self, iterator first, iterator last)
172 {
173     difference_type ix (std::distance(begin(),first));
174     difference_type delta (std::distance(first,last));
175     data_.erase(first,last);
176     updateIterators(self,ix,-delta);
177 }
178
179 ///////////////////////////////////////////////////////////////////////////
180 // senf::detail::PacketImpl::Guard
181
182 prefix_ senf::detail::PacketImpl::Guard::Guard(PacketImpl * impl)
183     : p (impl)
184 {
185     p->add_ref();
186 }
187
188 prefix_ senf::detail::PacketImpl::Guard::~Guard()
189 {
190     p->release();
191 }
192
193 ///////////////////////////////cci.e///////////////////////////////////////
194 #undef prefix_
195
196 \f
197 // Local Variables:
198 // mode: c++
199 // fill-column: 100
200 // c-file-style: "senf"
201 // indent-tabs-mode: nil
202 // ispell-local-dictionary: "american"
203 // compile-command: "scons -u test"
204 // comment-column: 40
205 // End: