From: g0dil Date: Wed, 5 May 2010 21:19:31 +0000 (+0000) Subject: Packets: Macros and unit-tests for the PacketRegistry priority support X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=c52ee9bb7875b227c8d28566c5c7905602e8c3a1;p=senf.git Packets: Macros and unit-tests for the PacketRegistry priority support git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1622 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/senf/Packets/PacketRegistry.cti b/senf/Packets/PacketRegistry.cti index dd19e9a..89b4868 100644 --- a/senf/Packets/PacketRegistry.cti +++ b/senf/Packets/PacketRegistry.cti @@ -36,9 +36,9 @@ template template prefix_ senf::PacketRegistry::RegistrationProxy:: -RegistrationProxy(typename Tag::key_t key) +RegistrationProxy(typename Tag::key_t key, int priority) { - PacketRegistry::template registerPacket(key); + PacketRegistry::template registerPacket(key,priority); } template @@ -57,9 +57,9 @@ prefix_ senf::PacketRegistry::RegistrationProxy::~RegistrationP template template -prefix_ void senf::PacketRegistry::registerPacket(typename Tag::key_t key) +prefix_ void senf::PacketRegistry::registerPacket(typename Tag::key_t key, int priority) { - registry().registerPacket(key); + registry().registerPacket(key, priority); } template @@ -70,9 +70,9 @@ prefix_ void senf::PacketRegistry::unregisterPacket() } template -prefix_ void senf::PacketRegistry::unregisterPacket(typename Tag::key_t key) +prefix_ void senf::PacketRegistry::unregisterPacket(typename Tag::key_t key, int priority) { - registry().unregisterPacket(key); + registry().unregisterPacket(key, priority); } template diff --git a/senf/Packets/PacketRegistry.hh b/senf/Packets/PacketRegistry.hh index beb3384..befc1a5 100644 --- a/senf/Packets/PacketRegistry.hh +++ b/senf/Packets/PacketRegistry.hh @@ -109,7 +109,7 @@ namespace senf { template struct RegistrationProxy { - RegistrationProxy(typename Tag::key_t key); + RegistrationProxy(typename Tag::key_t key, int priority=0); ~RegistrationProxy(); }; @@ -126,11 +126,11 @@ namespace senf { \param key The key of the packet */ template - static void registerPacket(typename Tag::key_t key); + static void registerPacket(typename Tag::key_t key, int priority=0); template static void unregisterPacket(); - static void unregisterPacket(typename Tag::key_t key); + static void unregisterPacket(typename Tag::key_t key, int priority=0); /** \brief Find key of a packet type @@ -210,10 +210,24 @@ namespace senf { \ingroup packet_module \hideinitializer */ -# define SENF_PACKET_REGISTRY_REGISTER( registry, value, type ) \ - namespace { \ - senf::PacketRegistry< registry >::RegistrationProxy< type > \ - BOOST_PP_CAT(packetRegistration_, __LINE__) ( value ); \ +# define SENF_PACKET_REGISTRY_REGISTER( registry, value, type ) \ + namespace { \ + senf::PacketRegistry< registry >::RegistrationProxy< type > \ + BOOST_PP_CAT(packetRegistration_, __LINE__) ( value ); \ + } + + /** \brief Statically add an entry to a packet registry + + This macro will declare an anonymous global variable in such a way, that constructing this + variable will add a registration to the given packet registry. + + \ingroup packet_module + \hideinitializer + */ +# define SENF_PACKET_REGISTRY_REGISTER_PRIORITY( registry, value, priority, type ) \ + namespace { \ + senf::PacketRegistry< registry >::RegistrationProxy< type > \ + BOOST_PP_CAT(packetRegistration_, __LINE__) ( value, priority ); \ } /** \brief Dump all packet registries diff --git a/senf/Packets/PacketRegistry.test.cc b/senf/Packets/PacketRegistry.test.cc index 321e19a..38e669c 100644 --- a/senf/Packets/PacketRegistry.test.cc +++ b/senf/Packets/PacketRegistry.test.cc @@ -64,7 +64,8 @@ namespace { } -SENF_PACKET_REGISTRY_REGISTER(StringTag, "foo", FooPacket); +SENF_PACKET_REGISTRY_REGISTER(StringTag, "foo", OtherPacket); +SENF_PACKET_REGISTRY_REGISTER_PRIORITY(StringTag, "foo", 1, FooPacket); SENF_AUTO_UNIT_TEST(packetRegistry_test) { @@ -79,6 +80,7 @@ SENF_AUTO_UNIT_TEST(packetRegistry_test) PacketTypeNotRegisteredException ); BOOST_CHECK_EQUAL( PacketRegistry::key(), "foo" ); + BOOST_CHECK( PacketRegistry::lookup("foo").type() == typeid(FooPacket) ); BOOST_CHECK( ! PacketRegistry::lookup("blub", senf::nothrow) ); BOOST_CHECK( PacketRegistry::lookup(1u, senf::nothrow) ); @@ -94,7 +96,7 @@ SENF_AUTO_UNIT_TEST(packetRegistry_test) &senf::PacketRegistry::Entry::key>()), elts1+0, elts1+sizeof(elts1)/sizeof(elts1[0]) ); - std::string elts2[] = { "bar", "foo" }; + std::string elts2[] = { "bar", "foo", "foo" }; BOOST_CHECK_EQUAL_COLLECTIONS( boost::make_transform_iterator( PacketRegistry::begin(), @@ -120,7 +122,8 @@ SENF_AUTO_UNIT_TEST(packetRegistry_test) (s.str()) ("(anonymous namespace)::StringTag:\n" " bar 0 (anonymous namespace)::BarPacketType\n" - " foo 0 (anonymous namespace)::FooPacketType\n" + " foo 1 (anonymous namespace)::FooPacketType\n" + " foo 0 (anonymous namespace)::OtherPacketType\n" "\n" )); } @@ -138,7 +141,8 @@ SENF_AUTO_UNIT_TEST(packetRegistry_test) (s.str()) ("(anonymous namespace)::StringTag:\n" " bar 0 (anonymous namespace)::BarPacketType\n" - " foo 0 (anonymous namespace)::FooPacketType\n" + " foo 1 (anonymous namespace)::FooPacketType\n" + " foo 0 (anonymous namespace)::OtherPacketType\n" "\n" )); SENF_CHECK_NO_THROW( PacketRegistry::unregisterPacket(1u) ); @@ -154,7 +158,8 @@ SENF_AUTO_UNIT_TEST(packetRegistry_test) contains, (s.str()) ("(anonymous namespace)::StringTag:\n" - " foo 0 (anonymous namespace)::FooPacketType\n" + " foo 1 (anonymous namespace)::FooPacketType\n" + " foo 0 (anonymous namespace)::OtherPacketType\n" "\n" )); } }