Replace all relative includes with abolute ones
[senf.git] / senf / Packets / PacketRegistry.ct
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief PacketRegistry non-inline template implementation */
25
26 #include "PacketRegistry.ih"
27
28 // Custom includes
29 #include <senf/Utils/senfassert.hh>
30 #include <iostream>
31 #include <senf/Utils/TypeInfo.hh>
32
33 #define prefix_
34 ///////////////////////////////ct.p////////////////////////////////////////
35
36 ///////////////////////////////////////////////////////////////////////////
37 // senf::detail::PkReg_EntryImpl<PacketType>
38
39 template <class PacketType>
40 prefix_ senf::PacketInterpreterBase::factory_t senf::detail::PkReg_EntryImpl<PacketType>::factory()
41     const
42 {
43     return PacketType::factory();
44 }
45
46 template <class PacketType>
47 prefix_ std::string senf::detail::PkReg_EntryImpl<PacketType>::name()
48     const
49 {
50     return prettyName(typeid(PacketType));
51 }
52
53 ///////////////////////////////////////////////////////////////////////////
54 // senf::PacketRegistry<Tag>
55
56 template <class Tag>
57 prefix_ typename senf::PacketRegistry<Tag>::Registry &
58 senf::PacketRegistry<Tag>::registry()
59 {
60     static Registry registry (prettyName(typeid(Tag)));
61     return registry;
62 }
63
64 ///////////////////////////////////////////////////////////////////////////
65 // senf::detail::PacketRegistryImpl<KeyType>:
66
67 template <class KeyType>
68 template <class PacketType>
69 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::registerPacket(key_t key)
70 {
71 #ifndef SENF_DEBUG
72     registry_.insert(std::make_pair(key, Entry_ptr(new detail::PkReg_EntryImpl<PacketType>())));
73     reverseRegistry_.insert(std::make_pair(senf::typeIdValue<PacketType>(), key));
74 #else
75     bool isUnique (
76         registry_.insert(
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 );
80     bool isNew (
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
84     SENF_ASSERT( isNew );
85 #endif
86 }
87
88 template <class KeyType>
89 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::key_t
90 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type)
91 {
92     typename ReversePacketMap::iterator i (reverseRegistry_.find(type));
93     if (i==reverseRegistry_.end())
94         throw PacketTypeNotRegisteredException();
95     return i->second;
96 }
97
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)
101 {
102     typename ReversePacketMap::iterator i (reverseRegistry_.find(type));
103     if (i==reverseRegistry_.end())
104         return boost::optional<key_t>();
105     return i->second;
106 }
107
108 template <class KeyType>
109 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const &
110 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key)
111 {
112     typename PacketMap::iterator i (registry_.find(key));
113     if (i==registry_.end())
114         throw PacketTypeNotRegisteredException();
115     return *(i->second);
116 }
117
118 template <class KeyType>
119 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const *
120 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key, bool)
121 {
122     typename PacketMap::iterator i (registry_.find(key));
123     if (i==registry_.end())
124         return 0;
125     return i->second.get();
126 }
127
128 template <class KeyType>
129 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_dump(std::ostream & os)
130 {
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";
135 }
136
137 ///////////////////////////////ct.e////////////////////////////////////////
138 #undef prefix_
139
140 \f
141 // Local Variables:
142 // mode: c++
143 // fill-column: 100
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
149 // End: