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