Nano
A C++ template metaprogramming library
functions.hpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------------------------------------
2 /// @file function.hpp
3 /// @brief Header file for metafunctions to operator on compile time data structures
4 // ----------------------------------------------------------------------------------------------------------
5 
6 /*
7  * ----------------------------------------------------------------------------------------------------------
8  * functions 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_FUNCTIONS_HPP
28 #define NANO_FUNCTIONS_HPP
29 
30 #include <nano/eval.hpp>
31 
32 namespace nano {
33 
34 // ----------------------------------------------------------------------------------------------------------
35 /// @struct equal_value
36 /// @brief Check if two types (which have value components) have the same value.
37 /// @tparam Type1 The first type to check for equality
38 /// @tparam Type2 The second type to check for equality
39 // ----------------------------------------------------------------------------------------------------------
40 template <typename Type1, typename Type2>
41 struct equal_value
42 {
43  static constexpr bool result = Type1::value == Type2::value ? true : false;
44 };
45 
46 // ----------------------------------------------------------------------------------------------------------
47 /// @struct is_found
48 /// @brief Ths function is supposed to be used with the find functions on lists when searching through
49 /// the list to find common or uncommon elements. It should be called by the zip HOF to determine
50 /// if Type1 and Type2 should be zipped and added to the list of elements to return. \n\n
51 /// Please see the implementation of the find_common and zip functions for clarificaiton.
52 /// @tparam Type1 This should NOT be an element which represents if the element was found or not. It
53 /// should be an index corresponding to the result of trying to find the element at the index
54 /// represented by Type1 in the first list, in the second list.
55 /// @tparam Type2 This should be the result of whether the element was found (either -1 if it wasn't
56 /// found or its index in list2 if it was found).
57 // ----------------------------------------------------------------------------------------------------------
58 template <typename Type1, typename Type2>
59 struct is_found
60 {
61  static constexpr bool result = Type2::value != -1 ? true : false;
62 };
63 
64 // ----------------------------------------------------------------------------------------------------------
65 /// @struct not_found
66 /// @brief Function to determine if and element is not found (has a value of -1)
67 /// @tparam Type The type to check if found
68 // ----------------------------------------------------------------------------------------------------------
69 template <typename Type>
70 struct not_found
71 {
72  static constexpr bool result = Type::value == -1 ? true : false;
73 };
74 
75 // ----------------------------------------------------------------------------------------------------------
76 /// @struct both_found
77 /// @brief Checks if both types are found (have values != -1), in which case result is true, otherwise
78 /// result is false
79 /// @tparam Type1 The first type to determine if it is found
80 /// @tparam Type2 The second type to determine if it is found
81 // ----------------------------------------------------------------------------------------------------------
82 template <typename Type1, typename Type2>
83 struct both_found
84 {
85  static constexpr bool result = ( (Type1::value != -1) && (Type2::value != -1) ) ? true : false;
86 };
87 
88 // ----------------------------------------------------------------------------------------------------------
89 /// @struct size_of
90 /// @brief Gets the number of types in a container
91 /// @tparam Container The container (list, range etc...) to get the size of
92 /// @tparam Types The types that are the elements of the container
93 // ----------------------------------------------------------------------------------------------------------
94 template <typename Container, typename... Types>
95 struct size_of;
96 
97 template <template <typename...> class Container, typename... Types>
98 struct size_of<Container<Types...>>
99 {
100  static constexpr int result = sizeof...(Types);
101 };
102 
103 // ----------------------------------------------------------------------------------------------------------
104 /// @struct multiply
105 /// @brief Multiplies two nano numeric types
106 /// @tparam Arg1 The forst argument for the multiplication
107 /// @tparam Arg2 The second argument for multiplication
108 // ----------------------------------------------------------------------------------------------------------
109 template <typename Arg1, typename Arg2>
110 struct multiply
111 {
114 
115  static constexpr typename Arg1::type result = Arg1::value * Arg2::value;
116 };
117 
118 // ----------------------------------------------------------------------------------------------------------
119 /// @struct add
120 /// @brief Addss two nano numeric types
121 /// @tparam Arg1 The first argument for the addition
122 /// @tparam Arg2 The second argument for addition
123 // ----------------------------------------------------------------------------------------------------------
124 template <typename Arg1, typename Arg2>
125 struct add
126 {
129 
130  static constexpr typename Arg1::type result = Arg1::value + Arg2::value;
131 };
132 
133 } // End namespace nano
134 
135 #endif // NANO_FUNCTIONS_HPP
static constexpr bool result
Definition: functions.hpp:43
Function to determine if and element is not found (has a value of -1)
Definition: functions.hpp:70
Multiplies two nano numeric types.
Definition: functions.hpp:110
Checks if both types are found (have values != -1), in which case result is true, otherwise result is...
Definition: functions.hpp:83
Header file for the eval metaclass to evaluate meta functions and classes.
static constexpr bool result
Definition: functions.hpp:72
static constexpr bool result
Definition: functions.hpp:85
Definition: containers.hpp:34
Check if two types (which have value components) have the same value.
Definition: functions.hpp:41
Gets the number of types in a container.
Definition: functions.hpp:95
static constexpr bool result
Definition: functions.hpp:61
Wrapper around size_t for static size_t types used by metaclass and metafunctions in nano...
Definition: numeric_types.hpp:40
Addss two nano numeric types.
Definition: functions.hpp:125
Ths function is supposed to be used with the find functions on lists when searching through the list ...
Definition: functions.hpp:59