Packets: Better packet registry dump and implement bundledump utility
[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
35 #define prefix_
36 ///////////////////////////////ct.p////////////////////////////////////////
37
38 ///////////////////////////////////////////////////////////////////////////
39 // senf::detail::PkReg_EntryImpl<PacketType>
40
41 template <class PacketType>
42 prefix_ senf::PacketInterpreterBase::factory_t senf::detail::PkReg_EntryImpl<PacketType>::factory()
43     const
44 {
45     return PacketType::factory();
46 }
47
48 template <class PacketType>
49 prefix_ std::string senf::detail::PkReg_EntryImpl<PacketType>::name()
50     const
51 {
52     return prettyName(typeid(PacketType));
53 }
54
55 ///////////////////////////////////////////////////////////////////////////
56 // senf::PacketRegistry<Tag>
57
58 template <class Tag>
59 prefix_ typename senf::PacketRegistry<Tag>::Registry &
60 senf::PacketRegistry<Tag>::registry()
61 {
62     static Registry registry (prettyName(typeid(Tag)));
63     return registry;
64 }
65
66 ///////////////////////////////////////////////////////////////////////////
67 // senf::detail::PacketRegistryImpl<KeyType>:
68
69 template <class KeyType>
70 template <class PacketType>
71 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::registerPacket(key_t key)
72 {
73 #ifndef SENF_DEBUG
74     registry_.insert(std::make_pair(key, Entry_ptr(new detail::PkReg_EntryImpl<PacketType>())));
75     reverseRegistry_.insert(std::make_pair(senf::typeIdValue<PacketType>(), key));
76 #else
77     bool isUnique (
78         registry_.insert(
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 );
82     bool isNew (
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
86     SENF_ASSERT( isNew );
87 #endif
88 }
89
90 template <class KeyType>
91 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::key_t
92 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type)
93 {
94     typename ReversePacketMap::iterator i (reverseRegistry_.find(type));
95     if (i==reverseRegistry_.end())
96         throw PacketTypeNotRegisteredException();
97     return i->second;
98 }
99
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)
103 {
104     typename ReversePacketMap::iterator i (reverseRegistry_.find(type));
105     if (i==reverseRegistry_.end())
106         return boost::optional<key_t>();
107     return i->second;
108 }
109
110 template <class KeyType>
111 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const &
112 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key)
113 {
114     typename PacketMap::iterator i (registry_.find(key));
115     if (i==registry_.end())
116         throw PacketTypeNotRegisteredException();
117     return *(i->second);
118 }
119
120 template <class KeyType>
121 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const *
122 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key, bool)
123 {
124     typename PacketMap::iterator i (registry_.find(key));
125     if (i==registry_.end())
126         return 0;
127     return i->second.get();
128 }
129
130 template <class KeyType>
131 prefix_ bool senf::detail::PacketRegistryImpl<KeyType>::v_empty()
132     const
133 {
134     return registry_.empty();
135 }
136
137 template <class KeyType>
138 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_dump(std::ostream & os)
139     const
140 {
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";
147     }
148 }
149
150 template <class KeyType>
151 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_clear()
152 {
153     registry_.clear();
154     reverseRegistry_.clear();
155 }
156
157 ///////////////////////////////////////////////////////////////////////////
158 // senf::detail::DumpKey<KeyType,is_integral>
159
160 template <class KeyType, bool is_integral>
161 prefix_ void senf::detail::DumpKey<KeyType,is_integral>::dump(KeyType const & v,
162                                                               std::ostream & os)
163 {
164     os << "  " << std::setw(16) << std::left << v << std::setw(0) << std::right;
165 }
166
167 // senf::detail::DumpKey<KeyType, true>
168
169 template <class KeyType>
170 prefix_ void senf::detail::DumpKey<KeyType, true>::dump(KeyType const & v, std::ostream & os)
171 {
172     int bytes ((std::numeric_limits<KeyType>::digits+7)/8);
173     int digs (int(2.4*bytes)+1);
174
175     os << "  0x" << std::setw(2*bytes) << std::setfill('0') << std::hex 
176        << typename senf::detail::CharToInt<KeyType>::type (v)
177        << " (" << std::setw(digs) << std::setfill(' ') << std::dec 
178        << typename senf::detail::CharToInt<KeyType>::type (v)
179        << ") (";
180     for (int i (bytes-1); i>=0; --i) {
181         char c ((v>>(8*i))&0xff);
182         os << ((c>=32 && c<=127) ? c : '.');
183     }
184     os << ')';
185 }
186
187 ///////////////////////////////ct.e////////////////////////////////////////
188 #undef prefix_
189
190 \f
191 // Local Variables:
192 // mode: c++
193 // fill-column: 100
194 // c-file-style: "senf"
195 // indent-tabs-mode: nil
196 // ispell-local-dictionary: "american"
197 // compile-command: "scons -u test"
198 // comment-column: 40
199 // End: