Packets: Macros and unit-tests for the PacketRegistry priority support
g0dil [Wed, 5 May 2010 21:19:31 +0000 (21:19 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1622 270642c3-0616-0410-b53a-bc976706d245

senf/Packets/PacketRegistry.cti
senf/Packets/PacketRegistry.hh
senf/Packets/PacketRegistry.test.cc

index dd19e9a..89b4868 100644 (file)
@@ -36,9 +36,9 @@
 template <class Tag>
 template <class PacketType>
 prefix_ senf::PacketRegistry<Tag>::RegistrationProxy<PacketType>::
-RegistrationProxy(typename Tag::key_t key)
+RegistrationProxy(typename Tag::key_t key, int priority)
 {
-    PacketRegistry<Tag>::template registerPacket<PacketType>(key);
+    PacketRegistry<Tag>::template registerPacket<PacketType>(key,priority);
 }
 
 template <class Tag>
@@ -57,9 +57,9 @@ prefix_ senf::PacketRegistry<Tag>::RegistrationProxy<PacketType>::~RegistrationP
 
 template <class Tag>
 template <class PacketType>
-prefix_ void senf::PacketRegistry<Tag>::registerPacket(typename Tag::key_t key)
+prefix_ void senf::PacketRegistry<Tag>::registerPacket(typename Tag::key_t key, int priority)
 {
-    registry().registerPacket<PacketType>(key);
+    registry().registerPacket<PacketType>(key, priority);
 }
 
 template <class Tag>
@@ -70,9 +70,9 @@ prefix_ void senf::PacketRegistry<Tag>::unregisterPacket()
 }
 
 template <class Tag>
-prefix_ void senf::PacketRegistry<Tag>::unregisterPacket(typename Tag::key_t key)
+prefix_ void senf::PacketRegistry<Tag>::unregisterPacket(typename Tag::key_t key, int priority)
 {
-    registry().unregisterPacket(key);
+    registry().unregisterPacket(key, priority);
 }
 
 template <class Tag>
index beb3384..befc1a5 100644 (file)
@@ -109,7 +109,7 @@ namespace senf {
         template <class PacketType>
         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 <class PacketType>
-        static void registerPacket(typename Tag::key_t key);
+        static void registerPacket(typename Tag::key_t key, int priority=0);
 
         template <class PacketType>
         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
index 321e19a..38e669c 100644 (file)
@@ -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<StringTag>::key<FooPacket>(), "foo" );
+        BOOST_CHECK( PacketRegistry<StringTag>::lookup("foo").type() == typeid(FooPacket) );
         BOOST_CHECK( ! PacketRegistry<StringTag>::lookup("blub", senf::nothrow) );
         BOOST_CHECK( PacketRegistry<BaseTag>::lookup(1u, senf::nothrow) );
 
@@ -94,7 +96,7 @@ SENF_AUTO_UNIT_TEST(packetRegistry_test)
                                            &senf::PacketRegistry<BaseTag>::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<StringTag>::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<BaseTag>::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" ));
     }
 }