Packets: Allow collections in derived parsers to reference aux fields in the base...
g0dil [Fri, 9 Jan 2009 12:54:11 +0000 (12:54 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1051 270642c3-0616-0410-b53a-bc976706d245

Packets/ParseHelpers.hh
Packets/ParseHelpers.ih
Packets/VectorParser.test.cc

index ef5ec45..9eff394 100644 (file)
 
         SENF_PARSER_INHERIT(BaseParser)
     \endcode
+    
+    If you want to define collection fields which reference auxiliary fields in the base parser,
+    <em>you must define the base parser as a variable parser not a fixed parser</em>.
 
     \param[in] base name of base class
     \hideinitializer
index 6392b69..f279f35 100644 (file)
 # // SENF_PARSER_I_FIELD_OFS_*
 #
 # define SENF_PARSER_I_FIELD_OFS_var(name, type, access)                                          \
+    protected:                                                                                    \
         size_type BOOST_PP_CAT(name,_offset)() const {                                            \
             return field_offset_(static_cast<senf::mpl::rv<BOOST_PP_CAT(name,_index)-1>*>(0));    \
         }                                                                                         \
         static size_type const BOOST_PP_CAT(name, _init_bytes) =                                  \
-            SENF_MPL_SLOT_GET(init_bytes);
+            SENF_MPL_SLOT_GET(init_bytes);                                                        \
+    private:
 #
 # define SENF_PARSER_I_FIELD_OFS_fix(name, type, access)                                          \
+    protected:                                                                                    \
         static size_type const BOOST_PP_CAT(name, _offset) =                                      \
-            SENF_MPL_SLOT_GET(offset);
+            SENF_MPL_SLOT_GET(offset);                                                            \
+    private:
 #
 # ////////////////////////////////////////
 # // SENF_PARSER_I_ADVANCE_OFS_*
             return BOOST_PP_CAT(name, _next_offset)();                                            \
         }                                                                                         \
         SENF_MPL_SLOT_SET(init_bytes, BOOST_PP_CAT(name,_next_init_bytes));                       \
+    protected:                                                                                    \
         static size_type const BOOST_PP_CAT(name, _group) = SENF_MPL_SLOT_GET(group) + isvar;     \
+    private:                                                                                      \
         SENF_MPL_SLOT_SET(group, BOOST_PP_CAT(name, _group));                                     \
     access:
 #
 # // SENF_PARSER_I_FIELD_VAL_*
 #
 # define SENF_PARSER_I_FIELD_VAL_rw(name, type, access)                                           \
-    private:                                                                                      \
+    protected:                                                                                    \
         BOOST_PP_CAT(name, _t) BOOST_PP_CAT(name, _)() const {                                    \
             return parse<type>( SENF_PARSER_OFFSET(name) );                                       \
         }                                                                                         \
         }
 #
 # define SENF_PARSER_I_FIELD_VAL_ro(name, type, access)                                           \
-    private:                                                                                      \
+    protected:                                                                                    \
         BOOST_PP_CAT(name, _t) BOOST_PP_CAT(name, _)() const {                                    \
             return parse<type>( SENF_PARSER_OFFSET(name) );                                       \
         }                                                                                         \
index 7e0d11d..c00b541 100644 (file)
@@ -284,6 +284,59 @@ BOOST_AUTO_UNIT_TEST(vectorMacro_create)
     BOOST_CHECK( equal( p.data().begin(), p.data().end(), data ));
 }
 
+namespace {
+    
+    struct TestVectorBaseParser 
+        : public senf::PacketParserBase
+    {
+#       include SENF_PARSER()
+
+        SENF_PARSER_PRIVATE_FIELD ( size1 , senf::UInt8Parser );
+        SENF_PARSER_FIELD_RO      ( size2 , senf::UInt8Parser );
+        SENF_PARSER_FIELD         ( dummy , senf::UInt32Parser );\
+
+        SENF_PARSER_FINALIZE( TestVectorBaseParser );
+    };
+
+    struct TestVectorDerivedParser
+        : public TestVectorBaseParser
+    {
+#       include SENF_PARSER()
+
+        SENF_PARSER_INHERIT(TestVectorBaseParser);
+
+        SENF_PARSER_VECTOR        ( vec1  , transform(TestTransform, size1) , senf::UInt16Parser );
+        SENF_PARSER_VECTOR        ( vec2  , bytes(size2) , senf::UInt16Parser );
+
+        SENF_PARSER_FINALIZE( TestVectorDerivedParser );
+    };
+
+}
+
+BOOST_AUTO_UNIT_TEST(vectorMacro_inherit)
+{
+    unsigned char data[] = { 0x05,                   // size1
+                             0x04,                   // size2
+                             0x01, 0x02, 0x03, 0x04, // dummy
+                             0x05, 0x06,             // vec1[0]
+                             0x07, 0x08,             // vec1[1]
+                             0x09, 0x0A,             // vec1[2]
+                             0x0B, 0x0C,             // vec2[0]
+                             0x0D, 0x0E };           // vec2[1]
+
+    senf::DataPacket p (senf::DataPacket::create(data));
+    TestVectorDerivedParser parser (p.data().begin(), &p.data());
+    
+    BOOST_CHECK_EQUAL( parser.vec1().size(), 3u );
+    BOOST_CHECK_EQUAL( parser.vec2().size(), 2u );
+    BOOST_CHECK_EQUAL( parser.dummy(), 0x01020304u );
+    BOOST_CHECK_EQUAL( parser.vec1()[0], 0x0506u );
+    BOOST_CHECK_EQUAL( parser.vec1()[1], 0x0708u );
+    BOOST_CHECK_EQUAL( parser.vec1()[2], 0x090Au );
+    BOOST_CHECK_EQUAL( parser.vec2()[0], 0x0B0Cu );
+    BOOST_CHECK_EQUAL( parser.vec2()[1], 0x0D0Eu );
+}
+
 ///////////////////////////////cc.e////////////////////////////////////////
 #undef prefix_