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

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.