dad629ecc7cd4e1b7c6f099dd9cbae9da94761f7
[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::PacketRegistryImpl<KeyType>::Entry
41
42 template <class KeyType>
43 prefix_ senf::detail::PacketRegistryImpl<KeyType>::Entry::Entry(KeyType const & key_,
44                                                                 int priority_)
45     : key (key_), priority (priority_)
46 {}
47
48 template <class KeyType>
49 prefix_ senf::detail::PacketRegistryImpl<KeyType>::Entry::~Entry()
50 {}
51
52 ///////////////////////////////////////////////////////////////////////////
53 // senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>
54
55 template <class KeyType>
56 template <class PacketType>
57 prefix_ senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::
58 EntryImpl(KeyType const & key, int priority)
59     : Entry (key, priority)
60 {}
61
62 template <class KeyType>
63 template <class PacketType>
64 prefix_ senf::Packet::factory_t
65 senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::factory()
66     const
67 {
68     return PacketType::factory();
69 }
70
71 template <class KeyType>
72 template <class PacketType>
73 prefix_ std::string senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::name()
74     const
75 {
76     return prettyName(typeid(PacketType));
77 }
78
79 template <class KeyType>
80 template <class PacketType>
81 prefix_ std::type_info const &
82 senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::type()
83     const
84 {
85     return typeid(PacketType);
86 }
87
88 ///////////////////////////////////////////////////////////////////////////
89 // senf::PacketRegistry<Tag>
90
91 template <class Tag>
92 prefix_ typename senf::PacketRegistry<Tag>::Registry &
93 senf::PacketRegistry<Tag>::registry()
94 {
95     static Registry registry (prettyName(typeid(Tag)));
96     return registry;
97 }
98
99 ///////////////////////////////////////////////////////////////////////////
100 // senf::detail::PacketRegistryImpl<KeyType>:
101
102 template <class KeyType>
103 template <class PacketType>
104 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::registerPacket(key_t key, int priority)
105 {
106     bool ok (registry_.insert(
107                  typename Entry::ptr(new EntryImpl<PacketType>(key,priority))).second);
108     SENF_ASSERT(ok && "Duplicate packet registration");
109 }
110
111 template <class KeyType>
112 template <class PacketType>
113 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::unregisterPacket()
114 {
115     registry_.template get<ByType>().erase(typeid(PacketType));
116 }
117
118 template <class KeyType>
119 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::unregisterPacket(key_t key, int priority)
120 {
121     // Why doesn't this work:
122     // registry_.erase(boost::make_tuple(key,priority));
123     typename Registry::iterator i (registry_.find(boost::make_tuple(key,priority)));
124     if (i != registry_.end())
125         registry_.erase(i);
126 }
127
128 template <class KeyType>
129 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::key_t
130 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type)
131 {
132     boost::optional<KeyType> k (key(type,true));
133     if (! k)
134         throw PacketTypeNotRegisteredException();
135     return *k;
136 }
137
138 template <class KeyType>
139 prefix_ boost::optional<typename senf::detail::PacketRegistryImpl<KeyType>::key_t>
140 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type, bool)
141 {
142     typedef typename Registry::template index<ByType>::type TypeIndex;
143     TypeIndex const & typeIndex (registry_.template get<ByType>());
144     typename TypeIndex::const_iterator i (typeIndex.find(type.id()));
145     if (i == typeIndex.end())
146         return boost::optional<key_t>();
147     return (*i)->key;
148 }
149
150 template <class KeyType>
151 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const &
152 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key)
153 {
154     Entry const * e (lookup(key, true));
155     if (!e)
156         throw PacketTypeNotRegisteredException();
157     return *e;
158 }
159
160 template <class KeyType>
161 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const *
162 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key, bool)
163 {
164     typedef typename Registry::template index<ByKey>::type KeyIndex;
165     KeyIndex const & keyIndex (registry_.template get<ByKey>());
166     typename KeyIndex::const_iterator i (keyIndex.lower_bound(key));
167     if (i == keyIndex.end() || (*i)->key != key)
168         return 0;
169     return i->get();
170 }
171
172 template <class KeyType>
173 prefix_ bool senf::detail::PacketRegistryImpl<KeyType>::v_empty()
174     const
175 {
176     return registry_.empty();
177 }
178
179 template <class KeyType>
180 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_dump(std::ostream & os)
181     const
182 {
183     typedef typename Registry::template index<ByKey>::type KeyIndex;
184     KeyIndex const & keyIndex (registry_.template get<ByKey>());
185     for (typename KeyIndex::iterator i (keyIndex.begin()), i_end (keyIndex.end());
186          i != i_end; ++i) {
187         std::string n ((*i)->name());
188         senf::detail::DumpKey<KeyType>::dump((*i)->key, os);
189         os << ' ' << std::setw(6) << (*i)->priority << ' ' << n.substr(21,n.size()-22) << '\n';
190     }
191 }
192
193 template <class KeyType>
194 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_clear()
195 {
196     registry_.clear();
197 }
198
199 ///////////////////////////////////////////////////////////////////////////
200 // senf::detail::DumpKey<KeyType,is_integral>
201
202 template <class KeyType, bool is_integral>
203 prefix_ void senf::detail::DumpKey<KeyType,is_integral>::dump(KeyType const & v,
204                                                               std::ostream & os)
205 {
206     os << "  " << std::setw(16) << std::left << v << std::setw(0) << std::right;
207 }
208
209 // senf::detail::DumpKey<KeyType, true>
210
211 template <class KeyType>
212 prefix_ void senf::detail::DumpKey<KeyType, true>::dump(KeyType const & v, std::ostream & os)
213 {
214     os << "  " << senf::format::dumpint(v);
215 }
216
217 ///////////////////////////////ct.e////////////////////////////////////////
218 #undef prefix_
219
220 \f
221 // Local Variables:
222 // mode: c++
223 // fill-column: 100
224 // c-file-style: "senf"
225 // indent-tabs-mode: nil
226 // ispell-local-dictionary: "american"
227 // compile-command: "scons -u test"
228 // comment-column: 40
229 // End: