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