From: g0dil Date: Wed, 18 Jul 2007 10:08:48 +0000 (+0000) Subject: Packets: Add SENF_PACKET_PARSER_DEFINE_[FIXED_]FIELDS_OFFSET X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=2c10a733c69cd95e10a312a471c84d07d52b7755;p=senf.git Packets: Add SENF_PACKET_PARSER_DEFINE_[FIXED_]FIELDS_OFFSET git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@319 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/Packets/PacketParser.hh b/Packets/PacketParser.hh index a52c888..21858ba 100644 --- a/Packets/PacketParser.hh +++ b/Packets/PacketParser.hh @@ -463,8 +463,37 @@ namespace senf { \hideinitializer */ # define SENF_PACKET_PARSER_DEFINE_FIELDS(fields) \ - SENF_PACKET_PARSER_I_DEFINE_FIELDS(fields) + SENF_PACKET_PARSER_I_DEFINE_FIELDS(0,fields) + /** \brief Define fields for a dynamically sized parser (with offset) + + Define the fields as specified in \a fields. This macro supports dynamically sized + subfields, the resulting parser will be dynamically sized. + + The \a offset argument gives the byte offset at which to start parsing the fields. This + helps defining extended parser deriving from a base parser: + \code + struct ExtendedParser : public BaseParser + { + SENF_PACKET_PARSER_NO_INIT(ExtendedParser); + + SENF_PACKET_PARSER_DEFINE_FIELDS_OFFSET(senf::bytes(BaseParser(*this)), + ( ... fields ... ) ); + + void init() { + BaseParser::init(); + defaultInit(); + // other init code + } + } + \endcode + + \ingroup packetparsermacros + \hideinitializer + */ +# define SENF_PACKET_PARSER_DEFINE_FIELDS_OFFSET(offset,fields) \ + SENF_PACKET_PARSER_I_DEFINE_FIELDS(offset,fields) + /** \brief Define fields for a fixed size parser Define the fields as specified in \a fields. This macro only supports fixed size @@ -474,7 +503,36 @@ namespace senf { \hideinitializer */ # define SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(fields) \ - SENF_PACKET_PARSER_I_DEFINE_FIXED_FIELDS(fields) + SENF_PACKET_PARSER_I_DEFINE_FIXED_FIELDS(0,fields) + + /** \brief Define fields for a fixed size parser + + Define the fields as specified in \a fields. This macro only supports fixed size + subfields, the resulting parser will also be a fixed size parser. + + The \a offset argument gives the byte offset at which to start parsing the fields. This + helps defining extended parser deriving from a base parser: + \code + struct ExtendedParser : public BaseParser + { + SENF_PACKET_PARSER_NO_INIT(ExtendedParser); + + SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS_OFFSET(BaseParser::fixed_bytes, + ( ... fields ... ) ); + + void init() { + BaseParser::init(); + defaultInit(); + // other init code + } + } + \endcode + + \ingroup packetparsermacros + \hideinitializer + */ +# define SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS_OFFSET(offset,fields) \ + SENF_PACKET_PARSER_I_DEFINE_FIXED_FIELDS(offset,fields) struct VoidPacketParser : public PacketParserBase diff --git a/Packets/PacketParser.mpp b/Packets/PacketParser.mpp index 581ef30..d445ebf 100644 --- a/Packets/PacketParser.mpp +++ b/Packets/PacketParser.mpp @@ -65,8 +65,8 @@ # define SENF_PACKET_PARSER_I_INITSIZE_C(_0,_1,n,elt) \ BOOST_PP_IF(n,+,) senf::init_bytes< SENF_PACKET_PARSER_I_GET_TYPE(elt) >::value # -# define SENF_PACKET_PARSER_I_DEFINE_FIELDS(fields) \ - size_type offset_0_() const { return 0; } \ +# define SENF_PACKET_PARSER_I_DEFINE_FIELDS(offset,fields) \ + size_type offset_0_() const { return offset; } \ BOOST_PP_SEQ_FOR_EACH_I(SENF_PACKET_PARSER_I_FIELD_C, _, fields) \ size_type bytes() const { \ return BOOST_PP_CAT(offset_,BOOST_PP_CAT(BOOST_PP_SEQ_SIZE(fields),_)) (); \ @@ -82,8 +82,8 @@ BOOST_PP_EXPAND( \ SENF_PACKET_PARSER_I_FIXED_FIELD_DISPATCH SENF_PACKET_PARSER_I_UNWRAP(n,elt)) # -# define SENF_PACKET_PARSER_I_DEFINE_FIXED_FIELDS(fields) \ - static const size_type offset_0_ = 0; \ +# define SENF_PACKET_PARSER_I_DEFINE_FIXED_FIELDS(offset,fields) \ + static const size_type offset_0_ = offset; \ BOOST_PP_SEQ_FOR_EACH_I(SENF_PACKET_PARSER_I_FIXED_FIELD_C, _, fields) \ static const size_type fixed_bytes = \ BOOST_PP_CAT(offset_,BOOST_PP_CAT(BOOST_PP_SEQ_SIZE(fields),_)); \