PacketParser.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 PacketParser inline template implementation */
16 
17 #include "PacketParser.ih"
18 
19 // Custom includes
20 
21 #define prefix_ inline
22 //-/////////////////////////////////////////////////////////////////////////////////////////////////
23 
24 #ifndef DOXYGEN
25 template <class Parser>
26 prefix_ typename boost::enable_if_c<
27  boost::is_base_of<senf::PacketParserBase, Parser>::value && senf::is_fixed<Parser>::value, Parser >::type
28  senf::operator<<(Parser const & target, Parser const & source)
29 {
30  std::copy( source.i(), source.i(Parser::fixed_bytes), target.i());
31  return target;
32 }
33 #endif
34 
35 //-/////////////////////////////////////////////////////////////////////////////////////////////////
36 // senf::PacketParserBase
37 
38 template <class Parser>
39 prefix_ Parser senf::PacketParserBase::parse(data_iterator i)
40  const
41 {
42  return Parser(i,state());
43 }
44 
45 template <class Parser, class Arg>
46 prefix_ Parser senf::PacketParserBase::parse(Arg const & arg, data_iterator i)
47  const
48 {
49  return Parser(arg, i, state());
50 }
51 
52 template <class Parser>
53 prefix_ Parser senf::PacketParserBase::parse(size_type n)
54  const
55 {
56  return Parser(boost::next(i(),n),state());
57 }
58 
59 template <class Parser, class Arg>
60 prefix_ Parser senf::PacketParserBase::parse(Arg const & arg, size_type n)
61  const
62 {
63  return Parser(arg, boost::next(i(),n), state());
64 }
65 
66 //-/////////////////////////////////////////////////////////////////////////////////////////////////
67 // namespace members
68 
69 template <class Parser>
70 prefix_ senf::PacketParserBase::size_type senf::bytes(Parser const & p)
71 {
72  return detail::packetParserSize(p, 0);
73 }
74 
75 #ifndef DOXYGEN
76 template <class Parser, class Value>
77 prefix_ typename boost::enable_if_c <
78  boost::is_base_of<senf::PacketParserBase, Parser>::value
79  && ! boost::is_base_of<senf::PacketParserBase, Value>::value,
80  Parser >::type senf::operator<<(Parser target, Value const & value)
81 {
82  target.value(value);
83  return target;
84 }
85 #else
86 template <class Parser, class Value>
87 prefix_ Parser senf::operator<<(Parser target, Value const & value)
88 {}
89 #endif
90 
91 #ifndef DOXYGEN
92 template <class Parser, class Value>
93 prefix_ typename boost::enable_if_c <
94  boost::is_base_of<senf::PacketParserBase, Parser>::value
95  && ! boost::is_base_of<senf::PacketParserBase, Value>::value,
96  Parser >::type senf::operator<<(Parser target, boost::optional<Value> const & value)
97 {
98  if (value)
99  target.value(*value);
100  return target;
101 }
102 #else
103 template <class Parser, class Value>
104 prefix_ Parser senf::operator<<(Parser target, Value const & value)
105 {}
106 #endif
107 
108 template <class Parser>
109 prefix_ senf::PacketParserBase::size_type
110 senf::detail::packetParserSize(Parser const & p, int, senf::mpl::take_uint<Parser::init_bytes> *)
111 {
112  return p.bytes();
113 }
114 
115 //-/////////////////////////////////////////////////////////////////////////////////////////////////
116 // senf::ArrayValueParserBase<Parser, ValueType, FixedBytes>
117 
118 template <class Parser, typename ValueType>
119 prefix_ senf::ArrayValueParserBase<Parser, ValueType>::ArrayValueParserBase(data_iterator i, state_type s)
120  : PacketParserBase(i, s, fixed_bytes)
121 { }
122 
123 template <class Parser, typename ValueType>
124 prefix_ senf::ArrayValueParserBase<Parser, ValueType>::operator value_type ()
125  const
126 {
127  return static_cast<Parser const *>(this)->value();
128 }
129 
130 template <class Parser, typename ValueType>
131 prefix_ senf::PacketParserBase::byte &
132 senf::ArrayValueParserBase<Parser, ValueType>::operator[](size_type index)
133 {
134  return *boost::next(i(), index);
135 }
136 
137 template <class Parser, typename ValueType>
138 prefix_ senf::PacketParserBase::byte const &
139 senf::ArrayValueParserBase<Parser, ValueType>::operator[](size_type index)
140  const
141 {
142  return *boost::next(i(), index);
143 }
144 
145 template <class Parser, typename ValueType>
146 prefix_ Parser const & senf::ArrayValueParserBase<Parser, ValueType>::operator= (value_type const & other)
147 {
148  static_cast<Parser*>(this)->value(other);
149  return *static_cast<Parser const *>(this);
150 }
151 
152 template <class Parser, typename ValueType>
153 prefix_ bool senf::operator==(ArrayValueParserBase<Parser, ValueType> const & parser, ValueType const & value)
154 {
155  return std::equal(value.begin(), value.end(), parser.i());
156 }
157 
158 template <class Parser, typename ValueType>
159 prefix_ bool senf::operator!=(ArrayValueParserBase<Parser, ValueType> const & parser, ValueType const & value)
160 {
161  return not std::equal(value.begin(), value.end(), parser.i());
162 }
163 
164 template <class Parser, typename ValueType>
165 prefix_ bool senf::operator==(ValueType const & value, ArrayValueParserBase<Parser, ValueType> const & parser)
166 {
167  return std::equal(value.begin(), value.end(), parser.i());
168 }
169 
170 template <class Parser, typename ValueType>
171 prefix_ bool senf::operator!=(ValueType const & value, ArrayValueParserBase<Parser, ValueType> const & parser)
172 {
173  return not std::equal(value.begin(), value.end(), parser.i());
174 }
175 
176 template <class Parser, typename ValueType>
177 prefix_ bool senf::ArrayValueParserBase<Parser, ValueType>::operator==(Parser const & other)
178  const
179 {
180  return std::equal(i(), i(fixed_bytes), other.i());
181 }
182 
183 //-/////////////////////////////////////////////////////////////////////////////////////////////////
184 #undef prefix_
185 
186 
187 // Local Variables:
188 // mode: c++
189 // fill-column: 100
190 // c-file-style: "senf"
191 // indent-tabs-mode: nil
192 // ispell-local-dictionary: "american"
193 // compile-command: "scons -u test"
194 // comment-column: 40
195 // End: