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>
36 ///////////////////////////////ct.p////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////////
39 // senf::detail::PkReg_EntryImpl<PacketType>
41 template <class PacketType>
42 prefix_ senf::PacketInterpreterBase::factory_t senf::detail::PkReg_EntryImpl<PacketType>::factory()
45 return PacketType::factory();
48 template <class PacketType>
49 prefix_ std::string senf::detail::PkReg_EntryImpl<PacketType>::name()
52 return prettyName(typeid(PacketType));
55 ///////////////////////////////////////////////////////////////////////////
56 // senf::PacketRegistry<Tag>
59 prefix_ typename senf::PacketRegistry<Tag>::Registry &
60 senf::PacketRegistry<Tag>::registry()
62 static Registry registry (prettyName(typeid(Tag)));
66 ///////////////////////////////////////////////////////////////////////////
67 // senf::detail::PacketRegistryImpl<KeyType>:
69 template <class KeyType>
70 template <class PacketType>
71 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::registerPacket(key_t key)
74 registry_.insert(std::make_pair(key, Entry_ptr(new detail::PkReg_EntryImpl<PacketType>())));
75 reverseRegistry_.insert(std::make_pair(senf::typeIdValue<PacketType>(), key));
79 std::make_pair(key, Entry_ptr(new detail::PkReg_EntryImpl<PacketType>()))).second);
80 // If this assertion fails, a Packet was registered with an already known key
81 SENF_ASSERT( isUnique );
83 reverseRegistry_.insert(
84 std::make_pair(senf::typeIdValue<PacketType>(), key)).second);
85 // If this assertion fails, the same Packet was registered with two different keys
90 template <class KeyType>
91 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::key_t
92 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type)
94 typename ReversePacketMap::iterator i (reverseRegistry_.find(type));
95 if (i==reverseRegistry_.end())
96 throw PacketTypeNotRegisteredException();
100 template <class KeyType>
101 prefix_ boost::optional<typename senf::detail::PacketRegistryImpl<KeyType>::key_t>
102 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type, bool)
104 typename ReversePacketMap::iterator i (reverseRegistry_.find(type));
105 if (i==reverseRegistry_.end())
106 return boost::optional<key_t>();
110 template <class KeyType>
111 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const &
112 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key)
114 typename PacketMap::iterator i (registry_.find(key));
115 if (i==registry_.end())
116 throw PacketTypeNotRegisteredException();
120 template <class KeyType>
121 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const *
122 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key, bool)
124 typename PacketMap::iterator i (registry_.find(key));
125 if (i==registry_.end())
127 return i->second.get();
130 template <class KeyType>
131 prefix_ bool senf::detail::PacketRegistryImpl<KeyType>::v_empty()
134 return registry_.empty();
137 template <class KeyType>
138 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_dump(std::ostream & os)
141 typename PacketMap::const_iterator i (registry_.begin());
142 typename PacketMap::const_iterator const i_end (registry_.end());
143 for (; i != i_end; ++i) {
144 std::string n (i->second->name());
145 senf::detail::DumpKey<KeyType>::dump(i->first, os);
146 os << ' ' << n.substr(21,n.size()-22) << "\n";
150 template <class KeyType>
151 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_clear()
154 reverseRegistry_.clear();
157 ///////////////////////////////////////////////////////////////////////////
158 // senf::detail::DumpKey<KeyType,is_integral>
160 template <class KeyType, bool is_integral>
161 prefix_ void senf::detail::DumpKey<KeyType,is_integral>::dump(KeyType const & v,
164 os << " " << std::setw(16) << std::left << v << std::setw(0) << std::right;
167 // senf::detail::DumpKey<KeyType, true>
169 template <class KeyType>
170 prefix_ void senf::detail::DumpKey<KeyType, true>::dump(KeyType const & v, std::ostream & os)
172 os << " " << senf::prettyNumber(v);
175 ///////////////////////////////ct.e////////////////////////////////////////
182 // c-file-style: "senf"
183 // indent-tabs-mode: nil
184 // ispell-local-dictionary: "american"
185 // compile-command: "scons -u test"
186 // comment-column: 40