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