ce424fa66267982b20a3e910f9616006918a06f5
[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 // Definition of non-inline template funPacketRegistry.ons
24
25 #include "PacketRegistry.ih"
26
27 // Custom includes
28 #include <iostream>
29 #include "Utils/TypeInfo.hh"
30
31 #define prefix_
32 ///////////////////////////////ct.p////////////////////////////////////////
33
34 template <class KeyType>
35 template <class OtherPacket>
36 prefix_ void senf::impl::PacketRegistryImpl<KeyType>::registerPacket(key_t key)
37 {
38     // If this assertion fails, a Packet was registered with an already known key
39     BOOST_ASSERT( registry_.insert(std::make_pair(
40         key, Entry_ptr(new impl::PkReg_EntryImpl<OtherPacket>()))).second );
41     // If this assertion fails, the same Packet was registered with two different keys
42     BOOST_ASSERT( reverseRegistry_.insert(
43         std::make_pair(senf::typeIdValue<OtherPacket>(), key)).second );
44 }
45
46 template <class KeyType>
47 prefix_ typename senf::impl::PacketRegistryImpl<KeyType>::key_t
48 senf::impl::PacketRegistryImpl<KeyType>::key(senf::TypeIdValue const & type)
49 {
50     typename ReversePacketMap::iterator i (reverseRegistry_.find(type));
51     if (i==reverseRegistry_.end())
52         throw PacketTypeNotRegistered();
53     return i->second;
54 }
55
56 template <class OtherPacket>
57 prefix_ void
58 senf::impl::PkReg_EntryImpl<OtherPacket>::registerInterpreter(Packet const * p,
59                                                                      Packet::iterator b,
60                                                                      Packet::iterator e)
61 {
62     p->template registerInterpreter<OtherPacket>(b,e);
63 }
64
65 template <class OtherPacket>
66 prefix_ senf::Packet::ptr
67 senf::impl::PkReg_EntryImpl<OtherPacket>::reinterpret(Packet * p)
68 {
69     return p->template reinterpret<OtherPacket>();
70 }
71
72 template <class KeyType>
73 prefix_ typename senf::impl::PacketRegistryImpl<KeyType>::Entry *
74 senf::impl::PacketRegistryImpl<KeyType>::lookup(key_t key)
75 {
76     typename PacketMap::iterator i (registry_.find(key));
77     if (i==registry_.end())
78         return &impl::pkreg_dataEntry;
79     return i->second.get();
80 }
81
82 template <class Tag>
83 template <class InputIterator>
84 prefix_ senf::Packet::ptr
85 senf::PacketRegistry<Tag>::create(typename Tag::key_t key, InputIterator b,
86                                          InputIterator e)
87 {
88     Packet::ptr p (Packet::create<DataPacket>(b,e));
89     return registry().lookup(key)->reinterpret(p.get());
90 }
91
92 template <class Tag>
93 prefix_ typename senf::PacketRegistry<Tag>::Registry &
94 senf::PacketRegistry<Tag>::registry()
95 {
96     static Registry registry;
97     return registry;
98 }
99
100 ///////////////////////////////ct.e////////////////////////////////////////
101 #undef prefix_
102
103 \f
104 // Local Variables:
105 // mode: c++
106 // c-file-style: "senf"
107 // End: