746281d251d21302312e25409c1f49923425605d
[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 <limits>
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>
39
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41
42 namespace senf {
43 namespace detail {
44
45     struct TypeInfoCompare
46     {
47         bool operator()(std::type_info const & a, std::type_info const & b) const
48             { return a.before(b); }
49     };
50
51     /** \brief Internal: Registry implementation base-class and registry of registries
52
53         \internal
54      */
55     class PacketRegistryImplBase
56         : private boost::noncopyable
57     {
58     public:
59         virtual ~PacketRegistryImplBase();
60
61         static void dump(std::ostream & os);
62         static void clear();
63
64     protected:
65         typedef std::map<std::string, PacketRegistryImplBase*>  RegistryMap;
66         static RegistryMap & registries();
67
68     private:
69         virtual bool v_empty() const = 0;
70         virtual void v_dump(std::ostream & os) const = 0;
71         virtual void v_clear() = 0;
72     };
73
74     /** \brief Internal: Singleton class implementing the packet registry.
75
76         \internal
77      */
78     template <class KeyType>
79     class PacketRegistryImpl
80         : public PacketRegistryImplBase
81     {
82     public:
83         typedef KeyType key_t;
84
85         struct Entry : public intrusive_refcount
86         {
87             typedef boost::intrusive_ptr<Entry> ptr;
88
89             Entry(KeyType const & key_, int priority_);
90             virtual ~Entry();
91
92             virtual Packet::factory_t factory() const = 0;
93
94             virtual std::string name() const = 0;
95             virtual std::type_info const & type() const = 0;
96
97             KeyType key;
98             int priority;
99         };
100
101     private:
102         struct ByKey {};
103         struct ByType {};
104
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<
110                         Entry,
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<
114                         std::less<KeyType>,
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>,
119                     TypeInfoCompare> >
120         {};
121
122         typedef boost::multi_index_container<typename Entry::ptr, RegistryIndices> Registry;
123
124         template <class PacketType>
125         struct EntryImpl : public Entry
126         {
127             EntryImpl(KeyType const & key, int priority);
128
129             virtual Packet::factory_t factory() const;
130             virtual std::string name() const;
131             virtual std::type_info const & type() const;
132         };
133
134     public:
135         //-/////////////////////////////////////////////////////////////////////////////////////////
136         // Types
137
138         typedef typename Registry::template index<ByKey>::type::const_iterator iterator;
139
140         //-/////////////////////////////////////////////////////////////////////////////////////////
141         ///\name Structors and default members
142         //\{
143
144         PacketRegistryImpl(std::string const & name);
145
146         //\}
147         //-/////////////////////////////////////////////////////////////////////////////////////////
148
149         template <class PacketType>
150         void registerPacket(key_t key, int priority=0);
151
152         template <class PacketType>
153         void unregisterPacket();
154         void unregisterPacket(key_t key, int priority=0);
155
156         key_t key(senf::TypeIdValue const & type);
157         boost::optional<key_t> key(senf::TypeIdValue const & type, bool);
158
159         Entry const & lookup(key_t key);
160         Entry const * lookup(key_t key, bool);
161
162         iterator begin() const;
163         iterator end() const;
164
165     protected:
166
167     private:
168         virtual bool v_empty() const;
169         virtual void v_dump(std::ostream & os) const;
170         virtual void v_clear();
171
172         Registry registry_;
173     };
174
175     template <class KeyType, bool is_integral=std::numeric_limits<KeyType>::is_integer>
176     struct DumpKey
177     {
178         static void dump(KeyType const & v, std::ostream & os);
179     };
180
181     template <class KeyType>
182     struct DumpKey<KeyType, true>
183     {
184         static void dump(KeyType const & v, std::ostream & os);
185     };
186
187 }}
188
189 //-/////////////////////////////////////////////////////////////////////////////////////////////////
190 #endif
191
192 \f
193 // Local Variables:
194 // mode: c++
195 // fill-column: 100
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
201 // End: