2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
15 \brief PacketRegistry non-inline template implementation */
17 #include "PacketRegistry.ih"
21 #include <senf/Utils/TypeInfo.hh>
22 #include <senf/Utils/Format.hh>
23 #include <senf/Utils/senfassert.hh>
24 #include <senf/Utils/senflikely.hh>
27 //-/////////////////////////////////////////////////////////////////////////////////////////////////
29 //-/////////////////////////////////////////////////////////////////////////////////////////////////
30 // senf::detail::PacketRegistryImpl<KeyType>::Entry
32 template <class KeyType>
33 prefix_ senf::detail::PacketRegistryImpl<KeyType>::Entry::Entry(KeyType const & key_,
35 : key (key_), priority (priority_)
38 template <class KeyType>
39 prefix_ senf::detail::PacketRegistryImpl<KeyType>::Entry::~Entry()
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43 // senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>
45 template <class KeyType>
46 template <class PacketType>
47 prefix_ senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::
48 EntryImpl(KeyType const & key, int priority)
49 : Entry (key, priority)
52 template <class KeyType>
53 template <class PacketType>
54 prefix_ senf::Packet::factory_t
55 senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::factory()
58 return PacketType::factory();
61 template <class KeyType>
62 template <class PacketType>
63 prefix_ std::string senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::name()
66 return prettyName(typeid(PacketType));
69 template <class KeyType>
70 template <class PacketType>
71 prefix_ std::type_info const &
72 senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::type()
75 return typeid(PacketType);
78 //-/////////////////////////////////////////////////////////////////////////////////////////////////
79 // senf::detail::PacketRegistryImpl<KeyType>:
81 template <class KeyType>
82 template <class PacketType>
83 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::registerPacket(key_t key, int priority)
85 SENF_ASSERT_EXPRESSION(
87 typename Entry::ptr(new EntryImpl<PacketType>(key,priority))).second,
88 "Duplicate packet registration");
91 template <class KeyType>
92 template <class PacketType>
93 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::unregisterPacket()
95 registryByType_.erase(typeid(PacketType));
98 template <class KeyType>
99 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::unregisterPacket(key_t key, int priority)
101 // Why doesn't this work:
102 // registry_.erase(boost::make_tuple(key,priority));
103 typename Registry::iterator i (registry_.find(boost::make_tuple(key,priority)));
104 if (i != registry_.end())
108 template <class KeyType>
109 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::key_t
110 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type)
112 boost::optional<KeyType> k (key(type,true));
113 if (SENF_UNLIKELY(!k))
114 throw PacketTypeNotRegisteredException();
118 template <class KeyType>
119 prefix_ boost::optional<typename senf::detail::PacketRegistryImpl<KeyType>::key_t>
120 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type, bool)
122 typename RegistryByType::const_iterator i (registryByType_.find(type.id()));
123 if (SENF_UNLIKELY(i == registryByType_.end()))
124 return boost::optional<key_t>();
128 template <class KeyType>
129 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const &
130 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key)
133 Entry const * e (lookup(key, true));
134 if (SENF_UNLIKELY(!e))
135 throw PacketTypeNotRegisteredException();
139 template <class KeyType>
140 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const *
141 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key, bool)
144 typename RegistryByKey::const_iterator i (registryByKey_.lower_bound(key));
145 if (SENF_UNLIKELY(i == registryByKey_.end() || (*i)->key != key))
150 template <class KeyType>
151 prefix_ bool senf::detail::PacketRegistryImpl<KeyType>::v_empty()
154 return registry_.empty();
157 template <class KeyType>
158 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_dump(std::ostream & os)
161 for (typename RegistryByKey::const_iterator i (registryByKey_.begin()), i_end (registryByKey_.end()); i != i_end; ++i) {
162 std::string n ((*i)->name());
163 senf::detail::DumpKey<KeyType>::dump((*i)->key, os);
164 os << ' ' << std::setw(6) << (*i)->priority << ' ' << n.substr(21,n.size()-22) << '\n';
168 template <class KeyType>
169 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_clear()
174 //-/////////////////////////////////////////////////////////////////////////////////////////////////
175 // senf::detail::DumpKey<KeyType,is_integral>
177 template <class KeyType, bool is_integral>
178 prefix_ void senf::detail::DumpKey<KeyType,is_integral>::dump(KeyType const & v,
181 os << " " << std::setw(16) << std::left << v << std::setw(0) << std::right;
184 // senf::detail::DumpKey<KeyType, true>
186 template <class KeyType>
187 prefix_ void senf::detail::DumpKey<KeyType, true>::dump(KeyType const & v, std::ostream & os)
189 os << " " << senf::format::dumpint(v);
192 //-/////////////////////////////////////////////////////////////////////////////////////////////////
199 // c-file-style: "senf"
200 // indent-tabs-mode: nil
201 // ispell-local-dictionary: "american"
202 // compile-command: "scons -u test"
203 // comment-column: 40