382ff30bfad10f0fd0d6e3504c68478bd454c768
[senf.git] / senf / Packets / PacketRegistry.ih
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief PacketRegistry internal header */
25
26 #ifndef IH_SENF_Packets_PacketRegistry_
27 #define IH_SENF_Packets_PacketRegistry_ 1
28
29 // Custom includes
30 #include <ext/functional>
31 #include <limits>
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>
40
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 namespace senf {
44 namespace detail {
45
46     struct TypeInfoCompare
47     {
48         bool operator()(std::type_info const & a, std::type_info const & b) const
49             { return a.before(b); }
50     };
51
52     /** \brief Internal: Registry implementation base-class and registry of registries
53
54         \internal
55      */
56     class PacketRegistryImplBase
57         : private boost::noncopyable
58     {
59     public:
60         virtual ~PacketRegistryImplBase();
61
62         static void dump(std::ostream & os);
63         static void clear();
64
65     protected:
66         typedef std::map<std::string, PacketRegistryImplBase*>  RegistryMap;
67         static RegistryMap & registries();
68
69     private:
70         virtual bool v_empty() const = 0;
71         virtual void v_dump(std::ostream & os) const = 0;
72         virtual void v_clear() = 0;
73     };
74
75     /** \brief Internal: Singleton class implementing the packet registry.
76
77         \internal
78      */
79     template <class KeyType>
80     class PacketRegistryImpl
81         : public PacketRegistryImplBase
82     {
83     public:
84         typedef KeyType key_t;
85
86         struct Entry : public intrusive_refcount
87         {
88             typedef boost::intrusive_ptr<Entry> ptr;
89
90             Entry(KeyType const & key_, int priority_);
91             virtual ~Entry();
92
93             virtual Packet::factory_t factory() const = 0;
94
95             virtual std::string name() const = 0;
96             virtual std::type_info const & type() const = 0;
97
98             KeyType key;
99             int priority;
100         };
101
102     private:
103         struct ByKey {};
104         struct ByType {};
105
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<
111                         Entry,
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<
115                         std::less<KeyType>,
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>,
120                     TypeInfoCompare> >
121         {};
122
123         typedef boost::multi_index_container<typename Entry::ptr, RegistryIndices> Registry;
124
125         template <class PacketType>
126         struct EntryImpl : public Entry
127         {
128             EntryImpl(KeyType const & key, int priority);
129
130             virtual Packet::factory_t factory() const;
131             virtual std::string name() const;
132             virtual std::type_info const & type() const;
133         };
134
135     public:
136         //-/////////////////////////////////////////////////////////////////////////////////////////
137         // Types
138
139         typedef typename Registry::template index<ByKey>::type::const_iterator iterator;
140
141         //-/////////////////////////////////////////////////////////////////////////////////////////
142         ///\name Structors and default members
143         //\{
144
145         PacketRegistryImpl(std::string const & name);
146
147         //\}
148         //-/////////////////////////////////////////////////////////////////////////////////////////
149
150         template <class PacketType>
151         void registerPacket(key_t key, int priority=0);
152
153         template <class PacketType>
154         void unregisterPacket();
155         void unregisterPacket(key_t key, int priority=0);
156
157         key_t key(senf::TypeIdValue const & type);
158         boost::optional<key_t> key(senf::TypeIdValue const & type, bool);
159
160         Entry const & lookup(key_t key);
161         Entry const * lookup(key_t key, bool);
162
163         iterator begin() const;
164         iterator end() const;
165
166     protected:
167
168     private:
169         virtual bool v_empty() const;
170         virtual void v_dump(std::ostream & os) const;
171         virtual void v_clear();
172
173         Registry registry_;
174     };
175
176     template <class KeyType, bool is_integral=std::numeric_limits<KeyType>::is_integer>
177     struct DumpKey
178     {
179         static void dump(KeyType const & v, std::ostream & os);
180     };
181
182     template <class KeyType>
183     struct DumpKey<KeyType, true>
184     {
185         static void dump(KeyType const & v, std::ostream & os);
186     };
187
188 }}
189
190 //-/////////////////////////////////////////////////////////////////////////////////////////////////
191 #endif
192
193 \f
194 // Local Variables:
195 // mode: c++
196 // fill-column: 100
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
202 // End: