Thrill  0.1
function_traits.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/common/function_traits.hpp
3  *
4  * Part of Project Thrill - http://project-thrill.org
5  *
6  * Copyright (C) 2015 Sebastian Lamm <[email protected]>
7  * Copyright (C) 2015 Timo Bingmann <[email protected]>
8  *
9  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
10  ******************************************************************************/
11 
12 #pragma once
13 #ifndef THRILL_COMMON_FUNCTION_TRAITS_HEADER
14 #define THRILL_COMMON_FUNCTION_TRAITS_HEADER
15 
16 #include <cstddef>
17 #include <tuple>
18 #include <type_traits>
19 
20 namespace thrill {
21 namespace common {
22 
23 #ifndef THRILL_DOXYGEN_IGNORE
24 
25 // taken from: http://stackoverflow.com/questions/7943525/is-it-possible-to-figure-out-the-parameter-type-and-return-type-of-a-lambda
26 template <typename T>
27 struct FunctionTraits : public FunctionTraits<decltype(&T::operator ())> { };
28 // For generic types, directly use the result of the signature of its 'operator()'
29 
30 #endif
31 
32 //! specialize for pointers to const member function
33 template <typename ClassType, typename ReturnType, typename... Args>
34 struct FunctionTraits<ReturnType (ClassType::*)(Args...) const> {
35 
36  //! arity is the number of arguments.
37  static constexpr size_t arity = sizeof ... (Args);
38 
39  using result_type = ReturnType;
40  using is_const = std::true_type;
41 
42  //! the tuple of arguments
43  using args_tuple = std::tuple<Args...>;
44 
45  //! the tuple of arguments: with remove_cv and remove_reference applied.
46  using args_tuple_plain = std::tuple<
47  typename std::remove_cv<
48  typename std::remove_reference<Args>::type>::type...>;
49 
50  //! the i-th argument is equivalent to the i-th tuple element of a tuple
51  //! composed of those arguments.
52  template <size_t i>
53  using arg = typename std::tuple_element<i, args_tuple>::type;
54 
55  //! return i-th argument reduced to plain type: remove_cv and
56  //! remove_reference.
57  template <size_t i>
58  using arg_plain =
59  typename std::remove_cv<
60  typename std::remove_reference<arg<i> >::type>::type;
61 };
62 
63 //! specialize for pointers to mutable member function
64 template <typename ClassType, typename ReturnType, typename... Args>
65 struct FunctionTraits<ReturnType (ClassType::*)(Args...)>
66  : public FunctionTraits<ReturnType (ClassType::*)(Args...) const> {
67  using is_const = std::false_type;
68 };
69 
70 //! specialize for function pointers
71 template <typename ReturnType, typename... Args>
72 struct FunctionTraits<ReturnType (*)(Args...)> {
73 
74  //! arity is the number of arguments.
75  static constexpr size_t arity = sizeof ... (Args);
76 
77  using result_type = ReturnType;
78  using is_const = std::true_type;
79 
80  //! the tuple of arguments
81  using args_tuple = std::tuple<Args...>;
82 
83  //! the tuple of arguments: with remove_cv and remove_reference applied.
84  using args_tuple_plain = std::tuple<
85  typename std::remove_cv<
86  typename std::remove_reference<Args>::type>::type...>;
87 
88  //! the i-th argument is equivalent to the i-th tuple element of a tuple
89  //! composed of those arguments.
90  template <size_t i>
91  using arg = typename std::tuple_element<i, args_tuple>::type;
92 
93  //! return i-th argument reduced to plain type: remove_cv and
94  //! remove_reference.
95  template <size_t i>
96  using arg_plain =
97  typename std::remove_cv<
98  typename std::remove_reference<arg<i> >::type>::type;
99 };
100 
101 } // namespace common
102 } // namespace thrill
103 
104 #endif // !THRILL_COMMON_FUNCTION_TRAITS_HEADER
105 
106 /******************************************************************************/
std::tuple< typename std::remove_cv< typename std::remove_reference< Args >::type >::type... > args_tuple_plain
the tuple of arguments: with remove_cv and remove_reference applied.
typename std::remove_cv< typename std::remove_reference< arg< i > >::type >::type arg_plain
std::tuple< Args... > args_tuple
the tuple of arguments
typename std::remove_cv< typename std::remove_reference< arg< i > >::type >::type arg_plain
typename std::tuple_element< i, args_tuple >::type arg
std::tuple< typename std::remove_cv< typename std::remove_reference< Args >::type >::type... > args_tuple_plain
the tuple of arguments: with remove_cv and remove_reference applied.