2cd02f8da7bea2152c8a0d396eafcf4f3d54a56a
[senf.git] / 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 "../Utils/senfassert.hh"
30 #include <iostream>
31 #include "../Utils/TypeInfo.hh"
32
33 #define prefix_
34 ///////////////////////////////ct.p////////////////////////////////////////
35
36 template <class PacketType>
37 prefix_ senf::PacketInterpreterBase::factory_t senf::detail::PkReg_EntryImpl<PacketType>::factory()
38     const
39 {
40     return PacketType::factory();
41 }
42
43 template <class PacketType>
44 prefix_ std::string senf::detail::PkReg_EntryImpl<PacketType>::name()
45     const
46 {
47     return prettyName(typeid(PacketType));
48 }
49
50 template <class KeyType>
51 template <class PacketType>
52 prefix_ void senf::detail::PacketRegistryImpl<KeyType>::registerPacket(key_t key)
53 {
54 #ifdef SENF_NO_DEBUG
55     registry_.insert(std::make_pair(key, Entry_ptr(new detail::PkReg_EntryImpl<PacketType>())));
56     reverseRegistry_.insert(std::make_pair(senf::typeIdValue<PacketType>(), key));
57 #else
58     bool isUnique (
59         registry_.insert(
60             std::make_pair(key, Entry_ptr(new detail::PkReg_EntryImpl<PacketType>()))).second);
61     // If this assertion fails, a Packet was registered with an already known key
62     SENF_ASSERT( isUnique );
63     bool isNew (
64         reverseRegistry_.insert(
65             std::make_pair(senf::typeIdValue<PacketType>(), key)).second);
66     // If this assertion fails, the same Packet was registered with two different keys
67     SENF_ASSERT( isNew );
68 #endif
69 }
70
71 template <class KeyType>
72 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::key_t
73 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type)
74 {
75     typename ReversePacketMap::iterator i (reverseRegistry_.find(type));
76     if (i==reverseRegistry_.end())
77         throw PacketTypeNotRegisteredException();
78     return i->second;
79 }
80
81 template <class KeyType>
82 prefix_ boost::optional<typename senf::detail::PacketRegistryImpl<KeyType>::key_t>
83 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type, bool)
84 {
85     typename ReversePacketMap::iterator i (reverseRegistry_.find(type));
86     if (i==reverseRegistry_.end())
87         return boost::optional<key_t>();
88     return i->second;
89 }
90
91 template <class KeyType>
92 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const &
93 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key)
94 {
95     typename PacketMap::iterator i (registry_.find(key));
96     if (i==registry_.end())
97         throw PacketTypeNotRegisteredException();
98     return *(i->second);
99 }
100
101 template <class KeyType>
102 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const *
103 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key, bool)
104 {
105     typename PacketMap::iterator i (registry_.find(key));
106     if (i==registry_.end())
107         return 0;
108     return i->second.get();
109 }
110
111 template <class Tag>
112 prefix_ typename senf::PacketRegistry<Tag>::Registry &
113 senf::PacketRegistry<Tag>::registry()
114 {
115     static Registry registry;
116     return registry;
117 }
118
119 ///////////////////////////////ct.e////////////////////////////////////////
120 #undef prefix_
121
122 \f
123 // Local Variables:
124 // mode: c++
125 // fill-column: 100
126 // c-file-style: "senf"
127 // indent-tabs-mode: nil
128 // ispell-local-dictionary: "american"
129 // compile-command: "scons -u test"
130 // comment-column: 40
131 // End: