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