switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / PacketParser.ih
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief PacketParser internal header */
30
31 #ifndef IH_SENF_Packets_PacketParser_
32 #define IH_SENF_Packets_PacketParser_ 1
33
34 // Custom includes
35 #include <senf/Utils/mpl.hh>
36
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38
39 namespace senf {
40 namespace detail {
41
42     // PLEASE don't add this to doxygen ... it just looks to weird and does not help ...
43
44 #   ifndef DOXYGEN
45
46     // Use SFINAE to check, if Parser has an integer-valued init_bytes member. If not,
47     // 'Parser_TakeNum<Parser::init_bytes>' fails and the overload is removed from the overload
48     // set.
49     template <class Parser>
50     PacketParserBase::size_type packetParserSize(
51         Parser p, int, senf::mpl::take_uint<Parser::init_bytes> * = 0);
52
53     // An ellipsis is always the worst match. A call 'packetParserSize(p,0) will prefer above
54     // overload if that is not disabled by SFINAE.
55     template <class Parser>
56     PacketParserBase::size_type packetParserSize(Parser p, ...);
57
58     // Same as above: This overload is only enabled, if Parser has an integer values 'init_bytes'
59     // member.
60     template <class Parser>
61     senf::mpl::rv<0> ParserInitBytes_Choose_(senf::mpl::take_uint<Parser::init_bytes> *);
62
63     template <class Parser>
64     senf::mpl::rv<1> ParserInitBytes_Choose_(...);
65
66     // This version of ParserInitBytes_Choose uses 'Parser::init_bytes' to provide 'value' (via
67     // 'boost::integral_constant')
68     template <class Parser, unsigned _>
69     struct ParserInitBytes_Choose
70         : public boost::integral_constant<PacketParserBase::size_type, Parser::init_bytes> {};
71     // ^^-- g++ error signaled here:
72     //    error: 'fixed_bytes' is not a member of 'some-class-name'
73     //
74     // The 'some-class-name' class (as given in the error message) does not seem to be a parser at
75     // all (it has neither a 'fixed_bytes' nor an 'init_bytes' member).
76     //
77     // Either 'some-class-name' is not the class you wanted to use (it really is no parser) or you
78     // left out either 'init_bytes' or 'fixed_bytes' when defining the parser. This will also
79     // happen, if you forget to call 'SENF_PARSER_FINALIZE()' when defining a composite parser.
80     //-/////////////////////////////////////////////////////////////////////////////////////////////
81
82     // If Parser::init_bytes is not defined, this specialization is chosen which instead uses
83     // 'Parser::fixed_bytes'
84     template <class Parser>
85     struct ParserInitBytes_Choose<Parser, 1>
86         : public boost::integral_constant<PacketParserBase::size_type, Parser::fixed_bytes> {};
87
88     template <class Parser>
89     struct ParserInitBytes
90         : public ParserInitBytes_Choose<Parser,SENF_MPL_RV(ParserInitBytes_Choose_<Parser>(0))> {};
91
92     template <class Parser, unsigned _>
93     struct ParserIsFixed_Choose
94         : public boost::false_type {};
95
96     template <class Parser>
97     struct ParserIsFixed_Choose<Parser, 1>
98         : public boost::true_type {};
99
100     template <class Parser>
101     struct ParserIsFixed
102         : public ParserIsFixed_Choose<Parser,SENF_MPL_RV(ParserInitBytes_Choose_<Parser>(0))> {};
103
104 #   endif
105
106 }}
107
108 //-/////////////////////////////////////////////////////////////////////////////////////////////////
109 #endif
110
111 \f
112 // Local Variables:
113 // mode: c++
114 // fill-column: 100
115 // c-file-style: "senf"
116 // indent-tabs-mode: nil
117 // ispell-local-dictionary: "american"
118 // compile-command: "scons -u test"
119 // comment-column: 40
120 // End: