Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright (C) 2007
// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
// Stefan Bund <g0dil@berlios.de>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the
// Free Software Foundation, Inc.,
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
/** \file
\brief PacketInterpreter public header */
#ifndef HH_PacketInterpreter_
#define HH_PacketInterpreter_ 1
// Custom includes
#include <boost/intrusive/ilist.hpp>
#include <boost/optional.hpp>
#include <boost/range.hpp>
#include <boost/type_traits/aligned_storage.hpp>
#include <boost/type_traits/alignment_of.hpp>
#include "Utils/intrusive_refcount.hh"
#include "Utils/pool_alloc_mixin.hh"
#include "PacketData.hh"
#include "typeidvalue.hh"
//#include "PacketInterpreter.mpp"
///////////////////////////////hh.p////////////////////////////////////////
namespace senf {
template <class PacketType> class PacketInterpreter;
/** \brief Internal: Base packet interpreter class
\internal
This is the base class for the persistent interpreter. This class encapsulates all the
functionality accessible via the packet handle, most handle operations are just forwarded.
*/
class PacketInterpreterBase
: protected PacketData,
public detail::packet::interpreter_list_base,
public intrusive_refcount_t<PacketInterpreterBase>
{
public:
///////////////////////////////////////////////////////////////////////////
// Types
typedef senf::detail::packet::smart_pointer<
PacketInterpreterBase>::ptr_t ptr;
typedef senf::detail::packet::iterator iterator;
typedef senf::detail::packet::const_iterator const_iterator;
typedef senf::detail::packet::size_type size_type;
typedef senf::detail::packet::difference_type difference_type;
typedef senf::detail::packet::byte byte;
typedef boost::iterator_range<iterator> range;
typedef boost::optional< boost::iterator_range<iterator> > optional_range;
typedef optional_range no_range;
enum Append_t { Append };
enum Prepend_t { Prepend };
enum NoInit_t { noinit };
/** \brief Internal: Abstract packet factory
\internal
This abstract class provides an abstract packet factory interface. It allows to call
almost any one of the create / createAfter / createBefore static PacketInterpreter
without static information on the type of packet to create.
*/
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
struct Factory {
virtual ~Factory();
// Create completely new packet
virtual ptr create() const = 0;
virtual ptr create(NoInit_t) const = 0;
virtual ptr create(size_type size) const = 0;
virtual ptr create(size_type size, NoInit_t) const = 0;
template <class ForwardReadableRange>
ptr create(ForwardReadableRange const & range) const;
// Create packet as new packet after a given packet
virtual ptr createAfter(PacketInterpreterBase::ptr packet) const = 0;
virtual ptr createAfter(PacketInterpreterBase::ptr packet, NoInit_t) const = 0;
virtual ptr createAfter(PacketInterpreterBase::ptr packet, size_type size) const = 0;
virtual ptr createAfter(PacketInterpreterBase::ptr packet, size_type size,
NoInit_t) const = 0;
template <class ForwardReadableRange>
ptr createAfter(PacketInterpreterBase::ptr packet,
ForwardReadableRange const & range) const;
// Create packet as new packet (header) const before a given packet
virtual ptr createBefore(PacketInterpreterBase::ptr packet) const = 0;
virtual ptr createBefore(PacketInterpreterBase::ptr packet, NoInit_t) const = 0;
// Parse next packet in chain
virtual ptr parseNext(ptr packet) const = 0;
};
typedef Factory const * factory_t;
///////////////////////////////////////////////////////////////////////////
///\name Structors and default members
///@{
// protected constructors
// no copy
// no conversion constructors
virtual ~PacketInterpreterBase();
static factory_t no_factory();
ptr clone();
///@}
///////////////////////////////////////////////////////////////////////////
///\name Interpreter chain access
///@{
ptr next();
ptr prev();
ptr first();
ptr last();
template <class Type> typename PacketInterpreter<Type>::ptr parseNextAs();
ptr parseNextAs(factory_t factory);
template <class Type> bool is();
template <class Type> typename PacketInterpreter<Type>::ptr as();
ptr append(ptr packet);
///@}
///\name Data access
///@{
using PacketData::valid;
PacketData & data();
///@}
///\name Access to the abstract interface
///@{
optional_range nextPacketRange();
void finalize();
void dump(std::ostream & os);
TypeIdValue typeId();
factory_t factory();
factory_t nextPacketType();
///@}
protected:
// protected structors
PacketInterpreterBase(detail::PacketImpl * impl, iterator b, iterator e, Append_t);
PacketInterpreterBase(detail::PacketImpl * impl, iterator b, iterator e, Prepend_t);
ptr appendClone(detail::PacketImpl * impl, iterator base, iterator new_base);
ptr appendClone(detail::PacketImpl * impl, range r);
// Need this for g++ < 4.0. Since PacketInterpreter is not publicly visible, it should not
// be a real problem to make impl() public here
using PacketData::impl;
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
private:
// abstract packet type interface
virtual optional_range v_nextPacketRange() = 0;
virtual ptr v_appendClone(detail::PacketImpl * impl, iterator base, iterator new_base) = 0;
virtual ptr v_appendClone(detail::PacketImpl * impl, range r) =0;
virtual void v_finalize() = 0;
virtual void v_dump(std::ostream & os) = 0;
virtual TypeIdValue v_type() = 0;
virtual factory_t v_factory() = 0;
virtual factory_t v_nextPacketType() = 0;
// reference/memory management. Only to be called by intrusive_refcount_t.
void add_ref();
bool release();
// containment management. Only to be called by PacketImpl.
void assignImpl(detail::PacketImpl *);
void releaseImpl();
friend class detail::PacketImpl;
friend class intrusive_refcount_t<PacketInterpreterBase>;
template <class PacketType> friend class PacketInterpreter;
friend class detail::packet::test::TestDriver;
};
/** \brief Internal: Concrete packet interpreter
\internal
Instantiations of this class build the interpreter chain. This class is accessed by the
packet handles. It provides the packet-type specific functionality in addition to the
interface defined in the PacketInterpreterBase class.
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
\see PacketTypeBase for the \a PacketType interface
*/
template <class PacketType>
class PacketInterpreter
: public PacketInterpreterBase,
public pool_alloc_mixin< PacketInterpreter<PacketType> >
{
public:
///////////////////////////////////////////////////////////////////////////
// Types
typedef typename senf::detail::packet::smart_pointer<
PacketInterpreter>::ptr_t ptr;
typedef PacketType type;
typedef typename type::parser parser;
///////////////////////////////////////////////////////////////////////////
///\name Structors and default members
///@{
// private constructors
// no copy
// no conversion constructors
~PacketInterpreter();
static factory_t factory();
// Create completely new packet
static ptr create();
static ptr create(NoInit_t);
static ptr create(size_type size);
static ptr create(size_type size, NoInit_t);
template <class ForwardReadableRange>
static ptr create(ForwardReadableRange const & range);
// Create packet as new packet after a given packet
static ptr createAfter(PacketInterpreterBase::ptr packet);
static ptr createAfter(PacketInterpreterBase::ptr packet, NoInit_t);
static ptr createAfter(PacketInterpreterBase::ptr packet, size_type size);
static ptr createAfter(PacketInterpreterBase::ptr packet, size_type size, NoInit_t);
template <class ForwardReadableRange>
static ptr createAfter(PacketInterpreterBase::ptr packet,
ForwardReadableRange const & range);
// Create packet as new packet (header) before a given packet
static ptr createBefore(PacketInterpreterBase::ptr packet);
static ptr createBefore(PacketInterpreterBase::ptr packet, NoInit_t);
// Create a clone of the current packet
ptr clone();
///@}
///////////////////////////////////////////////////////////////////////////
// Packet field access
parser fields();
parser * fields_p();
// PacketType access
static size_type initSize();
static size_type initHeadSize();
protected:
private:
// Private structors
PacketInterpreter(detail::PacketImpl * impl, iterator b, iterator e, Append_t);
PacketInterpreter(detail::PacketImpl * impl, iterator b, iterator e, Prepend_t);
static ptr create(detail::PacketImpl * impl, iterator b, iterator e, Append_t);
static ptr create(detail::PacketImpl * impl, iterator b, iterator e, Prepend_t);
// PacketType access
void init();
// virtual interface
virtual optional_range v_nextPacketRange();
virtual PacketInterpreterBase::ptr v_appendClone(detail::PacketImpl * impl,
iterator base, iterator new_base);
virtual PacketInterpreterBase::ptr v_appendClone(detail::PacketImpl * impl, range r);
virtual void v_finalize();
virtual void v_dump(std::ostream & os);
virtual TypeIdValue v_type();
virtual factory_t v_factory();
virtual factory_t v_nextPacketType();
// factory
/** \brief Internal: Implementation of abstract factory interface
\internal
Implements the abstract factory interface for \a PacketType
*/
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
struct FactoryImpl : public Factory {
// Create completely new packet
virtual PacketInterpreterBase::ptr create() const;
virtual PacketInterpreterBase::ptr create(NoInit_t) const;
virtual PacketInterpreterBase::ptr create(size_type size) const;
virtual PacketInterpreterBase::ptr create(size_type size,NoInit_t) const;
// Create packet as new packet after a given packet
virtual PacketInterpreterBase::ptr createAfter(PacketInterpreterBase::ptr packet)
const;
virtual PacketInterpreterBase::ptr createAfter(PacketInterpreterBase::ptr packet,
NoInit_t) const;
virtual PacketInterpreterBase::ptr createAfter(PacketInterpreterBase::ptr packet,
size_type size) const;
virtual PacketInterpreterBase::ptr createAfter(PacketInterpreterBase::ptr packet,
size_type size, NoInit_t) const;
// Create packet as new packet (header) before a given packet
virtual PacketInterpreterBase::ptr createBefore(PacketInterpreterBase::ptr packet)
const;
virtual PacketInterpreterBase::ptr createBefore(PacketInterpreterBase::ptr packet,
NoInit_t)
const;
// Parse next packet in chain
virtual PacketInterpreterBase::ptr parseNext(PacketInterpreterBase::ptr packet)
const;
};
static const FactoryImpl factory_;
parser * parser_p();
boost::aligned_storage< sizeof(parser),
boost::alignment_of<parser>::value > parserStorage_;
friend class detail::packet::test::TestDriver;
friend class PacketInterpreterBase;
friend class FactoryImpl;
};
/** \brief Invalid packet chain operation
This exception signals an invalid operation on the chain like trying to find a non-existent
chain member and other similar error conditions.
*/
struct InvalidPacketChainException : public std::exception
{ virtual char const * what() const throw() { return "invalid packet chain"; } };
}
///////////////////////////////hh.e////////////////////////////////////////
#endif
#if !defined(SENF_PACKETS_DECL_ONLY) && !defined(HH_PacketInterpreter_i_)
#define HH_PacketInterpreter_i_
#include "PacketInterpreter.cci"
#include "PacketInterpreter.ct"
#include "PacketInterpreter.cti"
#endif
// Local Variables:
// mode: c++
// fill-column: 100
// c-file-style: "senf"
// indent-tabs-mode: nil
// ispell-local-dictionary: "american"
// compile-command: "scons -u test"
// comment-column: 40