4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief PacketRegistry internal header */
26 #ifndef IH_SENF_Packets_PacketRegistry_
27 #define IH_SENF_Packets_PacketRegistry_ 1
30 #include <ext/functional>
32 #include <boost/intrusive_ptr.hpp>
33 #include <boost/iterator/transform_iterator.hpp>
34 #include <senf/Utils/TypeIdValue.hh>
36 ///////////////////////////////ih.p////////////////////////////////////////
40 /** \brief Registry entry
42 Value returned by a registry lookup
45 : public intrusive_refcount
47 virtual ~PkReg_Entry();
48 virtual Packet::factory_t factory() const = 0;
49 ///< Get factory of the registered packet type
50 virtual std::string name() const = 0;
55 /** \brief Internal: Registry entry implementation for a specific packet type
59 template <class PacketType>
60 struct PkReg_EntryImpl
63 virtual Packet::factory_t factory() const;
64 virtual std::string name() const;
67 /** \brief Internal: Registry implementation base-class and registry of registries
71 class PacketRegistryImplBase
72 : private boost::noncopyable
75 virtual ~PacketRegistryImplBase();
77 static void dump(std::ostream & os);
81 typedef std::map<std::string, PacketRegistryImplBase*> RegistryMap;
82 static RegistryMap & registries();
85 virtual bool v_empty() const = 0;
86 virtual void v_dump(std::ostream & os) const = 0;
87 virtual void v_clear() = 0;
90 /** \brief Internal: Singleton class implementing the packet registry.
94 template <class KeyType>
95 class PacketRegistryImpl
96 : public PacketRegistryImplBase
99 typedef KeyType key_t;
100 typedef PkReg_Entry Entry;
103 typedef boost::intrusive_ptr<Entry> Entry_ptr;
104 typedef std::map<key_t, Entry_ptr> PacketMap;
105 typedef std::map<senf::TypeIdValue, key_t> ReversePacketMap;
108 ///////////////////////////////////////////////////////////////////////////
111 typedef boost::transform_iterator< ::__gnu_cxx::select1st<typename PacketMap::value_type>,
112 typename PacketMap::const_iterator > iterator;
114 ///////////////////////////////////////////////////////////////////////////
115 ///\name Structors and default members
118 PacketRegistryImpl(std::string const & name);
121 ///////////////////////////////////////////////////////////////////////////
123 template <class PacketType>
124 void registerPacket(key_t key);
126 key_t key(senf::TypeIdValue const & type);
127 boost::optional<key_t> key(senf::TypeIdValue const & type, bool);
129 Entry const & lookup(key_t key);
130 Entry const * lookup(key_t key, bool);
132 iterator begin() const;
133 iterator end() const;
138 virtual bool v_empty() const;
139 virtual void v_dump(std::ostream & os) const;
140 virtual void v_clear();
143 ReversePacketMap reverseRegistry_;
146 template <class KeyType, bool is_integral=std::numeric_limits<KeyType>::is_integer>
149 static void dump(KeyType const & v, std::ostream & os);
152 template <class KeyType>
153 struct DumpKey<KeyType, true>
155 static void dump(KeyType const & v, std::ostream & os);
160 ///////////////////////////////ih.e////////////////////////////////////////
167 // c-file-style: "senf"
168 // indent-tabs-mode: nil
169 // ispell-local-dictionary: "american"
170 // compile-command: "scons -u test"
171 // comment-column: 40