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 "../Utils/senfassert.hh"
31 #include "../Utils/TypeInfo.hh"
34 ///////////////////////////////ct.p////////////////////////////////////////
36 ///////////////////////////////////////////////////////////////////////////
37 // senf::detail::PkReg_EntryImpl<PacketType>
39 template <class PacketType>
40 prefix_ senf::PacketInterpreterBase::factory_t senf::detail::PkReg_EntryImpl<PacketType>::factory()
43 return PacketType::factory();
46 template <class PacketType>
47 prefix_ std::string senf::detail::PkReg_EntryImpl<PacketType>::name()
50 return prettyName(typeid(PacketType));
53 ///////////////////////////////////////////////////////////////////////////
54 // senf::PacketRegistry<Tag>
57 prefix_ typename senf::PacketRegistry<Tag>::Registry &
58 senf::PacketRegistry<Tag>::registry()
60 static Registry registry (prettyName(typeid(Tag)));
64 ///////////////////////////////////////////////////////////////////////////
65 // senf::detail::PacketRegistryImpl<KeyType>:
67 template <class KeyType>
68 template <class PacketType>
69 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::registerPacket(key_t key)
72 registry_.insert(std::make_pair(key, Entry_ptr(new detail::PkReg_EntryImpl<PacketType>())));
73 reverseRegistry_.insert(std::make_pair(senf::typeIdValue<PacketType>(), key));
77 std::make_pair(key, Entry_ptr(new detail::PkReg_EntryImpl<PacketType>()))).second);
78 // If this assertion fails, a Packet was registered with an already known key
79 SENF_ASSERT( isUnique );
81 reverseRegistry_.insert(
82 std::make_pair(senf::typeIdValue<PacketType>(), key)).second);
83 // If this assertion fails, the same Packet was registered with two different keys
88 template <class KeyType>
89 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::key_t
90 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type)
92 typename ReversePacketMap::iterator i (reverseRegistry_.find(type));
93 if (i==reverseRegistry_.end())
94 throw PacketTypeNotRegisteredException();
98 template <class KeyType>
99 prefix_ boost::optional<typename senf::detail::PacketRegistryImpl<KeyType>::key_t>
100 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type, bool)
102 typename ReversePacketMap::iterator i (reverseRegistry_.find(type));
103 if (i==reverseRegistry_.end())
104 return boost::optional<key_t>();
108 template <class KeyType>
109 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const &
110 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key)
112 typename PacketMap::iterator i (registry_.find(key));
113 if (i==registry_.end())
114 throw PacketTypeNotRegisteredException();
118 template <class KeyType>
119 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const *
120 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key, bool)
122 typename PacketMap::iterator i (registry_.find(key));
123 if (i==registry_.end())
125 return i->second.get();
128 template <class KeyType>
129 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_dump(std::ostream & os)
131 typename PacketMap::const_iterator i (registry_.begin());
132 typename PacketMap::const_iterator const i_end (registry_.end());
133 for (; i != i_end; ++i)
134 os << i->first << " " << i->second->name() << "\n";
137 ///////////////////////////////ct.e////////////////////////////////////////
144 // c-file-style: "senf"
145 // indent-tabs-mode: nil
146 // ispell-local-dictionary: "american"
147 // compile-command: "scons -u test"
148 // comment-column: 40