GrPPI  0.2
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 namespace grppi {
25 
26 namespace internal {
27 
29 template <typename T>
30 struct identity {
31  using type = T;
32 };
33 
34 // Callable helper for function objects defers to pointer to call operator
35 template <typename T>
36 struct callable_helper : callable_helper<decltype(&T::operator())>
37 {};
38 
39 // Callable helper for function type
40 template <typename R, typename ... Args>
41 struct callable_helper<R(Args...)> : identity<R(Args...)>
42 {
43  using arity = typename std::integral_constant<size_t, sizeof...(Args)>::type;
44 };
45 
46 // Callable helper for pointer to function defers to callable helper for
47 // function type
48 template <typename R, typename ... Args>
49 struct callable_helper<R(*)(Args...)> : callable_helper<R(Args...)>
50 {};
51 
52 // Callalble helper for pointer to const member function defers to callable helper
53 // for function type
54 template <typename C, typename R, typename ... Args>
55 struct callable_helper<R(C::*)(Args...) const> : callable_helper<R(Args...)>
56 {};
57 
58 // Callalble helper for pointer to non-const member function defers to callable helper
59 // for function type
60 template <typename C, typename R, typename ... Args>
61 struct callable_helper<R(C::*)(Args...)> : callable_helper<R(Args...)>
62 {};
63 
64 // Callable single interface defers to correspondign callable helper
65 template <typename T>
67 {};
68 
69 // Convenience meta-function for getting arity of a callable
70 template <typename T>
71 constexpr size_t callable_arity() {
72  return typename callable<T>::arity();
73 }
74 
75 // Meta-function for determining if a callable has arguments
76 template <typename F>
77 constexpr bool has_arguments() {
78  return typename callable<F>::arity() != 0;
79 }
80 
81 } // end namespace internal
82 
83 // Concept emulation requiring a callable with no arguments
84 template <typename F>
86  typename std::enable_if_t<!internal::has_arguments<F>(), int>;
87 
88 // Concept emulation requiring a callable with one or more arguments
89 template <typename F>
90 using requires_arguments =
91  typename std::enable_if_t<internal::has_arguments<F>(), int>;
92 
93 
94 
95 
96 } // end namespace grppi
97 
98 #endif
Definition: callable_traits.h:24
typename std::enable_if_t< internal::has_arguments< F >(), int > requires_arguments
Definition: callable_traits.h:91
typename std::enable_if_t<!internal::has_arguments< F >(), int > requires_no_arguments
Definition: callable_traits.h:86
constexpr size_t callable_arity()
Definition: callable_traits.h:71
Definition: callable_traits.h:36
Definition: callable_traits.h:66
Identity type trait.
Definition: callable_traits.h:30
typename std::integral_constant< size_t, sizeof...(Args)>::type arity
Definition: callable_traits.h:43
R(Args...) type
Definition: callable_traits.h:31
constexpr bool has_arguments()
Definition: callable_traits.h:77