PacketParser.ih
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 internal header */
16 
17 #ifndef IH_SENF_Packets_PacketParser_
18 #define IH_SENF_Packets_PacketParser_ 1
19 
20 // Custom includes
21 #include <senf/Utils/mpl.hh>
22 
23 //-/////////////////////////////////////////////////////////////////////////////////////////////////
24 
25 namespace senf {
26 namespace detail {
27 
28  // PLEASE don't add this to doxygen ... it just looks to weird and does not help ...
29 
30 # ifndef DOXYGEN
31 
32  // Use SFINAE to check, if Parser has an integer-valued init_bytes member. If not,
33  // 'Parser_TakeNum<Parser::init_bytes>' fails and the overload is removed from the overload
34  // set.
35  template <class Parser>
36  PacketParserBase::size_type packetParserSize(
37  Parser const & p, int, senf::mpl::take_uint<Parser::init_bytes> * = 0);
38 
39  // An ellipsis is always the worst match. A call 'packetParserSize(p,0) will prefer above
40  // overload if that is not disabled by SFINAE.
41  template <class Parser>
42  PacketParserBase::size_type packetParserSize(Parser const & p, ...);
43 
44  // Same as above: This overload is only enabled, if Parser has an integer values 'init_bytes'
45  // member.
46  template <class Parser>
47  senf::mpl::rv<0> ParserInitBytes_Choose_(senf::mpl::take_uint<Parser::init_bytes> *);
48 
49  template <class Parser>
50  senf::mpl::rv<1> ParserInitBytes_Choose_(...);
51 
52  // This version of ParserInitBytes_Choose uses 'Parser::init_bytes' to provide 'value' (via
53  // 'boost::integral_constant')
54  template <class Parser, unsigned _>
55  struct ParserInitBytes_Choose
56  : public boost::integral_constant<PacketParserBase::size_type, Parser::init_bytes> {};
57  // ^^-- g++ error signaled here:
58  // error: 'fixed_bytes' is not a member of 'some-class-name'
59  //
60  // The 'some-class-name' class (as given in the error message) does not seem to be a parser at
61  // all (it has neither a 'fixed_bytes' nor an 'init_bytes' member).
62  //
63  // Either 'some-class-name' is not the class you wanted to use (it really is no parser) or you
64  // left out either 'init_bytes' or 'fixed_bytes' when defining the parser. This will also
65  // happen, if you forget to call 'SENF_PARSER_FINALIZE()' when defining a composite parser.
66  //-/////////////////////////////////////////////////////////////////////////////////////////////
67 
68  // If Parser::init_bytes is not defined, this specialization is chosen which instead uses
69  // 'Parser::fixed_bytes'
70  template <class Parser>
71  struct ParserInitBytes_Choose<Parser, 1>
72  : public boost::integral_constant<PacketParserBase::size_type, Parser::fixed_bytes> {};
73 
74  template <class Parser>
75  struct ParserInitBytes
76  : public ParserInitBytes_Choose<Parser,SENF_MPL_RV(ParserInitBytes_Choose_<Parser>(0))> {};
77 
78  template <class Parser, unsigned _>
79  struct ParserIsFixed_Choose
80  : public boost::false_type {};
81 
82  template <class Parser>
83  struct ParserIsFixed_Choose<Parser, 1>
84  : public boost::true_type {};
85 
86  template <class Parser>
87  struct ParserIsFixed
88  : public ParserIsFixed_Choose<Parser,SENF_MPL_RV(ParserInitBytes_Choose_<Parser>(0))> {};
89 
90 # endif
91 
92 }}
93 
94 //-/////////////////////////////////////////////////////////////////////////////////////////////////
95 #endif
96 
97 
98 // Local Variables:
99 // mode: c++
100 // fill-column: 100
101 // c-file-style: "senf"
102 // indent-tabs-mode: nil
103 // ispell-local-dictionary: "american"
104 // compile-command: "scons -u test"
105 // comment-column: 40
106 // End: