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/multi_index_container.hpp>
33 #include <boost/multi_index/ordered_index.hpp>
34 #include <boost/multi_index/composite_key.hpp>
35 #include <boost/multi_index/member.hpp>
36 #include <boost/multi_index/mem_fun.hpp>
37 #include <boost/intrusive_ptr.hpp>
38 #include <boost/iterator/transform_iterator.hpp>
39 #include <senf/Utils/TypeIdValue.hh>
41 ///////////////////////////////ih.p////////////////////////////////////////
46 struct TypeInfoCompare
48 bool operator()(std::type_info const & a, std::type_info const & b) const
49 { return a.before(b); }
52 /** \brief Internal: Registry implementation base-class and registry of registries
56 class PacketRegistryImplBase
57 : private boost::noncopyable
60 virtual ~PacketRegistryImplBase();
62 static void dump(std::ostream & os);
66 typedef std::map<std::string, PacketRegistryImplBase*> RegistryMap;
67 static RegistryMap & registries();
70 virtual bool v_empty() const = 0;
71 virtual void v_dump(std::ostream & os) const = 0;
72 virtual void v_clear() = 0;
75 /** \brief Internal: Singleton class implementing the packet registry.
79 template <class KeyType>
80 class PacketRegistryImpl
81 : public PacketRegistryImplBase
84 typedef KeyType key_t;
86 struct Entry : public intrusive_refcount
88 typedef boost::intrusive_ptr<Entry> ptr;
90 Entry(KeyType const & key_, int priority_);
93 virtual Packet::factory_t factory() const = 0;
95 virtual std::string name() const = 0;
96 virtual std::type_info const & type() const = 0;
106 struct RegistryIndices
107 : public boost::multi_index::indexed_by<
108 boost::multi_index::ordered_unique<
109 boost::multi_index::tag<ByKey>,
110 boost::multi_index::composite_key<
112 boost::multi_index::member<Entry,KeyType,&Entry::key>,
113 boost::multi_index::member<Entry,int,&Entry::priority> >,
114 boost::multi_index::composite_key_compare<
116 std::greater<int> > >,
117 boost::multi_index::ordered_unique<
118 boost::multi_index::tag<ByType>,
119 boost::multi_index::mem_fun<Entry const,std::type_info const &,&Entry::type>,
123 typedef boost::multi_index_container<typename Entry::ptr, RegistryIndices> Registry;
125 template <class PacketType>
126 struct EntryImpl : public Entry
128 EntryImpl(KeyType const & key, int priority);
130 virtual Packet::factory_t factory() const;
131 virtual std::string name() const;
132 virtual std::type_info const & type() const;
136 ///////////////////////////////////////////////////////////////////////////
139 typedef typename Registry::template index<ByKey>::type::const_iterator iterator;
141 ///////////////////////////////////////////////////////////////////////////
142 ///\name Structors and default members
145 PacketRegistryImpl(std::string const & name);
148 ///////////////////////////////////////////////////////////////////////////
150 template <class PacketType>
151 void registerPacket(key_t key, int priority=0);
153 template <class PacketType>
154 void unregisterPacket();
155 void unregisterPacket(key_t key, int priority=0);
157 key_t key(senf::TypeIdValue const & type);
158 boost::optional<key_t> key(senf::TypeIdValue const & type, bool);
160 Entry const & lookup(key_t key);
161 Entry const * lookup(key_t key, bool);
163 iterator begin() const;
164 iterator end() const;
169 virtual bool v_empty() const;
170 virtual void v_dump(std::ostream & os) const;
171 virtual void v_clear();
176 template <class KeyType, bool is_integral=std::numeric_limits<KeyType>::is_integer>
179 static void dump(KeyType const & v, std::ostream & os);
182 template <class KeyType>
183 struct DumpKey<KeyType, true>
185 static void dump(KeyType const & v, std::ostream & os);
190 ///////////////////////////////ih.e////////////////////////////////////////
197 // c-file-style: "senf"
198 // indent-tabs-mode: nil
199 // ispell-local-dictionary: "american"
200 // compile-command: "scons -u test"
201 // comment-column: 40