Packets: Add packet diagrams
[senf.git] / Packets / VectorParser.hh
index b98e406..db9e0de 100644 (file)
@@ -1,9 +1,9 @@
 // $Id$
 //
 // Copyright (C) 2006
-// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
-// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
-//     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
+//     Stefan Bund <g0dil@berlios.de>
 //
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
@@ -23,8 +23,8 @@
 /** \file
     \brief VectorParser public header */
 
-#ifndef HH_VectorParser_
-#define HH_VectorParser_ 1
+#ifndef HH_SENF_Packets_VectorParser_
+#define HH_SENF_Packets_VectorParser_ 1
 
 // Custom includes
 #include <boost/iterator/iterator_facade.hpp>
 #include <boost/type_traits.hpp>
 #include "PacketParser.hh"
 #include "ArrayParser.hh" // for ArrayParser_iterator
+#include "AuxParser.hh" // for the AuxPolicies
 
 //#include "VectorParser.mpp"
-#include "VectorParser.ih"
 ///////////////////////////////hh.p////////////////////////////////////////
 
 namespace senf {
 
-    template <class ElementParser, class Sizer> class VectorParser_Container;
+    template <class ElementParser, class AuxPolicy> class VectorParser_Container;
 
     /** \brief Collection of fixed-size elements
 
@@ -51,21 +51,28 @@ namespace senf {
         A vector is a model of an STL random-access sequence. The parser only provides a reduced
         interface, the container wrapper however completes this interface.
 
-        VectorParser makes use of a policy template argument, \a Sizer, to customize the way the
-        containers size is obtained. You will normally not instantiate Parser_Vector directly, you
-        will use one of the 'template typedefs' (which are templated structures since C++ does not
-        provide real template typedefs) provided with the policy implementations.
+        VectorParser makes use of a policy template argument, \a AuxPolicy, to customize the way the
+        containers size is obtained. You will normally not instantiate VectorParser directly, you
+        will use the \ref SENF_PARSER_VECTOR() helper macro.
+
+        Some basic vector access methods are defined as parser members. To access the complete list
+        API however you will need to instantiate a container wrapper for the vector. See \ref
+        packet_usage_fields_collection.
         
-        \todo Make the sizer a private base-class to profit from the empty-base-class optimization
+        \see
+            \ref How to access \ref packet_usage_fields_collection \n
+            SENF_PARSER_VECTOR() macro used to define vector fields \n
+            VectorParser_Container vector container wrapper API
 
-        \see ExampleVectorPolicy
         \ingroup parsecollection
      */
-    template <class ElementParser, class Sizer>
-    struct VectorParser : public PacketParserBase
+    template <class ElementParser, class AuxPolicy>
+    struct VectorParser 
+        : public PacketParserBase, 
+          private AuxPolicy
     {
         VectorParser(data_iterator i, state_type s);
-        VectorParser(Sizer sizer, data_iterator i, state_type s);
+        VectorParser(AuxPolicy policy, data_iterator i, state_type s);
                                         ///< Additional sizer specific constructor
                                         /**< This constructor may be used, if the sizer needs
                                              additional parameters. */
@@ -73,7 +80,7 @@ namespace senf {
         size_type bytes() const;
         void init() const;
 
-        static const size_type init_bytes = Sizer::init_bytes;
+        static const size_type init_bytes = AuxPolicy::aux_bytes;
 
         ///////////////////////////////////////////////////////////////////////////
         // Container interface
@@ -81,7 +88,7 @@ namespace senf {
         typedef ElementParser value_type;
         typedef detail::ArrayParser_iterator<value_type> iterator;
         typedef iterator const_iterator;
-        typedef VectorParser_Container<ElementParser,Sizer> container;
+        typedef VectorParser_Container<ElementParser,AuxPolicy> container;
 
         size_type size() const;
         bool empty() const;
@@ -107,65 +114,10 @@ namespace senf {
         template <class Value> void resize           (size_type n, Value value) const;
 
      private:
-        Sizer sizer_;
 
-        friend class VectorParser_Container<ElementParser,Sizer>;
+        friend class VectorParser_Container<ElementParser,AuxPolicy>;
     };
 
-    /** \brief Vector with prefix sizing
-        
-        This is a 'template typedef'. It defines a vector with a <em>directly preceding</em> size
-        field holding the number of vector elements. The size field is considered part of the
-        vector.
-        \code
-        // Define MyVector as a vector of 16bit unsigned elements with a directly preceding
-        // 8bit unsigned size field
-        typedef senf::VectorNParser<senf::UInt16Parser, senf::UInt8Parser>::parser MyVector;
-        \endcode
-
-        \param ElementParser \e fixed-size parser for parsing the vector elements
-        \param SizeParser parser for parsing the vector size (number of elements)
-
-        \see VectorParser
-        \ingroup parsecollection
-     */
-    template <class ElementParser, class SizeParser, unsigned Distance>
-    struct VectorNParser
-    {
-        typedef VectorParser< ElementParser,
-                              detail::VectorNParser_Sizer<SizeParser, Distance> > parser;
-    };
-
-    /** \brief Define VectorNParser field
-        
-        This macro is a special helper to define a senf::VectorNParser type field, a vector of
-        elements of type \a elt_type (a parser) which size is given by the \a size field.
-
-        \code
-        // The size field should be declared private (size is accessible via the vector)
-        SENF_PARSER_PRIVATE_FIELD ( vec_size_, senf::UInt16Parser );
-        // Define the vector, here it has 32bit unsigned integer elements
-        SENF_PARSER_VEC_N         ( vec,       _vec_size, senf::UInt32Parser );
-        \endcode
-        
-        \param[in] name field name
-        \param[in] size name of field giving the vector size
-        \param[in] elt_type vector element type
-        \hideinitializer
-        \ingroup packetparsermacros
-     */
-#   define SENF_PARSER_VEC_N(name, size, elt_type)                                                \
-        SENF_PARSER_VEC_N_I(SENF_PARSER_FIELD, name, size, elt_type)
-
-    /** \brief Define VectorNParser field
-
-        \see \ref SENF_PARSER_VEC_N()
-        \hideinitializer
-        \ingroup packetparsermacros
-     */
-#   define SENF_PARSER_PRIVATE_VEC_N(name, size, elt_type)                                        \
-        SENF_PARSER_VEC_N_I(SENF_PARSER_PRIVATE_FIELD, name, size, elt_type)
-
     /** \brief VectorParser container wrapper
 
         This is the container wrapper used for vector parsers. The container wrapper will stay valid
@@ -184,14 +136,15 @@ namespace senf {
 
         \see VectorParser
       */
-    template <class ElementParser, class Sizer>
+    template <class ElementParser, class AuxPolicy>
     class VectorParser_Container
+        : private AuxPolicy::WrapperPolicy
     {
     public:
         ///////////////////////////////////////////////////////////////////////////
         // Types
 
-        typedef VectorParser<ElementParser,Sizer> parser_type;
+        typedef VectorParser<ElementParser,AuxPolicy> parser_type;
         typedef PacketParserBase::data_iterator data_iterator;
         typedef PacketParserBase::size_type size_type;
         typedef PacketParserBase::difference_type difference_type;
@@ -276,17 +229,88 @@ namespace senf {
     private:
         void setSize(size_type value);
 
-        Sizer sizer_;
         state_type state_;
         size_type i_;
     };
 
+    /** \brief Define VectorParser field
+        
+        This macro is a special helper to define a senf::VectorParser type field, a vector of
+        elements of type \a elt_type (a parser) which size is given by the \a size field.
+
+        \code
+        // The size field should be declared private (size is accessible via the vector)
+        SENF_PARSER_PRIVATE_FIELD ( vec_size_, senf::UInt16Parser );
+        // Define the vector, here it has 32bit unsigned integer elements
+        SENF_PARSER_VECTOR        ( vec,       vec_size_, senf::UInt32Parser );
+        \endcode
+
+        \warning Realize, that the \a size field is controlled by the vector parser. This field
+            should therefore be declared either read-only or private and must be changed only via
+            the vector parser.
+
+        Further additional tags are supported which modify the way, the \a size field is
+        interpreted:
+
+        <table class="senf fixedcolumn">
+        <tr><td>\c bytes(\a size)</td><td>\a size gives the size of the vector in bytes not the
+        number of contained elements</td></tr>
+        
+        <tr><td>\c packetSize()</td><td>Use the size of the packet to get the vector size. The
+        vector will occupy all space up to the end of the packet.</td></tr>
+
+        <tr><td>\c transform(\a transform, \a size)</td><td>The \a transform is applied to the \a
+        size value, the value is not used directly</td>
+        </table>
+
+        The optional \a transform is a class with the following layout
+
+        \code
+        struct MyTransform
+        {
+            typedef ... value_type;
+            static value_type get(other_type v);
+            static other_type set(value_type v);
+        };
+        \endcode \c other_type is the \a size ::\c value_type where as the \c value_type typedef is
+        the arbitrary return type of the transform.
+
+        The tags are applied to the \a size parameter:
+        \code
+        SENF_PARSER_VECTOR ( vec, transform(MyTransform, vec_size_), senf::UInt32Parser );
+        SENF_PARSER_VECTOR ( vec, packetSize(), senf::UInt32Parser );
+        \endcode
+        
+        \param[in] name field name
+        \param[in] size name of field giving the vector size
+        \param[in] elt_type vector element type
+
+        \see 
+            How to use \ref packet_usage_fields_collection \n
+            senf::VectorParser the vector parser API for vector field access
+            senf::VectorParser_Container the vector parser container API for vector field access
+
+        \hideinitializer
+        \ingroup packetparsermacros
+     */
+#   define SENF_PARSER_VECTOR(name, size, elt_type) \
+        SENF_PARSER_VECTOR_I(public, name, size, elt_type)
+
+    /** \brief Define private VectorParser field
+
+        \see \ref SENF_PARSER_VECTOR()
+
+        \hideinitializer
+        \ingroup packetparsermacros
+     */
+#   define SENF_PARSER_PRIVATE_VECTOR(name, size, elt_type) \
+        SENF_PARSER_VECTOR_I(private, name, size, elt_type)
 }
 
 ///////////////////////////////hh.e////////////////////////////////////////
 #endif
-#if !defined(HH_Packets__decls_) && !defined(HH_VectorParser_i_)
-#define HH_VectorParser_i_
+#if !defined(HH_SENF_Packets_Packets__decls_) && !defined(HH_SENF_Packets_VectorParser_i_)
+#define HH_SENF_Packets_VectorParser_i_
 //#include "VectorParser.cci"
 #include "VectorParser.ct"
 #include "VectorParser.cti"