b4babccc279bdbea2cd4639a0f7c2333d0889f59
[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 <iomanip>
32 #include <cmath>
33 #include <senf/Utils/TypeInfo.hh>
34 #include <senf/Utils/Format.hh>
35
36 #define prefix_
37 ///////////////////////////////ct.p////////////////////////////////////////
38
39 ///////////////////////////////////////////////////////////////////////////
40 // senf::detail::PkReg_EntryImpl<PacketType>
41
42 template <class PacketType>
43 prefix_ senf::PacketInterpreterBase::factory_t senf::detail::PkReg_EntryImpl<PacketType>::factory()
44     const
45 {
46     return PacketType::factory();
47 }
48
49 template <class PacketType>
50 prefix_ std::string senf::detail::PkReg_EntryImpl<PacketType>::name()
51     const
52 {
53     return prettyName(typeid(PacketType));
54 }
55
56 ///////////////////////////////////////////////////////////////////////////
57 // senf::PacketRegistry<Tag>
58
59 template <class Tag>
60 prefix_ typename senf::PacketRegistry<Tag>::Registry &
61 senf::PacketRegistry<Tag>::registry()
62 {
63     static Registry registry (prettyName(typeid(Tag)));
64     return registry;
65 }
66
67 ///////////////////////////////////////////////////////////////////////////
68 // senf::detail::PacketRegistryImpl<KeyType>:
69
70 template <class KeyType>
71 template <class PacketType>
72 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::registerPacket(key_t key)
73 {
74 #ifndef SENF_DEBUG
75     registry_.insert(std::make_pair(key, Entry_ptr(new detail::PkReg_EntryImpl<PacketType>())));
76     reverseRegistry_.insert(std::make_pair(senf::typeIdValue<PacketType>(), key));
77 #else
78     bool isUnique (
79         registry_.insert(
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 );
83     bool isNew (
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
87     SENF_ASSERT( isNew );
88 #endif
89 }
90
91 template <class KeyType>
92 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::key_t
93 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type)
94 {
95     typename ReversePacketMap::iterator i (reverseRegistry_.find(type));
96     if (i==reverseRegistry_.end())
97         throw PacketTypeNotRegisteredException();
98     return i->second;
99 }
100
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)
104 {
105     typename ReversePacketMap::iterator i (reverseRegistry_.find(type));
106     if (i==reverseRegistry_.end())
107         return boost::optional<key_t>();
108     return i->second;
109 }
110
111 template <class KeyType>
112 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const &
113 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key)
114 {
115     typename PacketMap::iterator i (registry_.find(key));
116     if (i==registry_.end())
117         throw PacketTypeNotRegisteredException();
118     return *(i->second);
119 }
120
121 template <class KeyType>
122 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const *
123 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key, bool)
124 {
125     typename PacketMap::iterator i (registry_.find(key));
126     if (i==registry_.end())
127         return 0;
128     return i->second.get();
129 }
130
131 template <class KeyType>
132 prefix_ bool senf::detail::PacketRegistryImpl<KeyType>::v_empty()
133     const
134 {
135     return registry_.empty();
136 }
137
138 template <class KeyType>
139 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_dump(std::ostream & os)
140     const
141 {
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";
148     }
149 }
150
151 template <class KeyType>
152 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_clear()
153 {
154     registry_.clear();
155     reverseRegistry_.clear();
156 }
157
158 ///////////////////////////////////////////////////////////////////////////
159 // senf::detail::DumpKey<KeyType,is_integral>
160
161 template <class KeyType, bool is_integral>
162 prefix_ void senf::detail::DumpKey<KeyType,is_integral>::dump(KeyType const & v,
163                                                               std::ostream & os)
164 {
165     os << "  " << std::setw(16) << std::left << v << std::setw(0) << std::right;
166 }
167
168 // senf::detail::DumpKey<KeyType, true>
169
170 template <class KeyType>
171 prefix_ void senf::detail::DumpKey<KeyType, true>::dump(KeyType const & v, std::ostream & os)
172 {
173     os << "  " << senf::format::dumpint(v);
174 }
175
176 ///////////////////////////////ct.e////////////////////////////////////////
177 #undef prefix_
178
179 \f
180 // Local Variables:
181 // mode: c++
182 // fill-column: 100
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
188 // End: