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 ///////////////////////////////////////////////////////////////////////////
54 // senf::PacketInterpreter<PacketType>
56 template <class PacketType>
57 prefix_ senf::PacketInterpreter<PacketType>::~PacketInterpreter()
59 parser_p()->~parser();
62 template <class PacketType>
63 prefix_ typename senf::PacketInterpreter<PacketType>::factory_t
64 senf::PacketInterpreter<PacketType>::factory()
69 // Create completely new packet
71 template <class PacketType>
72 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
73 senf::PacketInterpreter<PacketType>::create()
75 return create(initSize());
78 template <class PacketType>
79 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
80 senf::PacketInterpreter<PacketType>::create(senf::NoInit_t)
82 return create(0,senf::noinit);
85 template <class PacketType>
86 template <class ForwardReadableRange>
87 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
88 senf::PacketInterpreter<PacketType>::create(ForwardReadableRange const & range)
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));
95 // Create packet as new packet after a given packet
97 template <class PacketType>
98 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
99 senf::PacketInterpreter<PacketType>::createAfter(PacketInterpreterBase::ptr packet)
101 return createAfter(packet, initSize());
104 template <class PacketType>
105 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
106 senf::PacketInterpreter<PacketType>::createAfter(PacketInterpreterBase::ptr packet, senf::NoInit_t)
108 return createAfter(packet, 0, senf::noinit);
111 // Create clone of current packet
113 template <class PacketType>
114 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
115 senf::PacketInterpreter<PacketType>::clone()
117 return boost::static_pointer_cast<typename ptr::element_type>(PacketInterpreterBase::clone());
120 // Packet field access
122 template <class PacketType>
123 prefix_ typename senf::PacketInterpreter<PacketType>::parser
124 senf::PacketInterpreter<PacketType>::fields()
126 return parser (data().begin(),&data());
129 template <class PacketType>
130 prefix_ typename senf::PacketInterpreter<PacketType>::parser *
131 senf::PacketInterpreter<PacketType>::fields_p()
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).
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.
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).
150 parser_p()->~parser();
151 new (parser_p()) parser (data().begin(),&data());
155 ////////////////////////////////////////
160 template <class PacketType>
161 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
162 senf::PacketInterpreter<PacketType>::create(detail::PacketImpl * impl, iterator b, iterator e,
165 return ptr(new PacketInterpreter(impl,b,e,Append));
168 template <class PacketType>
169 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
170 senf::PacketInterpreter<PacketType>::create(detail::PacketImpl * impl, iterator b, iterator e,
173 return ptr(new PacketInterpreter(impl,b,e,Prepend));
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)
181 new (parser_p()) parser (data().begin(),&data());
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)
189 new (parser_p()) parser (data().begin(),&data());
194 template <class PacketType>
195 prefix_ typename senf::PacketInterpreter<PacketType>::size_type
196 senf::PacketInterpreter<PacketType>::initSize()
198 return type::initSize();
201 template <class PacketType>
202 prefix_ typename senf::PacketInterpreter<PacketType>::size_type
203 senf::PacketInterpreter<PacketType>::initHeadSize()
205 size_type sz (type::initHeadSize());
206 return sz == size_type(-1) ? initSize() : sz ;
209 template <class PacketType>
210 prefix_ void senf::PacketInterpreter<PacketType>::init()
212 return type::init(ConcretePacket<PacketType>(ptr(this)));
215 template <class PacketType>
216 prefix_ typename senf::PacketInterpreter<PacketType>::parser *
217 senf::PacketInterpreter<PacketType>::parser_p()
219 return reinterpret_cast<parser *>(&parserStorage_);
222 ///////////////////////////////cti.e///////////////////////////////////////
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