From: tho Date: Thu, 28 Jan 2010 17:03:24 +0000 (+0000) Subject: Packets: added bytes() member to GenericTLVRegistry X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=33d8f945cadcf912e8637a59d97ac1d1b320af9f;p=senf.git Packets: added bytes() member to GenericTLVRegistry git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1554 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/senf/Packets/80221Bundle/MIHPacket.test.cc b/senf/Packets/80221Bundle/MIHPacket.test.cc index cd99702..0218893 100644 --- a/senf/Packets/80221Bundle/MIHPacket.test.cc +++ b/senf/Packets/80221Bundle/MIHPacket.test.cc @@ -296,7 +296,7 @@ SENF_AUTO_UNIT_TEST(MIHPayload_create) unsigned char tlv2_value[] = { 0x1a, 0x2b, 0x3c, 0x4d }; MIHGenericTLVParser tlv2 ( tlvListContainer.push_back_space()); - tlv2.type() = 0x0c; + tlv2.type() = 0x43; tlv2.value( tlv2_value); mihPacket.finalizeAll(); @@ -319,7 +319,7 @@ SENF_AUTO_UNIT_TEST(MIHPayload_create) 0x0a, // first bit not set, length=10 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, // value // second test tlv - 0x0c, // type + 0x43, // type 0x04, // first bit not set, length=4 0x1a, 0x2b, 0x3c, 0x4d // value }; diff --git a/senf/Packets/GenericTLV.ct b/senf/Packets/GenericTLV.ct index fb59a10..8d6c085 100644 --- a/senf/Packets/GenericTLV.ct +++ b/senf/Packets/GenericTLV.ct @@ -97,6 +97,7 @@ template prefix_ void senf::GenericTLVParserRegistry::registerParser() { Keytype key (Parser::typeId+0); + std::cout << "GenericTLVParserRegistry::registerParser " << unsigned(key) << std::endl; typename Map::iterator i (map_.find( key )); if (i == map_.end() ) map_.insert(key, new detail::GenericTLVParserRegistry_Entry() ); @@ -104,7 +105,7 @@ prefix_ void senf::GenericTLVParserRegistry::registerParser( template prefix_ void senf::GenericTLVParserRegistry::dump( - GenericTLVParserBase const & parser, std::ostream & os) + GenericTLVParser const & parser, std::ostream & os) const { typename Map::const_iterator i (map_.find( parser.type())); @@ -114,7 +115,7 @@ prefix_ void senf::GenericTLVParserRegistry::dump( template prefix_ void senf::GenericTLVParserRegistry::dump( - GenericTLVParserBase const & parser, Keytype const & key, std::ostream & os) + GenericTLVParser const & parser, Keytype const & key, std::ostream & os) const { typename Map::const_iterator i (map_.find( key)); @@ -122,6 +123,20 @@ prefix_ void senf::GenericTLVParserRegistry::dump( (i->second)->dump(parser, os); } +template +prefix_ senf::PacketParserBase::size_type senf::GenericTLVParserRegistry::bytes( + GenericTLVParser const & parser) + const +{ + typename Map::const_iterator i (map_.find( parser.type())); + if (i != map_.end()) + return (i->second)->bytes(parser); + else + throw TLVParserNotRegisteredException(); +} + + + ///////////////////////////////ct.e//////////////////////////////////////// #undef prefix_ diff --git a/senf/Packets/GenericTLV.cti b/senf/Packets/GenericTLV.cti index 55a4573..b4ba16e 100644 --- a/senf/Packets/GenericTLV.cti +++ b/senf/Packets/GenericTLV.cti @@ -36,7 +36,10 @@ template prefix_ senf::PacketParserBase::size_type senf::GenericTLVParserBase::bytes() { - return senf::bytes(self()) + this->length(); + if (Base::Registry::instance().isRegistered( *this)) + return Base::Registry::instance().bytes( *this); + else + return senf::bytes(self()) + this->length(); } template @@ -119,12 +122,21 @@ prefix_ void senf::detail::GenericTLVParserRegistry_Entry::d (parser.template as()).dump(os); } +template +prefix_ senf::PacketParserBase::size_type +senf::detail::GenericTLVParserRegistry_Entry::bytes( + GenericTLVParserBase const & parser) + const +{ + return senf::bytes( parser.template as()); +} + /////////////////////////////////////////////////////////////////////////// // senf::GenericTLVParserRegistry template prefix_ bool senf::GenericTLVParserRegistry::isRegistered( - GenericTLVParserBase const & parser) + GenericTLVParser const & parser) const { typename Map::const_iterator i (map_.find( parser.type())); diff --git a/senf/Packets/GenericTLV.hh b/senf/Packets/GenericTLV.hh index b1298e0..8d1357f 100644 --- a/senf/Packets/GenericTLV.hh +++ b/senf/Packets/GenericTLV.hh @@ -149,7 +149,7 @@ namespace senf { bool is() const; senf::PacketInterpreterBase::range value() const; - + void dump(std::ostream & os) const; #ifndef DOXYGEN @@ -185,6 +185,7 @@ namespace senf { template struct GenericTLVParserRegistry_EntryBase { virtual void dump(GenericTLVParserBase const & parser, std::ostream & os) const = 0; + virtual PacketParserBase::size_type bytes(GenericTLVParserBase const & parser) const = 0; }; template @@ -192,6 +193,7 @@ namespace senf { : GenericTLVParserRegistry_EntryBase { virtual void dump(GenericTLVParserBase const & parser, std::ostream & os) const; + virtual PacketParserBase::size_type bytes(GenericTLVParserBase const & parser) const; }; //Helper Functor for STL-compatible predicate (E.g. find_if, for_each ...) @@ -245,8 +247,7 @@ namespace senf { The registry provides a dump() member to dump an instance of a generic TLV parser. If the type value of the given TLV parser is registered the generic tlv will be casted to the registered concrete TLV parser and the dump member from this parser - will be called. Otherwise the generic TLV parser will be dumped in a generic way - (hexdump of the value). + will be called. \see GenericTLVParserBase for the general TLV class structure \n @@ -274,13 +275,23 @@ namespace senf { template void registerParser(); + typedef GenericTLVParserBase GenericTLVParser; + bool isRegistered(GenericTLVParserBase const & parser) const; bool isRegistered(Keytype const & key) const; - void dump(GenericTLVParserBase const & parser, std::ostream & os) const; - void dump(GenericTLVParserBase const & parser, Keytype const & key, std::ostream & os) const; + void dump(GenericTLVParser const & parser, std::ostream & os) const; + void dump(GenericTLVParser const & parser, Keytype const & key, std::ostream & os) const; + + PacketParserBase::size_type bytes(GenericTLVParser const & parser) const; }; + struct TLVParserNotRegisteredException : public senf::Exception + { + TLVParserNotRegisteredException() : senf::Exception("tlv parser not registered") {} + }; + + /** \brief Statically add an entry to a TLV parser registry This macro will declare an anonymous global variable in such a way, that constructing diff --git a/senf/Packets/GenericTLV.test.cc b/senf/Packets/GenericTLV.test.cc index 2af6ed6..b403c33 100644 --- a/senf/Packets/GenericTLV.test.cc +++ b/senf/Packets/GenericTLV.test.cc @@ -176,6 +176,7 @@ SENF_AUTO_UNIT_TEST(GenericTLV_packet) SENF_AUTO_UNIT_TEST(GenericTLV_registry) { + typedef senf::GenericTLVParserRegistry MyTLVParserRegistry; MyTestPacket p ( MyTestPacket::create()); MyTestPacket::Parser::tlv_list_t::container tlvContainer (p->tlv_list() ); MyConcreteTLVParser conreteTLVParser ( @@ -184,15 +185,20 @@ SENF_AUTO_UNIT_TEST(GenericTLV_registry) p.finalizeThis(); std::stringstream ss; - (*tlvContainer.begin()).dump( ss); + tlvContainer.begin()->dump( ss); BOOST_CHECK_EQUAL( ss.str().substr(0,58), " GenericTLVParser<(anonymous namespace)::MyTLVParserBase>" ); + BOOST_CHECK( ! MyTLVParserRegistry::instance().isRegistered( tlvContainer.begin()->type())); + + MyTLVParserRegistry::instance().registerParser(); + BOOST_CHECK( MyTLVParserRegistry::instance().isRegistered( tlvContainer.begin()->type())); + BOOST_CHECK_EQUAL( + MyTLVParserRegistry::instance().bytes( *tlvContainer.begin()), + senf::bytes( *tlvContainer.begin()) ); - senf::GenericTLVParserRegistry::instance() - .registerParser(); ss.str(""); ss.clear(); - (*tlvContainer.begin()).dump( ss); + tlvContainer.begin()->dump( ss); BOOST_CHECK_EQUAL( ss.str().substr(0,21), " MyConcreteTLVParser" ); }