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 inline template implementation */
17 //#include "GenericTLV.ih"
21 #define prefix_ inline
22 //-/////////////////////////////////////////////////////////////////////////////////////////////////
24 //-/////////////////////////////////////////////////////////////////////////////////////////////////
25 // senf::GenericTLVParserBase<Base>
28 prefix_ senf::PacketParserBase::size_type senf::GenericTLVParserBase<Base>::bytes()
31 if (Base::Registry::instance().isRegistered(*this))
32 return Base::Registry::instance().bytes(*this);
34 return senf::bytes(self()) + this->length();
38 prefix_ void senf::GenericTLVParserBase<Base>::init()
45 template <class Parser>
46 prefix_ Parser senf::GenericTLVParserBase<Base>::as()
49 return Parser(this->i(), this->state() );
53 template <class Parser>
54 prefix_ bool senf::GenericTLVParserBase<Base>::is()
57 return this->type().value() == Parser::typeId;
61 prefix_ Base & senf::GenericTLVParserBase<Base>::self()
63 return *static_cast<Base *>(this);
67 prefix_ Base const & senf::GenericTLVParserBase<Base>::self()
70 return *static_cast<Base const *>(this);
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 *)
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 *)
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 *)
99 this->type() = val.first;
106 //-/////////////////////////////////////////////////////////////////////////////////////////////////
107 // senf::detail:GenericTLVParserRegistry_Entry<BaseParser, Parser>
109 template <class BaseParser, class Parser>
110 prefix_ void senf::detail::GenericTLVParserRegistry_Entry<BaseParser, Parser>::dump(
111 GenericTLVParserBase<BaseParser> const & parser, std::ostream & os)
114 (parser.template as<Parser>()).dump(os);
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)
123 return senf::bytes(parser.template as<Parser>());
126 template <class BaseParser, class Parser>
127 prefix_ std::string senf::detail::GenericTLVParserRegistry_Entry<BaseParser, Parser>::name()
130 return senf::prettyName(typeid(Parser));
134 //-/////////////////////////////////////////////////////////////////////////////////////////////////
135 // senf::GenericTLVParserRegistry<BaseParser,Keytype>
137 template <class BaseParser, class Keytype>
138 prefix_ bool senf::GenericTLVParserRegistry<BaseParser,Keytype>::isRegistered(
139 GenericTLVParser const & parser)
142 return map_.find(parser.type()) != map_.end();
145 template <class BaseParser, class Keytype>
146 prefix_ bool senf::GenericTLVParserRegistry<BaseParser,Keytype>::isRegistered(Keytype const & key)
149 return map_.find(key) != map_.end();
152 //-/////////////////////////////////////////////////////////////////////////////////////////////////
153 // senf::GenericTLVParserRegistry<BaseParser,Keytype>::RegistrationProxy<PacketParser>
155 template <class BaseParser, class Keytype>
156 template <class PacketParser>
157 prefix_ senf::GenericTLVParserRegistry<BaseParser,Keytype>::RegistrationProxy<PacketParser>::RegistrationProxy()
159 GenericTLVParserRegistry<BaseParser,Keytype>::instance().template registerParser<PacketParser>();
162 //-/////////////////////////////////////////////////////////////////////////////////////////////////
163 // senf::TLVListParser<ListPolicy>
165 template <typename ListPolicy>
166 prefix_ senf::TLVListParser<ListPolicy>::TLVListParser(PacketParserBase::data_iterator i, PacketParserBase::state_type s)
167 : ListParser<ListPolicy>(i,s)
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)
175 template <typename ListPolicy>
176 template <typename TLVParser>
177 prefix_ bool senf::TLVListParser<ListPolicy>::contains()
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>())
188 template <typename ListPolicy>
189 prefix_ bool senf::TLVListParser<ListPolicy>::contains(typename ListPolicy::element_type::type_t::value_type typeId)
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)
200 template <typename ListPolicy>
201 template <typename TLVParser>
202 prefix_ TLVParser senf::TLVListParser<ListPolicy>::find()
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();
212 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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"