_template_helpers.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. Copyright (c) 2005-2020 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef __TBB_template_helpers_H
  14. #define __TBB_template_helpers_H
  15. #include <utility>
  16. #include <cstddef>
  17. #include "../tbb_config.h"
  18. #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT
  19. #include <type_traits>
  20. #endif
  21. #if __TBB_CPP11_PRESENT
  22. #include <iterator>
  23. #include <memory> // allocator_traits
  24. #endif
  25. namespace tbb { namespace internal {
  26. //! Enables one or the other code branches
  27. template<bool Condition, typename T = void> struct enable_if {};
  28. template<typename T> struct enable_if<true, T> { typedef T type; };
  29. //! Strips its template type argument from cv- and ref-qualifiers
  30. template<typename T> struct strip { typedef T type; };
  31. template<typename T> struct strip<const T> { typedef T type; };
  32. template<typename T> struct strip<volatile T> { typedef T type; };
  33. template<typename T> struct strip<const volatile T> { typedef T type; };
  34. template<typename T> struct strip<T&> { typedef T type; };
  35. template<typename T> struct strip<const T&> { typedef T type; };
  36. template<typename T> struct strip<volatile T&> { typedef T type; };
  37. template<typename T> struct strip<const volatile T&> { typedef T type; };
  38. //! Specialization for function pointers
  39. template<typename T> struct strip<T(&)()> { typedef T(*type)(); };
  40. #if __TBB_CPP11_RVALUE_REF_PRESENT
  41. template<typename T> struct strip<T&&> { typedef T type; };
  42. template<typename T> struct strip<const T&&> { typedef T type; };
  43. template<typename T> struct strip<volatile T&&> { typedef T type; };
  44. template<typename T> struct strip<const volatile T&&> { typedef T type; };
  45. #endif
  46. //! Specialization for arrays converts to a corresponding pointer
  47. template<typename T, std::size_t N> struct strip<T(&)[N]> { typedef T* type; };
  48. template<typename T, std::size_t N> struct strip<const T(&)[N]> { typedef const T* type; };
  49. template<typename T, std::size_t N> struct strip<volatile T(&)[N]> { typedef volatile T* type; };
  50. template<typename T, std::size_t N> struct strip<const volatile T(&)[N]> { typedef const volatile T* type; };
  51. //! Detects whether two given types are the same
  52. template<class U, class V> struct is_same_type { static const bool value = false; };
  53. template<class W> struct is_same_type<W,W> { static const bool value = true; };
  54. template<typename T> struct is_ref { static const bool value = false; };
  55. template<typename U> struct is_ref<U&> { static const bool value = true; };
  56. //! Partial support for std::is_integral
  57. template<typename T> struct is_integral_impl { static const bool value = false; };
  58. template<> struct is_integral_impl<bool> { static const bool value = true; };
  59. template<> struct is_integral_impl<char> { static const bool value = true; };
  60. #if __TBB_CPP11_PRESENT
  61. template<> struct is_integral_impl<char16_t> { static const bool value = true; };
  62. template<> struct is_integral_impl<char32_t> { static const bool value = true; };
  63. #endif
  64. template<> struct is_integral_impl<wchar_t> { static const bool value = true; };
  65. template<> struct is_integral_impl<short> { static const bool value = true; };
  66. template<> struct is_integral_impl<int> { static const bool value = true; };
  67. template<> struct is_integral_impl<long> { static const bool value = true; };
  68. template<> struct is_integral_impl<long long> { static const bool value = true; };
  69. template<typename T>
  70. struct is_integral : is_integral_impl<typename strip<T>::type> {};
  71. #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT
  72. //! std::void_t internal implementation (to avoid GCC < 4.7 "template aliases" absence)
  73. template<typename...> struct void_t { typedef void type; };
  74. #endif
  75. #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT
  76. // Generic SFINAE helper for expression checks, based on the idea demonstrated in ISO C++ paper n4502
  77. template<typename T, typename, template<typename> class... Checks>
  78. struct supports_impl { typedef std::false_type type; };
  79. template<typename T, template<typename> class... Checks>
  80. struct supports_impl<T, typename void_t<Checks<T>...>::type, Checks...> { typedef std::true_type type; };
  81. template<typename T, template<typename> class... Checks>
  82. using supports = typename supports_impl<T, void, Checks...>::type;
  83. #endif /* __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT */
  84. #if __TBB_CPP11_RVALUE_REF_PRESENT && __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT
  85. //! Allows to store a function parameter pack as a variable and later pass it to another function
  86. template< typename... Types >
  87. struct stored_pack;
  88. template<>
  89. struct stored_pack<>
  90. {
  91. typedef stored_pack<> pack_type;
  92. stored_pack() {}
  93. // Friend front-end functions
  94. template< typename F, typename Pack > friend void call( F&& f, Pack&& p );
  95. template< typename Ret, typename F, typename Pack > friend Ret call_and_return( F&& f, Pack&& p );
  96. protected:
  97. // Ideally, ref-qualified non-static methods would be used,
  98. // but that would greatly reduce the set of compilers where it works.
  99. template< typename Ret, typename F, typename... Preceding >
  100. static Ret call( F&& f, const pack_type& /*pack*/, Preceding&&... params ) {
  101. return std::forward<F>(f)( std::forward<Preceding>(params)... );
  102. }
  103. template< typename Ret, typename F, typename... Preceding >
  104. static Ret call( F&& f, pack_type&& /*pack*/, Preceding&&... params ) {
  105. return std::forward<F>(f)( std::forward<Preceding>(params)... );
  106. }
  107. };
  108. template< typename T, typename... Types >
  109. struct stored_pack<T, Types...> : stored_pack<Types...>
  110. {
  111. typedef stored_pack<T, Types...> pack_type;
  112. typedef stored_pack<Types...> pack_remainder;
  113. // Since lifetime of original values is out of control, copies should be made.
  114. // Thus references should be stripped away from the deduced type.
  115. typename strip<T>::type leftmost_value;
  116. // Here rvalue references act in the same way as forwarding references,
  117. // as long as class template parameters were deduced via forwarding references.
  118. stored_pack( T&& t, Types&&... types )
  119. : pack_remainder(std::forward<Types>(types)...), leftmost_value(std::forward<T>(t)) {}
  120. // Friend front-end functions
  121. template< typename F, typename Pack > friend void call( F&& f, Pack&& p );
  122. template< typename Ret, typename F, typename Pack > friend Ret call_and_return( F&& f, Pack&& p );
  123. protected:
  124. template< typename Ret, typename F, typename... Preceding >
  125. static Ret call( F&& f, pack_type& pack, Preceding&&... params ) {
  126. return pack_remainder::template call<Ret>(
  127. std::forward<F>(f), static_cast<pack_remainder&>(pack),
  128. std::forward<Preceding>(params)... , pack.leftmost_value
  129. );
  130. }
  131. template< typename Ret, typename F, typename... Preceding >
  132. static Ret call( F&& f, const pack_type& pack, Preceding&&... params ) {
  133. return pack_remainder::template call<Ret>(
  134. std::forward<F>(f), static_cast<const pack_remainder&>(pack),
  135. std::forward<Preceding>(params)... , pack.leftmost_value
  136. );
  137. }
  138. template< typename Ret, typename F, typename... Preceding >
  139. static Ret call( F&& f, pack_type&& pack, Preceding&&... params ) {
  140. return pack_remainder::template call<Ret>(
  141. std::forward<F>(f), static_cast<pack_remainder&&>(pack),
  142. std::forward<Preceding>(params)... , std::move(pack.leftmost_value)
  143. );
  144. }
  145. };
  146. //! Calls the given function with arguments taken from a stored_pack
  147. template< typename F, typename Pack >
  148. void call( F&& f, Pack&& p ) {
  149. strip<Pack>::type::template call<void>( std::forward<F>(f), std::forward<Pack>(p) );
  150. }
  151. template< typename Ret, typename F, typename Pack >
  152. Ret call_and_return( F&& f, Pack&& p ) {
  153. return strip<Pack>::type::template call<Ret>( std::forward<F>(f), std::forward<Pack>(p) );
  154. }
  155. template< typename... Types >
  156. stored_pack<Types...> save_pack( Types&&... types ) {
  157. return stored_pack<Types...>( std::forward<Types>(types)... );
  158. }
  159. #endif /* __TBB_CPP11_RVALUE_REF_PRESENT && __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT */
  160. #if __TBB_CPP14_INTEGER_SEQUENCE_PRESENT
  161. using std::index_sequence;
  162. using std::make_index_sequence;
  163. #elif __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT
  164. template<std::size_t... S> class index_sequence {};
  165. template<std::size_t N, std::size_t... S>
  166. struct make_index_sequence_impl : make_index_sequence_impl < N - 1, N - 1, S... > {};
  167. template<std::size_t... S>
  168. struct make_index_sequence_impl <0, S...> {
  169. using type = index_sequence<S...>;
  170. };
  171. template<std::size_t N>
  172. using make_index_sequence = typename tbb::internal::make_index_sequence_impl<N>::type;
  173. #endif /* __TBB_CPP14_INTEGER_SEQUENCE_PRESENT */
  174. #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT
  175. template<typename... Args>
  176. struct conjunction;
  177. template<typename First, typename... Args>
  178. struct conjunction<First, Args...>
  179. : std::conditional<bool(First::value), conjunction<Args...>, First>::type {};
  180. template<typename T>
  181. struct conjunction<T> : T {};
  182. template<>
  183. struct conjunction<> : std::true_type {};
  184. #endif
  185. #if __TBB_CPP11_PRESENT
  186. template< typename Iter >
  187. using iterator_value_t = typename std::iterator_traits<Iter>::value_type;
  188. template< typename Iter >
  189. using iterator_key_t = typename std::remove_const<typename iterator_value_t<Iter>::first_type>::type;
  190. template< typename Iter >
  191. using iterator_mapped_t = typename iterator_value_t<Iter>::second_type;
  192. template< typename A > using value_type = typename A::value_type;
  193. template< typename A > using alloc_ptr_t = typename std::allocator_traits<A>::pointer;
  194. template< typename A > using has_allocate = decltype(std::declval<alloc_ptr_t<A>&>() = std::declval<A>().allocate(0));
  195. template< typename A > using has_deallocate = decltype(std::declval<A>().deallocate(std::declval<alloc_ptr_t<A>>(), 0));
  196. // value_type should be checked first because it can be used in other checks (via allocator_traits)
  197. template< typename T >
  198. using is_allocator = supports<T, value_type, has_allocate, has_deallocate>;
  199. #if __TBB_CPP14_VARIABLE_TEMPLATES_PRESENT
  200. template< typename T >
  201. static constexpr bool is_allocator_v = is_allocator<T>::value;
  202. #endif /*__TBB_CPP14_VARIABLE_TEMPLATES */
  203. template< std::size_t N, typename... Args >
  204. struct pack_element {
  205. using type = void;
  206. };
  207. template< std::size_t N, typename T, typename... Args >
  208. struct pack_element<N, T, Args...> {
  209. using type = typename pack_element<N - 1, Args...>::type;
  210. };
  211. template< typename T, typename... Args >
  212. struct pack_element<0, T, Args...> {
  213. using type = T;
  214. };
  215. template< std::size_t N, typename... Args >
  216. using pack_element_t = typename pack_element<N, Args...>::type;
  217. // Helper alias for heterogeneous lookup functions in containers
  218. // template parameter K and std::conditional are needed to provide immediate context
  219. // and postpone getting is_trasparent from the compare functor until method instantiation.
  220. template <typename Comp, typename K>
  221. using is_transparent = typename std::conditional<true, Comp, K>::type::is_transparent;
  222. #endif /* __TBB_CPP11_PRESENT */
  223. template <typename F>
  224. struct body_arg_detector;
  225. template <typename Callable, typename ReturnType, typename T>
  226. struct body_arg_detector<ReturnType(Callable::*)(T)> {
  227. typedef T arg_type;
  228. };
  229. template <typename Callable, typename ReturnType, typename T>
  230. struct body_arg_detector<ReturnType(Callable::*)(T) const> {
  231. typedef T arg_type;
  232. };
  233. #if __TBB_CPP11_PRESENT
  234. using std::conditional;
  235. #else
  236. template <bool C, typename T, typename U>
  237. struct conditional {
  238. typedef U type;
  239. };
  240. template <typename T, typename U>
  241. struct conditional<true, T, U> {
  242. typedef T type;
  243. };
  244. #endif
  245. } } // namespace internal, namespace tbb
  246. #endif /* __TBB_template_helpers_H */