6c594b9bcdc481b19cc391d9ae4705cc2f8e452f
[senf.git] / senf / Packets / PacketRegistry.ct
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief PacketRegistry non-inline template implementation */
30
31 #include "PacketRegistry.ih"
32
33 // Custom includes
34 #include <iostream>
35 #include <iomanip>
36 #include <senf/Utils/TypeInfo.hh>
37 #include <senf/Utils/Format.hh>
38 #include <senf/Utils/senfassert.hh>
39
40 #define prefix_
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44 // senf::detail::PacketRegistryImpl<KeyType>::Entry
45
46 template <class KeyType>
47 prefix_ senf::detail::PacketRegistryImpl<KeyType>::Entry::Entry(KeyType const & key_,
48                                                                 int priority_)
49     : key (key_), priority (priority_)
50 {}
51
52 template <class KeyType>
53 prefix_ senf::detail::PacketRegistryImpl<KeyType>::Entry::~Entry()
54 {}
55
56 //-/////////////////////////////////////////////////////////////////////////////////////////////////
57 // senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>
58
59 template <class KeyType>
60 template <class PacketType>
61 prefix_ senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::
62 EntryImpl(KeyType const & key, int priority)
63     : Entry (key, priority)
64 {}
65
66 template <class KeyType>
67 template <class PacketType>
68 prefix_ senf::Packet::factory_t
69 senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::factory()
70     const
71 {
72     return PacketType::factory();
73 }
74
75 template <class KeyType>
76 template <class PacketType>
77 prefix_ std::string senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::name()
78     const
79 {
80     return prettyName(typeid(PacketType));
81 }
82
83 template <class KeyType>
84 template <class PacketType>
85 prefix_ std::type_info const &
86 senf::detail::PacketRegistryImpl<KeyType>::EntryImpl<PacketType>::type()
87     const
88 {
89     return typeid(PacketType);
90 }
91
92 //-/////////////////////////////////////////////////////////////////////////////////////////////////
93 // senf::detail::PacketRegistryImpl<KeyType>:
94
95 template <class KeyType>
96 template <class PacketType>
97 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::registerPacket(key_t key, int priority)
98 {
99     SENF_ASSERT_EXPRESSION(
100         registry_.insert(
101             typename Entry::ptr(new EntryImpl<PacketType>(key,priority))).second,
102         "Duplicate packet registration");
103 }
104
105 template <class KeyType>
106 template <class PacketType>
107 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::unregisterPacket()
108 {
109     registry_.template get<ByType>().erase(typeid(PacketType));
110 }
111
112 template <class KeyType>
113 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::unregisterPacket(key_t key, int priority)
114 {
115     // Why doesn't this work:
116     // registry_.erase(boost::make_tuple(key,priority));
117     typename Registry::iterator i (registry_.find(boost::make_tuple(key,priority)));
118     if (i != registry_.end())
119         registry_.erase(i);
120 }
121
122 template <class KeyType>
123 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::key_t
124 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type)
125 {
126     boost::optional<KeyType> k (key(type,true));
127     if (! k)
128         throw PacketTypeNotRegisteredException();
129     return *k;
130 }
131
132 template <class KeyType>
133 prefix_ boost::optional<typename senf::detail::PacketRegistryImpl<KeyType>::key_t>
134 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type, bool)
135 {
136     typedef typename Registry::template index<ByType>::type TypeIndex;
137     TypeIndex const & typeIndex (registry_.template get<ByType>());
138     typename TypeIndex::const_iterator i (typeIndex.find(type.id()));
139     if (i == typeIndex.end())
140         return boost::optional<key_t>();
141     return (*i)->key;
142 }
143
144 template <class KeyType>
145 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const &
146 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key)
147 {
148     Entry const * e (lookup(key, true));
149     if (!e)
150         throw PacketTypeNotRegisteredException();
151     return *e;
152 }
153
154 template <class KeyType>
155 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const *
156 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key, bool)
157 {
158     typedef typename Registry::template index<ByKey>::type KeyIndex;
159     KeyIndex const & keyIndex (registry_.template get<ByKey>());
160     typename KeyIndex::const_iterator i (keyIndex.lower_bound(key));
161     if (i == keyIndex.end() || (*i)->key != key)
162         return 0;
163     return i->get();
164 }
165
166 template <class KeyType>
167 prefix_ bool senf::detail::PacketRegistryImpl<KeyType>::v_empty()
168     const
169 {
170     return registry_.empty();
171 }
172
173 template <class KeyType>
174 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_dump(std::ostream & os)
175     const
176 {
177     typedef typename Registry::template index<ByKey>::type KeyIndex;
178     KeyIndex const & keyIndex (registry_.template get<ByKey>());
179     for (typename KeyIndex::iterator i (keyIndex.begin()), i_end (keyIndex.end());
180          i != i_end; ++i) {
181         std::string n ((*i)->name());
182         senf::detail::DumpKey<KeyType>::dump((*i)->key, os);
183         os << ' ' << std::setw(6) << (*i)->priority << ' ' << n.substr(21,n.size()-22) << '\n';
184     }
185 }
186
187 template <class KeyType>
188 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::v_clear()
189 {
190     registry_.clear();
191 }
192
193 //-/////////////////////////////////////////////////////////////////////////////////////////////////
194 // senf::detail::DumpKey<KeyType,is_integral>
195
196 template <class KeyType, bool is_integral>
197 prefix_ void senf::detail::DumpKey<KeyType,is_integral>::dump(KeyType const & v,
198                                                               std::ostream & os)
199 {
200     os << "  " << std::setw(16) << std::left << v << std::setw(0) << std::right;
201 }
202
203 // senf::detail::DumpKey<KeyType, true>
204
205 template <class KeyType>
206 prefix_ void senf::detail::DumpKey<KeyType, true>::dump(KeyType const & v, std::ostream & os)
207 {
208     os << "  " << senf::format::dumpint(v);
209 }
210
211 //-/////////////////////////////////////////////////////////////////////////////////////////////////
212 #undef prefix_
213
214 \f
215 // Local Variables:
216 // mode: c++
217 // fill-column: 100
218 // c-file-style: "senf"
219 // indent-tabs-mode: nil
220 // ispell-local-dictionary: "american"
221 // compile-command: "scons -u test"
222 // comment-column: 40
223 // End: