6693d0d5ff6c0a932133c6b44780488bb9a174c8
[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         bool is_shared() const;         ///< check if this packet shares data with any another packet handle.
498                                         /**< This method returns true if there is any other packet
499                                              handle pointing to any header in the packet chain. */
500
501         ///@}
502
503     protected:
504         explicit Packet(PacketInterpreterBase::ptr const & packet);
505
506         PacketInterpreterBase::ptr const & ptr() const;
507
508     private:
509         Packet getNext() const;
510         Packet getLast() const;
511
512         PacketInterpreterBase::ptr packet_;
513
514         template <class PacketType>
515         friend class ConcretePacket;
516         friend class PacketParserBase;
517     };
518
519     /** \brief Protocol specific packet handle
520
521         The ConcretePacket template class extends Packet to provide protocol/packet type specific
522         aspects. These are packet constructors and access to the parsed packet fields.
523
524         The \c PacketType template argument to ConcretePacket is a protocol specific and internal
525         policy class which defines the protocol specific behavior. To access a specific type of
526         packet, the library provides corresponding typedefs of ConcretePacket < \a SomePacketType >
527         (e.g. \ref EthernetPacket as typedef for \ref ConcretePacket < \ref EthernetPacketType >).
528
529         The new members provided by ConcretePacket over packet are mostly comprised of the packet
530         constructors. These come in three major flavors:
531
532         \li The create() family of constructors will create completely new packets.
533         \li The createAfter() family of constructors will create new packets (with new data for the
534             packet) \e after a given existing packet <em>thereby destroying and overwriting  any
535             possibly existing packets and data after the given packet</em>.
536         \li The createBefore() family of constructors will create new packets (again with new data)
537             \e before a given existing packet <em>thereby destroying and overwriting any possibly
538             existing packets and data before the given packet</em>.
539         \li The createInsertBefore() family of constructors will create new packets \e before a
540             given packet \e inserting them into the packet chain after any existing packets before
541             the given packet.
542
543         Whereas create() will create a completely new packet with it's own chain and data storage,
544         createAfter(), createBefore() and createInsertBefore() extend a packet with additional
545         headers/interpreters. createAfter() will set the payload of the given packet to the new
546         packet whereas createBefore() and createInsertBefore() will create a new packet with the
547         existing packet as it's payload.
548
549         createAfter() differs from Packet::parseNextAs() in that the former creates a new packet \e
550         replacing any possibly existing data whereas the latter will interpret the already \e
551         existing data as given by the type argument.
552
553         \see \ref PacketTypeBase for a specification of the interface to be provided by the \a
554             PacketType policy class.
555      */
556     template <class PacketType>
557     class ConcretePacket
558         : public Packet
559     {
560     public:
561         ///////////////////////////////////////////////////////////////////////////
562         // Types
563
564         typedef PacketType type;
565         typedef typename PacketType::parser Parser;
566
567         ///////////////////////////////////////////////////////////////////////////
568         ///\name Structors and default members
569         ///@{
570
571         // default copy constructor
572         // default copy assignment
573         // default destructor
574         // no conversion constructors
575
576         ConcretePacket();               ///< Create uninitialized packet handle
577                                         /**< An uninitialized handle is not valid(). It does not
578                                              allow any operation except assignment and checking for
579                                              validity. */
580
581         static factory_t factory();     ///< Return factory for packets of specific type
582                                         /**< This \e static member is like Packet::factory() for a
583                                              specific packet of type \a PacketType */
584
585         // Create completely new packet
586
587         static ConcretePacket create(); ///< Create default initialized packet
588                                         /**< The packet will be initialized to it's default empty
589                                              state. */
590         static ConcretePacket create(senf::NoInit_t); ///< Create uninitialized empty packet
591                                         /**< This will create a completely empty and uninitialized
592                                              packet with <tt>size() == 0</tt>.
593                                              \param[in] senf::noinit This parameter must always have
594                                                  the value \c senf::noinit. */
595         static ConcretePacket create(size_type size); ///< Create default initialized packet
596                                         /**< This member will create a default initialized packet
597                                              with the given size. If the size parameter is smaller
598                                              than the minimum allowed packet size an exception will
599                                              be thrown.
600                                              \param[in] size Size of the packet to create in bytes.
601                                              \throws TruncatedPacketException if \a size is smaller
602                                                  than the smallest permissible size for this type of
603                                                  packet. */
604         static ConcretePacket create(size_type size, senf::NoInit_t);
605                                         ///< Create uninitialized packet
606                                         /**< Creates an uninitialized (all-zero) packet of the exact
607                                              given size.
608                                              \param[in] size Size of the packet to create in bytes
609                                              \param[in] senf::noinit This parameter must always have
610                                                  the value \c senf::noinit. */
611 #ifndef DOXYGEN
612         template <class ForwardReadableRange>
613         static ConcretePacket create(
614             ForwardReadableRange const & range,
615             typename boost::disable_if< boost::is_integral<ForwardReadableRange> >::type * = 0);
616 #else
617         template <class ForwardReadableRange>
618         static ConcretePacket create(ForwardReadableRange const & range);
619                                         ///< Create packet from given data
620                                         /**< The packet will be created from a copy of the given
621                                              data. The data from the range will be copied directly
622                                              into the packet representation. The data will \e not be
623                                              validated in any way.
624
625                                              \param[in] range <a href="http://www.boost.org/doc/libs/release/libs/range/index.html">Boost.Range</a>
626                                                  of data to construct packet from. */
627 #endif
628
629         // Create packet as new packet after a given packet
630
631         static ConcretePacket createAfter(Packet const & packet);
632                                         ///< Create default initialized packet after \a packet
633                                         /**< The packet will be initialized to it's default empty
634                                              state. It will be appended as next header/interpreter
635                                              after \a packet in that packets interpreter chain.
636                                              \param[in] packet Packet to append new packet to. */
637         static ConcretePacket createAfter(Packet const & packet, senf::NoInit_t);
638                                         ///< Create uninitialized empty packet after\a packet
639                                         /**< This will create a completely empty and uninitialized
640                                              packet with <tt>size() == 0</tt>. It will be appended
641                                              as next header/interpreter after \a packet in that
642                                              packets interpreter chain.
643                                              \param[in] packet Packet to append new packet to.
644                                              \param[in] senf::noinit This parameter must always have
645                                                  the value \c senf::noinit. */
646         static ConcretePacket createAfter(Packet const & packet, size_type size);
647                                         ///< Create default initialized packet after \a packet
648                                         /**< This member will create a default initialized packet
649                                              with the given size. If the size parameter is smaller
650                                              than the minimum allowed packet size an exception will
651                                              be thrown. 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                                              \throws TruncatedPacketException if \a size is smaller
657                                                  than the smallest permissible size for this type of
658                                                  packet. */
659         static ConcretePacket createAfter(Packet const & packet, size_type size, senf::NoInit_t);
660                                         ///< Create uninitialized packet after \a packet
661                                         /**< Creates an uninitialized (all-zero) packet of the exact
662                                              given size.  It will be appended as next
663                                              header/interpreter after \a packet in that packets
664                                              interpreter chain.
665                                              \param[in] packet Packet to append new packet to.
666                                              \param[in] size Size of the packet to create in bytes
667                                              \param[in] senf::noinit This parameter must always have
668                                                  the value \c senf::noinit. */
669 #ifndef DOXYGEN
670         template <class ForwardReadableRange>
671         static ConcretePacket createAfter(
672             Packet const & packet,
673             ForwardReadableRange const & range,
674             typename boost::disable_if< boost::is_integral<ForwardReadableRange> >::type * = 0);
675 #else
676         template <class ForwardReadableRange>
677         static ConcretePacket createAfter(Packet const & packet,
678                                           ForwardReadableRange const & range);
679                                         ///< Create packet from given data after \a packet
680                                         /**< The packet will be created from a copy of the given
681                                              data. The data from the range will be copied directly
682                                              into the packet representation. The data will \e not be
683                                              validated in any way.  It will be appended as next
684                                              header/interpreter after \a packet in that packets
685                                              interpreter chain.
686                                              \param[in] packet Packet to append new packet to.
687                                              \param[in] range <a href="http://www.boost.org/doc/libs/release/libs/range/index.html">Boost.Range</a>
688                                                  of data to construct packet from. */
689 #endif
690
691         // Create packet as new packet (header) before a given packet
692
693         static ConcretePacket createBefore(Packet const & packet);
694                                         ///< Create default initialized packet before \a packet
695                                         /**< The packet will be initialized to it's default empty
696                                              state. It will be prepended as previous
697                                              header/interpreter before \a packet in that packets
698                                              interpreter chain.
699                                              \warning This constructor will destroy any existing
700                                                  headers before \a packet and replace them with the
701                                                  new header.
702                                              \param[in] packet Packet to prepend new packet to. */
703         static ConcretePacket createBefore(Packet const & packet, senf::NoInit_t);
704                                         ///< Create uninitialized empty packet before \a packet
705                                         /**< Creates a completely empty and uninitialized packet. It
706                                              will be prepended as previous header/interpreter before
707                                              \a packet in that packets interpreter chain.
708                                              \warning This constructor will destroy any existing
709                                                  headers before \a packet and replace them with the
710                                                  new header.
711                                              \param[in] packet Packet to prepend new packet to. */
712
713         static ConcretePacket createInsertBefore(Packet const & packet);
714                                         ///< Insert default initialized packet before \a packet
715                                         /**< The new packet header will be initialized to it' s
716                                              default empty state. It will be inserted into the
717                                              packet chain before \a packet.
718                                              \param[in] packet Packet before which to insert the new
719                                                  packet */
720         static ConcretePacket createInsertBefore(Packet const & packet, senf::NoInit_t);
721                                         ///< Insert uninitialized empty packet before \a packet
722                                         /**< Inserts a completely empty and unitialized packet
723                                              before \a packet into the header/interpreter chain.
724                                              \param[in] packet Packet before which to insert the new
725                                                  packet */
726
727         // Create a clone of the current packet
728
729         ConcretePacket clone() const;
730
731         ///@}
732         ///////////////////////////////////////////////////////////////////////////
733
734         // Field access
735
736         struct ParserProxy
737         {
738             ParserProxy(Parser const & p) : p_ (p) {}
739             Parser * operator->() { return &p_; }
740             Parser p_;
741         };
742
743         ParserProxy operator->() const;    ///< Access packet fields
744                                         /**< This operator allows to access the parsed fields of the
745                                              packet using the notation <tt>packet->field()</tt>. The
746                                              fields of the packet are specified by the PacketType's
747                                              \c parser member.
748
749                                              The members are not strictly restricted to simple field
750                                              access. The parser class may have any member which is
751                                              needed for full packet access (e.g. checksum validation
752                                              / recreation ...)
753                                              \see \ref packetparser for the parser interface. */
754
755         Parser parser() const;          ///< Access packet field parser directly
756                                         /**< Access the parser of the packet. This is the same
757                                              object returned by the operator->() operator. The
758                                              operator however does not allow to access this object
759                                              itself, only it's members.
760                                              \see \ref packetparser for the parser interface */
761
762     protected:
763
764     private:
765         typedef PacketInterpreter<PacketType> interpreter;
766
767         ConcretePacket(typename interpreter::ptr const & packet_);
768
769         interpreter * ptr() const;
770
771         friend class Packet;
772         friend class PacketInterpreter<PacketType>;
773     };
774
775     /** \brief Generic parser copying
776
777         This operator allows to copy the value of identical parsers. This operation does \e not
778         depend on the parsers detailed implementation, it will just replace the data bytes of the
779         target parser with those from the source packet.
780      */
781     template <class PacketType, class Parser>
782     Parser operator<<(Parser target, ConcretePacket<PacketType> const & packet);
783
784     ///@}
785
786 }
787
788 ///////////////////////////////hh.e////////////////////////////////////////
789 #endif
790 #if !defined(HH_SENF_Packets_Packets__decls_) && !defined(HH_SENF_Packets_Packet_i_)
791 #define HH_SENF_Packets_Packet_i_
792 #include "Packet.cci"
793 #include "Packet.ct"
794 #include "Packet.cti"
795 #endif
796
797 \f
798 // Local Variables:
799 // mode: c++
800 // fill-column: 100
801 // c-file-style: "senf"
802 // indent-tabs-mode: nil
803 // ispell-local-dictionary: "american"
804 // compile-command: "scons -u test"
805 // comment-column: 40
806 // End:
807