Packets: static assertion for duplicate tlv parser registration
[senf.git] / senf / Packets / PacketRegistry.ct
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief PacketRegistry non-inline template implementation */
30
31 #include "PacketRegistry.ih"
32
33 // Custom includes
34 #include <iostream>
35 #include <iomanip>
36 #include <senf/Utils/TypeInfo.hh>
37 #include <senf/Utils/Format.hh>
38 #include <senf/Utils/senfassert.hh>
39
40 #define prefix_
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44 // senf::detail::PacketRegistryImpl<KeyType>::Entry
45
46 template <class KeyType>
47 prefix_ senf::detail::PacketRegistryImpl<KeyType>::Entry::Entry(KeyType const & key_,
48                                                                 int priority_)
49     : key (key_), priority (priority_)
50 {}
51
52 template <class KeyType>
53 prefix_ senf::detail::PacketRegistryImpl<KeyType>::Entry::~Entry()
54 {}
55
56 //-/////////////////////////////////////////////////////////////////////////////////////////////////
57 // senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>
58
59 template <class KeyType>
60 template <class PacketType>
61 prefix_ senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::
62 EntryImpl(KeyType const & key, int priority)
63     : Entry (key, priority)
64 {}
65
66 template <class KeyType>
67 template <class PacketType>
68 prefix_ senf::Packet::factory_t
69 senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::factory()
70     const
71 {
72     return PacketType::factory();
73 }
74
75 template <class KeyType>
76 template <class PacketType>
77 prefix_ std::string senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::name()
78     const
79 {
80     return prettyName(typeid(PacketType));
81 }
82
83 template <class KeyType>
84 template <class PacketType>
85 prefix_ std::type_info const &
86 senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::type()
87     const
88 {
89     return typeid(PacketType);
90 }
91
92 //-/////////////////////////////////////////////////////////////////////////////////////////////////
93 // senf::detail::PacketRegistryImpl<KeyType>:
94
95 template <class KeyType>
96 template <class PacketType>
97 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::registerPacket(key_t key, int priority)
98 {
99     SENF_ASSERT_EXPRESSION(
100         registry_.insert(
101             typename Entry::ptr(new EntryImpl<PacketType>(key,priority))).second,
102         "Duplicate packet registration");
103 }
104
105 template <class KeyType>
106 template <class PacketType>
107 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::unregisterPacket()
108 {
109     registryByType_.erase(typeid(PacketType));
110 }
111
112 template <class KeyType>
113 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::unregisterPacket(key_t key, int priority)
114 {
115     // Why doesn't this work:
116     // registry_.erase(boost::make_tuple(key,priority));
117     typename Registry::iterator i (registry_.find(boost::make_tuple(key,priority)));
118     if (i != registry_.end())
119         registry_.erase(i);
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     boost::optional<KeyType> k (key(type,true));
127     if (! k)
128         throw PacketTypeNotRegisteredException();
129     return *k;
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 RegistryByType::const_iterator i (registryByType_.find(type.id()));
137     if (i == registryByType_.end())
138         return boost::optional<key_t>();
139     return (*i)->key;
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     const
146 {
147     Entry const * e (lookup(key, true));
148     if (!e)
149         throw PacketTypeNotRegisteredException();
150     return *e;
151 }
152
153 template <class KeyType>
154 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const *
155 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key, bool)
156     const
157 {
158     typename RegistryByKey::const_iterator i (registryByKey_.lower_bound(key));
159     if (i == registryByKey_.end() || (*i)->key != key)
160         return 0;
161     return i->get();
162 }
163
164 template <class KeyType>
165 prefix_ bool senf::detail::PacketRegistryImpl<KeyType>::v_empty()
166     const
167 {
168     return registry_.empty();
169 }
170
171 template <class KeyType>
172 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_dump(std::ostream & os)
173     const
174 {
175     for (typename RegistryByKey::const_iterator i (registryByKey_.begin()), i_end (registryByKey_.end()); i != i_end; ++i) {
176         std::string n ((*i)->name());
177         senf::detail::DumpKey<KeyType>::dump((*i)->key, os);
178         os << ' ' << std::setw(6) << (*i)->priority << ' ' << 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 }
187
188 //-/////////////////////////////////////////////////////////////////////////////////////////////////
189 // senf::detail::DumpKey<KeyType,is_integral>
190
191 template <class KeyType, bool is_integral>
192 prefix_ void senf::detail::DumpKey<KeyType,is_integral>::dump(KeyType const & v,
193                                                               std::ostream & os)
194 {
195     os << "  " << std::setw(16) << std::left << v << std::setw(0) << std::right;
196 }
197
198 // senf::detail::DumpKey<KeyType, true>
199
200 template <class KeyType>
201 prefix_ void senf::detail::DumpKey<KeyType, true>::dump(KeyType const & v, std::ostream & os)
202 {
203     os << "  " << senf::format::dumpint(v);
204 }
205
206 //-/////////////////////////////////////////////////////////////////////////////////////////////////
207 #undef prefix_
208
209 \f
210 // Local Variables:
211 // mode: c++
212 // fill-column: 100
213 // c-file-style: "senf"
214 // indent-tabs-mode: nil
215 // ispell-local-dictionary: "american"
216 // compile-command: "scons -u test"
217 // comment-column: 40
218 // End: