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
31 #include <boost/multi_index_container.hpp>
32 #include <boost/multi_index/ordered_index.hpp>
33 #include <boost/multi_index/composite_key.hpp>
34 #include <boost/multi_index/member.hpp>
35 #include <boost/multi_index/mem_fun.hpp>
36 #include <boost/intrusive_ptr.hpp>
37 #include <boost/utility.hpp> // for boost::noncopyable
38 #include <senf/Utils/TypeIdValue.hh>
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45 struct TypeInfoCompare
47 bool operator()(std::type_info const & a, std::type_info const & b) const
48 { return a.before(b); }
51 /** \brief Internal: Registry implementation base-class and registry of registries
55 class PacketRegistryImplBase
56 : private boost::noncopyable
59 virtual ~PacketRegistryImplBase();
61 static void dump(std::ostream & os);
65 typedef std::map<std::string, PacketRegistryImplBase*> RegistryMap;
66 static RegistryMap & registries();
69 virtual bool v_empty() const = 0;
70 virtual void v_dump(std::ostream & os) const = 0;
71 virtual void v_clear() = 0;
74 /** \brief Internal: Singleton class implementing the packet registry.
78 template <class KeyType>
79 class PacketRegistryImpl
80 : public PacketRegistryImplBase
83 typedef KeyType key_t;
85 struct Entry : public intrusive_refcount
87 typedef boost::intrusive_ptr<Entry> ptr;
89 Entry(KeyType const & key_, int priority_);
92 virtual Packet::factory_t factory() const = 0;
94 virtual std::string name() const = 0;
95 virtual std::type_info const & type() const = 0;
105 struct RegistryIndices
106 : public boost::multi_index::indexed_by<
107 boost::multi_index::ordered_unique<
108 boost::multi_index::tag<ByKey>,
109 boost::multi_index::composite_key<
111 boost::multi_index::member<Entry,KeyType,&Entry::key>,
112 boost::multi_index::member<Entry,int,&Entry::priority> >,
113 boost::multi_index::composite_key_compare<
115 std::greater<int> > >,
116 boost::multi_index::ordered_unique<
117 boost::multi_index::tag<ByType>,
118 boost::multi_index::mem_fun<Entry const,std::type_info const &,&Entry::type>,
122 typedef boost::multi_index_container<typename Entry::ptr, RegistryIndices> Registry;
124 template <class PacketType>
125 struct EntryImpl : public Entry
127 EntryImpl(KeyType const & key, int priority);
129 virtual Packet::factory_t factory() const;
130 virtual std::string name() const;
131 virtual std::type_info const & type() const;
135 //-/////////////////////////////////////////////////////////////////////////////////////////
138 typedef typename Registry::template index<ByKey>::type::const_iterator iterator;
140 //-/////////////////////////////////////////////////////////////////////////////////////////
141 ///\name Structors and default members
144 PacketRegistryImpl(std::string const & name);
147 //-/////////////////////////////////////////////////////////////////////////////////////////
149 template <class PacketType>
150 void registerPacket(key_t key, int priority=0);
152 template <class PacketType>
153 void unregisterPacket();
154 void unregisterPacket(key_t key, int priority=0);
156 key_t key(senf::TypeIdValue const & type);
157 boost::optional<key_t> key(senf::TypeIdValue const & type, bool);
159 Entry const & lookup(key_t key);
160 Entry const * lookup(key_t key, bool);
162 iterator begin() const;
163 iterator end() const;
168 virtual bool v_empty() const;
169 virtual void v_dump(std::ostream & os) const;
170 virtual void v_clear();
175 template <class KeyType, bool is_integral=std::numeric_limits<KeyType>::is_integer>
178 static void dump(KeyType const & v, std::ostream & os);
181 template <class KeyType>
182 struct DumpKey<KeyType, true>
184 static void dump(KeyType const & v, std::ostream & os);
189 //-/////////////////////////////////////////////////////////////////////////////////////////////////
196 // c-file-style: "senf"
197 // indent-tabs-mode: nil
198 // ispell-local-dictionary: "american"
199 // compile-command: "scons -u test"
200 // comment-column: 40