4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief PacketInterpreter inline template implementation */
26 //#include "PacketInterpreter.ih"
30 #define prefix_ inline
31 ///////////////////////////////cti.p///////////////////////////////////////
33 ///////////////////////////////////////////////////////////////////////////
34 // senf::PacketInterpreterBase
36 // Interpreter chain access
39 prefix_ bool senf::PacketInterpreterBase::is()
41 { static void const * const _ ((void*)&Type::dump); (void) _; }
42 return dynamic_cast< PacketInterpreter<Type>* >(this);
46 prefix_ typename senf::PacketInterpreter<Type>::ptr senf::PacketInterpreterBase::as()
48 { static void const * const _ ((void*)&Type::dump); (void) _; }
49 return typename PacketInterpreter<Type>::ptr(
50 static_cast< PacketInterpreter<Type>* >(this));
53 template <class Annotation>
54 prefix_ Annotation & senf::PacketInterpreterBase::annotation()
56 return impl().annotation<Annotation>();
59 ///////////////////////////////////////////////////////////////////////////
60 // senf::PacketInterpreter<PacketType>
62 template <class PacketType>
63 prefix_ senf::PacketInterpreter<PacketType>::~PacketInterpreter()
65 parser_p()->~parser();
68 template <class PacketType>
69 prefix_ typename senf::PacketInterpreter<PacketType>::factory_t
70 senf::PacketInterpreter<PacketType>::factory()
75 // Create completely new packet
77 template <class PacketType>
78 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
79 senf::PacketInterpreter<PacketType>::create()
81 return create(initSize());
84 template <class PacketType>
85 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
86 senf::PacketInterpreter<PacketType>::create(senf::NoInit_t)
88 return create(0,senf::noinit);
91 template <class PacketType>
92 template <class ForwardReadableRange>
93 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
94 senf::PacketInterpreter<PacketType>::create(ForwardReadableRange const & range)
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));
101 // Create packet as new packet after a given packet
103 template <class PacketType>
104 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
105 senf::PacketInterpreter<PacketType>::createAfter(PacketInterpreterBase::ptr packet)
107 return createAfter(packet, initSize());
110 template <class PacketType>
111 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
112 senf::PacketInterpreter<PacketType>::createAfter(PacketInterpreterBase::ptr packet, senf::NoInit_t)
114 return createAfter(packet, 0, senf::noinit);
117 // Create clone of current packet
119 template <class PacketType>
120 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
121 senf::PacketInterpreter<PacketType>::clone()
123 return boost::static_pointer_cast<typename ptr::element_type>(PacketInterpreterBase::clone());
126 // Packet field access
128 template <class PacketType>
129 prefix_ typename senf::PacketInterpreter<PacketType>::parser
130 senf::PacketInterpreter<PacketType>::fields()
132 return parser (data().begin(),&data());
135 template <class PacketType>
136 prefix_ typename senf::PacketInterpreter<PacketType>::parser *
137 senf::PacketInterpreter<PacketType>::fields_p()
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).
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.
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).
156 parser_p()->~parser();
157 new (parser_p()) parser (data().begin(),&data());
161 ////////////////////////////////////////
166 template <class PacketType>
167 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
168 senf::PacketInterpreter<PacketType>::create(detail::PacketImpl * impl, iterator b, iterator e,
171 return ptr(new PacketInterpreter(impl,b,e,Append));
174 template <class PacketType>
175 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
176 senf::PacketInterpreter<PacketType>::create(detail::PacketImpl * impl, iterator b, iterator e,
179 return ptr(new PacketInterpreter(impl,b,e,Prepend));
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)
187 new (parser_p()) parser (data().begin(),&data());
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)
195 new (parser_p()) parser (data().begin(),&data());
200 template <class PacketType>
201 prefix_ typename senf::PacketInterpreter<PacketType>::size_type
202 senf::PacketInterpreter<PacketType>::initSize()
204 return type::initSize();
207 template <class PacketType>
208 prefix_ typename senf::PacketInterpreter<PacketType>::size_type
209 senf::PacketInterpreter<PacketType>::initHeadSize()
211 size_type sz (type::initHeadSize());
212 return sz == size_type(-1) ? initSize() : sz ;
215 template <class PacketType>
216 prefix_ void senf::PacketInterpreter<PacketType>::init()
218 return type::init(ConcretePacket<PacketType>(ptr(this)));
221 template <class PacketType>
222 prefix_ typename senf::PacketInterpreter<PacketType>::parser *
223 senf::PacketInterpreter<PacketType>::parser_p()
225 return reinterpret_cast<parser *>(&parserStorage_);
228 ///////////////////////////////cti.e///////////////////////////////////////
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