GenericTLV.cti
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 inline template implementation */
16 
17 //#include "GenericTLV.ih"
18 
19 // Custom includes
20 
21 #define prefix_ inline
22 //-/////////////////////////////////////////////////////////////////////////////////////////////////
23 
24 //-/////////////////////////////////////////////////////////////////////////////////////////////////
25 // senf::GenericTLVParserBase<Base>
26 
27 template <class Base>
28 prefix_ senf::PacketParserBase::size_type senf::GenericTLVParserBase<Base>::bytes()
29  const
30 {
31  if (Base::Registry::instance().isRegistered(*this))
32  return Base::Registry::instance().bytes(*this);
33  else
34  return senf::bytes(self()) + this->length();
35 }
36 
37 template <class Base>
38 prefix_ void senf::GenericTLVParserBase<Base>::init()
39  const
40 {
41  this->defaultInit();
42 }
43 
44 template <class Base>
45 template <class Parser>
46 prefix_ Parser senf::GenericTLVParserBase<Base>::as()
47  const
48 {
49  return Parser(this->i(), this->state() );
50 }
51 
52 template <class Base>
53 template <class Parser>
54 prefix_ bool senf::GenericTLVParserBase<Base>::is()
55  const
56 {
57  return this->type().value() == Parser::typeId;
58 }
59 
60 template <class Base>
61 prefix_ Base & senf::GenericTLVParserBase<Base>::self()
62 {
63  return *static_cast<Base *>(this);
64 }
65 
66 template <class Base>
67 prefix_ Base const & senf::GenericTLVParserBase<Base>::self()
68  const
69 {
70  return *static_cast<Base const *>(this);
71 }
72 
73 #ifndef DOXYGEN
74 
75 template <class Base>
76 template <class ForwardReadableRange>
77 prefix_ void senf::GenericTLVParserBase<Base>::value(
78  ForwardReadableRange const & val,
79  typename boost::disable_if<senf::is_pair<ForwardReadableRange> >::type *)
80 {
81  value_(val);
82 }
83 
84 template <class Base>
85 template <class First, class Second>
86 prefix_ void senf::GenericTLVParserBase<Base>::value(
87  std::pair<First, Second> const & val,
88  typename boost::disable_if<boost::is_convertible<First, typename Base::type_t::value_type> >::type *)
89 {
90  value_(val);
91 }
92 
93 template <class Base>
94 template <class Type, class ForwardReadableRange>
95 prefix_ void senf::GenericTLVParserBase<Base>::value(
96  std::pair<Type, ForwardReadableRange> const & val,
97  typename boost::enable_if<boost::is_convertible<Type, typename Base::type_t::value_type> >::type *)
98 {
99  this->type() = val.first;
100  value_(val.second);
101 }
102 
103 #endif
104 
105 
106 //-/////////////////////////////////////////////////////////////////////////////////////////////////
107 // senf::detail:GenericTLVParserRegistry_Entry<BaseParser, Parser>
108 
109 template <class BaseParser, class Parser>
110 prefix_ void senf::detail::GenericTLVParserRegistry_Entry<BaseParser, Parser>::dump(
111  GenericTLVParserBase<BaseParser> const & parser, std::ostream & os)
112  const
113 {
114  (parser.template as<Parser>()).dump(os);
115 }
116 
117 template <class BaseParser, class Parser>
118 prefix_ senf::PacketParserBase::size_type
119 senf::detail::GenericTLVParserRegistry_Entry<BaseParser, Parser>::bytes(
120  GenericTLVParserBase<BaseParser> const & parser)
121  const
122 {
123  return senf::bytes(parser.template as<Parser>());
124 }
125 
126 template <class BaseParser, class Parser>
127 prefix_ std::string senf::detail::GenericTLVParserRegistry_Entry<BaseParser, Parser>::name()
128  const
129 {
130  return senf::prettyName(typeid(Parser));
131 }
132 
133 
134 //-/////////////////////////////////////////////////////////////////////////////////////////////////
135 // senf::GenericTLVParserRegistry<BaseParser,Keytype>
136 
137 template <class BaseParser, class Keytype>
138 prefix_ bool senf::GenericTLVParserRegistry<BaseParser,Keytype>::isRegistered(
139  GenericTLVParser const & parser)
140  const
141 {
142  return map_.find(parser.type()) != map_.end();
143 }
144 
145 template <class BaseParser, class Keytype>
146 prefix_ bool senf::GenericTLVParserRegistry<BaseParser,Keytype>::isRegistered(Keytype const & key)
147  const
148 {
149  return map_.find(key) != map_.end();
150 }
151 
152 //-/////////////////////////////////////////////////////////////////////////////////////////////////
153 // senf::GenericTLVParserRegistry<BaseParser,Keytype>::RegistrationProxy<PacketParser>
154 
155 template <class BaseParser, class Keytype>
156 template <class PacketParser>
157 prefix_ senf::GenericTLVParserRegistry<BaseParser,Keytype>::RegistrationProxy<PacketParser>::RegistrationProxy()
158 {
159  GenericTLVParserRegistry<BaseParser,Keytype>::instance().template registerParser<PacketParser>();
160 };
161 
162 //-/////////////////////////////////////////////////////////////////////////////////////////////////
163 // senf::TLVListParser<ListPolicy>
164 
165 template <typename ListPolicy>
166 prefix_ senf::TLVListParser<ListPolicy>::TLVListParser(PacketParserBase::data_iterator i, PacketParserBase::state_type s)
167  : ListParser<ListPolicy>(i,s)
168 {}
169 
170 template <typename ListPolicy>
171 prefix_ senf::TLVListParser<ListPolicy>::TLVListParser(ListPolicy policy, PacketParserBase::data_iterator i, PacketParserBase::state_type s)
172  : ListParser<ListPolicy>(policy, i,s)
173 {}
174 
175 template <typename ListPolicy>
176 template <typename TLVParser>
177 prefix_ bool senf::TLVListParser<ListPolicy>::contains()
178  const
179 {
180  typedef typename ListPolicy::container_type container_type;
181  container_type c (*this);
182  for (typename container_type::const_iterator i = c.begin(); i != c.end(); ++i)
183  if (i->template is<TLVParser>())
184  return true;
185  return false;
186 }
187 
188 template <typename ListPolicy>
189 prefix_ bool senf::TLVListParser<ListPolicy>::contains(typename ListPolicy::element_type::type_t::value_type typeId)
190  const
191 {
192  typedef typename ListPolicy::container_type container_type;
193  container_type c (*this);
194  for (typename container_type::const_iterator i = c.begin(); i != c.end(); ++i)
195  if (i->type() == typeId)
196  return true;
197  return false;
198 }
199 
200 template <typename ListPolicy>
201 template <typename TLVParser>
202 prefix_ TLVParser senf::TLVListParser<ListPolicy>::find()
203  const
204 {
205  typedef typename ListPolicy::container_type container_type;
206  container_type c (*this);
207  for (typename container_type::const_iterator i = c.begin(); i != c.end(); ++i)
208  if (i->template is<TLVParser>())
209  return i->template as<TLVParser>();
210  throw InvalidPacketChainException();
211 }
212 //-/////////////////////////////////////////////////////////////////////////////////////////////////
213 #undef prefix_
214 
215 
216 // Local Variables:
217 // mode: c++
218 // fill-column: 100
219 // comment-column: 40
220 // c-file-style: "senf"
221 // indent-tabs-mode: nil
222 // ispell-local-dictionary: "american"
223 // compile-command: "scons -u test"
224 // End: