9c364318ca5fd6636301da65e329bddbf66367ba
[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 ///////////////////////////////ct.p////////////////////////////////////////
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_  senf::PacketInterpreterBase::range senf::GenericTLVParserBase<Base>::value() 
56     const 
57 {
58     senf::PacketData::iterator begin ( boost::next(this->i(), senf::bytes( self())) );
59     return PacketInterpreterBase::range(begin, boost::next( begin, this->length()) );
60 }
61
62 template <class Base>
63 prefix_  void senf::GenericTLVParserBase<Base>::dump(std::ostream & os)
64     const 
65 {
66     boost::io::ios_all_saver ias(os);
67     os << "GenericTLVParser<" << prettyName(typeid(Base)) << ">\n"
68        << "  type:   " << senf::format::dumpint(this->type()) << "\n"
69        << "  length: " << senf::format::dumpint(this->length()) << "\n"
70        << "  value:\n";
71     hexdump(value().begin(), value().end(), os);
72 }
73
74 template <class Base>
75 template <class ForwardReadableRange>
76 prefix_ void senf::GenericTLVParserBase<Base>::value_(ForwardReadableRange const &range)
77 {
78     //typename boost::range_difference<ForwardReadableRange>::type rangeSize ( boost::size(range));
79     unsigned rangeSize ( boost::size(range));
80     if ( rangeSize != this->length() )
81         resize( bytes(), rangeSize + senf::bytes(self()) );
82     std::copy( boost::begin(range), boost::end(range), boost::next(
83             this->i(), senf::bytes( self())) );
84     this->length_() = rangeSize;
85 }
86
87
88 ///////////////////////////////////////////////////////////////////////////
89 // senf::GenericTLVParserRegistry<BaseParser>
90
91 template <class BaseParser>
92 template <typename Parser>
93 prefix_ void senf::GenericTLVParserRegistry<BaseParser>::registerParser()
94 {
95     typename Map::iterator i (map_.find( Parser::typeId ));
96     if (i == map_.end() ) {
97         typename BaseParser::type_t::value_type k (Parser::typeId);
98         map_.insert(k , new detail::GenericTLVParserRegistry_Entry<BaseParser, Parser>() );
99     }
100 }
101
102 template <class BaseParser>
103 prefix_ void senf::GenericTLVParserRegistry<BaseParser>::dump(
104         std::ostream & os, GenericTLVParserBase<BaseParser> const & parser)
105 {
106     typename Map::iterator i (map_.find( parser.type()));
107     if (i == map_.end())
108         parser.dump(os);
109     else
110         (i->second)->dump(os, parser);
111 }
112
113 ///////////////////////////////ct.e////////////////////////////////////////
114 #undef prefix_
115
116 \f
117 // Local Variables:
118 // mode: c++
119 // fill-column: 100
120 // comment-column: 40
121 // c-file-style: "senf"
122 // indent-tabs-mode: nil
123 // ispell-local-dictionary: "american"
124 // compile-command: "scons -u test"
125 // End: