4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
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
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.
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.
19 // The Original Code is Fraunhofer FOKUS code.
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.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief PacketRegistry non-inline template implementation */
31 #include "PacketRegistry.ih"
36 #include <senf/Utils/TypeInfo.hh>
37 #include <senf/Utils/Format.hh>
38 #include <senf/Utils/senfassert.hh>
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44 // senf::detail::PacketRegistryImpl<KeyType>::Entry
46 template <class KeyType>
47 prefix_ senf::detail::PacketRegistryImpl<KeyType>::Entry::Entry(KeyType const & key_,
49 : key (key_), priority (priority_)
52 template <class KeyType>
53 prefix_ senf::detail::PacketRegistryImpl<KeyType>::Entry::~Entry()
56 //-/////////////////////////////////////////////////////////////////////////////////////////////////
57 // senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>
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)
66 template <class KeyType>
67 template <class PacketType>
68 prefix_ senf::Packet::factory_t
69 senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::factory()
72 return PacketType::factory();
75 template <class KeyType>
76 template <class PacketType>
77 prefix_ std::string senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::name()
80 return prettyName(typeid(PacketType));
83 template <class KeyType>
84 template <class PacketType>
85 prefix_ std::type_info const &
86 senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::type()
89 return typeid(PacketType);
92 //-/////////////////////////////////////////////////////////////////////////////////////////////////
93 // senf::detail::PacketRegistryImpl<KeyType>:
95 template <class KeyType>
96 template <class PacketType>
97 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::registerPacket(key_t key, int priority)
99 SENF_ASSERT_EXPRESSION(
101 typename Entry::ptr(new EntryImpl<PacketType>(key,priority))).second,
102 "Duplicate packet registration");
105 template <class KeyType>
106 template <class PacketType>
107 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::unregisterPacket()
109 registryByType_.erase(typeid(PacketType));
112 template <class KeyType>
113 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::unregisterPacket(key_t key, int priority)
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())
122 template <class KeyType>
123 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::key_t
124 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type)
126 boost::optional<KeyType> k (key(type,true));
128 throw PacketTypeNotRegisteredException();
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)
136 typename RegistryByType::const_iterator i (registryByType_.find(type.id()));
137 if (i == registryByType_.end())
138 return boost::optional<key_t>();
142 template <class KeyType>
143 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const &
144 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key)
147 Entry const * e (lookup(key, true));
149 throw PacketTypeNotRegisteredException();
153 template <class KeyType>
154 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const *
155 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key, bool)
158 typename RegistryByKey::const_iterator i (registryByKey_.lower_bound(key));
159 if (i == registryByKey_.end() || (*i)->key != key)
164 template <class KeyType>
165 prefix_ bool senf::detail::PacketRegistryImpl<KeyType>::v_empty()
168 return registry_.empty();
171 template <class KeyType>
172 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_dump(std::ostream & os)
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';
182 template <class KeyType>
183 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_clear()
188 //-/////////////////////////////////////////////////////////////////////////////////////////////////
189 // senf::detail::DumpKey<KeyType,is_integral>
191 template <class KeyType, bool is_integral>
192 prefix_ void senf::detail::DumpKey<KeyType,is_integral>::dump(KeyType const & v,
195 os << " " << std::setw(16) << std::left << v << std::setw(0) << std::right;
198 // senf::detail::DumpKey<KeyType, true>
200 template <class KeyType>
201 prefix_ void senf::detail::DumpKey<KeyType, true>::dump(KeyType const & v, std::ostream & os)
203 os << " " << senf::format::dumpint(v);
206 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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