GenericTLV.ct
Go to the documentation of this file.
1 //
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
6 //
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
11 //
12 
13 
14 /** \file
15  \brief GenericTLV non-inline template implementation */
16 
17 //#include "GenericTLV.ih"
18 
19 // Custom includes
20 #include <senf/Utils/hexdump.hh>
21 #include <senf/Utils/TypeInfo.hh>
22 #include <senf/Utils/Format.hh>
23 
24 #define prefix_
25 //-/////////////////////////////////////////////////////////////////////////////////////////////////
26 
27 //-/////////////////////////////////////////////////////////////////////////////////////////////////
28 // senf::GenericTLVParserBase<Base>
29 
30 template <class Base>
31 template <class Parser>
32 prefix_ Parser senf::GenericTLVParserBase<Base>::init()
33 {
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;
42 }
43 
44 template <class Base>
45 prefix_ void senf::GenericTLVParserBase<Base>::dump(std::ostream & os)
46  const
47 {
48  if (Base::Registry::instance().isRegistered(*this)) {
49  Base::Registry::instance().dump(*this, os);
50  } else {
51  os << " GenericTLVParser<" << prettyName(typeid(Base)) << ">\n"
52  << " type: " << format::dumpint(this->type()) << "\n"
53  << " length: " << format::dumpint(this->length()) << "\n"
54  << " value:\n";
55  hexdump(value().begin(), value().end(), os);
56  }
57 }
58 
59 template <class Base>
60 prefix_ senf::PacketInterpreterBase::range senf::GenericTLVParserBase<Base>::value()
61  const
62 {
63  PacketData::iterator begin (boost::next(this->i(), senf::bytes( self())) );
64  return PacketInterpreterBase::range(begin, boost::next( begin, this->length()) );
65 }
66 
67 template <class Base>
68 template <class ForwardReadableRange>
69 prefix_ void senf::GenericTLVParserBase<Base>::value_(ForwardReadableRange const &range)
70 {
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;
78 }
79 
80 
81 //-/////////////////////////////////////////////////////////////////////////////////////////////////
82 // senf::GenericTLVParserRegistry<BaseParser,Keytype>
83 
84 template <class BaseParser, class Keytype>
85 template <typename Parser>
86 prefix_ void senf::GenericTLVParserRegistry<BaseParser,Keytype>::registerParser()
87 {
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");
92 }
93 
94 template <class BaseParser, class Keytype>
95 prefix_ void senf::GenericTLVParserRegistry<BaseParser,Keytype>::dump(
96  GenericTLVParser const & parser, std::ostream & os)
97  const
98 {
99  typename Map::const_iterator i (map_.find( parser.type()));
100  if (i != map_.end())
101  i->second->dump(parser, os);
102 }
103 
104 template <class BaseParser, class Keytype>
105 prefix_ void senf::GenericTLVParserRegistry<BaseParser,Keytype>::dump(
106  GenericTLVParser const & parser, Keytype const & key, std::ostream & os)
107  const
108 {
109  typename Map::const_iterator i (map_.find( key));
110  if (i != map_.end())
111  i->second->dump(parser, os);
112 }
113 
114 template <class BaseParser, class Keytype>
115 prefix_ senf::PacketParserBase::size_type senf::GenericTLVParserRegistry<BaseParser,Keytype>::bytes(
116  GenericTLVParser const & parser)
117  const
118 {
119  typename Map::const_iterator i (map_.find( parser.type()));
120  if (i != map_.end())
121  return i->second->bytes(parser);
122  else
123  throw TLVParserNotRegisteredException();
124 }
125 
126 template <class BaseParser, class Keytype>
127 prefix_ senf::PacketParserBase::size_type senf::GenericTLVParserRegistry<BaseParser,Keytype>::bytes(
128  GenericTLVParser const & parser, Keytype const & key)
129  const
130 {
131  typename Map::const_iterator i (map_.find( key));
132  if (i != map_.end())
133  return i->second->bytes(parser);
134  else
135  throw TLVParserNotRegisteredException();
136 }
137 
138 template <class BaseParser, class Keytype>
139 prefix_ typename senf::GenericTLVParserRegistry<BaseParser,Keytype>::const_iterator
140 senf::GenericTLVParserRegistry<BaseParser,Keytype>::begin()
141  const
142 {
143  return map_.begin();
144 }
145 
146 template <class BaseParser, class Keytype>
147 prefix_ typename senf::GenericTLVParserRegistry<BaseParser,Keytype>::const_iterator
148 senf::GenericTLVParserRegistry<BaseParser,Keytype>::end()
149  const
150 {
151  return map_.end();
152 }
153 
154 //-/////////////////////////////////////////////////////////////////////////////////////////////////
155 #undef prefix_
156 
157 
158 // Local Variables:
159 // mode: c++
160 // fill-column: 100
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"
166 // End: