169d2757e3de344d8ff4f42620a30c7d200fa4c7
[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/intrusive_ptr.hpp>
33 #include <boost/iterator/transform_iterator.hpp>
34 #include <senf/Utils/TypeIdValue.hh>
35
36 ///////////////////////////////ih.p////////////////////////////////////////
37
38 namespace senf {
39
40     /** \brief Registry entry
41
42         Value returned by a registry lookup
43      */
44     struct PkReg_Entry
45         : public intrusive_refcount
46     {
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;
51     };
52
53 namespace detail {
54
55     /** \brief Internal: Registry entry implementation for a specific packet type
56
57         \internal
58      */
59     template <class PacketType>
60     struct PkReg_EntryImpl
61         : public PkReg_Entry
62     {
63         virtual Packet::factory_t factory() const;
64         virtual std::string name() const;
65     };
66
67     /** \brief Internal: Registry implementation base-class and registry of registries
68
69         \internal
70      */
71     class PacketRegistryImplBase
72         : private boost::noncopyable
73     {
74     public:
75         virtual ~PacketRegistryImplBase();
76
77         static void dump(std::ostream & os);
78         static void clear();
79
80     protected:
81         typedef std::map<std::string, PacketRegistryImplBase*>  RegistryMap;
82         static RegistryMap & registries();
83
84     private:
85         virtual bool v_empty() const = 0;
86         virtual void v_dump(std::ostream & os) const = 0;
87         virtual void v_clear() = 0;
88     };
89
90     /** \brief Internal: Singleton class implementing the packet registry.
91
92         \internal
93      */
94     template <class KeyType>
95     class PacketRegistryImpl
96         : public PacketRegistryImplBase
97     {
98     public:
99         typedef KeyType key_t;
100         typedef PkReg_Entry Entry;
101
102     private:
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;
106
107     public:
108         ///////////////////////////////////////////////////////////////////////////
109         // Types
110
111         typedef boost::transform_iterator< ::__gnu_cxx::select1st<typename PacketMap::value_type>,
112                                            typename PacketMap::const_iterator > iterator;
113
114         ///////////////////////////////////////////////////////////////////////////
115         ///\name Structors and default members
116         ///@{
117
118         PacketRegistryImpl(std::string const & name);
119
120         ///@}
121         ///////////////////////////////////////////////////////////////////////////
122
123         template <class PacketType>
124         void registerPacket(key_t key);
125
126         key_t key(senf::TypeIdValue const & type);
127         boost::optional<key_t> key(senf::TypeIdValue const & type, bool);
128
129         Entry const & lookup(key_t key);
130         Entry const * lookup(key_t key, bool);
131
132         iterator begin() const;
133         iterator end() const;
134
135     protected:
136
137     private:
138         virtual bool v_empty() const;
139         virtual void v_dump(std::ostream & os) const;
140         virtual void v_clear();
141
142         PacketMap registry_;
143         ReversePacketMap reverseRegistry_;
144     };
145
146     template <class KeyType, bool is_integral=std::numeric_limits<KeyType>::is_integer>
147     struct DumpKey
148     {
149         static void dump(KeyType const & v, std::ostream & os);
150     };
151
152     template <class KeyType>
153     struct DumpKey<KeyType, true>
154     {
155         static void dump(KeyType const & v, std::ostream & os);
156     };
157
158 }}
159
160 ///////////////////////////////ih.e////////////////////////////////////////
161 #endif
162
163 \f
164 // Local Variables:
165 // mode: c++
166 // fill-column: 100
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
172 // End: