87c3efbeff0cef0cb44124a41cb6509b423f55bb
[platform/upstream/tbb.git] / include / tbb / internal / _template_helpers.h
1 /*
2     Copyright (c) 2005-2019 Intel Corporation
3
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
7
8         http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
17 #ifndef __TBB_template_helpers_H
18 #define __TBB_template_helpers_H
19
20 #include <utility>
21 #include <cstddef>
22 #include "../tbb_config.h"
23 #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT
24 #include <type_traits>
25 #endif
26 #if __TBB_CPP11_PRESENT
27 #include <iterator>
28 #include <memory> // allocator_traits
29 #endif
30
31 namespace tbb { namespace internal {
32
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; };
36
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; };
53 #endif
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; };
59
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; };
63
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; };
66
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;  };
74 #endif
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;  };
80
81 template<typename T>
82 struct is_integral : is_integral_impl<typename strip<T>::type> {};
83
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; };
87 #endif
88
89 #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT
90
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; };
96
97 template<typename T, template<typename> class... Checks>
98 using supports = typename supports_impl<T, void, Checks...>::type;
99
100 #endif /* __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT */
101
102 #if __TBB_CPP11_RVALUE_REF_PRESENT && __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT
103
104 //! Allows to store a function parameter pack as a variable and later pass it to another function
105 template< typename... Types >
106 struct stored_pack;
107
108 template<>
109 struct stored_pack<>
110 {
111     typedef stored_pack<> pack_type;
112     stored_pack() {}
113
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 );
117
118 protected:
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)... );
124     }
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)... );
128     }
129 };
130
131 template< typename T, typename... Types >
132 struct stored_pack<T, Types...> : stored_pack<Types...>
133 {
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;
139
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)) {}
144
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 );
148
149 protected:
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
155         );
156     }
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
162         );
163     }
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)
169         );
170     }
171 };
172
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) );
177 }
178
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) );
182 }
183
184 template< typename... Types >
185 stored_pack<Types...> save_pack( Types&&... types ) {
186     return stored_pack<Types...>( std::forward<Types>(types)... );
187 }
188
189 #endif /* __TBB_CPP11_RVALUE_REF_PRESENT && __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT */
190
191 #if __TBB_CPP14_INTEGER_SEQUENCE_PRESENT
192
193 using std::index_sequence;
194 using std::make_index_sequence;
195
196 #elif __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT
197
198 template<std::size_t... S> class index_sequence {};
199
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... > {};
202
203 template<std::size_t... S>
204 struct make_index_sequence_impl <0, S...> {
205     using type = index_sequence<S...>;
206 };
207
208 template<std::size_t N>
209 using make_index_sequence = typename tbb::internal::make_index_sequence_impl<N>::type;
210
211 #endif /* __TBB_CPP14_INTEGER_SEQUENCE_PRESENT */
212
213 #if __TBB_CPP11_PRESENT
214
215 template< typename Iter >
216 using iterator_value_t = typename std::iterator_traits<Iter>::value_type;
217
218 template< typename Iter >
219 using iterator_key_t = typename std::remove_const<typename iterator_value_t<Iter>::first_type>::type;
220
221 template< typename Iter >
222 using iterator_mapped_t = typename iterator_value_t<Iter>::second_type;
223
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));
228
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>;
232
233 #if __TBB_CPP14_VARIABLE_TEMPLATES_PRESENT
234
235 template< typename T >
236 static constexpr bool is_allocator_v = is_allocator<T>::value;
237
238 #endif /*__TBB_CPP14_VARIABLE_TEMPLATES */
239
240 template< std::size_t N, typename... Args >
241 struct pack_element {
242     using type = void;
243 };
244
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;
248 };
249
250 template< typename T, typename... Args >
251 struct pack_element<0, T, Args...> {
252     using type = T;
253 };
254
255 template< std::size_t N, typename... Args >
256 using pack_element_t = typename pack_element<N, Args...>::type;
257
258 template <typename Comp> using is_transparent = typename Comp::is_transparent;
259
260 template <typename Comp>
261 using has_is_transparent = supports<Comp, is_transparent>;
262
263 #endif /* __TBB_CPP11_PRESENT */
264
265 } } // namespace internal, namespace tbb
266
267 #endif /* __TBB_template_helpers_H */