Fix documentation build under maverick (doxygen 1.7.1)
[senf.git] / senf / Packets / GenericTLV.ct
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Thorsten Horstmann <tho@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 GenericTLV non-inline template implementation  */
25
26 //#include "GenericTLV.ih"
27
28 // Custom includes
29 #include <boost/io/ios_state.hpp>
30 #include <senf/Utils/hexdump.hh>
31 #include <senf/Utils/TypeInfo.hh>
32 #include <senf/Utils/Format.hh>
33
34 #define prefix_
35 //-/////////////////////////////////////////////////////////////////////////////////////////////////
36
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38 // senf::GenericTLVParserBase<Base>
39
40 template <class Base>
41 template <class Parser>
42 prefix_ Parser senf::GenericTLVParserBase<Base>::init()
43 {
44     senf::PacketParserBase::size_type oldSize (bytes() );
45     senf::PacketParserBase::size_type newParserSize ( senf::init_bytes<Parser>::value );
46     this->resize(  oldSize, newParserSize);
47     std::fill(this->i(), boost::next(this->i(), newParserSize), 0u);
48     Parser concreteParser = Parser(this->i(), this->state() );
49     concreteParser.init();
50 //    concreteParser.length() = newParserSize - senf::init_bytes<Base>::value;
51     return concreteParser;
52 }
53
54 template <class Base>
55 prefix_  void senf::GenericTLVParserBase<Base>::dump(std::ostream & os)
56     const
57 {
58     if (Base::Registry::instance().isRegistered( *this)) {
59         Base::Registry::instance().dump( *this, os);
60     } else {
61         boost::io::ios_all_saver ias(os);
62         os << "  GenericTLVParser<" << prettyName(typeid(Base)) << ">\n"
63            << "    type:   " << senf::format::dumpint(this->type()) << "\n"
64            << "    length: " << senf::format::dumpint(this->length()) << "\n"
65            << "    value:\n";
66         hexdump(value().begin(), value().end(), os);
67     }
68 }
69
70 template <class Base>
71 prefix_  senf::PacketInterpreterBase::range senf::GenericTLVParserBase<Base>::value()
72     const
73 {
74     senf::PacketData::iterator begin ( boost::next(this->i(), senf::bytes( self())) );
75     return PacketInterpreterBase::range(begin, boost::next( begin, this->length()) );
76 }
77
78 template <class Base>
79 template <class ForwardReadableRange>
80 prefix_ void senf::GenericTLVParserBase<Base>::value_(ForwardReadableRange const &range)
81 {
82     //typename boost::range_difference<ForwardReadableRange>::type rangeSize ( boost::size(range));
83     unsigned rangeSize ( boost::size(range));
84     if ( rangeSize != this->length() )
85         resize( bytes(), rangeSize + senf::bytes(self()) );
86     std::copy( boost::begin(range), boost::end(range), boost::next(
87             this->i(), senf::bytes( self())) );
88     this->length_() = rangeSize;
89 }
90
91
92 //-/////////////////////////////////////////////////////////////////////////////////////////////////
93 // senf::GenericTLVParserRegistry<BaseParser,Keytype>
94
95 template <class BaseParser, class Keytype>
96 template <typename Parser>
97 prefix_ void senf::GenericTLVParserRegistry<BaseParser,Keytype>::registerParser()
98 {
99     Keytype key (Parser::typeId+0);
100     typename Map::iterator i (map_.find( key ));
101     if (i == map_.end() )
102         map_.insert(key, new detail::GenericTLVParserRegistry_Entry<BaseParser, Parser>() );
103 }
104
105 // Wow ... this is stupid .. Boost 1.33 ptr_map API is broken ...
106
107 #if BOOST_VERSION < 103400
108 #define PTRMAP_GET_CONTENTS(v) (v)
109 #else
110 #define PTRMAP_GET_CONTENTS(v) (*(v).second)
111 #endif
112
113 template <class BaseParser, class Keytype>
114 prefix_ void senf::GenericTLVParserRegistry<BaseParser,Keytype>::dump(
115         GenericTLVParser const & parser, std::ostream & os)
116     const
117 {
118     typename Map::const_iterator i (map_.find( parser.type()));
119     if (i != map_.end())
120         PTRMAP_GET_CONTENTS(*i).dump(parser, os);
121 }
122
123 template <class BaseParser, class Keytype>
124 prefix_ void senf::GenericTLVParserRegistry<BaseParser,Keytype>::dump(
125         GenericTLVParser const & parser, Keytype const & key, std::ostream & os)
126     const
127 {
128     typename Map::const_iterator i (map_.find( key));
129     if (i != map_.end())
130         PTRMAP_GET_CONTENTS(*i).dump(parser, os);
131 }
132
133 template <class BaseParser, class Keytype>
134 prefix_ senf::PacketParserBase::size_type senf::GenericTLVParserRegistry<BaseParser,Keytype>::bytes(
135         GenericTLVParser const & parser)
136     const
137 {
138     typename Map::const_iterator i (map_.find( parser.type()));
139     if (i != map_.end())
140         return PTRMAP_GET_CONTENTS(*i).bytes(parser);
141     else
142         throw TLVParserNotRegisteredException();
143 }
144
145 template <class BaseParser, class Keytype>
146 prefix_ senf::PacketParserBase::size_type senf::GenericTLVParserRegistry<BaseParser,Keytype>::bytes(
147         GenericTLVParser const & parser, Keytype const & key)
148     const
149 {
150     typename Map::const_iterator i (map_.find( key));
151     if (i != map_.end())
152         return PTRMAP_GET_CONTENTS(*i).bytes(parser);
153     else
154         throw TLVParserNotRegisteredException();
155 }
156
157 #undef PTRMAP_GET_CONTENTS
158
159 //-/////////////////////////////////////////////////////////////////////////////////////////////////
160 #undef prefix_
161
162 \f
163 // Local Variables:
164 // mode: c++
165 // fill-column: 100
166 // comment-column: 40
167 // c-file-style: "senf"
168 // indent-tabs-mode: nil
169 // ispell-local-dictionary: "american"
170 // compile-command: "scons -u test"
171 // End: