Utils/Daemon: Add warning when the scheduler has registered events at a fork()
[senf.git] / Packets / PacketInterpreter.cti
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 PacketInterpreter inline template implementation */
25
26 //#include "PacketInterpreter.ih"
27
28 // Custom includes
29
30 #define prefix_ inline
31 ///////////////////////////////cti.p///////////////////////////////////////
32
33 ///////////////////////////////////////////////////////////////////////////
34 // senf::PacketInterpreterBase
35
36 // Interpreter chain access
37
38 template <class Type>
39 prefix_ bool senf::PacketInterpreterBase::is()
40 {
41     { static void const * const _ ((void*)&Type::dump); (void) _; }
42     return dynamic_cast< PacketInterpreter<Type>* >(this);
43 }
44
45 template <class Type>
46 prefix_ typename senf::PacketInterpreter<Type>::ptr senf::PacketInterpreterBase::as()
47 {
48     { static void const * const _ ((void*)&Type::dump); (void) _; }
49     return typename PacketInterpreter<Type>::ptr(
50         static_cast< PacketInterpreter<Type>* >(this));
51 }
52
53 template <class Annotation>
54 prefix_ Annotation & senf::PacketInterpreterBase::annotation()
55 {
56     return impl().annotation<Annotation>();
57 }
58
59 ///////////////////////////////////////////////////////////////////////////
60 // senf::PacketInterpreter<PacketType>
61
62 template <class PacketType>
63 prefix_ senf::PacketInterpreter<PacketType>::~PacketInterpreter()
64 {
65     parser_p()->~parser();
66 }
67
68 template <class PacketType>
69 prefix_ typename senf::PacketInterpreter<PacketType>::factory_t
70 senf::PacketInterpreter<PacketType>::factory()
71 {
72     return & factory_;
73 }
74
75 // Create completely new packet
76
77 template <class PacketType>
78 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
79 senf::PacketInterpreter<PacketType>::create()
80 {
81     return create(initSize());
82 }
83
84 template <class PacketType>
85 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
86 senf::PacketInterpreter<PacketType>::create(senf::NoInit_t)
87 {
88     return create(0,senf::noinit);
89 }
90
91 template <class PacketType>
92 template <class ForwardReadableRange>
93 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
94 senf::PacketInterpreter<PacketType>::create(ForwardReadableRange const & range)
95 {
96     detail::PacketImpl::Guard p (new detail::PacketImpl(boost::begin(range),boost::end(range)));
97     ptr pi (create(p.p,p.p->begin(),p.p->end(),Append));
98     return pi;
99 }
100
101 // Create packet as new packet after a given packet
102
103 template <class PacketType>
104 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
105 senf::PacketInterpreter<PacketType>::createAfter(PacketInterpreterBase::ptr packet)
106 {
107     return createAfter(packet, initSize());
108 }
109
110 template <class PacketType>
111 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
112 senf::PacketInterpreter<PacketType>::createAfter(PacketInterpreterBase::ptr packet, senf::NoInit_t)
113 {
114     return createAfter(packet, 0, senf::noinit);
115 }
116
117 // Create clone of current packet
118
119 template <class PacketType>
120 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
121 senf::PacketInterpreter<PacketType>::clone()
122 {
123     return boost::static_pointer_cast<typename ptr::element_type>(PacketInterpreterBase::clone());
124 }
125
126 // Packet field access
127
128 template <class PacketType>
129 prefix_ typename senf::PacketInterpreter<PacketType>::parser
130 senf::PacketInterpreter<PacketType>::fields()
131 {
132     return parser (data().begin(),&data());
133 }
134
135 template <class PacketType>
136 prefix_ typename senf::PacketInterpreter<PacketType>::parser *
137 senf::PacketInterpreter<PacketType>::fields_p()
138 {
139     // This is somewhat awkward. We want to allow the user to access the packet's field using the
140     // 'operator->' member of the packet class (the handle). Now, 'operator->' *must* return a
141     // pointer to a non-dynamically allocated object. So where should it point to? We need to return
142     // a pointer to a parser instance, but parser instances are designed to be transient (they are
143     // invalidated whenever a packet's size is changed).
144
145     // What we do is the following: parserStorage_ is an (initialy uninitialized) storage area
146     // within the interpreter with enough space (and correct alignment) to take hold of a parser
147     // instance. In the constructor we use placement new to construct a parser in this area which we
148     // explicit dispose of in the destructor. Now, whenever the fields_p() member is called, we
149     // destroy the parser object and recreate it. 
150
151     // This does introduce one additional problem: It is not safe for multiple threads to
152     // concurrently read from the same packet. On the other hand, the packet classes are not
153     // syncronized in any way and are not safe to use from multiple threads anyways (e.g. the lazy
154     // packet chain makes some read-only operations change the packet which is not thread safe).
155
156     parser_p()->~parser();
157     new (parser_p()) parser (data().begin(),&data());
158     return parser_p();
159 }
160
161 ////////////////////////////////////////
162 // private members
163
164 // Private structors
165
166 template <class PacketType>
167 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
168 senf::PacketInterpreter<PacketType>::create(detail::PacketImpl * impl, iterator b, iterator e,
169                                             Append_t)
170 {
171     return ptr(new PacketInterpreter(impl,b,e,Append));
172 }
173
174 template <class PacketType>
175 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
176 senf::PacketInterpreter<PacketType>::create(detail::PacketImpl * impl, iterator b, iterator e,
177                                             Prepend_t)
178 {
179     return ptr(new PacketInterpreter(impl,b,e,Prepend));
180 }
181
182 template <class PacketType>
183 prefix_ senf::PacketInterpreter<PacketType>::PacketInterpreter(detail::PacketImpl * impl,
184                                                                iterator b, iterator e, Append_t)
185     : PacketInterpreterBase(impl,b,e,Append)
186 {
187     new (parser_p()) parser (data().begin(),&data());
188 }
189
190 template <class PacketType>
191 prefix_ senf::PacketInterpreter<PacketType>::PacketInterpreter(detail::PacketImpl * impl,
192                                                                iterator b, iterator e, Prepend_t)
193     : PacketInterpreterBase(impl,b,e,Prepend)
194 {
195     new (parser_p()) parser (data().begin(),&data());
196 }
197
198 // PacketType access
199
200 template <class PacketType>
201 prefix_ typename senf::PacketInterpreter<PacketType>::size_type
202 senf::PacketInterpreter<PacketType>::initSize()
203 {
204     return type::initSize();
205 }
206
207 template <class PacketType>
208 prefix_ typename senf::PacketInterpreter<PacketType>::size_type
209 senf::PacketInterpreter<PacketType>::initHeadSize()
210 {
211     size_type sz (type::initHeadSize());
212     return  sz == size_type(-1) ? initSize() : sz ;
213 }
214
215 template <class PacketType>
216 prefix_ void senf::PacketInterpreter<PacketType>::init()
217 {
218     return type::init(ConcretePacket<PacketType>(ptr(this)));
219 }
220
221 template <class PacketType>
222 prefix_ typename senf::PacketInterpreter<PacketType>::parser *
223 senf::PacketInterpreter<PacketType>::parser_p()
224 {
225     return reinterpret_cast<parser *>(&parserStorage_);
226 }
227
228 ///////////////////////////////cti.e///////////////////////////////////////
229 #undef prefix_
230
231 \f
232 // Local Variables:
233 // mode: c++
234 // fill-column: 100
235 // c-file-style: "senf"
236 // indent-tabs-mode: nil
237 // ispell-local-dictionary: "american"
238 // compile-command: "scons -u test"
239 // comment-column: 40
240 // End: