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 return dynamic_cast< PacketInterpreter<Type>* >(this);
45 prefix_ typename senf::PacketInterpreter<Type>::ptr senf::PacketInterpreterBase::as()
47 return typename PacketInterpreter<Type>::ptr(
48 static_cast< PacketInterpreter<Type>* >(this));
51 ///////////////////////////////////////////////////////////////////////////
52 // senf::PacketInterpreter<PacketType>
54 template <class PacketType>
55 prefix_ senf::PacketInterpreter<PacketType>::~PacketInterpreter()
57 parser_p()->~parser();
60 template <class PacketType>
61 prefix_ typename senf::PacketInterpreter<PacketType>::factory_t
62 senf::PacketInterpreter<PacketType>::factory()
67 // Create completely new packet
69 template <class PacketType>
70 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
71 senf::PacketInterpreter<PacketType>::create()
73 return create(initSize());
76 template <class PacketType>
77 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
78 senf::PacketInterpreter<PacketType>::create(NoInit_t)
80 return create(0,noinit);
83 template <class PacketType>
84 template <class ForwardReadableRange>
85 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
86 senf::PacketInterpreter<PacketType>::create(ForwardReadableRange const & range)
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));
93 // Create packet as new packet after a given packet
95 template <class PacketType>
96 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
97 senf::PacketInterpreter<PacketType>::createAfter(PacketInterpreterBase::ptr packet)
99 return createAfter(packet, initSize());
102 template <class PacketType>
103 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
104 senf::PacketInterpreter<PacketType>::createAfter(PacketInterpreterBase::ptr packet, NoInit_t)
106 return createAfter(packet, 0, noinit);
109 // Create clone of current packet
111 template <class PacketType>
112 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
113 senf::PacketInterpreter<PacketType>::clone()
115 return boost::static_pointer_cast<typename ptr::element_type>(PacketInterpreterBase::clone());
118 // Packet field access
120 template <class PacketType>
121 prefix_ typename senf::PacketInterpreter<PacketType>::parser
122 senf::PacketInterpreter<PacketType>::fields()
124 return parser (data().begin(),&data());
127 template <class PacketType>
128 prefix_ typename senf::PacketInterpreter<PacketType>::parser *
129 senf::PacketInterpreter<PacketType>::fields_p()
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).
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.
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).
148 parser_p()->~parser();
149 new (parser_p()) parser (data().begin(),&data());
153 ////////////////////////////////////////
158 template <class PacketType>
159 prefix_ typename senf::PacketInterpreter<PacketType>::ptr
160 senf::PacketInterpreter<PacketType>::create(detail::PacketImpl * impl, iterator b, iterator e,
163 return ptr(new PacketInterpreter(impl,b,e,Append));
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,Prepend));
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)
179 new (parser_p()) parser (data().begin(),&data());
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)
187 new (parser_p()) parser (data().begin(),&data());
192 template <class PacketType>
193 prefix_ typename senf::PacketInterpreter<PacketType>::size_type
194 senf::PacketInterpreter<PacketType>::initSize()
196 return type::initSize();
199 template <class PacketType>
200 prefix_ typename senf::PacketInterpreter<PacketType>::size_type
201 senf::PacketInterpreter<PacketType>::initHeadSize()
203 size_type sz (type::initHeadSize());
204 return sz == size_type(-1) ? initSize() : sz ;
207 template <class PacketType>
208 prefix_ void senf::PacketInterpreter<PacketType>::init()
210 return type::init(ConcretePacket<PacketType>(ptr(this)));
213 template <class PacketType>
214 prefix_ typename senf::PacketInterpreter<PacketType>::parser *
215 senf::PacketInterpreter<PacketType>::parser_p()
217 return reinterpret_cast<parser *>(&parserStorage_);
220 ///////////////////////////////cti.e///////////////////////////////////////
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