Packets: Add PacketParserBase::i(size_type) utility
[senf.git] / Packets / MPEGDVBBundle / SNDUPacket.hh
index 4ab5824..34e83b8 100644 (file)
@@ -1,9 +1,9 @@
 // $Id$
 //
 // Copyright (C) 2007
-// 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 
+//     Thorsten Horstmann <tho@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
 // Custom includes
 #include <algorithm>
 #include <boost/crc.hpp>
-#include "Packets/PacketType.hh"
-#include "Packets/ParseInt.hh"
-#include "Packets/PacketRegistry.hh"
-#include "Packets/PacketParser.hh"
-#include "Packets/DefaultBundle/EthernetPacket.hh"
+#include "../../Packets/Packets.hh"
+#include "../DefaultBundle/EthernetPacket.hh"
 
 //#include "SNDUPacket.mpp"
 ///////////////////////////////hh.p////////////////////////////////////////
@@ -46,71 +43,78 @@ namespace senf {
         
         \see SNDUPacketType
      */
-    struct Parse_SNDUPacket : public PacketParserBase
+    struct SNDUPacketParser : public PacketParserBase
     {
-#       ifndef DOXYGEN
-        
-        SENF_PACKET_PARSER_NO_INIT(Parse_SNDUPacket);
+#       include SENF_PARSER()
 
-#       endif
-        
-        typedef Parse_Flag      <     0 > Parse_daaf;  // Destination Address Absent Field
-        typedef Parse_UIntField < 1, 16 > Parse_length;
-                
-        Parse_daaf d_bit() const {
-            return parse<Parse_daaf>( 0 );
-        }
-        Parse_length length() const {
-            return parse<Parse_length>( 0 );
-        }
-        Parse_UInt16 type() const {
-            return parse<Parse_UInt16>( 2 );
-        }
-        Parse_MAC destination() const {
-            BOOST_ASSERT( ! d_bit() );
-            return parse<Parse_MAC>( 4 );
-        }
-        Parse_UInt32 crc() const { 
-            return parse<Parse_UInt32>( data().size()-4 ); 
-        }
+        /* We first define the fields as they appear in the field. Some of the fields are declared
+           private. We provide custom accessors for those fields further down. */
+
+        SENF_PARSER_PRIVATE_BITFIELD ( d_bit_       ,  1 , unsigned );
+        SENF_PARSER_BITFIELD         ( length       , 15 , unsigned );
+        SENF_PARSER_FIELD            ( type         , UInt16Parser  );
+        SENF_PARSER_PRIVATE_VARIANT  ( destination_ , d_bit_        , 
+                                                      (MACAddressParser) (VoidPacketParser) );
         
-        void init() const {
-            defaultInit();
-            d_bit() = false;
-        }
+        SENF_PARSER_FINALIZE( SNDUPacketParser );
         
-        PacketParserBase::size_type bytes() const;
+        MACAddressParser destination()         /// Only defined if d_bit() == \c false
+            { return destination_().get<0>(); }
+
+        bool d_bit()                    /// Destination absent bit
+            { return d_bit_(); }
+
+        void withDestination()          /// Clear destination absent bit
+            { destination_().init<0>(); }
         
-        static const size_type init_bytes = 2+2+4; // D-Bit + 15 bits length + 16 bits type field + 32 bits crc
+        void withoutDestination()       /// Set destination absent bit
+            { destination_().init<1>(); }
+
+        UInt32Parser crc() 
+            { return parse<UInt32Parser>( data().size() - 4 ); }
 
         boost::uint32_t calcCrc() const;
     };
 
+    
+    struct ULEExtHeaderTypes {
+        typedef boost::uint16_t key_t;
+    };
+    
     /** \brief ULE SNDU Packet
         
         \par Packet type (typedef):
             \ref SNDUPacket
 
         \par Fields:
-            \ref Parse_SNDUPacket
+            \ref SNDUPacketParser
 
         \ingroup protocolbundle_mpegdvb
      */
     struct SNDUPacketType
-        : public PacketTypeBase,
-          public PacketTypeMixin<SNDUPacketType>
+        : public PacketTypeBase
+//          public PacketTypeMixin<SNDUPacketType, ULEExtHeaderTypes>
     {
-        typedef PacketTypeMixin<SNDUPacketType> mixin;
+//        typedef PacketTypeMixin<SNDUPacketType, ULEExtHeaderType> mixin;
         typedef ConcretePacket<SNDUPacketType> packet;
-        typedef Parse_SNDUPacket parser;
+        typedef SNDUPacketParser parser;
 
-        using mixin::nextPacketRange;
-//          using mixin::nextPacketType;
-        using mixin::init;
+//        using mixin::nextPacketRange;
+//        using mixin::nextPacketType;
+//        using mixin::init;
+        
+//        static key_t nextPacketKey(packet p);
+        
+        static void init(packet p);
+
+        static factory_t nextPacketType(packet p);
+        
+        static optional_range nextPacketRange(packet p);
         
         static void dump(packet p, std::ostream & os);
         
         static PacketParserBase::size_type initSize();
+        
         static PacketParserBase::size_type initHeadSize();
     };