Generate an accompaniement for a given melody
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Traits.hpp
Go to the documentation of this file.
1 
10 #ifndef TRAITS_HPP_INCLUDED
11 #define TRAITS_HPP_INCLUDED
12 
13 #include <type_traits>
14 //#include <typeinfo>
15 #include <tuple>
16 
17 namespace function_traits{
18 
19  namespace _implem{
20 
24  template<class F>
26 
27  // function pointer
28  template<class R, class... Args>
29  struct function_traits<R(*)(Args...)> : public function_traits<R(Args...)>
30  {};
31 
32  //we allow also const methods
33  template<class T,class R, class... Args>
34  struct function_traits<R (T::*)(Args...) const> : public function_traits<R (T::*)(Args...)>
35  {};
36 
37 
38  template<class T,class R, class... Args>
39  struct function_traits<R (T::*)(Args...)>
40  {
41  using return_type = R;
42 
43  static constexpr std::size_t arity = sizeof...(Args);
44 
45  template <std::size_t N>
46  struct get_arg
47  {
48  static_assert(N < arity, "error: invalid parameter index.");
49  using type = typename std::tuple_element<N,std::tuple<Args...>>::type;
50  };
51  };
52 
53 
54 
62  template<int pos,typename T, typename... Args>
64 
65 
66  template<int pos,typename T, typename Arg>
67  struct check_signature<pos,T,Arg>
68  {
69  using Traits = typename function_traits<T>::template get_arg<pos>;
70  static constexpr bool value = std::is_same<Arg,typename Traits::type >::value;
71  };
72 
73  template<int pos,typename T, typename Head, typename... Tail>
74  struct check_signature<pos,T,Head,Tail...>
75  {
76  using Traits = typename function_traits<T>::template get_arg<pos>;
77  static constexpr bool value = std::is_same<Head,typename Traits::type >::value && check_signature<pos+1,T,Tail...>::value;
78  };
79 
80 
86  template<bool b1,bool b2>
87  struct static_and
88  {
89  static constexpr bool value = false;
90  };
91  template<>
92  struct static_and<true,true>
93  {
94  static constexpr bool value = true;
95  };
96 
97  } //namespace _implem
98 
99 }
100 
106 #define CREATE_MEMBER_DETECTOR(X) \
107  namespace function_traits{template<typename C,typename F> \
108  class has_##X; \
109  \
110  template<typename C, typename Ret, typename... Args> \
111  class has_##X<C,Ret(Args...)> { \
112  private: \
113  template<class U, class P = typename std::enable_if<function_traits::_implem::static_and< \
114  std::is_same< decltype( std::declval<U>().X( std::declval<Args>()... ) ), Ret >::value, \
115  function_traits::_implem::check_signature<0,decltype(&U::X),Args...>::value \
116  >::value>::type> \
117  static std::true_type check(int); \
118  template <class,class P = void> \
119  static std::false_type check(...); \
120  public: \
121  static constexpr bool value = decltype(check<C>(0))::value; \
122  };}
123 
127 #define CREATE_STATIC_MEMBER_DETECTOR(X) \
128  namespace function_traits{template <class T> \
129  class has_##X \
130  { \
131  template<class U, class = typename std::enable_if<!std::is_member_pointer<decltype(&U::X)>::value>::type> \
132  static std::true_type check(int); \
133  template <class> \
134  static std::false_type check(...); \
135  public: \
136  static constexpr bool value = decltype(check<T>(0))::value; \
137  };}
138 
139 
140 #endif
Definition: Traits.hpp:87