090cd661e3b2cba735eb90f2ecfa2c24a5890066
[senf.git] / senf / Packets / Packet.hh
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 Packet public header */
25
26 #ifndef HH_SENF_Packets_Packet_
27 #define HH_SENF_Packets_Packet_ 1
28
29 // Custom includes
30 #include <boost/operators.hpp>
31 #include <boost/utility.hpp>
32 #include <boost/type_traits/is_integral.hpp>
33 #include <senf/Utils/Tags.hh>
34 #include <senf/Utils/safe_bool.hh>
35 #include "PacketInterpreter.hh"
36
37 //#include "Packet.mpp"
38 ///////////////////////////////hh.p////////////////////////////////////////
39
40 namespace senf {
41
42     /** \defgroup packet_module Packet Handling
43
44         The basic groundwork of the %Packet library is the packet handling:
45
46         \li The packet classes provide access to a chain of packet headers (more generically called
47             interpreters).
48         \li They automatically manage the required memory resources and the shared packet data.
49
50         \section packet_module_chain The Interpreter Chain
51
52         The central data structure for a packet is the interpreter chain
53
54         \image html structure.png The Interpreter Chain
55
56         This image depicts a packet with several headers. Each interpreter is responsible for a
57         specific sub-range of the complete packet. This range always \e includes the packets payload
58         (This is, why we call the data structure interpreter and not header: The interpreter is
59         responsible for interpreting a range of the packet according to a specific protocol), the
60         packet interpreters are nested inside each other.
61
62         For each interpreter, this structure automatically divides the packet into three areas (each
63         of which are optional): The header, the payload and the trailer. Every packet will have
64         either a header or a payload section while most don't have a trailer.
65
66         As user of the library you always interact with the chain through one (or more) of the
67         interpreters. The interpreter provides methods to traverse to the following or preceding
68         header (interpreter) and provides two levels of access to the packet data: Generic low-level
69         access in the form of an STL compatible sequence and access to the parsed fields which are
70         provided by the parser associated with the concrete packet type.
71
72         \section packet_module_management Resource Management
73
74         The interface to the packet library is provided using a handle class (\ref Packet for
75         generic, protocol agnostic access and \ref ConcretePacket derived from \ref Packet to access
76         a specific protocol). This handle automatically manages the resources associated with the
77         packet (the interpreter chain and the data storage holding the packet data). The resources
78         are automatically released when the last packet handle referencing a specific packet is
79         destroyed.
80
81         \implementation The packet chain is provided on two levels: The internal representation \ref
82             PacketInterpreterBase and \ref PacketInterpreter which are referenced by the Handle
83             classes \ref Packet and \ref ConcretePacket. \n
84             The internal representation classes are pertinent in the sense, that they exist
85             regardless of the existence of a handle referencing them (as long as the packet
86             exists). Still the interpreter chain is lazy and packet interpreters beside the first
87             are only created dynamically when accessed (this is implemented in the handle not in the
88             internal representation). \n
89             The packet interpreters make use of a pool allocator. This provides extremely efficient
90             creation and destruction of packet interpreter's and removes the dynamic memory
91             management overhead from the packet interpreter management. The packet implementation
92             class (\ref PacketImpl which holds the packet data itself) however is still dynamically
93             managed (however there is only a single instance for each packet).
94      */
95
96     template <class PackeType> class ConcretePacket;
97
98     ///\addtogroup packet_module
99     ///@{
100
101     /** \brief Main %Packet class
102
103         %Packet is the main externally visible class of the packet library. %Packet is a handle into
104         the internal packet representation. From %Packet you may access the data of that specific
105         sub-packet/header/interpreter and navigate to the neighboring
106         sub-packets/headers/interpreters.
107
108         %Packet is protocol agnostic. This class only provides non-protocol dependent members. To
109         access the protocol specific features of a packet (like header fields) the ConcretePacket
110         class extending %Packet is provided.
111
112         \section packet_semantics Semantics
113
114         All operations accessing the data of \c this packet in some way will ignore any preceding
115         packets/headers/interpreters in the chain. It does not matter, whether a given packet is
116         taken from the middle or the beginning of the chain, all operations (except those explicitly
117         accessing the chain of course) should work the same.
118
119         This especially includes members like clone() or append(): clone() will clone \e only from
120         \c this packet until the end of the chain, append() will append the given packet \e ignoring
121         any possibly preceding packets/headers/interpreters.
122
123         In the same way, the data() member provides an STL-sequence compatible view of the packet
124         data. This only includes the data which is part of \c this packet including header, trailer
125         \e and payload but \e not the headers or trailers of packets \e before \c this packet in the
126         packet/header/interpreter chain (nonetheless, this data overlaps with the data of other
127         packets).
128
129         Several members are member templates taking an \a OtherPacket template parameter. This
130         parameter must be the ConcretePacket instantiation associated with some concrete packet type
131         (protocol). For each implemented protocol, typedefs should be provided for these
132         instantiations (Example: \ref EthernetPacket is a typedef for
133         \ref ConcretePacket < \ref EthernetPacketType >).
134
135         \see
136             \ref ConcretePacket for the %type specific interface\n
137             \ref PacketData for the sequence interface\n
138             \ref packetparser for a specification of the parser interface
139      */
140     class Packet
141         : public safe_bool<Packet>,
142           public boost::equality_comparable<Packet>
143     {
144     public:
145         ///////////////////////////////////////////////////////////////////////////
146         // Types
147
148         typedef void type;              ///< Type of the packet.
149         typedef senf::detail::packet::size_type size_type;
150         ///< Unsigned type to represent packet size
151         typedef PacketInterpreterBase::factory_t factory_t; ///< Packet factory type (see below)
152
153         ///////////////////////////////////////////////////////////////////////////
154         ///\name Structors and default members
155         ///@{
156
157         // default copy constructor
158         // default copy assignment
159         // default destructor
160
161         Packet();                       ///< Create uninitialized packet handle
162                                         /**< An uninitialized handle is in - valid(). It does not
163                                              allow any operation except assignment and checking for
164                                              validity. */
165         Packet clone() const;           ///< Create copy packet
166                                         /**< clone() will create a complete copy of \c this
167                                              packet. The returned packet will have the same data and
168                                              packet chain. It does however not share any data with
169                                              the original packet. */
170
171         // conversion constructors
172
173         template <class PacketType>
174         Packet(ConcretePacket<PacketType> const & packet);
175                                         ///< Copy-construct Packet from ConcretePacket
176                                         /**< This constructor allows to convert an arbitrary
177                                              ConcretePacket into a general Packet, loosing the
178                                              protocol specific interface. */
179
180         ///@}
181         ///////////////////////////////////////////////////////////////////////////
182
183         ///\name Interpreter chain access
184         ///@{
185
186         Packet      next() const;       ///< Get next packet in chain
187                                         /**< \throws InvalidPacketChainException if no next packet
188                                              exists */
189         Packet      next(NoThrow_t) const; ///< Get next packet in chain
190                                         /**< \returns in - valid() packet if no next packet
191                                              exists */
192         template <class OtherPacket> OtherPacket next() const;
193                                         ///< Get next packet in chain and cast to \a OtherPacket
194                                         /**< \throws std::bad_cast if the next() packet is not of
195                                              type \a OtherPacket
196                                              \throws InvalidPacketChainException if no next packet
197                                                  exists */
198         template <class OtherPacket> OtherPacket next(NoThrow_t) const;
199                                         ///< Get next packet in chain and cast to \a OtherPacket
200                                         /**< \returns in - valid() packet if no next packet
201                                                  exists or if next() packet is not of
202                                                  type \a OtherPacket */
203         template <class OtherPacket> OtherPacket find() const;
204                                         ///< Search chain forward for packet of type \a OtherPacket
205                                         /**< The search will start with the current packet.
206                                              \throws InvalidPacketChainException if no packet of
207                                                  type \a OtherPacket can be found. */
208         template <class OtherPacket> OtherPacket find(NoThrow_t) const;
209                                         ///< Search chain forward for packet of type \a OtherPacket
210                                         /**< The search will start with the current packet.
211                                              \returns in - valid() packet if no packet of type \a
212                                                  OtherPacket can be found. */
213
214         Packet      prev() const;       ///< Get previous packet in chain
215                                         /**< \throws InvalidPacketChainException if no previous
216                                              packet exists */
217         Packet      prev(NoThrow_t) const; ///< Get previous packet in chain
218                                         /**< \returns in - valid() packet if no previous packet
219                                              exists */
220         template <class OtherPacket> OtherPacket prev() const;
221                                         ///< Get previous packet in chain and cast to \a OtherPacket
222                                         /**< \throws std::bad_cast, if the previous packet is not of
223                                              type \a OtherPacket
224                                              \throws InvalidPacketChainException if no previous
225                                                  packet exists */
226         template <class OtherPacket> OtherPacket prev(NoThrow_t) const;
227                                         ///< Get previous packet in chain and cast to \a OtherPacket
228                                         /**< \returns in - valid() packet if no previous packet
229                                                  exists or if the previous packet is not of
230                                                  type \a OtherPacket */
231         template <class OtherPacket> OtherPacket rfind() const;
232                                         ///< Search chain backwards for packet of type \a OtherPacket
233                                         /**< The search will start with the current packet.
234                                              \throws InvalidPacketChainException if no packet of
235                                                  type \a OtherPacket can be found. */
236         template <class OtherPacket> OtherPacket rfind(NoThrow_t) const;
237                                         ///< Search chain backwards for packet of type \a OtherPacket
238                                         /**< The search will start with the current packet.
239                                              \returns in - valid() packet if no packet of type \a
240                                                  OtherPacket can be found. */
241
242
243         Packet      first() const;      ///< Return first packet in chain
244         template <class OtherPacket> OtherPacket first() const;
245                                         ///< Return first packet in chain and cast
246                                         /**< \throws std::bad_cast if the first() packet is not of
247                                              type \a OtherPacket */
248
249         Packet      last() const;       ///< Return last packet in chain
250         template <class OtherPacket> OtherPacket last() const;
251                                         ///< Return last packet in chain and cast
252                                         /**< \throws std::bad_cast if the last() packet is not of
253                                              type \a OtherPacket  */
254
255
256         template <class OtherPacket> OtherPacket parseNextAs() const;
257                                         ///< Interpret payload of \c this as \a OtherPacket
258                                         /**< parseNextAs() will throw away the packet chain after
259                                              the current packet if necessary. It will then parse the
260                                              payload section of \c this packet as given by \a
261                                              OtherPacket. The new packet is added to the chain after
262                                              \c this.
263                                              \returns new packet instance sharing the same data and
264                                                  placed after \c this packet in the chain.
265                                              \throws InvalidPacketChainException if no next packet
266                                                  header is allowed (viz. nextPacketRange() of the
267                                                  the current PacketType returns no_range() ) */
268         Packet      parseNextAs(factory_t factory) const;
269                                         ///< Interpret payload of \c this as \a factory type packet
270                                         /**< parseNextAs() will throw away the packet chain after
271                                              the current packet if necessary. It will then parse the
272                                              payload section of \c this packet as given by \a
273                                              factory. The new packet is added to the chain after
274                                              \c this.
275                                              \returns new packet instance sharing the same data and
276                                                  placed after \c this packet in the chain.
277                                              \throws InvalidPacketChainException if no next packet
278                                                  header is allowed (viz. nextPacketRange() of the
279                                                  the current PacketType returns no_range() ) */
280
281         template <class OtherPacket> bool        is() const;
282                                         ///< Check, whether \c this packet is of the given type
283         template <class OtherPacket> OtherPacket as() const;
284                                         ///< Cast current packet to the given type
285                                         /**< This operations returns a handle to the same packet
286                                              header/interpreter however upcast to the given
287                                              ConcretePacket type which have been instantiated
288                                              before.
289                                              \throws std::bad_cast if the current packet is not of
290                                                  type \a OtherPacket */
291         template <class OtherPacket> OtherPacket as(NoThrow_t) const;
292                                         ///< Cast current packet to the given type
293                                         /**< This operations returns a handle to the same packet
294                                              header/interpreter however upcast to the given
295                                              ConcretePacket type which have been instantiated
296                                              before.
297                                              \warning You must make absolutely sure that the packet
298                                              is of the given type. If not, calling this member
299                                              crashes your program in a unkindly way. */
300
301         Packet append(Packet const & packet) const; ///< Append the given packet to \c this packet
302                                         /**< This operation will replace the payload section of \c
303                                              this packet with \a packet. This operation will replace
304                                              the packet chain after \c this packet with a clone of
305                                              \a packet and will replace the raw data of the payload
306                                              of \c this with the raw data of \a packet. \c this
307                                              packet will not share any data with \a packet.
308                                              \returns Packet handle to the cloned \a packet, placed
309                                                  after \c this in the packet/header/interpreter
310                                                  chain. */
311
312         ///@}
313
314         ///\name Data access
315         ///@{
316
317         PacketData & data() const;      ///< Access the packets raw data container
318         size_type size() const;         ///< Return size of packet in bytes
319                                         /**< This size does \e not include the size of any preceding
320                                              headers/packets/interpreters. It does however include
321                                              \c this packets payload. */
322
323         ///@}
324
325         ///\name Annotations
326         ///@{
327
328         template <class Annotation>
329         Annotation & annotation();      ///< Get packet annotation
330                                         /**< This member will retrieve an arbitrary packet
331                                              annotation. Every annotation is identified by a unique
332                                              \a Annotation type. This type should \e always be a \c
333                                              struct.
334
335                                              \code
336                                              struct MyAnnotation {
337                                                  int value;
338                                              };
339
340                                              senf::Packet p (...);
341
342                                              p.annotation<MyAnnotation>().value = 1;
343                                              \endcode
344
345                                              Annotations are shared by all headers / interpreters
346                                              within a single packet chain.
347
348                                              If an annotation is \e not a POD type (more
349                                              specifically, if it's constructor or destructor is not
350                                              trivial including base classes and members), the \a
351                                              Annotation type \e must inherit from
352                                              senf::ComplexAnnotation. Failing to follow this rule
353                                              will result in undefined behavior and will probably
354                                              lead to a program crash.
355
356                                              \code
357                                              struct MyStringAnnotation : senf::ComplexAnnotation {
358                                                  std::string value;
359                                              };
360                                              \endcode
361                                              (This type is not POD since \c std::string is not POD)
362
363                                              \see \ref packet_usage_annotation
364
365                                              \implementation The annotation system is implemented
366                                                  quite efficiently since annotations are stored
367                                                  within a packet embedded vector of fixed size (the
368                                                  size is determined automatically at runtime by the
369                                                  number of different annotations
370                                                  used). Additionally, non-complex small annotations
371                                                  require no additional memory management (\c new /
372                                                  \c delete).
373
374                                              \idea Pool the annotation vectors: In the destructor
375                                                  swap the vector into a vector graveyard (swapping
376                                                  two vectors is an O(1) no allocation operation). In
377                                                  the constructor, if there is a vector in the
378                                                  graveyard, swap it in from there. Of course, it
379                                                  would be better to do away with the vector and just
380                                                  allocate the space together with the packet but
381                                                  that looks quite complicated to do ... especially
382                                                  considering that the packetimpl itself uses a pool.
383                                           */
384
385         ///@}
386
387         template <class Annotation>
388         Annotation const & annotation() const; ///< Get packet annotation
389                                         /**< \see annotation() */
390
391         ///\name Other methods
392         ///@{
393
394         bool operator==(Packet const & other) const; ///< Check for packet identity
395                                         /**< Two packet handles compare equal if they really are the
396                                              same packet header in the same packet chain. */
397         bool boolean_test() const;      ///< Check, whether the packet is valid()
398                                         /**< \see valid() */
399         bool valid() const;             ///< Check, whether the packet is valid()
400                                         /**< An in - valid() packet does not allow any operation
401                                              except checking for validity and assignment. in -
402                                              valid() packets serve the same role as 0-pointers.
403
404                                              This is an alias for boolean_test() which is called
405                                              when using a packet in a boolean context. */
406
407         void finalizeThis();            ///< Update calculated fields
408                                         /**< The finalize() family of members will update
409                                              calculated packet fields: checksums, size fields and so
410                                              on. This includes any field, which can be set from
411                                              other information in the packet. Each concrete packet
412                                              type should document, which fields are set by
413                                              finalize().
414
415                                              finalizeThis() will \e only process the current
416                                              header. Even if only changing fields in this protocol,
417                                              depending on the protocol it may not be enough to
418                                              finalize this header only. See the packet type
419                                              documentation. */
420
421         template <class Other>
422         void finalizeTo();              ///< Update calculated fields
423                                         /**< The finalize() family of members will update
424                                              calculated packet fields: checksums, size fields and so
425                                              on. This includes any field, which can be set from
426                                              other information in the packet. Each concrete packet
427                                              type should document, which fields are set by
428                                              finalize().
429
430                                              finalizeTo() will automatically process all
431                                              packets/headers/interpreters from the \e first
432                                              occurrence of packet type \a Other (beginning at \c
433                                              this packet searching forward towards deeper nested
434                                              packets) backwards up to \c this.
435
436                                              This call is equivalent to
437                                              \code
438                                                  p.finalizeTo(p.next<Other>())
439                                              \endcode */
440
441         void finalizeTo(Packet const & other);  ///< Update calculated fields
442                                         /**< The finalize() family of members will update
443                                              calculated packet fields: checksums, size fields and so
444                                              on. This includes any field, which can be set from
445                                              other information in the packet. Each concrete packet
446                                              type should document, which fields are set by
447                                              finalize().
448
449                                              finalizeTo(other) will automatically process all
450                                              packets/headers/interpreters beginning at \a other
451                                              backwards towards outer packets up to \c this. */
452
453         void finalizeAll();             ///< Update calculated fields
454                                         /**< The finalize() fammily of members will update
455                                              calculated packet fields: checksums, size fields and so
456                                              on. This includes any field, which can be set from
457                                              other information in the packet. Each concrete packet
458                                              type should document, which fields are set by
459                                              finalize().
460
461                                              finalizeAll() will automatically process all
462                                              packets/headers/interpreters from the end of the chain
463                                              (the most inner packet) backwards up to \c this.
464
465                                              This call is equivalent to
466                                              \code
467                                                  p.finalizeTo(p.last())
468                                              \endcode
469
470                                              Beware, that finalizeAll() will \e not finalize any
471                                              headers before \c this, it will \e only process inner
472                                              headers. */
473
474         void dump(std::ostream & os) const; ///< Write out a printable packet representation
475                                         /**< This method is provided mostly to help debugging packet
476                                              problems. Each concrete packet should implement a dump
477                                              method writing out all fields of the packet in a
478                                              readable representation. dump() will call this member
479                                              for each packet/header/interpreter in the chain from \c
480                                              this packet up to the end of the chain. */
481
482         TypeIdValue typeId() const;     ///< Get type of \c this packet
483                                         /**< This value is used e.g. in the packet registry to
484                                              associate packet types with other information.
485                                              \returns A type holding the same information as a
486                                              type_info object, albeit assignable */
487         factory_t factory() const;      ///< Return factory instance of \c this packet
488                                         /**< The returned factory instance can be used to create new
489                                              packets of the given type without knowing the concrete
490                                              type of the packet. The value may be stored away for
491                                              later use if needed. */
492
493         unsigned long id() const;       ///< Unique packet id
494                                         /**< Get a unique packet id. If two packets have the same
495                                              id, they share the internal data representation.. */
496
497         ///@}
498
499     protected:
500         explicit Packet(PacketInterpreterBase::ptr const & packet);
501
502         PacketInterpreterBase::ptr const & ptr() const;
503
504     private:
505         Packet getNext() const;
506         Packet getLast() const;
507
508         PacketInterpreterBase::ptr packet_;
509
510         template <class PacketType>
511         friend class ConcretePacket;
512         friend class PacketParserBase;
513     };
514
515     /** \brief Protocol specific packet handle
516
517         The ConcretePacket template class extends Packet to provide protocol/packet type specific
518         aspects. These are packet constructors and access to the parsed packet fields.
519
520         The \c PacketType template argument to ConcretePacket is a protocol specific and internal
521         policy class which defines the protocol specific behavior. To access a specific type of
522         packet, the library provides corresponding typedefs of ConcretePacket < \a SomePacketType >
523         (e.g. \ref EthernetPacket as typedef for \ref ConcretePacket < \ref EthernetPacketType >).
524
525         The new members provided by ConcretePacket over packet are mostly comprised of the packet
526         constructors. These come in three major flavors:
527
528         \li The create() family of constructors will create completely new packets.
529         \li The createAfter() family of constructors will create new packets (with new data for the
530             packet) \e after a given existing packet <em>thereby destroying and overwriting  any
531             possibly existing packets and data after the given packet</em>.
532         \li The createBefore() family of constructors will create new packets (again with new data)
533             \e before a given existing packet <em>thereby destroying and overwriting any possibly
534             existing packets and data before the given packet</em>.
535         \li The createInsertBefore() family of constructors will create new packets \e before a
536             given packet \e inserting them into the packet chain after any existing packets before
537             the given packet.
538
539         Whereas create() will create a completely new packet with it's own chain and data storage,
540         createAfter(), createBefore() and createInsertBefore() extend a packet with additional
541         headers/interpreters. createAfter() will set the payload of the given packet to the new
542         packet whereas createBefore() and createInsertBefore() will create a new packet with the
543         existing packet as it's payload.
544
545         createAfter() differs from Packet::parseNextAs() in that the former creates a new packet \e
546         replacing any possibly existing data whereas the latter will interpret the already \e
547         existing data as given by the type argument.
548
549         \see \ref PacketTypeBase for a specification of the interface to be provided by the \a
550             PacketType policy class.
551      */
552     template <class PacketType>
553     class ConcretePacket
554         : public Packet
555     {
556     public:
557         ///////////////////////////////////////////////////////////////////////////
558         // Types
559
560         typedef PacketType type;
561         typedef typename PacketType::parser Parser;
562
563         ///////////////////////////////////////////////////////////////////////////
564         ///\name Structors and default members
565         ///@{
566
567         // default copy constructor
568         // default copy assignment
569         // default destructor
570         // no conversion constructors
571
572         ConcretePacket();               ///< Create uninitialized packet handle
573                                         /**< An uninitialized handle is not valid(). It does not
574                                              allow any operation except assignment and checking for
575                                              validity. */
576
577         static factory_t factory();     ///< Return factory for packets of specific type
578                                         /**< This \e static member is like Packet::factory() for a
579                                              specific packet of type \a PacketType */
580
581         // Create completely new packet
582
583         static ConcretePacket create(); ///< Create default initialized packet
584                                         /**< The packet will be initialized to it's default empty
585                                              state. */
586         static ConcretePacket create(senf::NoInit_t); ///< Create uninitialized empty packet
587                                         /**< This will create a completely empty and uninitialized
588                                              packet with <tt>size() == 0</tt>.
589                                              \param[in] senf::noinit This parameter must always have
590                                                  the value \c senf::noinit. */
591         static ConcretePacket create(size_type size); ///< Create default initialized packet
592                                         /**< This member will create a default initialized packet
593                                              with the given size. If the size parameter is smaller
594                                              than the minimum allowed packet size an exception will
595                                              be thrown.
596                                              \param[in] size Size of the packet to create in bytes.
597                                              \throws TruncatedPacketException if \a size is smaller
598                                                  than the smallest permissible size for this type of
599                                                  packet. */
600         static ConcretePacket create(size_type size, senf::NoInit_t);
601                                         ///< Create uninitialized packet
602                                         /**< Creates an uninitialized (all-zero) packet of the exact
603                                              given size.
604                                              \param[in] size Size of the packet to create in bytes
605                                              \param[in] senf::noinit This parameter must always have
606                                                  the value \c senf::noinit. */
607 #ifndef DOXYGEN
608         template <class ForwardReadableRange>
609         static ConcretePacket create(
610             ForwardReadableRange const & range,
611             typename boost::disable_if< boost::is_integral<ForwardReadableRange> >::type * = 0);
612 #else
613         template <class ForwardReadableRange>
614         static ConcretePacket create(ForwardReadableRange const & range);
615                                         ///< Create packet from given data
616                                         /**< The packet will be created from a copy of the given
617                                              data. The data from the range will be copied directly
618                                              into the packet representation. The data will \e not be
619                                              validated in any way.
620
621                                              \param[in] range <a href="http://www.boost.org/doc/libs/release/libs/range/index.html">Boost.Range</a>
622                                                  of data to construct packet from. */
623 #endif
624
625         // Create packet as new packet after a given packet
626
627         static ConcretePacket createAfter(Packet const & packet);
628                                         ///< Create default initialized packet after \a packet
629                                         /**< The packet will be initialized to it's default empty
630                                              state. It will be appended as next header/interpreter
631                                              after \a packet in that packets interpreter chain.
632                                              \param[in] packet Packet to append new packet to. */
633         static ConcretePacket createAfter(Packet const & packet, senf::NoInit_t);
634                                         ///< Create uninitialized empty packet after\a packet
635                                         /**< This will create a completely empty and uninitialized
636                                              packet with <tt>size() == 0</tt>. It will be appended
637                                              as next header/interpreter after \a packet in that
638                                              packets interpreter chain.
639                                              \param[in] packet Packet to append new packet to.
640                                              \param[in] senf::noinit This parameter must always have
641                                                  the value \c senf::noinit. */
642         static ConcretePacket createAfter(Packet const & packet, size_type size);
643                                         ///< Create default initialized packet after \a packet
644                                         /**< This member will create a default initialized packet
645                                              with the given size. If the size parameter is smaller
646                                              than the minimum allowed packet size an exception will
647                                              be thrown. It will be appended as next
648                                              header/interpreter after \a packet in that packets
649                                              interpreter chain.
650                                              \param[in] packet Packet to append new packet to.
651                                              \param[in] size Size of the packet to create in bytes.
652                                              \throws TruncatedPacketException if \a size is smaller
653                                                  than the smallest permissible size for this type of
654                                                  packet. */
655         static ConcretePacket createAfter(Packet const & packet, size_type size, senf::NoInit_t);
656                                         ///< Create uninitialized packet after \a packet
657                                         /**< Creates an uninitialized (all-zero) packet of the exact
658                                              given size.  It will be appended as next
659                                              header/interpreter after \a packet in that packets
660                                              interpreter chain.
661                                              \param[in] packet Packet to append new packet to.
662                                              \param[in] size Size of the packet to create in bytes
663                                              \param[in] senf::noinit This parameter must always have
664                                                  the value \c senf::noinit. */
665 #ifndef DOXYGEN
666         template <class ForwardReadableRange>
667         static ConcretePacket createAfter(
668             Packet const & packet,
669             ForwardReadableRange const & range,
670             typename boost::disable_if< boost::is_integral<ForwardReadableRange> >::type * = 0);
671 #else
672         template <class ForwardReadableRange>
673         static ConcretePacket createAfter(Packet const & packet,
674                                           ForwardReadableRange const & range);
675                                         ///< Create packet from given data after \a packet
676                                         /**< The packet will be created from a copy of the given
677                                              data. The data from the range will be copied directly
678                                              into the packet representation. The data will \e not be
679                                              validated in any way.  It will be appended as next
680                                              header/interpreter after \a packet in that packets
681                                              interpreter chain.
682                                              \param[in] packet Packet to append new packet to.
683                                              \param[in] range <a href="http://www.boost.org/doc/libs/release/libs/range/index.html">Boost.Range</a>
684                                                  of data to construct packet from. */
685 #endif
686
687         // Create packet as new packet (header) before a given packet
688
689         static ConcretePacket createBefore(Packet const & packet);
690                                         ///< Create default initialized packet before \a packet
691                                         /**< The packet will be initialized to it's default empty
692                                              state. It will be prepended as previous
693                                              header/interpreter before \a packet in that packets
694                                              interpreter chain.
695                                              \warning This constructor will destroy any existing
696                                                  headers before \a packet and replace them with the
697                                                  new header.
698                                              \param[in] packet Packet to prepend new packet to. */
699         static ConcretePacket createBefore(Packet const & packet, senf::NoInit_t);
700                                         ///< Create uninitialized empty packet before \a packet
701                                         /**< Creates a completely empty and uninitialized packet. It
702                                              will be prepended as previous header/interpreter before
703                                              \a packet in that packets interpreter chain.
704                                              \warning This constructor will destroy any existing
705                                                  headers before \a packet and replace them with the
706                                                  new header.
707                                              \param[in] packet Packet to prepend new packet to. */
708
709         static ConcretePacket createInsertBefore(Packet const & packet);
710                                         ///< Insert default initialized packet before \a packet
711                                         /**< The new packet header will be initialized to it' s
712                                              default empty state. It will be inserted into the
713                                              packet chain before \a packet.
714                                              \param[in] packet Packet before which to insert the new
715                                                  packet */
716         static ConcretePacket createInsertBefore(Packet const & packet, senf::NoInit_t);
717                                         ///< Insert uninitialized empty packet before \a packet
718                                         /**< Inserts a completely empty and unitialized packet
719                                              before \a packet into the header/interpreter chain.
720                                              \param[in] packet Packet before which to insert the new
721                                                  packet */
722
723         // Create a clone of the current packet
724
725         ConcretePacket clone() const;
726
727         ///@}
728         ///////////////////////////////////////////////////////////////////////////
729
730         // Field access
731
732         struct ParserProxy
733         {
734             ParserProxy(Parser const & p) : p_ (p) {}
735             Parser * operator->() { return &p_; }
736             Parser p_;
737         };
738
739         ParserProxy operator->() const;    ///< Access packet fields
740                                         /**< This operator allows to access the parsed fields of the
741                                              packet using the notation <tt>packet->field()</tt>. The
742                                              fields of the packet are specified by the PacketType's
743                                              \c parser member.
744
745                                              The members are not strictly restricted to simple field
746                                              access. The parser class may have any member which is
747                                              needed for full packet access (e.g. checksum validation
748                                              / recreation ...)
749                                              \see \ref packetparser for the parser interface. */
750
751         Parser parser() const;          ///< Access packet field parser directly
752                                         /**< Access the parser of the packet. This is the same
753                                              object returned by the operator->() operator. The
754                                              operator however does not allow to access this object
755                                              itself, only it's members.
756                                              \see \ref packetparser for the parser interface */
757
758     protected:
759
760     private:
761         typedef PacketInterpreter<PacketType> interpreter;
762
763         ConcretePacket(typename interpreter::ptr const & packet_);
764
765         interpreter * ptr() const;
766
767         friend class Packet;
768         friend class PacketInterpreter<PacketType>;
769     };
770
771     /** \brief Generic parser copying
772
773         This operator allows to copy the value of identical parsers. This operation does \e not
774         depend on the parsers detailed implementation, it will just replace the data bytes of the
775         target parser with those from the source packet.
776      */
777     template <class PacketType, class Parser>
778     Parser operator<<(Parser target, ConcretePacket<PacketType> const & packet);
779
780     ///@}
781
782 }
783
784 ///////////////////////////////hh.e////////////////////////////////////////
785 #endif
786 #if !defined(HH_SENF_Packets_Packets__decls_) && !defined(HH_SENF_Packets_Packet_i_)
787 #define HH_SENF_Packets_Packet_i_
788 #include "Packet.cci"
789 #include "Packet.ct"
790 #include "Packet.cti"
791 #endif
792
793 \f
794 // Local Variables:
795 // mode: c++
796 // fill-column: 100
797 // c-file-style: "senf"
798 // indent-tabs-mode: nil
799 // ispell-local-dictionary: "american"
800 // compile-command: "scons -u test"
801 // comment-column: 40
802 // End:
803