4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief PacketRegistry non-inline template implementation */
26 #include "PacketRegistry.ih"
31 #include <senf/Utils/TypeInfo.hh>
32 #include <senf/Utils/Format.hh>
33 #include <senf/Utils/senfassert.hh>
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39 // senf::detail::PacketRegistryImpl<KeyType>::Entry
41 template <class KeyType>
42 prefix_ senf::detail::PacketRegistryImpl<KeyType>::Entry::Entry(KeyType const & key_,
44 : key (key_), priority (priority_)
47 template <class KeyType>
48 prefix_ senf::detail::PacketRegistryImpl<KeyType>::Entry::~Entry()
51 //-/////////////////////////////////////////////////////////////////////////////////////////////////
52 // senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>
54 template <class KeyType>
55 template <class PacketType>
56 prefix_ senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::
57 EntryImpl(KeyType const & key, int priority)
58 : Entry (key, priority)
61 template <class KeyType>
62 template <class PacketType>
63 prefix_ senf::Packet::factory_t
64 senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::factory()
67 return PacketType::factory();
70 template <class KeyType>
71 template <class PacketType>
72 prefix_ std::string senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::name()
75 return prettyName(typeid(PacketType));
78 template <class KeyType>
79 template <class PacketType>
80 prefix_ std::type_info const &
81 senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::type()
84 return typeid(PacketType);
87 //-/////////////////////////////////////////////////////////////////////////////////////////////////
88 // senf::detail::PacketRegistryImpl<KeyType>:
90 template <class KeyType>
91 template <class PacketType>
92 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::registerPacket(key_t key, int priority)
94 SENF_ASSERT_EXPRESSION(
96 typename Entry::ptr(new EntryImpl<PacketType>(key,priority))).second,
97 "Duplicate packet registration");
100 template <class KeyType>
101 template <class PacketType>
102 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::unregisterPacket()
104 registry_.template get<ByType>().erase(typeid(PacketType));
107 template <class KeyType>
108 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::unregisterPacket(key_t key, int priority)
110 // Why doesn't this work:
111 // registry_.erase(boost::make_tuple(key,priority));
112 typename Registry::iterator i (registry_.find(boost::make_tuple(key,priority)));
113 if (i != registry_.end())
117 template <class KeyType>
118 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::key_t
119 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type)
121 boost::optional<KeyType> k (key(type,true));
123 throw PacketTypeNotRegisteredException();
127 template <class KeyType>
128 prefix_ boost::optional<typename senf::detail::PacketRegistryImpl<KeyType>::key_t>
129 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type, bool)
131 typedef typename Registry::template index<ByType>::type TypeIndex;
132 TypeIndex const & typeIndex (registry_.template get<ByType>());
133 typename TypeIndex::const_iterator i (typeIndex.find(type.id()));
134 if (i == typeIndex.end())
135 return boost::optional<key_t>();
139 template <class KeyType>
140 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const &
141 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key)
143 Entry const * e (lookup(key, true));
145 throw PacketTypeNotRegisteredException();
149 template <class KeyType>
150 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const *
151 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key, bool)
153 typedef typename Registry::template index<ByKey>::type KeyIndex;
154 KeyIndex const & keyIndex (registry_.template get<ByKey>());
155 typename KeyIndex::const_iterator i (keyIndex.lower_bound(key));
156 if (i == keyIndex.end() || (*i)->key != key)
161 template <class KeyType>
162 prefix_ bool senf::detail::PacketRegistryImpl<KeyType>::v_empty()
165 return registry_.empty();
168 template <class KeyType>
169 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_dump(std::ostream & os)
172 typedef typename Registry::template index<ByKey>::type KeyIndex;
173 KeyIndex const & keyIndex (registry_.template get<ByKey>());
174 for (typename KeyIndex::iterator i (keyIndex.begin()), i_end (keyIndex.end());
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