From: g0dil Date: Fri, 9 Jan 2009 12:54:11 +0000 (+0000) Subject: Packets: Allow collections in derived parsers to reference aux fields in the base... X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=4891cce7dda76b60f270edfdba16b63d6756c4b0;p=senf.git Packets: Allow collections in derived parsers to reference aux fields in the base parser git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1051 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/Packets/ParseHelpers.hh b/Packets/ParseHelpers.hh index ef5ec45..9eff394 100644 --- a/Packets/ParseHelpers.hh +++ b/Packets/ParseHelpers.hh @@ -353,6 +353,9 @@ SENF_PARSER_INHERIT(BaseParser) \endcode + + If you want to define collection fields which reference auxiliary fields in the base parser, + you must define the base parser as a variable parser not a fixed parser. \param[in] base name of base class \hideinitializer diff --git a/Packets/ParseHelpers.ih b/Packets/ParseHelpers.ih index 6392b69..f279f35 100644 --- a/Packets/ParseHelpers.ih +++ b/Packets/ParseHelpers.ih @@ -147,15 +147,19 @@ # // 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*>(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_* @@ -180,7 +184,9 @@ 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: # @@ -195,7 +201,7 @@ # // 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( SENF_PARSER_OFFSET(name) ); \ } \ @@ -205,7 +211,7 @@ } # # define SENF_PARSER_I_FIELD_VAL_ro(name, type, access) \ - private: \ + protected: \ BOOST_PP_CAT(name, _t) BOOST_PP_CAT(name, _)() const { \ return parse( SENF_PARSER_OFFSET(name) ); \ } \ diff --git a/Packets/VectorParser.test.cc b/Packets/VectorParser.test.cc index 7e0d11d..c00b541 100644 --- a/Packets/VectorParser.test.cc +++ b/Packets/VectorParser.test.cc @@ -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_