Support Class Library
A set of tools providing classes and utility
fn_introspect.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional>
7 
8 namespace scl{
9  namespace tools{
10  namespace meta{
14  template <class>
16 
22  template <class R, class... Args>
23  struct function_traits<std::function<R(Args...)>>{
28  constexpr static size_t arity = sizeof...(Args);
29 
34  using return_type = R;
35 
41  template <size_t i>
42  using arg_t = typename std::tuple_element<i, std::tuple<Args...>>::type;
43  };
44 
45  template <class T>
47 
48  template <class T, size_t i>
50 
51 
52 
53  /*
54  * Huge thanks to
55  * https://stackoverflow.com/A/27826081/7316365
56  */
57  template <class, class>
58  struct fn;
59 
60  template <class R, class... Args>
61  struct fn<R(*)(Args...), void> /*: public std::function<R(Args...)>*/{
62  using fn_type = std::function<R(Args...)>;
63  };
64 
65  template <class Class, class R, class... Args>
66  struct fn<R(Class::* const)(Args...), void> /*: public std::function<R(const Class&, Args...)>*/{
67  using fn_type = std::function<R(const Class&, Args...)>;
68  };
69 
70  template <class Class, class R, class... Args>
71  struct fn<R(Class::* const)(Args...), std::nullptr_t>{
72  using fn_type = std::function<R(Args...)>;
73  };
74 
75  template <class F>
76  struct fn<F, /*META::enable_if_t<
77  META::exists<*/decltype(&F::operator())/*>()
78  >*/> : public fn<decltype(&F::operator()), std::nullptr_t>{
80  };
81 
82  template <class F>
83  using fn_t = fn<F, void>;
84 
85  template <class F>
86  using std_fn_type = typename fn_t<F>::fn_type;
87 
88  template <class R, class... Args>
89  std_fn_type<R(*)(Args...)> as_fn(R(*f)(Args...)){
90  return (std_fn_type<R(*)(Args...)>){f};
91  }
92 
93  template <class Class, class R, class... Args>
94  std_fn_type<R(Class::* const)(Args...)> as_fn(R(Class::* const f)(Args...) const){
95 // return (typename META::fn<R(Class::*)(Args...)>::fn_type){f};
96  return std::mem_fn(f);
97  }
98 
99  template <class F>
101  return (typename fn<F, void>::fn_type){f};
102  }
103  }
104  }
105 }
typename fn< decltype(&F::operator()), std::nullptr_t >::fn_type fn_type
Definition: fn_introspect.h:79
typename function_traits< T >::template arg_t< i > arg_t
Definition: fn_introspect.h:49
Global namespace of the SCL.
Definition: alias.hpp:3
STL namespace.
std_fn_type< R(*)(Args...)> as_fn(R(*f)(Args...))
Definition: fn_introspect.h:89
typename std::tuple_element< i, std::tuple< Args... > >::type arg_t
Definition: fn_introspect.h:42
std::function< R(const Class &, Args...)> fn_type
Definition: fn_introspect.h:67
typename fn_t< F >::fn_type std_fn_type
Definition: fn_introspect.h:86
Traits for callable types.
Definition: fn_introspect.h:15
typename function_traits< T >::return_type return_t
Definition: fn_introspect.h:46