GrPPI  0.3.1
Generic and Reusable Parallel Pattern Interface
callable_traits.h
Go to the documentation of this file.
1 
21 #ifndef GRPPI_COMMON_CALLABLE_TRAITS_H
22 #define GRPPI_COMMON_CALLABLE_TRAITS_H
23 
24 #include <type_traits>
25 
26 namespace grppi {
27 
28 namespace internal {
29 
31 template <typename T>
32 struct identity {
33  using type = T;
34 };
35 
36 // Callable helper for function objects defers to pointer to call operator
37 template <typename T>
38 struct callable_helper : callable_helper<decltype(&T::operator())>
39 {};
40 
41 // Callable helper for function type
42 template <typename R, typename ... Args>
43 struct callable_helper<R(Args...)> : identity<R(Args...)>
44 {
45  using arity = typename std::integral_constant<size_t, sizeof...(Args)>::type;
46 };
47 
48 // Callable helper for pointer to function defers to callable helper for
49 // function type
50 template <typename R, typename ... Args>
51 struct callable_helper<R(*)(Args...)> : callable_helper<R(Args...)>
52 {};
53 
54 // Callalble helper for pointer to const member function defers to callable helper
55 // for function type
56 template <typename C, typename R, typename ... Args>
57 struct callable_helper<R(C::*)(Args...) const> : callable_helper<R(Args...)>
58 {};
59 
60 // Callalble helper for pointer to non-const member function defers to callable helper
61 // for function type
62 template <typename C, typename R, typename ... Args>
63 struct callable_helper<R(C::*)(Args...)> : callable_helper<R(Args...)>
64 {};
65 
66 // Callable single interface defers to correspondign callable helper
67 template <typename T>
69 {};
70 
71 // Convenience meta-function for getting arity of a callable
72 template <typename T>
73 constexpr size_t callable_arity() {
74  return typename callable<T>::arity();
75 }
76 
77 // Meta-function for determining if a callable returns void
78 template <typename G>
79 constexpr bool has_void_return() {
80  return std::is_same<void,
81  typename std::result_of<G()>::type
82  >::value;
83 }
84 
85 template <typename F, typename I>
86 constexpr bool has_void_return() {
87  return std::is_same<void,
88  typename std::result_of<F(I)>::type
89  >::value;
90 }
91 
92 // Meta-function for determining if a callable has arguments
93 template <typename F>
94 constexpr bool has_arguments() {
95  return typename internal::callable<F>::arity() != 0;
96 }
97 
98 } // end namespace internal
99 
100 
101 // Meta-function for determining if F is consumer of I
102 template <typename F, typename I>
103 constexpr bool is_consumer = internal::has_void_return<F,I>();
104 
105 // Meta-function for determining if G is a generator
106 template <typename G>
107 constexpr bool is_generator =
108  !internal::has_void_return<G>();
109 
110 // Concept emulation requiring a callable generating values
111 template <typename G>
112 using requires_generator =
113  typename std::enable_if_t<is_generator<G>, int>;
114 
115 
116 // Concept emulation requiring a callable with one or more arguments
117 template <typename F>
118 using requires_arguments =
119  typename std::enable_if_t<internal::has_arguments<F>(), int>;
120 
121 // Concept emulation requiring a callable consuming values
122 template <typename F, typename I>
123 using requires_consumer =
124  typename std::enable_if_t<internal::has_void_return<F(I)>(), int>;
125 
126 
127 
128 } // end namespace grppi
129 
130 #endif
typename std::enable_if_t< internal::has_void_return< F(I)>(), int > requires_consumer
Definition: callable_traits.h:124
Definition: callable_traits.h:26
typename std::enable_if_t< internal::has_arguments< F >(), int > requires_arguments
Definition: callable_traits.h:119
constexpr bool is_consumer
Definition: callable_traits.h:103
constexpr bool has_void_return()
Definition: callable_traits.h:79
constexpr size_t callable_arity()
Definition: callable_traits.h:73
Definition: callable_traits.h:38
Definition: callable_traits.h:68
constexpr bool is_generator
Definition: callable_traits.h:107
Identity type trait.
Definition: callable_traits.h:32
typename std::integral_constant< size_t, sizeof...(Args)>::type arity
Definition: callable_traits.h:45
R(Args...) type
Definition: callable_traits.h:33
typename std::enable_if_t< is_generator< G >, int > requires_generator
Definition: callable_traits.h:113
constexpr bool has_arguments()
Definition: callable_traits.h:94