Nano
A C++ template metaprogramming library
eval.hpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------------------------------------
2 /// @file eval.hpp
3 /// @brief Header file for the eval metaclass to evaluate meta functions and classes
4 // ----------------------------------------------------------------------------------------------------------
5 
6 /*
7  * ----------------------------------------------------------------------------------------------------------
8  * eval header file for nano library.
9  * Copyright (C) 2015 Rob Clucas
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  * ----------------------------------------------------------------------------------------------------------
25  */
26 
27 #ifndef NANO_EVAL_HPP
28 #define NANO_EVAL_HPP
29 
30 #include <nano/numeric_types.hpp>
31 
32 namespace nano {
33 
34 // ----------------------------------------------------------------------------------------------------------
35 /// @struct identify
36 /// @brief Simply an identifier to identify meta types and meta functions.
37 /// @tparam Type The type to identify
38 // ----------------------------------------------------------------------------------------------------------
39 template <typename Type>
40 struct identify
41 {
42  using result = Type;
43 };
44 
45 // ----------------------------------------------------------------------------------------------------------
46 /// @struct args_list
47 /// @brief A list of arguments
48 /// @tparam Args The list of types which make up the arguments
49 // ----------------------------------------------------------------------------------------------------------
50 template <typename... Args>
51 struct args_list;
52 
53 // Define an empty args list
55 
56 // ----------------------------------------------------------------------------------------------------------
57 /// @struct expand
58 /// @brief Expands a list into its types.
59 /// @tparam Expression The expression to expand
60 /// @tparam Arg The argument to expand into the expression
61 /// @tparam Expandable If the expression E is expandable
62 // ----------------------------------------------------------------------------------------------------------
63 template <typename Expression, typename Arg, bool Expandable>
64 struct expand;
65 
66 // ----------------------------------------------------------------------------------------------------------
67 /// @struct eval
68 /// @brief Evaluates an expression with arguments
69 /// @tparam Expresison The expression to evaluate
70 /// @tparam Arg The argument to evaluate the expression with
71 // ----------------------------------------------------------------------------------------------------------
72 template <typename Expression, typename Arg>
73 struct eval
74 {
75  // Base case, evaluates to the expression itself
76  using result = Expression;
77 };
78 
79 // Specializing expand - we needed eval's definition first
80 
81 // Case for when the the expression is not expandable
82 template <typename Expression, typename Arg>
83 struct expand<Expression, Arg, false>
84 {
85  // If not expandable then expression evaluates to itself
86  using result = Expression;
87 };
88 
89 // Case for when the expression is expandable
90 template <typename Expression, typename Arg>
91 struct expand<Expression, Arg, true>
92 {
94 };
95 
96 // Coming back to specialize eval now
97 
98 // Case for when there are no arguments
99 template <typename Expression>
100 struct eval<Expression, no_args>
101 {
102  // Expression must evaluate to itself
103  using result = Expression;
104 };
105 
106 // Case for when the expression is actually a meta-function
107 template <template <typename...> class Function, typename... Args>
108 struct eval<Function<Args...>, no_args>
109 {
110  // Get the function type when each argument
111  // of As is applied to the function F
112  using function = Function<typename expand<Args, no_args, true>::result...>;
113 
114  // The value is then the function result
115  using result = typename function::result;
116 };
117 
118 } // End namespace nano
119 
120 #endif // NANO_EVAL_HPP
Simply an identifier to identify meta types and meta functions.
Definition: eval.hpp:40
Definition: containers.hpp:34
Evaluates an expression with arguments.
Definition: eval.hpp:73
Expression result
Definition: eval.hpp:103
typename function::result result
Definition: eval.hpp:115
Meta class that holds types, and allows functions to be applied to the elements of the list using the...
Definition: list.hpp:51
Header file for static numeric types to use for meta functions.
Expression result
Definition: eval.hpp:76
A list of arguments.
Definition: eval.hpp:51
typename eval< Expression, Arg >::result result
Definition: eval.hpp:93
Expression result
Definition: eval.hpp:86
Expands a list into its types.
Definition: eval.hpp:64