Shaka Player Embedded
templates.h
Go to the documentation of this file.
1 // Copyright 2016 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SHAKA_EMBEDDED_UTIL_TEMPLATES_H_
16 #define SHAKA_EMBEDDED_UTIL_TEMPLATES_H_
17 
18 #include <tuple>
19 #include <type_traits>
20 
21 #if defined(USING_V8)
22 # include <v8.h>
23 #endif
24 
25 namespace shaka {
26 namespace util {
27 
32 template <typename T, typename = void>
33 struct is_v8_type : std::false_type {};
34 // Can only call sizeof(T) for complete types.
35 #if defined(USING_V8)
36 template <typename T>
37 struct is_v8_type<T, decltype(void(sizeof(T)))> : std::is_base_of<v8::Data, T> {
38 };
39 #endif
40 
45 template <typename T>
46 struct is_number
47  : std::integral_constant<bool, std::is_arithmetic<T>::value &&
48  !std::is_same<T, bool>::value> {};
49 
50 template <typename A, typename B>
52  : std::is_same<typename std::decay<A>::type, typename std::decay<B>::type> {
53 };
54 
55 template <bool B, typename T = void>
57 
58 template <typename...>
59 using void_t = void;
60 
61 
62 namespace impl {
63 
64 template <typename T> int has_call_operator(decltype(&T::operator()));
65 template <typename T> char has_call_operator(...);
66 
67 // \cond Doxygen_Skip
68 template <typename Func, typename = void>
69 struct func_traits {};
70 // \endcond Doxygen_Skip
71 
72 template <typename Func>
75 
76 template <typename Ret, typename... Args>
77 struct func_traits<Ret(*)(Args...), void> {
78  using return_type = Ret;
79  using this_type = void;
80  using argument_types = std::tuple<Args...>;
81  template <size_t I>
83 
84  static constexpr const size_t argument_count = sizeof...(Args);
85 };
86 
87 template <typename Ret, typename This, typename... Args>
88 struct func_traits<Ret(This::*)(Args...), void> {
89  using return_type = Ret;
90  using this_type = This;
91  using argument_types = std::tuple<Args...>;
92  template <size_t I>
94 
95  static constexpr const size_t argument_count = sizeof...(Args);
96 };
97 
98 template <typename Ret, typename This, typename... Args>
99 struct func_traits<Ret(This::*)(Args...) const, void> {
100  using return_type = Ret;
101  using this_type = This;
102  using argument_types = std::tuple<Args...>;
103  template <size_t I>
105 
106  static constexpr const size_t argument_count = sizeof...(Args);
107 };
108 
109 } // namespace impl
110 
116 template <typename Func>
117 struct func_traits : impl::func_traits<typename std::decay<Func>::type> {
118  using type = Func;
119 };
120 
121 template <typename T, typename = void>
122 struct is_callable : std::false_type {};
123 template <typename T>
124 struct is_callable<T, void_t<typename func_traits<T>::return_type>>
125  : std::true_type {};
126 
127 } // namespace util
128 } // namespace shaka
129 
130 #endif // SHAKA_EMBEDDED_UTIL_TEMPLATES_H_
char has_call_operator(...)
void void_t
Definition: templates.h:59
typename std::tuple_element< I, argument_types >::type argument_type
Definition: templates.h:93
typename std::tuple_element< I, argument_types >::type argument_type
Definition: templates.h:82
typename std::enable_if< B, T >::type enable_if_t
Definition: templates.h:56
ExceptionCode type
typename std::tuple_element< I, argument_types >::type argument_type
Definition: templates.h:104