PacketRegistry.ct
Go to the documentation of this file.
1 //
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
6 //
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
11 //
12 
13 
14 /** \file
15  \brief PacketRegistry non-inline template implementation */
16 
17 #include "PacketRegistry.ih"
18 
19 // Custom includes
20 #include <iomanip>
21 #include <senf/Utils/TypeInfo.hh>
22 #include <senf/Utils/Format.hh>
23 #include <senf/Utils/senfassert.hh>
24 #include <senf/Utils/senflikely.hh>
25 
26 #define prefix_
27 //-/////////////////////////////////////////////////////////////////////////////////////////////////
28 
29 //-/////////////////////////////////////////////////////////////////////////////////////////////////
30 // senf::detail::PacketRegistryImpl<KeyType>::Entry
31 
32 template <class KeyType>
33 prefix_ senf::detail::PacketRegistryImpl<KeyType>::Entry::Entry(KeyType const & key_,
34  int priority_)
35  : key (key_), priority (priority_)
36 {}
37 
38 template <class KeyType>
39 prefix_ senf::detail::PacketRegistryImpl<KeyType>::Entry::~Entry()
40 {}
41 
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43 // senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>
44 
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)
50 {}
51 
52 template <class KeyType>
53 template <class PacketType>
54 prefix_ senf::Packet::factory_t
55 senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::factory()
56  const
57 {
58  return PacketType::factory();
59 }
60 
61 template <class KeyType>
62 template <class PacketType>
63 prefix_ std::string senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::name()
64  const
65 {
66  return prettyName(typeid(PacketType));
67 }
68 
69 template <class KeyType>
70 template <class PacketType>
71 prefix_ std::type_info const &
72 senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::type()
73  const
74 {
75  return typeid(PacketType);
76 }
77 
78 //-/////////////////////////////////////////////////////////////////////////////////////////////////
79 // senf::detail::PacketRegistryImpl<KeyType>:
80 
81 template <class KeyType>
82 template <class PacketType>
83 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::registerPacket(key_t key, int priority)
84 {
85  SENF_ASSERT_EXPRESSION(
86  registry_.insert(
87  typename Entry::ptr(new EntryImpl<PacketType>(key,priority))).second,
88  "Duplicate packet registration");
89 }
90 
91 template <class KeyType>
92 template <class PacketType>
93 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::unregisterPacket()
94 {
95  registryByType_.erase(typeid(PacketType));
96 }
97 
98 template <class KeyType>
99 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::unregisterPacket(key_t key, int priority)
100 {
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())
105  registry_.erase(i);
106 }
107 
108 template <class KeyType>
109 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::key_t
110 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type)
111 {
112  boost::optional<KeyType> k (key(type,true));
113  if (SENF_UNLIKELY(!k))
114  throw PacketTypeNotRegisteredException();
115  return *k;
116 }
117 
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)
121 {
122  typename RegistryByType::const_iterator i (registryByType_.find(type.id()));
123  if (SENF_UNLIKELY(i == registryByType_.end()))
124  return boost::optional<key_t>();
125  return (*i)->key;
126 }
127 
128 template <class KeyType>
129 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const &
130 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key)
131  const
132 {
133  Entry const * e (lookup(key, true));
134  if (SENF_UNLIKELY(!e))
135  throw PacketTypeNotRegisteredException();
136  return *e;
137 }
138 
139 template <class KeyType>
140 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const *
141 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key, bool)
142  const
143 {
144  typename RegistryByKey::const_iterator i (registryByKey_.lower_bound(key));
145  if (SENF_UNLIKELY(i == registryByKey_.end() || (*i)->key != key))
146  return 0;
147  return i->get();
148 }
149 
150 template <class KeyType>
151 prefix_ bool senf::detail::PacketRegistryImpl<KeyType>::v_empty()
152  const
153 {
154  return registry_.empty();
155 }
156 
157 template <class KeyType>
158 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_dump(std::ostream & os)
159  const
160 {
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';
165  }
166 }
167 
168 template <class KeyType>
169 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_clear()
170 {
171  registry_.clear();
172 }
173 
174 //-/////////////////////////////////////////////////////////////////////////////////////////////////
175 // senf::detail::DumpKey<KeyType,is_integral>
176 
177 template <class KeyType, bool is_integral>
178 prefix_ void senf::detail::DumpKey<KeyType,is_integral>::dump(KeyType const & v,
179  std::ostream & os)
180 {
181  os << " " << std::setw(16) << std::left << v << std::setw(0) << std::right;
182 }
183 
184 // senf::detail::DumpKey<KeyType, true>
185 
186 template <class KeyType>
187 prefix_ void senf::detail::DumpKey<KeyType, true>::dump(KeyType const & v, std::ostream & os)
188 {
189  os << " " << senf::format::dumpint(v);
190 }
191 
192 //-/////////////////////////////////////////////////////////////////////////////////////////////////
193 #undef prefix_
194 
195 
196 // Local Variables:
197 // mode: c++
198 // fill-column: 100
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
204 // End: