All Classes Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
function.hpp
1 // The MIT License (MIT)
2 
3 // Copyright (c) 2012-2014 Danny Y., Rapptz
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy of
6 // this software and associated documentation files (the "Software"), to deal in
7 // the Software without restriction, including without limitation the rights to
8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 // the Software, and to permit persons to whom the Software is furnished to do so,
10 // subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 #ifndef GEARS_CONCEPTS_FUNCTION_HPP
23 #define GEARS_CONCEPTS_FUNCTION_HPP
24 
25 #include "alias.hpp"
26 #include <utility>
27 
28 namespace gears {
29 namespace concepts {
30 namespace detail {
31 template<typename...>
32 struct void_type {
33  using type = void;
34 };
35 
36 template<typename... Args>
37 using Void_ = typename void_type<Args...>::type;
38 
39 template<typename T>
40 void sink(T);
41 
42 template<typename T, typename... Args>
43 using FRet = decltype(std::declval<T>()(std::declval<Args>()...));
44 
45 template<typename T, typename Signature, typename = void>
46 struct is_callable : std::false_type {
47  static_assert(std::is_function<Signature>(), "Signature must be a function");
48 };
49 
50 template<typename T, typename = concept_checker_t>
51 struct is_function_impl : std::is_function<T> {};
52 
53 template<typename T>
54 struct is_function_impl<T, TrueIf<std::is_class<Bare<T>>>> {
55  using yes = char;
56  using no = struct { char s[2]; };
57 
58  struct F { void operator()(); };
59  struct Derived : T, F { };
60  template<typename U, U> struct Check;
61 
62  template<typename V>
63  static no test(Check<void (F::*)(), &V::operator()>*);
64 
65  template<typename>
66  static yes test(...);
67 
68  static const bool value = sizeof(test<Derived>(0)) == sizeof(yes);
69 };
70 
71 template<typename T, typename R, typename... Args>
72 struct is_callable<T, R(Args...), Void_<decltype(sink<R>(std::declval<T>()(std::declval<Args>()...)))>> : std::true_type {};
73 
74 template<typename T, typename... Args>
75 struct is_callable<T, void(Args...), Void_<FRet<T, Args...>>> : std::is_void<FRet<T, Args...>> {};
76 
77 struct is_generator {
78  template<typename T>
79  static auto test(int) -> decltype(std::declval<T&>()(), std::true_type{}) {}
80  template<typename>
81  static std::false_type test(...);
82 };
83 } // detail
84 
108 template<typename T, typename Signature>
109 struct Callable : detail::is_callable<T, Signature> {};
110 
120 template<typename T>
121 struct Generator : TraitOf<detail::is_generator, T> {};
122 
131 template<typename T>
132 struct Function : std::integral_constant<bool, detail::is_function_impl<typename std::remove_pointer<T>::type>::value> {};
133 } // concepts
134 } // gears
135 
136 #endif // GEARS_CONCEPTS_FUNCTION_HPP