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