2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
15 \brief GenericTLV non-inline template implementation */
17 //#include "GenericTLV.ih"
20 #include <senf/Utils/hexdump.hh>
21 #include <senf/Utils/TypeInfo.hh>
22 #include <senf/Utils/Format.hh>
25 //-/////////////////////////////////////////////////////////////////////////////////////////////////
27 //-/////////////////////////////////////////////////////////////////////////////////////////////////
28 // senf::GenericTLVParserBase<Base>
31 template <class Parser>
32 prefix_ Parser senf::GenericTLVParserBase<Base>::init()
34 PacketParserBase::size_type oldSize (bytes());
35 PacketParserBase::size_type newParserSize (senf::init_bytes<Parser>::value);
36 this->resize(oldSize, newParserSize);
37 std::fill(this->i(), boost::next(this->i(), newParserSize), 0u);
38 Parser concreteParser = Parser(this->i(), this->state() );
39 concreteParser.init();
40 // concreteParser.length() = newParserSize - senf::init_bytes<Base>::value;
41 return concreteParser;
45 prefix_ void senf::GenericTLVParserBase<Base>::dump(std::ostream & os)
48 if (Base::Registry::instance().isRegistered(*this)) {
49 Base::Registry::instance().dump(*this, os);
51 os << " GenericTLVParser<" << prettyName(typeid(Base)) << ">\n"
52 << " type: " << format::dumpint(this->type()) << "\n"
53 << " length: " << format::dumpint(this->length()) << "\n"
55 hexdump(value().begin(), value().end(), os);
60 prefix_ senf::PacketInterpreterBase::range senf::GenericTLVParserBase<Base>::value()
63 PacketData::iterator begin (boost::next(this->i(), senf::bytes( self())) );
64 return PacketInterpreterBase::range(begin, boost::next( begin, this->length()) );
68 template <class ForwardReadableRange>
69 prefix_ void senf::GenericTLVParserBase<Base>::value_(ForwardReadableRange const &range)
71 //typename boost::range_difference<ForwardReadableRange>::type rangeSize ( boost::size(range));
72 unsigned rangeSize (boost::size(range));
73 if (rangeSize != this->length())
74 Base::resize( bytes(), rangeSize + senf::bytes(self()) );
75 std::copy( boost::begin(range), boost::end(range), boost::next(
76 this->i(), senf::bytes(self())) );
77 this->length_() = rangeSize;
81 //-/////////////////////////////////////////////////////////////////////////////////////////////////
82 // senf::GenericTLVParserRegistry<BaseParser,Keytype>
84 template <class BaseParser, class Keytype>
85 template <typename Parser>
86 prefix_ void senf::GenericTLVParserRegistry<BaseParser,Keytype>::registerParser()
88 Keytype key (Parser::typeId+0);
89 SENF_ASSERT_EXPRESSION(
90 map_.insert( key, new detail::GenericTLVParserRegistry_Entry<BaseParser, Parser>()).second,
91 "Duplicate TLV registration");
94 template <class BaseParser, class Keytype>
95 prefix_ void senf::GenericTLVParserRegistry<BaseParser,Keytype>::dump(
96 GenericTLVParser const & parser, std::ostream & os)
99 typename Map::const_iterator i (map_.find( parser.type()));
101 i->second->dump(parser, os);
104 template <class BaseParser, class Keytype>
105 prefix_ void senf::GenericTLVParserRegistry<BaseParser,Keytype>::dump(
106 GenericTLVParser const & parser, Keytype const & key, std::ostream & os)
109 typename Map::const_iterator i (map_.find( key));
111 i->second->dump(parser, os);
114 template <class BaseParser, class Keytype>
115 prefix_ senf::PacketParserBase::size_type senf::GenericTLVParserRegistry<BaseParser,Keytype>::bytes(
116 GenericTLVParser const & parser)
119 typename Map::const_iterator i (map_.find( parser.type()));
121 return i->second->bytes(parser);
123 throw TLVParserNotRegisteredException();
126 template <class BaseParser, class Keytype>
127 prefix_ senf::PacketParserBase::size_type senf::GenericTLVParserRegistry<BaseParser,Keytype>::bytes(
128 GenericTLVParser const & parser, Keytype const & key)
131 typename Map::const_iterator i (map_.find( key));
133 return i->second->bytes(parser);
135 throw TLVParserNotRegisteredException();
138 template <class BaseParser, class Keytype>
139 prefix_ typename senf::GenericTLVParserRegistry<BaseParser,Keytype>::const_iterator
140 senf::GenericTLVParserRegistry<BaseParser,Keytype>::begin()
146 template <class BaseParser, class Keytype>
147 prefix_ typename senf::GenericTLVParserRegistry<BaseParser,Keytype>::const_iterator
148 senf::GenericTLVParserRegistry<BaseParser,Keytype>::end()
154 //-/////////////////////////////////////////////////////////////////////////////////////////////////
161 // comment-column: 40
162 // c-file-style: "senf"
163 // indent-tabs-mode: nil
164 // ispell-local-dictionary: "american"
165 // compile-command: "scons -u test"