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