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-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 
18 
19 */
20 
21 #ifndef __TBB_template_helpers_H
22 #define __TBB_template_helpers_H
23 
24 #include <utility>
25 #include <cstddef>
26 #include "../tbb_config.h"
27 #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT
28 #include <type_traits>
29 #endif
30 #if __TBB_CPP11_PRESENT
31 #include <iterator>
32 #include <memory> // allocator_traits
33 #endif
34 
35 namespace tbb { namespace internal {
36 
38 template<bool Condition, typename T = void> struct enable_if {};
39 template<typename T> struct enable_if<true, T> { typedef T type; };
40 
42 template<typename T> struct strip { 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 template<typename T> struct strip<T&> { typedef T type; };
47 template<typename T> struct strip<const T&> { typedef T type; };
48 template<typename T> struct strip<volatile T&> { typedef T type; };
49 template<typename T> struct strip<const volatile T&> { typedef T type; };
51 template<typename T> struct strip<T(&)()> { typedef T(*type)(); };
52 #if __TBB_CPP11_RVALUE_REF_PRESENT
53 template<typename T> struct strip<T&&> { typedef T type; };
54 template<typename T> struct strip<const T&&> { typedef T type; };
55 template<typename T> struct strip<volatile T&&> { typedef T type; };
56 template<typename T> struct strip<const volatile T&&> { typedef T type; };
57 #endif
58 template<typename T, std::size_t N> struct strip<T(&)[N]> { typedef T* type; };
60 template<typename T, std::size_t N> struct strip<const T(&)[N]> { typedef const T* type; };
61 template<typename T, std::size_t N> struct strip<volatile T(&)[N]> { typedef volatile T* type; };
62 template<typename T, std::size_t N> struct strip<const volatile T(&)[N]> { typedef const volatile T* type; };
63 
65 template<class U, class V> struct is_same_type { static const bool value = false; };
66 template<class W> struct is_same_type<W,W> { static const bool value = true; };
67 
68 template<typename T> struct is_ref { static const bool value = false; };
69 template<typename U> struct is_ref<U&> { static const bool value = true; };
70 
71 #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT
72 template<typename...> struct void_t { typedef void type; };
74 #endif
75 
76 #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT
77 
78 // Generic SFINAE helper for expression checks, based on the idea demonstrated in ISO C++ paper n4502
79 template<typename T, typename, template<typename> class... Checks>
80 struct supports_impl { typedef std::false_type type; };
81 template<typename T, template<typename> class... Checks>
82 struct supports_impl<T, typename void_t<Checks<T>...>::type, Checks...> { typedef std::true_type type; };
83 
84 template<typename T, template<typename> class... Checks>
85 using supports = typename supports_impl<T, void, Checks...>::type;
86 
87 #endif /* __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT */
88 
89 #if __TBB_CPP11_RVALUE_REF_PRESENT && __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT
90 
92 template< typename... Types >
93 struct stored_pack;
94 
95 template<>
96 struct stored_pack<>
97 {
100 
101  // Friend front-end functions
102  template< typename F, typename Pack > friend void call( F&& f, Pack&& p );
103  template< typename Ret, typename F, typename Pack > friend Ret call_and_return( F&& f, Pack&& p );
104 
105 protected:
106  // Ideally, ref-qualified non-static methods would be used,
107  // but that would greatly reduce the set of compilers where it works.
108  template< typename Ret, typename F, typename... Preceding >
109  static Ret call( F&& f, const pack_type& /*pack*/, Preceding&&... params ) {
110  return std::forward<F>(f)( std::forward<Preceding>(params)... );
111  }
112  template< typename Ret, typename F, typename... Preceding >
113  static Ret call( F&& f, pack_type&& /*pack*/, Preceding&&... params ) {
114  return std::forward<F>(f)( std::forward<Preceding>(params)... );
115  }
116 };
117 
118 template< typename T, typename... Types >
119 struct stored_pack<T, Types...> : stored_pack<Types...>
120 {
121  typedef stored_pack<T, Types...> pack_type;
122  typedef stored_pack<Types...> pack_remainder;
123  // Since lifetime of original values is out of control, copies should be made.
124  // Thus references should be stripped away from the deduced type.
126 
127  // Here rvalue references act in the same way as forwarding references,
128  // as long as class template parameters were deduced via forwarding references.
129  stored_pack( T&& t, Types&&... types )
130  : pack_remainder(std::forward<Types>(types)...), leftmost_value(std::forward<T>(t)) {}
131 
132  // Friend front-end functions
133  template< typename F, typename Pack > friend void call( F&& f, Pack&& p );
134  template< typename Ret, typename F, typename Pack > friend Ret call_and_return( F&& f, Pack&& p );
135 
136 protected:
137  template< typename Ret, typename F, typename... Preceding >
138  static Ret call( F&& f, pack_type& pack, Preceding&&... params ) {
139  return pack_remainder::template call<Ret>(
140  std::forward<F>(f), static_cast<pack_remainder&>(pack),
141  std::forward<Preceding>(params)... , pack.leftmost_value
142  );
143  }
144  template< typename Ret, typename F, typename... Preceding >
145  static Ret call( F&& f, const pack_type& pack, Preceding&&... params ) {
146  return pack_remainder::template call<Ret>(
147  std::forward<F>(f), static_cast<const pack_remainder&>(pack),
148  std::forward<Preceding>(params)... , pack.leftmost_value
149  );
150  }
151  template< typename Ret, typename F, typename... Preceding >
152  static Ret call( F&& f, pack_type&& pack, Preceding&&... params ) {
153  return pack_remainder::template call<Ret>(
154  std::forward<F>(f), static_cast<pack_remainder&&>(pack),
155  std::forward<Preceding>(params)... , std::move(pack.leftmost_value)
156  );
157  }
158 };
159 
161 template< typename F, typename Pack >
162 void call( F&& f, Pack&& p ) {
163  strip<Pack>::type::template call<void>( std::forward<F>(f), std::forward<Pack>(p) );
164 }
165 
166 template< typename Ret, typename F, typename Pack >
167 Ret call_and_return( F&& f, Pack&& p ) {
168  return strip<Pack>::type::template call<Ret>( std::forward<F>(f), std::forward<Pack>(p) );
169 }
170 
171 template< typename... Types >
172 stored_pack<Types...> save_pack( Types&&... types ) {
173  return stored_pack<Types...>( std::forward<Types>(types)... );
174 }
175 
176 #endif /* __TBB_CPP11_RVALUE_REF_PRESENT && __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT */
177 
178 #if __TBB_CPP14_INTEGER_SEQUENCE_PRESENT
179 
180 using std::index_sequence;
182 
183 #elif __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT
184 
185 template<std::size_t... S> class index_sequence {};
186 
187 template<std::size_t N, std::size_t... S>
188 struct make_index_sequence_impl : make_index_sequence_impl < N - 1, N - 1, S... > {};
189 
190 template<std::size_t... S>
191 struct make_index_sequence_impl <0, S...> {
192  using type = index_sequence<S...>;
193 };
194 
195 template<std::size_t N>
197 
198 #endif /* __TBB_CPP14_INTEGER_SEQUENCE_PRESENT */
199 
200 #if __TBB_CPP11_PRESENT
201 
202 template< typename Iter >
203 using iterator_value_t = typename std::iterator_traits<Iter>::value_type;
204 
205 template< typename Iter >
206 using iterator_key_t = typename std::remove_const<typename iterator_value_t<Iter>::first_type>::type;
207 
208 template< typename Iter >
209 using iterator_mapped_t = typename iterator_value_t<Iter>::second_type;
210 
211 template< typename A > using value_type = typename A::value_type;
212 template< typename A > using alloc_ptr_t = typename std::allocator_traits<A>::pointer;
213 template< typename A > using has_allocate = decltype(std::declval<alloc_ptr_t<A>&>() = std::declval<A>().allocate(0));
214 template< typename A > using has_deallocate = decltype(std::declval<A>().deallocate(std::declval<alloc_ptr_t<A>>(), 0));
215 
216 // value_type should be checked first because it can be used in other checks (via allocator_traits)
217 template< typename T >
219 
220 #if __TBB_CPP14_VARIABLE_TEMPLATES_PRESENT
221 
222 template< typename T >
223 static constexpr bool is_allocator_v = is_allocator<T>::value;
224 
225 #endif /*__TBB_CPP14_VARIABLE_TEMPLATES */
226 
227 template< std::size_t N, typename... Args >
228 struct pack_element {
229  using type = void;
230 };
231 
232 template< std::size_t N, typename T, typename... Args >
233 struct pack_element<N, T, Args...> {
234  using type = typename pack_element<N - 1, Args...>::type;
235 };
236 
237 template< typename T, typename... Args >
238 struct pack_element<0, T, Args...> {
239  using type = T;
240 };
241 
242 template< std::size_t N, typename... Args >
243 using pack_element_t = typename pack_element<N, Args...>::type;
244 
245 #endif /* __TBB_CPP11_PRESENT */
246 
247 } } // namespace internal, namespace tbb
248 
249 #endif /* __TBB_template_helpers_H */
Allows to store a function parameter pack as a variable and later pass it to another function.
bool_constant< true > true_type
Definition: tbb_stddef.h:472
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_id __itt_string_handle __itt_metadata_type type
Detects whether two given types are the same.
typename supports_impl< T, void, Checks... >::type supports
static Ret call(F &&f, pack_type &pack, Preceding &&... params)
void const char const char int ITT_FORMAT __itt_group_sync p
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_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
static Ret call(F &&f, const pack_type &, Preceding &&... params)
stored_pack< Types... > save_pack(Types &&... types)
static Ret call(F &&f, const pack_type &pack, Preceding &&... params)
The graph class.
Enables one or the other code branches.
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 call(F &&f, Pack &&p)
Calls the given function with arguments taken from a stored_pack.
void move(tbb_thread &t1, tbb_thread &t2)
Definition: tbb_thread.h:309
static Ret call(F &&f, pack_type &&, Preceding &&... params)
static Ret call(F &&f, pack_type &&pack, Preceding &&... params)
bool_constant< false > false_type
Definition: tbb_stddef.h:473
Ret call_and_return(F &&f, Pack &&p)
Strips its template type argument from cv- and ref-qualifiers.
static const bool value
std::void_t internal implementation (to avoid GCC < 4.7 "template aliases" absence)
typename tbb::internal::make_index_sequence_impl< N >::type make_index_sequence

Copyright © 2005-2019 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.