PPI: Checkin of first compiling (yet not working) version
[senf.git] / Packets / PacketRegistry.ct
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.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     bool isUnique (
46         registry_.insert(
47             std::make_pair(key, Entry_ptr(new detail::PkReg_EntryImpl<PacketType>()))).second);
48     // If this assertion fails, a Packet was registered with an already known key
49     BOOST_ASSERT( isUnique );
50     bool isNew (
51         reverseRegistry_.insert(
52             std::make_pair(senf::typeIdValue<PacketType>(), key)).second);
53     // If this assertion fails, the same Packet was registered with two different keys
54     BOOST_ASSERT( isNew );
55 }
56
57 template <class KeyType>
58 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::key_t
59 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type)
60 {
61     typename ReversePacketMap::iterator i (reverseRegistry_.find(type));
62     if (i==reverseRegistry_.end())
63         throw PacketTypeNotRegisteredException();
64     return i->second;
65 }
66
67 template <class KeyType>
68 prefix_ boost::optional<typename senf::detail::PacketRegistryImpl<KeyType>::key_t>
69 senf::detail::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type, bool)
70 {
71     typename ReversePacketMap::iterator i (reverseRegistry_.find(type));
72     if (i==reverseRegistry_.end())
73         return boost::optional<key_t>();
74     return i->second;
75 }
76
77 template <class KeyType>
78 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const &
79 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key)
80 {
81     typename PacketMap::iterator i (registry_.find(key));
82     if (i==registry_.end())
83         throw PacketTypeNotRegisteredException();
84     return *(i->second);
85 }
86
87 template <class KeyType>
88 prefix_ typename senf::detail::PacketRegistryImpl<KeyType>::Entry const *
89 senf::detail::PacketRegistryImpl<KeyType>::lookup(key_t key, bool)
90 {
91     typename PacketMap::iterator i (registry_.find(key));
92     if (i==registry_.end())
93         return 0;
94     return i->second.get();
95 }
96
97 template <class Tag>
98 prefix_ typename senf::PacketRegistry<Tag>::Registry &
99 senf::PacketRegistry<Tag>::registry()
100 {
101     static Registry registry;
102     return registry;
103 }
104
105 ///////////////////////////////ct.e////////////////////////////////////////
106 #undef prefix_
107
108 \f
109 // Local Variables:
110 // mode: c++
111 // fill-column: 100
112 // c-file-style: "senf"
113 // indent-tabs-mode: nil
114 // ispell-local-dictionary: "american"
115 // compile-command: "scons -u test"
116 // comment-column: 40
117 // End: