PacketParser.hh
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 
144 #ifndef HH_SENF_Packets_PacketParser_
145 #define HH_SENF_Packets_PacketParser_ 1
146 
147 // Custom includes
148 #include <boost/utility/enable_if.hpp>
149 #include <boost/type_traits/is_base_of.hpp>
150 #include <boost/optional.hpp>
151 #include "PacketTypes.hh"
152 #include "PacketData.hh"
153 #include "ParseHelpers.hh"
154 #include "SafeIterator.hh"
155 
156 //#include "PacketParser.mpp"
157 //-/////////////////////////////////////////////////////////////////////////////////////////////////
158 
159 namespace senf {
160 
161  class Packet;
162 
229  {
230  public:
231  //-////////////////////////////////////////////////////////////////////////
232  // Types
233 
238  typedef PacketData * state_type;
240 
241  //-////////////////////////////////////////////////////////////////////////
243  //\{
244 
245  // no default constructor
246  // default copy
247  // default destructor
248  // no conversion constructors
249 
250  //\}
251  //-////////////////////////////////////////////////////////////////////////
252 
253  data_iterator i() const;
254 
259  data_iterator i(size_type offset) const;
260 
270  state_type state() const;
271 
275  PacketData & data() const;
276 
279  void init() const;
280 
284  private:
285  struct ParserProtector {
286  senf::safe_data_iterator safe_i_;
287  mutable PacketParserBase const * parser_;
288 
289  ParserProtector(PacketParserBase const * parser);
290  ParserProtector(ParserProtector const & other_);
291  ~ParserProtector();
292  };
293  protected:
294  ParserProtector protect() const;
295 
296  PacketParserBase(data_iterator i, state_type s);
297 
301  PacketParserBase(data_iterator i, state_type s, size_type size);
303 
319  bool check(size_type size) const;
320 
323  void validate(size_type size) const;
324 
328  template <class Parser> Parser parse(data_iterator i) const;
329 
333  template <class Parser, class Arg> Parser parse(Arg const & arg, data_iterator i) const;
335 
339  template <class Parser> Parser parse(size_type n) const;
340 
344  template <class Parser, class Arg> Parser parse(Arg const & arg, size_type n) const;
346 
349  void defaultInit() const;
350 
354  Packet packet() const;
355 
359  void resize(size_type oldSize, size_type newSize);
360 
369  private:
370  data_iterator end() const;
371 
372  data_iterator i_;
373  PacketData * data_;
374 
375  template <class Parser> friend class SafePacketParserWrapper;
376  friend struct ParserProtector;
377  };
378 
393  template <class Parser>
394  PacketParserBase::size_type bytes(Parser const & p);
395 
396  namespace detail { template <class Parser> struct ParserInitBytes; }
397  namespace detail { template <class Parser> struct ParserIsFixed; }
398 
414  template <class Parser>
415  struct init_bytes : public detail::ParserInitBytes<Parser>
416  {};
417 
432  template <class Parser>
433  struct is_fixed : public detail::ParserIsFixed<Parser>
434  {};
435 
436 # ifndef DOXYGEN
437 
438  template <class Parser>
439  typename boost::enable_if_c<
440  boost::is_base_of<PacketParserBase, Parser>::value
442  Parser >::type
443  operator<<(Parser const & target, Parser const & source);
444 
445  template <class Parser>
446  typename boost::enable_if_c<
447  boost::is_base_of<PacketParserBase, Parser>::value
448  && ! senf::is_fixed<Parser>::value,
449  Parser >::type
450  operator<<(Parser const & target, Parser const & source);
451 
452 # else
453 
465  template <class Parser>
466  Parser operator<<(Parser const & target, Parser const & source);
467 # endif
468 
469 # ifndef DOXYGEN
470  template <class Parser, class Value>
471  typename boost::enable_if_c <
472  boost::is_base_of<PacketParserBase, Parser>::value
473  && ! boost::is_base_of<PacketParserBase, Value>::value,
474  Parser >::type
475  operator<<(Parser target, Value const & value);
476 # else
477 
485  template <class Parser, class Value>
486  Parser operator<<(Parser target, Value const & value);
487 # endif
488 
489 # ifndef DOXYGEN
490  template <class Parser, class Value>
491  typename boost::enable_if_c <
492  boost::is_base_of<PacketParserBase, Parser>::value
493  && ! boost::is_base_of<PacketParserBase, Value>::value,
494  Parser >::type
495  operator<<(Parser target, boost::optional<Value> const & value);
496 # else
497 
507  template <class Parser, class Value>
508  Parser operator<<(Parser target, boost::optional<Value> const & value);
509 # endif
510 
514  : public PacketParserBase
515  {
516 # include SENF_FIXED_PARSER()
518  };
519 
520 
521  template <class Parser, typename ValueType>
523  {
525 
526  static size_type const fixed_bytes = ValueType::static_size;
527  typedef ValueType value_type;
528 
530 
531  value_type value() const { return value_type::from_data(i()); }
532  void value(value_type const & v) { std::copy(v.begin(), v.end(), i()); }
533 
534  operator value_type () const;
535  byte & operator[](size_type index);
536  byte const & operator[](size_type index) const;
537  Parser const & operator= (value_type const & other);
538 
539  bool operator==(Parser const & other) const;
540  };
541 
542  template <class Parser, typename ValueType>
543  bool operator==(ArrayValueParserBase<Parser, ValueType> const & parser, ValueType const & value);
544 
545  template <class Parser, typename ValueType>
546  bool operator!=(ArrayValueParserBase<Parser, ValueType> const & parser, ValueType const & value);
547 
548  template <class Parser, typename ValueType>
549  bool operator==(ValueType const & value, ArrayValueParserBase<Parser, ValueType> const & parser);
550 
551  template <class Parser, typename ValueType>
552  bool operator!=(ValueType const & value, ArrayValueParserBase<Parser, ValueType> const & parser);
553 
554 }
555 
556 //-/////////////////////////////////////////////////////////////////////////////////////////////////
557 #endif
558 #if !defined(HH_SENF_Packets_Packets__decls_) && !defined(HH_SENF_Packets_PacketParser_i_)
559 #define HH_SENF_Packets_PacketParser_i_
560 #include "PacketParser.cci"
561 #include "PacketParser.ct"
562 #include "PacketParser.cti"
563 #endif
564 
565 
566 // Local Variables:
567 // mode: c++
568 // fill-column: 100
569 // c-file-style: "senf"
570 // indent-tabs-mode: nil
571 // ispell-local-dictionary: "american"
572 // compile-command: "scons -u test"
573 // comment-column: 40
574 // End:
575 
Packet packet() const
Get packet this parser is parsing from.
ParseHelpers public header.
void copy(ListParser< ListPolicy > const &listParser, OutputIterator result)
Definition: algorithms.hh:58
void value(value_type const &v)
PacketTypes public header.
PacketData * state_type
Type of the &#39;state&#39; parameter.
SafeIterator public header.
PacketData public header.
unspecified_keyword_type parser
value_type value() const
Parser parse(data_iterator i) const
Create sub-parser.
std::ostream & operator<<(std::ostream &os, Packet const &packet)
Definition: Packet.cc:75
Return number of bytes to allocate to new object of given type.
PacketParserBase parser_base_type
Base type of the next parser.
Test, whether a parser is a fixed-size parser.
PacketParserBase::size_type bytes(Parser const &p)
Return raw size parsed by the given parser object.
PacketParserBase(data_iterator i, state_type s)
Standard constructor.
#define SENF_PARSER_FINALIZE(name)
Generate parser control members.
detail::packet::byte byte
Unsigned 8bit value, the raw value type.
bool operator==(ArrayValueParserBase< Parser, ValueType > const &parser, ValueType const &value)
Main Packet class.
Definition: Packet.hh:131
void defaultInit() const
Default implementation.
Packet data STL-sequence view.
Definition: PacketData.hh:61
raw_container::size_type size_type
Definition: PacketTypes.hh:66
detail::packet::difference_type difference_type
Signed integral type.
data_iterator i() const
Return beginning of data to parse.
Default parser parsing nothing.
raw_container::iterator iterator
Definition: PacketTypes.hh:69
void init() const
Default implementation.
Re-validating data iterator.
Definition: SafeIterator.hh:47
detail::packet::size_type size_type
Unsigned integral type.
bool operator!=(ArrayValueParserBase< Parser, ValueType > const &parser, ValueType const &value)
bool check(size_type size) const
Check size of data container.
friend struct ParserProtector
raw_container::difference_type difference_type
Definition: PacketTypes.hh:67
void resize(size_type oldSize, size_type newSize)
Resize data container.
Definition: PacketParser.cc:26
Parser Base class.
PacketData & data() const
Access the packets raw data container.
boost::uint8_t byte
Definition: PacketTypes.hh:60
Iterator re-validating Parser wrapper.
void validate(size_type size) const
Validate size of data container.
state_type state() const
Return state of this parser.
ArrayValueParserBase< Parser, ValueType > Base
ParserProtector protect() const
detail::packet::iterator data_iterator
Raw data iterator type.