Packets: Packet Registry unregister() support
[senf.git] / senf / Packets / PacketRegistry.ct
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 non-inline template implementation */
25
26 #include "PacketRegistry.ih"
27
28 // Custom includes
29 #include <senf/Utils/senfassert.hh>
30 #include <iostream>
31 #include <iomanip>
32 #include <cmath>
33 #include <senf/Utils/TypeInfo.hh>
34 #include <senf/Utils/Format.hh>
35
36 #define prefix_
37 ///////////////////////////////ct.p////////////////////////////////////////
38
39 ///////////////////////////////////////////////////////////////////////////
40 // senf::detail::PkReg_EntryImpl<PacketType>
41
42 template <class PacketType>
43 prefix_ senf::PacketInterpreterBase::factory_t senf::detail::PkReg_EntryImpl<PacketType>::factory()
44     const
45 {
46     return PacketType::factory();
47 }
48
49 template <class PacketType>
50 prefix_ std::string senf::detail::PkReg_EntryImpl<PacketType>::name()
51     const
52 {
53     return prettyName(typeid(PacketType));
54 }
55
56 template <class PacketType>
57 prefix_ senf::TypeIdValue senf::detail::PkReg_EntryImpl<PacketType>::typeIdValue()
58     const
59 {
60     return typeid(PacketType);
61 }
62
63 ///////////////////////////////////////////////////////////////////////////
64 // senf::PacketRegistry<Tag>
65
66 template <class Tag>
67 prefix_ typename senf::PacketRegistry<Tag>::Registry &
68 senf::PacketRegistry<Tag>::registry()
69 {
70     static Registry registry (prettyName(typeid(Tag)));
71     return registry;
72 }
73
74 ///////////////////////////////////////////////////////////////////////////
75 // senf::detail::PacketRegistryImpl<KeyType>:
76
77 template <class KeyType>
78 template <class PacketType>
79 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::registerPacket(key_t key)
80 {
81 #ifndef SENF_DEBUG
82     registry_.insert(std::make_pair(key, Entry_ptr(new detail::PkReg_EntryImpl<PacketType>())));
83     reverseRegistry_.insert(std::make_pair(senf::typeIdValue<PacketType>(), key));
84 #else
85     bool isUnique (
86         registry_.insert(
87             std::make_pair(key, Entry_ptr(new detail::PkReg_EntryImpl<PacketType>()))).second);
88     // If this assertion fails, a Packet was registered with an already known key
89     SENF_ASSERT( isUnique );
90     bool isNew (
91         reverseRegistry_.insert(
92             std::make_pair(senf::typeIdValue<PacketType>(), key)).second);
93     // If this assertion fails, the same Packet was registered with two different keys
94     SENF_ASSERT( isNew );
95 #endif
96 }
97
98 template <class KeyType>
99 template <class PacketType>
100 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::unregisterPacket()
101 {
102     typename PacketMap::iterator i (registry_.find(key(typeIdValue<PacketType>())));
103     typename ReversePacketMap::iterator j (reverseRegistry_.find(typeIdValue<PacketType>()));
104     if (i != registry_.end())
105         registry_.erase(i);
106     if (j != reverseRegistry_.end())
107         reverseRegistry_.erase(j);
108 }
109
110 template <class KeyType>
111 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::unregisterPacket(key_t key)
112 {
113     typename PacketMap::iterator i (registry_.find(key));
114     if (i == registry_.end())
115         throw PacketTypeNotRegisteredException();
116     typename ReversePacketMap::iterator j (reverseRegistry_.find(i->second->typeIdValue()));
117     registry_.erase(i);
118     if (j != reverseRegistry_.end())
119         reverseRegistry_.erase(j);
120 }
121
122 template <class KeyType>
123 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::key_t
124 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type)
125 {
126     typename ReversePacketMap::iterator i (reverseRegistry_.find(type));
127     if (i==reverseRegistry_.end())
128         throw PacketTypeNotRegisteredException();
129     return i->second;
130 }
131
132 template <class KeyType>
133 prefix_ boost::optional<typename senf::detail::PacketRegistryImpl<KeyType>::key_t>
134 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type, bool)
135 {
136     typename ReversePacketMap::iterator i (reverseRegistry_.find(type));
137     if (i==reverseRegistry_.end())
138         return boost::optional<key_t>();
139     return i->second;
140 }
141
142 template <class KeyType>
143 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const &
144 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key)
145 {
146     typename PacketMap::iterator i (registry_.find(key));
147     if (i==registry_.end())
148         throw PacketTypeNotRegisteredException();
149     return *(i->second);
150 }
151
152 template <class KeyType>
153 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const *
154 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key, bool)
155 {
156     typename PacketMap::iterator i (registry_.find(key));
157     if (i==registry_.end())
158         return 0;
159     return i->second.get();
160 }
161
162 template <class KeyType>
163 prefix_ bool senf::detail::PacketRegistryImpl<KeyType>::v_empty()
164     const
165 {
166     return registry_.empty();
167 }
168
169 template <class KeyType>
170 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_dump(std::ostream & os)
171     const
172 {
173     typename PacketMap::const_iterator i (registry_.begin());
174     typename PacketMap::const_iterator const i_end (registry_.end());
175     for (; i != i_end; ++i) {
176         std::string n (i->second->name());
177         senf::detail::DumpKey<KeyType>::dump(i->first, os);
178         os << ' ' << n.substr(21,n.size()-22) << "\n";
179     }
180 }
181
182 template <class KeyType>
183 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_clear()
184 {
185     registry_.clear();
186     reverseRegistry_.clear();
187 }
188
189 ///////////////////////////////////////////////////////////////////////////
190 // senf::detail::DumpKey<KeyType,is_integral>
191
192 template <class KeyType, bool is_integral>
193 prefix_ void senf::detail::DumpKey<KeyType,is_integral>::dump(KeyType const & v,
194                                                               std::ostream & os)
195 {
196     os << "  " << std::setw(16) << std::left << v << std::setw(0) << std::right;
197 }
198
199 // senf::detail::DumpKey<KeyType, true>
200
201 template <class KeyType>
202 prefix_ void senf::detail::DumpKey<KeyType, true>::dump(KeyType const & v, std::ostream & os)
203 {
204     os << "  " << senf::format::dumpint(v);
205 }
206
207 ///////////////////////////////ct.e////////////////////////////////////////
208 #undef prefix_
209
210 \f
211 // Local Variables:
212 // mode: c++
213 // fill-column: 100
214 // c-file-style: "senf"
215 // indent-tabs-mode: nil
216 // ispell-local-dictionary: "american"
217 // compile-command: "scons -u test"
218 // comment-column: 40
219 // End: