Utils/Logger: Complete unit testing
[senf.git] / Packets / PacketType.cti
1 // Copyright (C) 2007 
2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 //     Stefan Bund <g0dil@berlios.de>
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
21 /** \file
22     \brief PacketType inline template implementation */
23
24 //#include "PacketType.ih"
25
26 // Custom includes
27 #include <boost/utility.hpp>
28 #include "PacketRegistry.hh"
29
30 #define prefix_ inline
31 ///////////////////////////////cti.p///////////////////////////////////////
32
33 ///////////////////////////////////////////////////////////////////////////
34 // senf::PacketTypeBase
35
36 template <class PacketType>
37 prefix_ senf::PacketTypeBase::factory_t senf::PacketTypeBase::factory()
38 {
39     return PacketInterpreter<PacketType>::factory();
40 }
41
42 ///////////////////////////////////////////////////////////////////////////
43 // senf::PacketTypeMixin<Self,Registry>
44
45 template <class Self, class Registry>
46 prefix_ senf::PacketInterpreterBase::optional_range
47 senf::PacketTypeMixin<Self,Registry>::nextPacketRange(Packet p)
48 {
49     // Call the member defined in the specialization below
50     return PacketTypeMixin<Self>::nextPacketRange(p);
51 }
52
53 template <class Self, class Registry>
54 prefix_ senf::PacketInterpreterBase::factory_t
55 senf::PacketTypeMixin<Self,Registry>::nextPacketType(Packet p)
56 {
57     if (p.data().size() < Self::initSize())
58         return Self::no_factory();
59     PkReg_Entry const * e (PacketRegistry<Registry>::lookup( 
60                                Self::nextPacketKey(p.as< ConcretePacket<Self> >()), nothrow));
61     return e ? e->factory() : PacketTypeBase::no_factory();
62 }
63
64 template <class Self, class Registry>
65 prefix_ senf::PacketInterpreterBase::size_type senf::PacketTypeMixin<Self,Registry>::initSize()
66 {
67     return senf::init_bytes< typename Self::parser >::value;
68 }
69
70 template <class Self, class Registry>
71 prefix_ void senf::PacketTypeMixin<Self,Registry>::init(Packet p)
72 {
73     p.as< ConcretePacket<Self> >()->init();
74 }
75
76 template <class Self, class Registry>
77 prefix_ typename senf::PacketTypeMixin<Self,Registry>::optional_registry_key_t
78 senf::PacketTypeMixin<Self,Registry>::key(Packet p)
79 {
80     return p ? PacketRegistry<Registry>::key(p, nothrow) : optional_registry_key_t();
81 }
82
83 ///////////////////////////////////////////////////////////////////////////
84 // senf::PacketTypeMixin<Self,void>
85
86 template <class Self>
87 prefix_ senf::PacketInterpreterBase::optional_range
88 senf::PacketTypeMixin<Self,void>::nextPacketRange(Packet p)
89 {
90     if (p.data().size() < Self::initSize())
91         return PacketTypeBase::no_range();
92     typename Self::size_type sz (Self::initHeadSize());
93     ///\idea This if condition could be replaced with a compile time switch by checking, wether
94     /// (the function address) Self::initHeadSize is different from PacketTypeBase::initHeadSize
95     if (sz == PacketTypeBase::size_type(-1))
96         return PacketTypeBase::range(boost::next(p.data().begin(),Self::initSize()),
97                                      p.data().end());
98     else
99         return PacketTypeBase::range(boost::next(p.data().begin(),sz),
100                                      boost::prior(p.data().end(),Self::initSize()-sz));
101 }
102
103 template <class Self>
104 prefix_ senf::PacketInterpreterBase::size_type senf::PacketTypeMixin<Self,void>::initSize()
105 {
106     return senf::init_bytes< typename Self::parser >::value;
107 }
108
109 template <class Self>
110 prefix_ void senf::PacketTypeMixin<Self,void>::init(Packet p)
111 {
112     p.as< ConcretePacket<Self> >()->init();
113 }
114
115 ///////////////////////////////cti.e///////////////////////////////////////
116 #undef prefix_
117
118 \f
119 // Local Variables:
120 // mode: c++
121 // fill-column: 100
122 // c-file-style: "senf"
123 // indent-tabs-mode: nil
124 // ispell-local-dictionary: "american"
125 // compile-command: "scons -u test"
126 // comment-column: 40
127 // End: