Home ⌂Doc Index ◂Up ▴
Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
_template_helpers.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2005-2020 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 
34 template<bool Condition, typename T = void> struct enable_if {};
35 template<typename T> struct enable_if<true, T> { typedef T type; };
36 
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; };
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 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 
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 
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 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 
105 template< typename... Types >
106 struct stored_pack;
107 
108 template<>
109 struct stored_pack<>
110 {
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.
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 
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;
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>
210 
211 #endif /* __TBB_CPP14_INTEGER_SEQUENCE_PRESENT */
212 
213 #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT
214 template<typename... Args>
215 struct conjunction;
216 
217 template<typename First, typename... Args>
218 struct conjunction<First, Args...>
219  : std::conditional<bool(First::value), conjunction<Args...>, First>::type {};
220 
221 template<typename T>
222 struct conjunction<T> : T {};
223 
224 template<>
226 
227 #endif
228 
229 #if __TBB_CPP11_PRESENT
230 
231 template< typename Iter >
232 using iterator_value_t = typename std::iterator_traits<Iter>::value_type;
233 
234 template< typename Iter >
235 using iterator_key_t = typename std::remove_const<typename iterator_value_t<Iter>::first_type>::type;
236 
237 template< typename Iter >
238 using iterator_mapped_t = typename iterator_value_t<Iter>::second_type;
239 
240 template< typename A > using value_type = typename A::value_type;
241 template< typename A > using alloc_ptr_t = typename std::allocator_traits<A>::pointer;
242 template< typename A > using has_allocate = decltype(std::declval<alloc_ptr_t<A>&>() = std::declval<A>().allocate(0));
243 template< typename A > using has_deallocate = decltype(std::declval<A>().deallocate(std::declval<alloc_ptr_t<A>>(), 0));
244 
245 // value_type should be checked first because it can be used in other checks (via allocator_traits)
246 template< typename T >
248 
249 #if __TBB_CPP14_VARIABLE_TEMPLATES_PRESENT
250 
251 template< typename T >
252 static constexpr bool is_allocator_v = is_allocator<T>::value;
253 
254 #endif /*__TBB_CPP14_VARIABLE_TEMPLATES */
255 
256 template< std::size_t N, typename... Args >
257 struct pack_element {
258  using type = void;
259 };
260 
261 template< std::size_t N, typename T, typename... Args >
262 struct pack_element<N, T, Args...> {
263  using type = typename pack_element<N - 1, Args...>::type;
264 };
265 
266 template< typename T, typename... Args >
267 struct pack_element<0, T, Args...> {
268  using type = T;
269 };
270 
271 template< std::size_t N, typename... Args >
272 using pack_element_t = typename pack_element<N, Args...>::type;
273 
274 // Helper alias for heterogeneous lookup functions in containers
275 // template parameter K and std::conditional are needed to provide immediate context
276 // and postpone getting is_trasparent from the compare functor until method instantiation.
277 template <typename Comp, typename K>
278 using is_transparent = typename std::conditional<true, Comp, K>::type::is_transparent;
279 
280 #endif /* __TBB_CPP11_PRESENT */
281 
282 template <typename F>
284 
285 template <typename Callable, typename ReturnType, typename T>
286 struct body_arg_detector<ReturnType(Callable::*)(T)> {
287  typedef T arg_type;
288 };
289 
290 template <typename Callable, typename ReturnType, typename T>
291 struct body_arg_detector<ReturnType(Callable::*)(T) const> {
292  typedef T arg_type;
293 };
294 
295 #if __TBB_CPP11_PRESENT
296 using std::conditional;
297 #else
298 template <bool C, typename T, typename U>
299 struct conditional {
300  typedef U type;
301 };
302 
303 template <typename T, typename U>
304 struct conditional<true, T, U> {
305  typedef T type;
306 };
307 #endif
308 
309 } } // namespace internal, namespace tbb
310 
311 #endif /* __TBB_template_helpers_H */
bool_constant< true > true_type
Definition: tbb_stddef.h:489
void call(F &&f, Pack &&p)
Calls the given function with arguments taken from a stored_pack.
typename tbb::internal::make_index_sequence_impl< N >::type make_index_sequence
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_id __itt_string_handle __itt_metadata_type size_t void ITT_FORMAT p const __itt_domain __itt_id __itt_string_handle const wchar_t size_t ITT_FORMAT lu const __itt_domain __itt_id __itt_relation __itt_id ITT_FORMAT p const wchar_t int ITT_FORMAT __itt_group_mark S
stored_pack< Types... > save_pack(Types &&... types)
bool_constant< false > false_type
Definition: tbb_stddef.h:490
Allows to store a function parameter pack as a variable and later pass it to another function.
Partial support for std::is_integral.
std::void_t internal implementation (to avoid GCC < 4.7 "template aliases" absence)
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_id __itt_string_handle __itt_metadata_type size_t void ITT_FORMAT p const __itt_domain __itt_id __itt_string_handle const wchar_t size_t ITT_FORMAT lu const __itt_domain __itt_id __itt_relation __itt_id ITT_FORMAT p const wchar_t int ITT_FORMAT __itt_group_mark d int
static const bool value
void const char const char int ITT_FORMAT __itt_group_sync p
static Ret call(F &&f, pack_type &pack, Preceding &&... params)
static Ret call(F &&f, const pack_type &, Preceding &&... params)
Enables one or the other code branches.
Detects whether two given types are the same.
static Ret call(F &&f, pack_type &&pack, Preceding &&... params)
typename supports_impl< T, void, Checks... >::type supports
Strips its template type argument from cv- and ref-qualifiers.
static Ret call(F &&f, pack_type &&, Preceding &&... params)
static Ret call(F &&f, const pack_type &pack, Preceding &&... params)
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long value
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_id __itt_string_handle __itt_metadata_type type
The graph class.
Ret call_and_return(F &&f, Pack &&p)
void move(tbb_thread &t1, tbb_thread &t2)
Definition: tbb_thread.h:319

Copyright © 2005-2020 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.