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 registry_.template get<ByType>().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 typedef typename Registry::template index<ByType>::type TypeIndex;
137 TypeIndex const & typeIndex (registry_.template get<ByType>());
138 typename TypeIndex::const_iterator i (typeIndex.find(type.id()));
139 if (i == typeIndex.end())
140 return boost::optional<key_t>();
144 template <class KeyType>
145 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const &
146 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key)
148 Entry const * e (lookup(key, true));
150 throw PacketTypeNotRegisteredException();
154 template <class KeyType>
155 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const *
156 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key, bool)
158 typedef typename Registry::template index<ByKey>::type KeyIndex;
159 KeyIndex const & keyIndex (registry_.template get<ByKey>());
160 typename KeyIndex::const_iterator i (keyIndex.lower_bound(key));
161 if (i == keyIndex.end() || (*i)->key != key)
166 template <class KeyType>
167 prefix_ bool senf::detail::PacketRegistryImpl<KeyType>::v_empty()
170 return registry_.empty();
173 template <class KeyType>
174 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_dump(std::ostream & os)
177 typedef typename Registry::template index<ByKey>::type KeyIndex;
178 KeyIndex const & keyIndex (registry_.template get<ByKey>());
179 for (typename KeyIndex::iterator i (keyIndex.begin()), i_end (keyIndex.end());
181 std::string n ((*i)->name());
182 senf::detail::DumpKey<KeyType>::dump((*i)->key, os);
183 os << ' ' << std::setw(6) << (*i)->priority << ' ' << n.substr(21,n.size()-22) << '\n';
187 template <class KeyType>
188 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_clear()
193 //-/////////////////////////////////////////////////////////////////////////////////////////////////
194 // senf::detail::DumpKey<KeyType,is_integral>
196 template <class KeyType, bool is_integral>
197 prefix_ void senf::detail::DumpKey<KeyType,is_integral>::dump(KeyType const & v,
200 os << " " << std::setw(16) << std::left << v << std::setw(0) << std::right;
203 // senf::detail::DumpKey<KeyType, true>
205 template <class KeyType>
206 prefix_ void senf::detail::DumpKey<KeyType, true>::dump(KeyType const & v, std::ostream & os)
208 os << " " << senf::format::dumpint(v);
211 //-/////////////////////////////////////////////////////////////////////////////////////////////////
218 // c-file-style: "senf"
219 // indent-tabs-mode: nil
220 // ispell-local-dictionary: "american"
221 // compile-command: "scons -u test"
222 // comment-column: 40