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 PacketParser internal header */
17 #ifndef IH_SENF_Packets_PacketParser_
18 #define IH_SENF_Packets_PacketParser_ 1
21 #include <senf/Utils/mpl.hh>
23 //-/////////////////////////////////////////////////////////////////////////////////////////////////
28 // PLEASE don't add this to doxygen ... it just looks to weird and does not help ...
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
35 template <class Parser>
36 PacketParserBase::size_type packetParserSize(
37 Parser const & p, int, senf::mpl::take_uint<Parser::init_bytes> * = 0);
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, ...);
44 // Same as above: This overload is only enabled, if Parser has an integer values 'init_bytes'
46 template <class Parser>
47 senf::mpl::rv<0> ParserInitBytes_Choose_(senf::mpl::take_uint<Parser::init_bytes> *);
49 template <class Parser>
50 senf::mpl::rv<1> ParserInitBytes_Choose_(...);
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'
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).
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 //-/////////////////////////////////////////////////////////////////////////////////////////////
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> {};
74 template <class Parser>
75 struct ParserInitBytes
76 : public ParserInitBytes_Choose<Parser,SENF_MPL_RV(ParserInitBytes_Choose_<Parser>(0))> {};
78 template <class Parser, unsigned _>
79 struct ParserIsFixed_Choose
80 : public boost::false_type {};
82 template <class Parser>
83 struct ParserIsFixed_Choose<Parser, 1>
84 : public boost::true_type {};
86 template <class Parser>
88 : public ParserIsFixed_Choose<Parser,SENF_MPL_RV(ParserInitBytes_Choose_<Parser>(0))> {};
94 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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