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::PkReg_EntryImpl<PacketType>
42 template <class PacketType>
43 prefix_ senf::PacketInterpreterBase::factory_t senf::detail::PkReg_EntryImpl<PacketType>::factory()
46 return PacketType::factory();
49 template <class PacketType>
50 prefix_ std::string senf::detail::PkReg_EntryImpl<PacketType>::name()
53 return prettyName(typeid(PacketType));
56 ///////////////////////////////////////////////////////////////////////////
57 // senf::PacketRegistry<Tag>
60 prefix_ typename senf::PacketRegistry<Tag>::Registry &
61 senf::PacketRegistry<Tag>::registry()
63 static Registry registry (prettyName(typeid(Tag)));
67 ///////////////////////////////////////////////////////////////////////////
68 // senf::detail::PacketRegistryImpl<KeyType>:
70 template <class KeyType>
71 template <class PacketType>
72 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::registerPacket(key_t key)
75 registry_.insert(std::make_pair(key, Entry_ptr(new detail::PkReg_EntryImpl<PacketType>())));
76 reverseRegistry_.insert(std::make_pair(senf::typeIdValue<PacketType>(), key));
80 std::make_pair(key, Entry_ptr(new detail::PkReg_EntryImpl<PacketType>()))).second);
81 // If this assertion fails, a Packet was registered with an already known key
82 SENF_ASSERT( isUnique );
84 reverseRegistry_.insert(
85 std::make_pair(senf::typeIdValue<PacketType>(), key)).second);
86 // If this assertion fails, the same Packet was registered with two different keys
91 template <class KeyType>
92 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::key_t
93 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type)
95 typename ReversePacketMap::iterator i (reverseRegistry_.find(type));
96 if (i==reverseRegistry_.end())
97 throw PacketTypeNotRegisteredException();
101 template <class KeyType>
102 prefix_ boost::optional<typename senf::detail::PacketRegistryImpl<KeyType>::key_t>
103 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type, bool)
105 typename ReversePacketMap::iterator i (reverseRegistry_.find(type));
106 if (i==reverseRegistry_.end())
107 return boost::optional<key_t>();
111 template <class KeyType>
112 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const &
113 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key)
115 typename PacketMap::iterator i (registry_.find(key));
116 if (i==registry_.end())
117 throw PacketTypeNotRegisteredException();
121 template <class KeyType>
122 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const *
123 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key, bool)
125 typename PacketMap::iterator i (registry_.find(key));
126 if (i==registry_.end())
128 return i->second.get();
131 template <class KeyType>
132 prefix_ bool senf::detail::PacketRegistryImpl<KeyType>::v_empty()
135 return registry_.empty();
138 template <class KeyType>
139 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_dump(std::ostream & os)
142 typename PacketMap::const_iterator i (registry_.begin());
143 typename PacketMap::const_iterator const i_end (registry_.end());
144 for (; i != i_end; ++i) {
145 std::string n (i->second->name());
146 senf::detail::DumpKey<KeyType>::dump(i->first, os);
147 os << ' ' << n.substr(21,n.size()-22) << "\n";
151 template <class KeyType>
152 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_clear()
155 reverseRegistry_.clear();
158 ///////////////////////////////////////////////////////////////////////////
159 // senf::detail::DumpKey<KeyType,is_integral>
161 template <class KeyType, bool is_integral>
162 prefix_ void senf::detail::DumpKey<KeyType,is_integral>::dump(KeyType const & v,
165 os << " " << std::setw(16) << std::left << v << std::setw(0) << std::right;
168 // senf::detail::DumpKey<KeyType, true>
170 template <class KeyType>
171 prefix_ void senf::detail::DumpKey<KeyType, true>::dump(KeyType const & v, std::ostream & os)
173 os << " " << senf::format::dumpint(v);
176 ///////////////////////////////ct.e////////////////////////////////////////
183 // c-file-style: "senf"
184 // indent-tabs-mode: nil
185 // ispell-local-dictionary: "american"
186 // compile-command: "scons -u test"
187 // comment-column: 40