GrPPI  1.0
Generic and Reusable Parallel Pattern Interface
callable_traits.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Universidad Carlos III de Madrid
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef GRPPI_COMMON_CALLABLE_TRAITS_H
17 #define GRPPI_COMMON_CALLABLE_TRAITS_H
18 
19 #include <type_traits>
20 
21 namespace grppi {
22 
23 namespace internal {
24 
26 template <typename T>
27 struct identity {
28  using type = T;
29 };
30 
31 // Callable helper for function objects defers to pointer to call operator
32 template <typename T>
33 struct callable_helper : callable_helper<decltype(&T::operator())>
34 {};
35 
36 // Callable helper for function type
37 template <typename R, typename ... Args>
38 struct callable_helper<R(Args...)> : identity<R(Args...)>
39 {
40  using arity = typename std::integral_constant<size_t, sizeof...(Args)>::type;
41 };
42 
43 // Callable helper for pointer to function defers to callable helper for
44 // function type
45 template <typename R, typename ... Args>
46 struct callable_helper<R(*)(Args...)> : callable_helper<R(Args...)>
47 {};
48 
49 // Callable helper for pointer to const member function defers to callable helper
50 // for function type
51 template <typename C, typename R, typename ... Args>
52 struct callable_helper<R(C::*)(Args...) const> : callable_helper<R(Args...)>
53 {};
54 
55 // Callable helper for pointer to non-const member function defers to callable helper
56 // for function type
57 template <typename C, typename R, typename ... Args>
58 struct callable_helper<R(C::*)(Args...)> : callable_helper<R(Args...)>
59 {};
60 
61 // Callable single interface defers to corresponding callable helper
62 template <typename T>
64 {};
65 
66 // Convenience meta-function for getting arity of a callable
67 template <typename T>
68 constexpr size_t callable_arity() {
69  return typename callable<T>::arity();
70 }
71 
72 // Meta-function for determining if a callable returns void
73 template <typename G>
74 constexpr bool has_void_return() {
75  return std::is_same<void,
76  typename std::result_of<G()>::type
77  >::value;
78 }
79 
80 template <typename F, typename I>
81 constexpr bool has_void_return() {
82  return std::is_same<void,
83  typename std::result_of<F(I)>::type
84  >::value;
85 }
86 
87 // Meta-function for determining if a callable has arguments
88 template <typename F>
89 constexpr bool has_arguments() {
90  return typename internal::callable<F>::arity() != 0;
91 }
92 
93 } // end namespace internal
94 
95 
96 // Meta-function for determining if F is consumer of I
97 template <typename F, typename I>
98 constexpr bool is_consumer = internal::has_void_return<F,I>();
99 
100 // Meta-function for determining if G is a generator
101 template <typename G>
102 constexpr bool is_generator =
103  !internal::has_void_return<G>();
104 
105 // Concept emulation requiring a callable generating values
106 template <typename G>
108  typename std::enable_if_t<is_generator<G>, int>;
109 
110 
111 // Concept emulation requiring a callable with one or more arguments
112 template <typename F>
114  typename std::enable_if_t<internal::has_arguments<F>(), int>;
115 
116 // Concept emulation requiring a callable consuming values
117 template <typename F, typename I>
119  typename std::enable_if_t<internal::has_void_return<F(I)>(), int>;
120 
121 
122 
123 } // end namespace grppi
124 
125 #endif
constexpr size_t callable_arity()
Definition: callable_traits.h:68
constexpr bool has_void_return()
Definition: callable_traits.h:74
constexpr bool has_arguments()
Definition: callable_traits.h:89
Definition: callable_traits.h:21
typename std::enable_if_t< internal::has_void_return< F(I)>(), int > requires_consumer
Definition: callable_traits.h:119
typename std::enable_if_t< is_generator< G >, int > requires_generator
Definition: callable_traits.h:108
constexpr bool is_generator
Definition: callable_traits.h:102
constexpr bool is_consumer
Definition: callable_traits.h:98
typename std::enable_if_t< internal::has_arguments< F >(), int > requires_arguments
Definition: callable_traits.h:114
typename std::integral_constant< size_t, sizeof...(Args)>::type arity
Definition: callable_traits.h:40
Definition: callable_traits.h:34
Definition: callable_traits.h:64
Identity type trait.
Definition: callable_traits.h:27
T type
Definition: callable_traits.h:28