Packets: Add PacketRegistry::begin()/end() and senf::dumpPacketRegistries() utility
[senf.git] / Packets / PacketRegistry.ih
index 77d7598..44198ce 100644 (file)
@@ -1,9 +1,9 @@
 // $Id$
 //
 // Copyright (C) 2006
-// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
-// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
-//     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
+//     Stefan Bund <g0dil@berlios.de>
 //
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
 #define IH_PacketRegistryImpl_ 1
 
 // Custom includes
+#include <ext/functional>
 #include <boost/intrusive_ptr.hpp>
-#include "typeidvalue.hh"
+#include <boost/iterator/transform_iterator.hpp>
+#include "../Utils/TypeIdValue.hh"
 
 ///////////////////////////////ih.p////////////////////////////////////////
 
 namespace senf {
+
+    /** \brief Registry entry
+
+        Value returned by a registry lookup
+     */
+    struct PkReg_Entry 
+        : public intrusive_refcount
+    {
+        virtual ~PkReg_Entry();
+        virtual Packet::factory_t factory() const = 0;
+                                        ///< Get factory of the registered packet type
+        virtual std::string name() const = 0;
+    };
+
 namespace detail {
     
-    /** \brief Internal
+    /** \brief Internal: Registry entry implementation for a specific packet type
+
         \internal
      */
     template <class PacketType>
@@ -43,6 +60,27 @@ namespace detail {
         : public PkReg_Entry
     {
         virtual Packet::factory_t factory() const;
+        virtual std::string name() const;
+    };
+
+    /** \brief Internal: Registry implementation base-class and registry of registries
+        
+        \internal
+     */
+    class PacketRegistryImplBase
+        : private boost::noncopyable
+    {
+    public:
+        virtual ~PacketRegistryImplBase();
+
+        static void dump(std::ostream & os);
+
+    protected:
+        typedef std::map<std::string, PacketRegistryImplBase*>  RegistryMap;
+        static RegistryMap & registries();
+
+    private:
+        virtual void v_dump(std::ostream & os) = 0;
     };
 
     /** \brief Internal: Singleton class implementing the packet registry.
@@ -51,25 +89,29 @@ namespace detail {
      */
     template <class KeyType>
     class PacketRegistryImpl 
-        : private boost::noncopyable
+        : public PacketRegistryImplBase
     {
     public:
+        typedef KeyType key_t;
+        typedef PkReg_Entry Entry;
+
+    private:
+        typedef boost::intrusive_ptr<Entry> Entry_ptr;
+        typedef std::map<key_t, Entry_ptr> PacketMap;
+        typedef std::map<senf::TypeIdValue, key_t> ReversePacketMap;
+
+    public:
         ///////////////////////////////////////////////////////////////////////////
         // Types
 
-        typedef KeyType key_t;
-
-        typedef PkReg_Entry Entry;
+        typedef boost::transform_iterator< __gnu_cxx::select1st<typename PacketMap::value_type>,
+                                           typename PacketMap::const_iterator > iterator;
 
         ///////////////////////////////////////////////////////////////////////////
         ///\name Structors and default members
         ///@{
-
-        // default default constructor
-        // no copy constructor
-        // no copy assignment
-        // default destructor
-        // no conversion constructors
+        
+        PacketRegistryImpl(std::string const & name);
 
         ///@}
         ///////////////////////////////////////////////////////////////////////////
@@ -83,12 +125,13 @@ namespace detail {
         Entry const & lookup(key_t key);
         Entry const * lookup(key_t key, bool);
 
+        iterator begin() const;
+        iterator end() const;
+
     protected:
 
     private:
-        typedef boost::intrusive_ptr<Entry> Entry_ptr;
-        typedef std::map<key_t, Entry_ptr> PacketMap;
-        typedef std::map<senf::TypeIdValue, key_t> ReversePacketMap;
+        virtual void v_dump(std::ostream & os);
 
         PacketMap registry_;
         ReversePacketMap reverseRegistry_;