NEW FILE HEADER / COPYRIGHT FORMAT
[senf.git] / Packets / PacketType.cti
1 // $Id$
2 //
3 // Copyright (C) 2007 
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 PacketType inline template implementation */
25
26 //#include "PacketType.ih"
27
28 // Custom includes
29 #include <boost/utility.hpp>
30 #include "PacketRegistry.hh"
31
32 #define prefix_ inline
33 ///////////////////////////////cti.p///////////////////////////////////////
34
35 ///////////////////////////////////////////////////////////////////////////
36 // senf::PacketTypeBase
37
38 template <class PacketType>
39 prefix_ senf::PacketTypeBase::factory_t senf::PacketTypeBase::factory()
40 {
41     return PacketInterpreter<PacketType>::factory();
42 }
43
44 ///////////////////////////////////////////////////////////////////////////
45 // senf::PacketTypeMixin<Self,Registry>
46
47 template <class Self, class Registry>
48 prefix_ senf::PacketInterpreterBase::optional_range
49 senf::PacketTypeMixin<Self,Registry>::nextPacketRange(Packet p)
50 {
51     // Call the member defined in the specialization below
52     return PacketTypeMixin<Self>::nextPacketRange(p);
53 }
54
55 template <class Self, class Registry>
56 prefix_ senf::PacketInterpreterBase::factory_t
57 senf::PacketTypeMixin<Self,Registry>::nextPacketType(Packet p)
58 {
59     if (p.data().size() < Self::initSize())
60         return Self::no_factory();
61     PkReg_Entry const * e (PacketRegistry<Registry>::lookup( 
62                                Self::nextPacketKey(p.as< ConcretePacket<Self> >()), nothrow));
63     return e ? e->factory() : PacketTypeBase::no_factory();
64 }
65
66 template <class Self, class Registry>
67 prefix_ senf::PacketInterpreterBase::size_type senf::PacketTypeMixin<Self,Registry>::initSize()
68 {
69     return senf::init_bytes< typename Self::parser >::value;
70 }
71
72 template <class Self, class Registry>
73 prefix_ void senf::PacketTypeMixin<Self,Registry>::init(Packet p)
74 {
75     p.as< ConcretePacket<Self> >()->init();
76 }
77
78 template <class Self, class Registry>
79 prefix_ typename senf::PacketTypeMixin<Self,Registry>::optional_registry_key_t
80 senf::PacketTypeMixin<Self,Registry>::key(Packet p)
81 {
82     return p ? PacketRegistry<Registry>::key(p, nothrow) : optional_registry_key_t();
83 }
84
85 ///////////////////////////////////////////////////////////////////////////
86 // senf::PacketTypeMixin<Self,void>
87
88 template <class Self>
89 prefix_ senf::PacketInterpreterBase::optional_range
90 senf::PacketTypeMixin<Self,void>::nextPacketRange(Packet p)
91 {
92     if (p.data().size() < Self::initSize())
93         return PacketTypeBase::no_range();
94     typename Self::size_type sz (Self::initHeadSize());
95     ///\idea This if condition could be replaced with a compile time switch by checking, wether
96     /// (the function address) Self::initHeadSize is different from PacketTypeBase::initHeadSize
97     if (sz == PacketTypeBase::size_type(-1))
98         return PacketTypeBase::range(boost::next(p.data().begin(),Self::initSize()),
99                                      p.data().end());
100     else
101         return PacketTypeBase::range(boost::next(p.data().begin(),sz),
102                                      boost::prior(p.data().end(),Self::initSize()-sz));
103 }
104
105 template <class Self>
106 prefix_ senf::PacketInterpreterBase::size_type senf::PacketTypeMixin<Self,void>::initSize()
107 {
108     return senf::init_bytes< typename Self::parser >::value;
109 }
110
111 template <class Self>
112 prefix_ void senf::PacketTypeMixin<Self,void>::init(Packet p)
113 {
114     p.as< ConcretePacket<Self> >()->init();
115 }
116
117 ///////////////////////////////cti.e///////////////////////////////////////
118 #undef prefix_
119
120 \f
121 // Local Variables:
122 // mode: c++
123 // fill-column: 100
124 // c-file-style: "senf"
125 // indent-tabs-mode: nil
126 // ispell-local-dictionary: "american"
127 // compile-command: "scons -u test"
128 // comment-column: 40
129 // End: