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"
29 #include <senf/Utils/senfassert.hh>
33 #include <senf/Utils/TypeInfo.hh>
34 #include <senf/Utils/Format.hh>
37 ///////////////////////////////ct.p////////////////////////////////////////
39 ///////////////////////////////////////////////////////////////////////////
40 // senf::detail::PacketRegistryImpl<KeyType>::Entry
42 template <class KeyType>
43 prefix_ senf::detail::PacketRegistryImpl<KeyType>::Entry::Entry(KeyType const & key_,
45 : key (key_), priority (priority_)
48 template <class KeyType>
49 prefix_ senf::detail::PacketRegistryImpl<KeyType>::Entry::~Entry()
52 ///////////////////////////////////////////////////////////////////////////
53 // senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>
55 template <class KeyType>
56 template <class PacketType>
57 prefix_ senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::
58 EntryImpl(KeyType const & key, int priority)
59 : Entry (key, priority)
62 template <class KeyType>
63 template <class PacketType>
64 prefix_ senf::Packet::factory_t
65 senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::factory()
68 return PacketType::factory();
71 template <class KeyType>
72 template <class PacketType>
73 prefix_ std::string senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::name()
76 return prettyName(typeid(PacketType));
79 template <class KeyType>
80 template <class PacketType>
81 prefix_ std::type_info const &
82 senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::type()
85 return typeid(PacketType);
88 ///////////////////////////////////////////////////////////////////////////
89 // senf::PacketRegistry<Tag>
92 prefix_ typename senf::PacketRegistry<Tag>::Registry &
93 senf::PacketRegistry<Tag>::registry()
95 static Registry registry (prettyName(typeid(Tag)));
99 ///////////////////////////////////////////////////////////////////////////
100 // senf::detail::PacketRegistryImpl<KeyType>:
102 template <class KeyType>
103 template <class PacketType>
104 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::registerPacket(key_t key, int priority)
106 SENF_ASSERT_EXPRESSION(
108 typename Entry::ptr(new EntryImpl<PacketType>(key,priority))).second,
109 "Duplicate packet registration");
112 template <class KeyType>
113 template <class PacketType>
114 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::unregisterPacket()
116 registry_.template get<ByType>().erase(typeid(PacketType));
119 template <class KeyType>
120 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::unregisterPacket(key_t key, int priority)
122 // Why doesn't this work:
123 // registry_.erase(boost::make_tuple(key,priority));
124 typename Registry::iterator i (registry_.find(boost::make_tuple(key,priority)));
125 if (i != registry_.end())
129 template <class KeyType>
130 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::key_t
131 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type)
133 boost::optional<KeyType> k (key(type,true));
135 throw PacketTypeNotRegisteredException();
139 template <class KeyType>
140 prefix_ boost::optional<typename senf::detail::PacketRegistryImpl<KeyType>::key_t>
141 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type, bool)
143 typedef typename Registry::template index<ByType>::type TypeIndex;
144 TypeIndex const & typeIndex (registry_.template get<ByType>());
145 typename TypeIndex::const_iterator i (typeIndex.find(type.id()));
146 if (i == typeIndex.end())
147 return boost::optional<key_t>();
151 template <class KeyType>
152 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const &
153 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key)
155 Entry const * e (lookup(key, true));
157 throw PacketTypeNotRegisteredException();
161 template <class KeyType>
162 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const *
163 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key, bool)
165 typedef typename Registry::template index<ByKey>::type KeyIndex;
166 KeyIndex const & keyIndex (registry_.template get<ByKey>());
167 typename KeyIndex::const_iterator i (keyIndex.lower_bound(key));
168 if (i == keyIndex.end() || (*i)->key != key)
173 template <class KeyType>
174 prefix_ bool senf::detail::PacketRegistryImpl<KeyType>::v_empty()
177 return registry_.empty();
180 template <class KeyType>
181 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_dump(std::ostream & os)
184 typedef typename Registry::template index<ByKey>::type KeyIndex;
185 KeyIndex const & keyIndex (registry_.template get<ByKey>());
186 for (typename KeyIndex::iterator i (keyIndex.begin()), i_end (keyIndex.end());
188 std::string n ((*i)->name());
189 senf::detail::DumpKey<KeyType>::dump((*i)->key, os);
190 os << ' ' << std::setw(6) << (*i)->priority << ' ' << n.substr(21,n.size()-22) << '\n';
194 template <class KeyType>
195 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_clear()
200 ///////////////////////////////////////////////////////////////////////////
201 // senf::detail::DumpKey<KeyType,is_integral>
203 template <class KeyType, bool is_integral>
204 prefix_ void senf::detail::DumpKey<KeyType,is_integral>::dump(KeyType const & v,
207 os << " " << std::setw(16) << std::left << v << std::setw(0) << std::right;
210 // senf::detail::DumpKey<KeyType, true>
212 template <class KeyType>
213 prefix_ void senf::detail::DumpKey<KeyType, true>::dump(KeyType const & v, std::ostream & os)
215 os << " " << senf::format::dumpint(v);
218 ///////////////////////////////ct.e////////////////////////////////////////
225 // c-file-style: "senf"
226 // indent-tabs-mode: nil
227 // ispell-local-dictionary: "american"
228 // compile-command: "scons -u test"
229 // comment-column: 40