25d0f56a4227934fee280876819e9a9e2f25c329
[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                                         /**< \throws std::bad_cast if the next() packet is not of
201                                              type \a OtherPacket
202                                              \returns in - valid() packet if no next packet
203                                                  exists */
204         template <class OtherPacket> OtherPacket find() const;
205                                         ///< Search chain forward for packet of type \a OtherPacket
206                                         /**< The search will start with the current packet.
207                                              \throws InvalidPacketChainException if no packet of
208                                                  type \a OtherPacket can be found. */
209         template <class OtherPacket> OtherPacket find(NoThrow_t) const;
210                                         ///< Search chain forward for packet of type \a OtherPacket
211                                         /**< The search will start with the current packet.
212                                              \returns in - valid() packet if no packet of type \a
213                                                  OtherPacket can be found. */
214
215         Packet      prev() const;       ///< Get previous packet in chain
216                                         /**< \throws InvalidPacketChainException if no previous
217                                              packet exists */
218         Packet      prev(NoThrow_t) const; ///< Get previous packet in chain
219                                         /**< \returns in - valid() packet if no previous packet
220                                              exists */
221         template <class OtherPacket> OtherPacket prev() const;
222                                         ///< Get previous packet in chain and cast to \a OtherPacket
223                                         /**< \throws std::bad_cast, if the previous packet is not of
224                                              type \a OtherPacket
225                                              \throws InvalidPacketChainException if no previous
226                                                  packet exists */
227         template <class OtherPacket> OtherPacket prev(NoThrow_t) const;
228                                         ///< Get previous packet in chain and cast to \a OtherPacket
229                                         /**< \throws std::bad_cast, if the previous packet is not of
230                                              type \a OtherPacket
231                                              \returns in - valid() packet if no previous packet
232                                                  exists */
233         template <class OtherPacket> OtherPacket rfind() const;
234                                         ///< Search chain backwards for packet of type \a OtherPacket
235                                         /**< The search will start with the current packet.
236                                              \throws InvalidPacketChainException if no packet of
237                                                  type \a OtherPacket can be found. */
238         template <class OtherPacket> OtherPacket rfind(NoThrow_t) const;
239                                         ///< Search chain backwards for packet of type \a OtherPacket
240                                         /**< The search will start with the current packet.
241                                              \returns in - valid() packet if no packet of type \a
242                                                  OtherPacket can be found. */
243
244
245         Packet      first() const;      ///< Return first packet in chain
246         template <class OtherPacket> OtherPacket first() const;
247                                         ///< Return first packet in chain and cast
248                                         /**< \throws std::bad_cast if the first() packet is not of
249                                              type \a OtherPacket */
250
251         Packet      last() const;       ///< Return last packet in chain
252         template <class OtherPacket> OtherPacket last() const;
253                                         ///< Return last packet in chain and cast
254                                         /**< \throws std::bad_cast if the last() packet is not of
255                                              type \a OtherPacket  */
256
257
258         template <class OtherPacket> OtherPacket parseNextAs() const;
259                                         ///< Interpret payload of \c this as \a OtherPacket
260                                         /**< parseNextAs() will throw away the packet chain after
261                                              the current packet if necessary. It will then parse the
262                                              payload section of \c this packet as given by \a
263                                              OtherPacket. The new packet is added to the chain after
264                                              \c this.
265                                              \returns new packet instance sharing the same data and
266                                                  placed after \c this packet in the chain.
267                                              \throws InvalidPacketChainException if no next packet
268                                                  header is allowed (viz. nextPacketRange() of the
269                                                  the current PacketType returns no_range() ) */
270         Packet      parseNextAs(factory_t factory) const;
271                                         ///< Interpret payload of \c this as \a factory type packet
272                                         /**< parseNextAs() will throw away the packet chain after
273                                              the current packet if necessary. It will then parse the
274                                              payload section of \c this packet as given by \a
275                                              factory. The new packet is added to the chain after
276                                              \c this.
277                                              \returns new packet instance sharing the same data and
278                                                  placed after \c this packet in the chain.
279                                              \throws InvalidPacketChainException if no next packet
280                                                  header is allowed (viz. nextPacketRange() of the
281                                                  the current PacketType returns no_range() ) */
282
283         template <class OtherPacket> bool        is() const;
284                                         ///< Check, whether \c this packet is of the given type
285         template <class OtherPacket> OtherPacket as() const;
286                                         ///< Cast current packet to the given type
287                                         /**< This operations returns a handle to the same packet
288                                              header/interpreter however upcast to the given
289                                              ConcretePacket type which have been instantiated
290                                              before.
291                                              \throws std::bad_cast if the current packet is not of
292                                                  type \a OtherPacket */
293
294         Packet append(Packet const & packet) const; ///< Append the given packet to \c this packet
295                                         /**< This operation will replace the payload section of \c
296                                              this packet with \a packet. This operation will replace
297                                              the packet chain after \c this packet with a clone of
298                                              \a packet and will replace the raw data of the payload
299                                              of \c this with the raw data of \a packet. \c this
300                                              packet will not share any data with \a packet.
301                                              \returns Packet handle to the cloned \a packet, placed
302                                                  after \c this in the packet/header/interpreter
303                                                  chain. */
304
305         ///@}
306
307         ///\name Data access
308         ///@{
309
310         PacketData & data() const;      ///< Access the packets raw data container
311         size_type size() const;         ///< Return size of packet in bytes
312                                         /**< This size does \e not include the size of any preceding
313                                              headers/packets/interpreters. It does however include
314                                              \c this packets payload. */
315
316         ///@}
317
318         ///\name Annotations
319         ///@{
320
321         template <class Annotation>
322         Annotation & annotation();      ///< Get packet annotation
323                                         /**< This member will retrieve an arbitrary packet
324                                              annotation. Every annotation is identified by a unique
325                                              \a Annotation type. This type should \e always be a \c
326                                              struct.
327
328                                              \code
329                                              struct MyAnnotation {
330                                                  int value;
331                                              };
332
333                                              senf::Packet p (...);
334
335                                              p.annotation<MyAnnotation>().value = 1;
336                                              \endcode
337
338                                              Annotations are shared by all headers / interpreters
339                                              within a single packet chain.
340
341                                              If an annotation is \e not a POD type (more
342                                              specifically, if it's constructor or destructor is not
343                                              trivial including base classes and members), the \a
344                                              Annotation type \e must inherit from
345                                              senf::ComplexAnnotation. Failing to follow this rule
346                                              will result in undefined behavior and will probably
347                                              lead to a program crash.
348
349                                              \code
350                                              struct MyStringAnnotation : senf::ComplexAnnotation {
351                                                  std::string value;
352                                              };
353                                              \endcode
354                                              (This type is not POD since \c std::string is not POD)
355
356                                              \see \ref packet_usage_annotation
357
358                                              \implementation The annotation system is implemented
359                                                  quite efficiently since annotations are stored
360                                                  within a packet embedded vector of fixed size (the
361                                                  size is determined automatically at runtime by the
362                                                  number of different annotations
363                                                  used). Additionally, non-complex small annotations
364                                                  require no additional memory management (\c new /
365                                                  \c delete).
366
367                                              \idea Pool the annotation vectors: In the destructor
368                                                  swap the vector into a vector graveyard (swapping
369                                                  two vectors is an O(1) no allocation operation). In
370                                                  the constructor, if there is a vector in the
371                                                  graveyard, swap it in from there. Of course, it
372                                                  would be better to do away with the vector and just
373                                                  allocate the space together with the packet but
374                                                  that looks quite complicated to do ... especially
375                                                  considering that the packetimpl itself uses a pool.
376                                           */
377
378         ///@}
379
380         template <class Annotation>
381         Annotation const & annotation() const; ///< Get packet annotation
382                                         /**< \see annotation() */
383
384         ///\name Other methods
385         ///@{
386
387         bool operator==(Packet const & other) const; ///< Check for packet identity
388                                         /**< Two packet handles compare equal if they really are the
389                                              same packet header in the same packet chain. */
390         bool boolean_test() const;      ///< Check, whether the packet is valid()
391                                         /**< \see valid() */
392         bool valid() const;             ///< Check, whether the packet is valid()
393                                         /**< An in - valid() packet does not allow any operation
394                                              except checking for validity and assignment. in -
395                                              valid() packets serve the same role as 0-pointers.
396
397                                              This is an alias for boolean_test() which is called
398                                              when using a packet in a boolean context. */
399
400         void finalizeThis();            ///< Update calculated fields
401                                         /**< The finalize() fammily of members will update
402                                              calculated packet fields: checksums, size fields and so
403                                              on. This includes any field, which can be set from
404                                              other information in the packet. Each concrete packet
405                                              type should document, which fields are set by
406                                              finalize().
407
408                                              finalizeThis() will \e only process the current
409                                              header. Even if only changing fields in this protocol,
410                                              depending on the protocol it may not be enough to
411                                              finalize this header only. See the packet type
412                                              documentation. */
413
414         template <class Other>
415         void finalizeTo();              ///< Update calculated fields
416                                         /**< The finalize() fammily of members will update
417                                              calculated packet fields: checksums, size fields and so
418                                              on. This includes any field, which can be set from
419                                              other information in the packet. Each concrete packet
420                                              type should document, which fields are set by
421                                              finalize().
422
423                                              finalizeTo() will automatically process all
424                                              packets/headers/interpreters from the \e first
425                                              occurrence of packet type \a Other (beginning at \c
426                                              this packet searching forward towards deeper nested
427                                              packets) backwards up to \c this.
428
429                                              This call is equivalent to
430                                              \code
431                                                  p.finalizeTo(p.next<Other>())
432                                              \endcode */
433
434         void finalizeTo(Packet const & other);  ///< Update calculated fields
435                                         /**< The finalize() fammily of members will update
436                                              calculated packet fields: checksums, size fields and so
437                                              on. This includes any field, which can be set from
438                                              other information in the packet. Each concrete packet
439                                              type should document, which fields are set by
440                                              finalize().
441
442                                              finalizeTo(other) will automatically process all
443                                              packets/headers/interpreters beginning at \a other
444                                              backwards towards outer packets up to \c this. */
445
446         void finalizeAll();             ///< Update calculated fields
447                                         /**< The finalize() fammily of members will update
448                                              calculated packet fields: checksums, size fields and so
449                                              on. This includes any field, which can be set from
450                                              other information in the packet. Each concrete packet
451                                              type should document, which fields are set by
452                                              finalize().
453
454                                              finalizeAll() will automatically process all
455                                              packets/headers/interpreters from the end of the chain
456                                              (the most inner packet) backwards up to \c this.
457
458                                              This call is equivalent to
459                                              \code
460                                                  p.finalizeTo(p.last())
461                                              \endcode
462
463                                              Beware, that finalizeAll() will \e not finalize any
464                                              headers before \c this, it will \e only process inner
465                                              headers. */
466
467         void dump(std::ostream & os) const; ///< Write out a printable packet representation
468                                         /**< This method is provided mostly to help debugging packet
469                                              problems. Each concrete packet should implement a dump
470                                              method writing out all fields of the packet in a
471                                              readable representation. dump() will call this member
472                                              for each packet/header/interpreter in the chain from \c
473                                              this packet up to the end of the chain. */
474
475         TypeIdValue typeId() const;     ///< Get type of \c this packet
476                                         /**< This value is used e.g. in the packet registry to
477                                              associate packet types with other information.
478                                              \returns A type holding the same information as a
479                                              type_info object, albeit assignable */
480         factory_t factory() const;      ///< Return factory instance of \c this packet
481                                         /**< The returned factory instance can be used to create new
482                                              packets of the given type without knowing the concrete
483                                              type of the packet. The value may be stored away for
484                                              later use if needed. */
485
486         unsigned long id() const;       ///< Unique packet id
487                                         /**< Get a unique packet id. If two packets have the same
488                                              id, they share the internal data representation.. */
489
490         ///@}
491
492     protected:
493         explicit Packet(PacketInterpreterBase::ptr const & packet);
494
495         PacketInterpreterBase::ptr const & ptr() const;
496
497     private:
498         Packet checkNext() const;
499         Packet checkLast() const;
500
501         PacketInterpreterBase::ptr packet_;
502
503         template <class PacketType>
504         friend class ConcretePacket;
505         friend class PacketParserBase;
506     };
507
508     /** \brief Protocol specific packet handle
509
510         The ConcretePacket template class extends Packet to provide protocol/packet type specific
511         aspects. These are packet constructors and access to the parsed packet fields.
512
513         The \c PacketType template argument to ConcretePacket is a protocol specific and internal
514         policy class which defines the protocol specific behavior. To access a specific type of
515         packet, the library provides corresponding typedefs of ConcretePacket < \a SomePacketType >
516         (e.g. \ref EthernetPacket as typedef for \ref ConcretePacket < \ref EthernetPacketType >).
517
518         The new members provided by ConcretePacket over packet are mostly comprised of the packet
519         constructors. These come in three major flavors:
520
521         \li The create() family of constructors will create completely new packets.
522         \li The createAfter() family of constructors will create new packets (with new data for the
523             packet) \e after a given existing packet <em>thereby destroying and overwriting  any
524             possibly existing packets and data after the given packet</em>.
525         \li The createBefore() family of constructors will create new packets (again with new data)
526             \e before a given existing packet <em>thereby destroying and overwriting any possibly
527             existing packets and data before the given packet</em>.
528         \li The createInsertBefore() family of constructors will create new packets \e before a
529             given packet \e inserting them into the packet chain after any existing packets before
530             the given packet.
531
532         Whereas create() will create a completely new packet with it's own chain and data storage,
533         createAfter(), createBefore() and createInsertBefore() extend a packet with additional
534         headers/interpreters. createAfter() will set the payload of the given packet to the new
535         packet whereas createBefore() and createInsertBefore() will create a new packet with the
536         existing packet as it's payload.
537
538         createAfter() differs from Packet::parseNextAs() in that the former creates a new packet \e
539         replacing any possibly existing data whereas the latter will interpret the already \e
540         existing data as given by the type argument.
541
542         \see \ref PacketTypeBase for a specification of the interface to be provided by the \a
543             PacketType policy class.
544      */
545     template <class PacketType>
546     class ConcretePacket
547         : public Packet
548     {
549     public:
550         ///////////////////////////////////////////////////////////////////////////
551         // Types
552
553         typedef PacketType type;
554         typedef typename PacketType::parser Parser;
555
556         ///////////////////////////////////////////////////////////////////////////
557         ///\name Structors and default members
558         ///@{
559
560         // default copy constructor
561         // default copy assignment
562         // default destructor
563         // no conversion constructors
564
565         ConcretePacket();               ///< Create uninitialized packet handle
566                                         /**< An uninitialized handle is not valid(). It does not
567                                              allow any operation except assignment and checking for
568                                              validity. */
569
570         static factory_t factory();     ///< Return factory for packets of specific type
571                                         /**< This \e static member is like Packet::factory() for a
572                                              specific packet of type \a PacketType */
573
574         // Create completely new packet
575
576         static ConcretePacket create(); ///< Create default initialized packet
577                                         /**< The packet will be initialized to it's default empty
578                                              state. */
579         static ConcretePacket create(senf::NoInit_t); ///< Create uninitialized empty packet
580                                         /**< This will create a completely empty and uninitialized
581                                              packet with <tt>size() == 0</tt>.
582                                              \param[in] senf::noinit This parameter must always have
583                                                  the value \c senf::noinit. */
584         static ConcretePacket create(size_type size); ///< Create default initialized packet
585                                         /**< This member will create a default initialized packet
586                                              with the given size. If the size parameter is smaller
587                                              than the minimum allowed packet size an exception will
588                                              be thrown.
589                                              \param[in] size Size of the packet to create in bytes.
590                                              \throws TruncatedPacketException if \a size is smaller
591                                                  than the smallest permissible size for this type of
592                                                  packet. */
593         static ConcretePacket create(size_type size, senf::NoInit_t);
594                                         ///< Create uninitialized packet
595                                         /**< Creates an uninitialized (all-zero) packet of the exact
596                                              given size.
597                                              \param[in] size Size of the packet to create in bytes
598                                              \param[in] senf::noinit This parameter must always have
599                                                  the value \c senf::noinit. */
600 #ifndef DOXYGEN
601         template <class ForwardReadableRange>
602         static ConcretePacket create(
603             ForwardReadableRange const & range,
604             typename boost::disable_if< boost::is_integral<ForwardReadableRange> >::type * = 0);
605 #else
606         template <class ForwardReadableRange>
607         static ConcretePacket create(ForwardReadableRange const & range);
608                                         ///< Create packet from given data
609                                         /**< The packet will be created from a copy of the given
610                                              data. The data from the range will be copied directly
611                                              into the packet representation. The data will \e not be
612                                              validated in any way.
613
614                                              \param[in] range <a href="http://www.boost.org/libs/range/index.html">Boost.Range</a>
615                                                  of data to construct packet from. */
616 #endif
617
618         // Create packet as new packet after a given packet
619
620         static ConcretePacket createAfter(Packet const & packet);
621                                         ///< Create default initialized packet after \a packet
622                                         /**< The packet will be initialized to it's default empty
623                                              state. It will be appended as next header/interpreter
624                                              after \a packet in that packets interpreter chain.
625                                              \param[in] packet Packet to append new packet to. */
626         static ConcretePacket createAfter(Packet const & packet, senf::NoInit_t);
627                                         ///< Create uninitialized empty packet after\a packet
628                                         /**< This will create a completely empty and uninitialized
629                                              packet with <tt>size() == 0</tt>. It will be appended
630                                              as next header/interpreter after \a packet in that
631                                              packets interpreter chain.
632                                              \param[in] packet Packet to append new packet to.
633                                              \param[in] senf::noinit This parameter must always have
634                                                  the value \c senf::noinit. */
635         static ConcretePacket createAfter(Packet const & packet, size_type size);
636                                         ///< Create default initialized packet after \a packet
637                                         /**< This member will create a default initialized packet
638                                              with the given size. If the size parameter is smaller
639                                              than the minimum allowed packet size an exception will
640                                              be thrown. It will be appended as next
641                                              header/interpreter after \a packet in that packets
642                                              interpreter chain.
643                                              \param[in] packet Packet to append new packet to.
644                                              \param[in] size Size of the packet to create in bytes.
645                                              \throws TruncatedPacketException if \a size is smaller
646                                                  than the smallest permissible size for this type of
647                                                  packet. */
648         static ConcretePacket createAfter(Packet const & packet, size_type size, senf::NoInit_t);
649                                         ///< Create uninitialized packet after \a packet
650                                         /**< Creates an uninitialized (all-zero) packet of the exact
651                                              given size.  It will be appended as next
652                                              header/interpreter after \a packet in that packets
653                                              interpreter chain.
654                                              \param[in] packet Packet to append new packet to.
655                                              \param[in] size Size of the packet to create in bytes
656                                              \param[in] senf::noinit This parameter must always have
657                                                  the value \c senf::noinit. */
658 #ifndef DOXYGEN
659         template <class ForwardReadableRange>
660         static ConcretePacket createAfter(
661             Packet const & packet,
662             ForwardReadableRange const & range,
663             typename boost::disable_if< boost::is_integral<ForwardReadableRange> >::type * = 0);
664 #else
665         template <class ForwardReadableRange>
666         static ConcretePacket createAfter(Packet const & packet,
667                                           ForwardReadableRange const & range);
668                                         ///< Create packet from given data after \a packet
669                                         /**< The packet will be created from a copy of the given
670                                              data. The data from the range will be copied directly
671                                              into the packet representation. The data will \e not be
672                                              validated in any way.  It will be appended as next
673                                              header/interpreter after \a packet in that packets
674                                              interpreter chain.
675                                              \param[in] packet Packet to append new packet to.
676                                              \param[in] range <a href="http://www.boost.org/libs/range/index.html">Boost.Range</a>
677                                                  of data to construct packet from. */
678 #endif
679
680         // Create packet as new packet (header) before a given packet
681
682         static ConcretePacket createBefore(Packet const & packet);
683                                         ///< Create default initialized packet before \a packet
684                                         /**< The packet will be initialized to it's default empty
685                                              state. It will be prepended as previous
686                                              header/interpreter before \a packet in that packets
687                                              interpreter chain.
688                                              \warning This constructor will destroy any existing
689                                                  headers before \a packet and replace them with the
690                                                  new header.
691                                              \param[in] packet Packet to prepend new packet to. */
692         static ConcretePacket createBefore(Packet const & packet, senf::NoInit_t);
693                                         ///< Create uninitialized empty packet before \a packet
694                                         /**< Creates a completely empty and uninitialized packet. It
695                                              will be prepended as previous header/interpreter before
696                                              \a packet in that packets interpreter chain.
697                                              \warning This constructor will destroy any existing
698                                                  headers before \a packet and replace them with the
699                                                  new header.
700                                              \param[in] packet Packet to prepend new packet to. */
701
702         static ConcretePacket createInsertBefore(Packet const & packet);
703                                         ///< Insert default initialized packet before \a packet
704                                         /**< The new packet header will be initialized to it' s
705                                              default empty state. It will be inserted into the
706                                              packet chain before \a packet.
707                                              \param[in] packet Packet before which to insert the new
708                                                  packet */
709         static ConcretePacket createInsertBefore(Packet const & packet, senf::NoInit_t);
710                                         ///< Insert uninitialized empty packet before \a packet
711                                         /**< Inserts a completely empty and unitialized packet
712                                              before \a packet into the header/interpreter chain.
713                                              \param[in] packet Packet before which to insert the new
714                                                  packet */
715
716         // Create a clone of the current packet
717
718         ConcretePacket clone() const;
719
720         ///@}
721         ///////////////////////////////////////////////////////////////////////////
722
723         // Field access
724
725         struct ParserProxy
726         {
727             ParserProxy(Parser const & p) : p_ (p) {}
728             Parser * operator->() { return &p_; }
729             Parser p_;
730         };
731
732         ParserProxy operator->() const;    ///< Access packet fields
733                                         /**< This operator allows to access the parsed fields of the
734                                              packet using the notation <tt>packet->field()</tt>. The
735                                              fields of the packet are specified by the PacketType's
736                                              \c parser member.
737
738                                              The members are not strictly restricted to simple field
739                                              access. The parser class may have any member which is
740                                              needed for full packet access (e.g. checksum validation
741                                              / recreation ...)
742                                              \see \ref packetparser for the parser interface. */
743
744         Parser parser() const;          ///< Access packet field parser directly
745                                         /**< Access the parser of the packet. This is the same
746                                              object returned by the operator->() operator. The
747                                              operator however does not allow to access this object
748                                              itself, only it's members.
749                                              \see \ref packetparser for the parser interface */
750
751     protected:
752
753     private:
754         typedef PacketInterpreter<PacketType> interpreter;
755
756         ConcretePacket(typename interpreter::ptr const & packet_);
757
758         typename interpreter::ptr ptr() const;
759
760         friend class Packet;
761         friend class PacketInterpreter<PacketType>;
762     };
763
764     /** \brief Generic parser copying
765
766         This operator allows to copy the value of identical parsers. This operation does \e not
767         depend on the parsers detailed implementation, it will just replace the data bytes of the
768         target parser with those from the source packet.
769      */
770     template <class PacketType, class Parser>
771     Parser operator<<(Parser target, ConcretePacket<PacketType> const & packet);
772
773     ///@}
774
775 }
776
777 ///////////////////////////////hh.e////////////////////////////////////////
778 #endif
779 #if !defined(HH_SENF_Packets_Packets__decls_) && !defined(HH_SENF_Packets_Packet_i_)
780 #define HH_SENF_Packets_Packet_i_
781 #include "Packet.cci"
782 #include "Packet.ct"
783 #include "Packet.cti"
784 #endif
785
786 \f
787 // Local Variables:
788 // mode: c++
789 // fill-column: 100
790 // c-file-style: "senf"
791 // indent-tabs-mode: nil
792 // ispell-local-dictionary: "american"
793 // compile-command: "scons -u test"
794 // comment-column: 40
795 // End:
796