2 Copyright (c) 2005-2019 Intel Corporation
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
17 #ifndef __TBB_template_helpers_H
18 #define __TBB_template_helpers_H
22 #include "../tbb_config.h"
23 #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT
24 #include <type_traits>
26 #if __TBB_CPP11_PRESENT
28 #include <memory> // allocator_traits
31 namespace tbb { namespace internal {
33 //! Enables one or the other code branches
34 template<bool Condition, typename T = void> struct enable_if {};
35 template<typename T> struct enable_if<true, T> { typedef T type; };
37 //! Strips its template type argument from cv- and ref-qualifiers
38 template<typename T> struct strip { typedef T type; };
39 template<typename T> struct strip<const T> { typedef T type; };
40 template<typename T> struct strip<volatile T> { typedef T type; };
41 template<typename T> struct strip<const volatile T> { typedef T type; };
42 template<typename T> struct strip<T&> { typedef T type; };
43 template<typename T> struct strip<const T&> { typedef T type; };
44 template<typename T> struct strip<volatile T&> { typedef T type; };
45 template<typename T> struct strip<const volatile T&> { typedef T type; };
46 //! Specialization for function pointers
47 template<typename T> struct strip<T(&)()> { typedef T(*type)(); };
48 #if __TBB_CPP11_RVALUE_REF_PRESENT
49 template<typename T> struct strip<T&&> { typedef T type; };
50 template<typename T> struct strip<const T&&> { typedef T type; };
51 template<typename T> struct strip<volatile T&&> { typedef T type; };
52 template<typename T> struct strip<const volatile T&&> { typedef T type; };
54 //! Specialization for arrays converts to a corresponding pointer
55 template<typename T, std::size_t N> struct strip<T(&)[N]> { typedef T* type; };
56 template<typename T, std::size_t N> struct strip<const T(&)[N]> { typedef const T* type; };
57 template<typename T, std::size_t N> struct strip<volatile T(&)[N]> { typedef volatile T* type; };
58 template<typename T, std::size_t N> struct strip<const volatile T(&)[N]> { typedef const volatile T* type; };
60 //! Detects whether two given types are the same
61 template<class U, class V> struct is_same_type { static const bool value = false; };
62 template<class W> struct is_same_type<W,W> { static const bool value = true; };
64 template<typename T> struct is_ref { static const bool value = false; };
65 template<typename U> struct is_ref<U&> { static const bool value = true; };
67 //! Partial support for std::is_integral
68 template<typename T> struct is_integral_impl { static const bool value = false; };
69 template<> struct is_integral_impl<bool> { static const bool value = true; };
70 template<> struct is_integral_impl<char> { static const bool value = true; };
71 #if __TBB_CPP11_PRESENT
72 template<> struct is_integral_impl<char16_t> { static const bool value = true; };
73 template<> struct is_integral_impl<char32_t> { static const bool value = true; };
75 template<> struct is_integral_impl<wchar_t> { static const bool value = true; };
76 template<> struct is_integral_impl<short> { static const bool value = true; };
77 template<> struct is_integral_impl<int> { static const bool value = true; };
78 template<> struct is_integral_impl<long> { static const bool value = true; };
79 template<> struct is_integral_impl<long long> { static const bool value = true; };
82 struct is_integral : is_integral_impl<typename strip<T>::type> {};
84 #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT
85 //! std::void_t internal implementation (to avoid GCC < 4.7 "template aliases" absence)
86 template<typename...> struct void_t { typedef void type; };
89 #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT
91 // Generic SFINAE helper for expression checks, based on the idea demonstrated in ISO C++ paper n4502
92 template<typename T, typename, template<typename> class... Checks>
93 struct supports_impl { typedef std::false_type type; };
94 template<typename T, template<typename> class... Checks>
95 struct supports_impl<T, typename void_t<Checks<T>...>::type, Checks...> { typedef std::true_type type; };
97 template<typename T, template<typename> class... Checks>
98 using supports = typename supports_impl<T, void, Checks...>::type;
100 #endif /* __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT */
102 #if __TBB_CPP11_RVALUE_REF_PRESENT && __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT
104 //! Allows to store a function parameter pack as a variable and later pass it to another function
105 template< typename... Types >
111 typedef stored_pack<> pack_type;
114 // Friend front-end functions
115 template< typename F, typename Pack > friend void call( F&& f, Pack&& p );
116 template< typename Ret, typename F, typename Pack > friend Ret call_and_return( F&& f, Pack&& p );
119 // Ideally, ref-qualified non-static methods would be used,
120 // but that would greatly reduce the set of compilers where it works.
121 template< typename Ret, typename F, typename... Preceding >
122 static Ret call( F&& f, const pack_type& /*pack*/, Preceding&&... params ) {
123 return std::forward<F>(f)( std::forward<Preceding>(params)... );
125 template< typename Ret, typename F, typename... Preceding >
126 static Ret call( F&& f, pack_type&& /*pack*/, Preceding&&... params ) {
127 return std::forward<F>(f)( std::forward<Preceding>(params)... );
131 template< typename T, typename... Types >
132 struct stored_pack<T, Types...> : stored_pack<Types...>
134 typedef stored_pack<T, Types...> pack_type;
135 typedef stored_pack<Types...> pack_remainder;
136 // Since lifetime of original values is out of control, copies should be made.
137 // Thus references should be stripped away from the deduced type.
138 typename strip<T>::type leftmost_value;
140 // Here rvalue references act in the same way as forwarding references,
141 // as long as class template parameters were deduced via forwarding references.
142 stored_pack( T&& t, Types&&... types )
143 : pack_remainder(std::forward<Types>(types)...), leftmost_value(std::forward<T>(t)) {}
145 // Friend front-end functions
146 template< typename F, typename Pack > friend void call( F&& f, Pack&& p );
147 template< typename Ret, typename F, typename Pack > friend Ret call_and_return( F&& f, Pack&& p );
150 template< typename Ret, typename F, typename... Preceding >
151 static Ret call( F&& f, pack_type& pack, Preceding&&... params ) {
152 return pack_remainder::template call<Ret>(
153 std::forward<F>(f), static_cast<pack_remainder&>(pack),
154 std::forward<Preceding>(params)... , pack.leftmost_value
157 template< typename Ret, typename F, typename... Preceding >
158 static Ret call( F&& f, const pack_type& pack, Preceding&&... params ) {
159 return pack_remainder::template call<Ret>(
160 std::forward<F>(f), static_cast<const pack_remainder&>(pack),
161 std::forward<Preceding>(params)... , pack.leftmost_value
164 template< typename Ret, typename F, typename... Preceding >
165 static Ret call( F&& f, pack_type&& pack, Preceding&&... params ) {
166 return pack_remainder::template call<Ret>(
167 std::forward<F>(f), static_cast<pack_remainder&&>(pack),
168 std::forward<Preceding>(params)... , std::move(pack.leftmost_value)
173 //! Calls the given function with arguments taken from a stored_pack
174 template< typename F, typename Pack >
175 void call( F&& f, Pack&& p ) {
176 strip<Pack>::type::template call<void>( std::forward<F>(f), std::forward<Pack>(p) );
179 template< typename Ret, typename F, typename Pack >
180 Ret call_and_return( F&& f, Pack&& p ) {
181 return strip<Pack>::type::template call<Ret>( std::forward<F>(f), std::forward<Pack>(p) );
184 template< typename... Types >
185 stored_pack<Types...> save_pack( Types&&... types ) {
186 return stored_pack<Types...>( std::forward<Types>(types)... );
189 #endif /* __TBB_CPP11_RVALUE_REF_PRESENT && __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT */
191 #if __TBB_CPP14_INTEGER_SEQUENCE_PRESENT
193 using std::index_sequence;
194 using std::make_index_sequence;
196 #elif __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT
198 template<std::size_t... S> class index_sequence {};
200 template<std::size_t N, std::size_t... S>
201 struct make_index_sequence_impl : make_index_sequence_impl < N - 1, N - 1, S... > {};
203 template<std::size_t... S>
204 struct make_index_sequence_impl <0, S...> {
205 using type = index_sequence<S...>;
208 template<std::size_t N>
209 using make_index_sequence = typename tbb::internal::make_index_sequence_impl<N>::type;
211 #endif /* __TBB_CPP14_INTEGER_SEQUENCE_PRESENT */
213 #if __TBB_CPP11_PRESENT
215 template< typename Iter >
216 using iterator_value_t = typename std::iterator_traits<Iter>::value_type;
218 template< typename Iter >
219 using iterator_key_t = typename std::remove_const<typename iterator_value_t<Iter>::first_type>::type;
221 template< typename Iter >
222 using iterator_mapped_t = typename iterator_value_t<Iter>::second_type;
224 template< typename A > using value_type = typename A::value_type;
225 template< typename A > using alloc_ptr_t = typename std::allocator_traits<A>::pointer;
226 template< typename A > using has_allocate = decltype(std::declval<alloc_ptr_t<A>&>() = std::declval<A>().allocate(0));
227 template< typename A > using has_deallocate = decltype(std::declval<A>().deallocate(std::declval<alloc_ptr_t<A>>(), 0));
229 // value_type should be checked first because it can be used in other checks (via allocator_traits)
230 template< typename T >
231 using is_allocator = supports<T, value_type, has_allocate, has_deallocate>;
233 #if __TBB_CPP14_VARIABLE_TEMPLATES_PRESENT
235 template< typename T >
236 static constexpr bool is_allocator_v = is_allocator<T>::value;
238 #endif /*__TBB_CPP14_VARIABLE_TEMPLATES */
240 template< std::size_t N, typename... Args >
241 struct pack_element {
245 template< std::size_t N, typename T, typename... Args >
246 struct pack_element<N, T, Args...> {
247 using type = typename pack_element<N - 1, Args...>::type;
250 template< typename T, typename... Args >
251 struct pack_element<0, T, Args...> {
255 template< std::size_t N, typename... Args >
256 using pack_element_t = typename pack_element<N, Args...>::type;
258 template <typename Comp> using is_transparent = typename Comp::is_transparent;
260 template <typename Comp>
261 using has_is_transparent = supports<Comp, is_transparent>;
263 #endif /* __TBB_CPP11_PRESENT */
265 } } // namespace internal, namespace tbb
267 #endif /* __TBB_template_helpers_H */