/* Copyright (c) 2005-2020 Intel Corporation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #ifndef __TBB_template_helpers_H #define __TBB_template_helpers_H #include #include #include "../tbb_config.h" #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT #include #endif #if __TBB_CPP11_PRESENT #include #include // allocator_traits #endif namespace tbb { namespace internal { //! Enables one or the other code branches template struct enable_if {}; template struct enable_if { typedef T type; }; //! Strips its template type argument from cv- and ref-qualifiers template struct strip { typedef T type; }; template struct strip { typedef T type; }; template struct strip { typedef T type; }; template struct strip { typedef T type; }; template struct strip { typedef T type; }; template struct strip { typedef T type; }; template struct strip { typedef T type; }; template struct strip { typedef T type; }; //! Specialization for function pointers template struct strip { typedef T(*type)(); }; #if __TBB_CPP11_RVALUE_REF_PRESENT template struct strip { typedef T type; }; template struct strip { typedef T type; }; template struct strip { typedef T type; }; template struct strip { typedef T type; }; #endif //! Specialization for arrays converts to a corresponding pointer template struct strip { typedef T* type; }; template struct strip { typedef const T* type; }; template struct strip { typedef volatile T* type; }; template struct strip { typedef const volatile T* type; }; //! Detects whether two given types are the same template struct is_same_type { static const bool value = false; }; template struct is_same_type { static const bool value = true; }; template struct is_ref { static const bool value = false; }; template struct is_ref { static const bool value = true; }; //! Partial support for std::is_integral template struct is_integral_impl { static const bool value = false; }; template<> struct is_integral_impl { static const bool value = true; }; template<> struct is_integral_impl { static const bool value = true; }; #if __TBB_CPP11_PRESENT template<> struct is_integral_impl { static const bool value = true; }; template<> struct is_integral_impl { static const bool value = true; }; #endif template<> struct is_integral_impl { static const bool value = true; }; template<> struct is_integral_impl { static const bool value = true; }; template<> struct is_integral_impl { static const bool value = true; }; template<> struct is_integral_impl { static const bool value = true; }; template<> struct is_integral_impl { static const bool value = true; }; template struct is_integral : is_integral_impl::type> {}; #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT //! std::void_t internal implementation (to avoid GCC < 4.7 "template aliases" absence) template struct void_t { typedef void type; }; #endif #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT // Generic SFINAE helper for expression checks, based on the idea demonstrated in ISO C++ paper n4502 template class... Checks> struct supports_impl { typedef std::false_type type; }; template class... Checks> struct supports_impl...>::type, Checks...> { typedef std::true_type type; }; template class... Checks> using supports = typename supports_impl::type; #endif /* __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT */ #if __TBB_CPP11_RVALUE_REF_PRESENT && __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT //! Allows to store a function parameter pack as a variable and later pass it to another function template< typename... Types > struct stored_pack; template<> struct stored_pack<> { typedef stored_pack<> pack_type; stored_pack() {} // Friend front-end functions template< typename F, typename Pack > friend void call( F&& f, Pack&& p ); template< typename Ret, typename F, typename Pack > friend Ret call_and_return( F&& f, Pack&& p ); protected: // Ideally, ref-qualified non-static methods would be used, // but that would greatly reduce the set of compilers where it works. template< typename Ret, typename F, typename... Preceding > static Ret call( F&& f, const pack_type& /*pack*/, Preceding&&... params ) { return std::forward(f)( std::forward(params)... ); } template< typename Ret, typename F, typename... Preceding > static Ret call( F&& f, pack_type&& /*pack*/, Preceding&&... params ) { return std::forward(f)( std::forward(params)... ); } }; template< typename T, typename... Types > struct stored_pack : stored_pack { typedef stored_pack pack_type; typedef stored_pack pack_remainder; // Since lifetime of original values is out of control, copies should be made. // Thus references should be stripped away from the deduced type. typename strip::type leftmost_value; // Here rvalue references act in the same way as forwarding references, // as long as class template parameters were deduced via forwarding references. stored_pack( T&& t, Types&&... types ) : pack_remainder(std::forward(types)...), leftmost_value(std::forward(t)) {} // Friend front-end functions template< typename F, typename Pack > friend void call( F&& f, Pack&& p ); template< typename Ret, typename F, typename Pack > friend Ret call_and_return( F&& f, Pack&& p ); protected: template< typename Ret, typename F, typename... Preceding > static Ret call( F&& f, pack_type& pack, Preceding&&... params ) { return pack_remainder::template call( std::forward(f), static_cast(pack), std::forward(params)... , pack.leftmost_value ); } template< typename Ret, typename F, typename... Preceding > static Ret call( F&& f, const pack_type& pack, Preceding&&... params ) { return pack_remainder::template call( std::forward(f), static_cast(pack), std::forward(params)... , pack.leftmost_value ); } template< typename Ret, typename F, typename... Preceding > static Ret call( F&& f, pack_type&& pack, Preceding&&... params ) { return pack_remainder::template call( std::forward(f), static_cast(pack), std::forward(params)... , std::move(pack.leftmost_value) ); } }; //! Calls the given function with arguments taken from a stored_pack template< typename F, typename Pack > void call( F&& f, Pack&& p ) { strip::type::template call( std::forward(f), std::forward(p) ); } template< typename Ret, typename F, typename Pack > Ret call_and_return( F&& f, Pack&& p ) { return strip::type::template call( std::forward(f), std::forward(p) ); } template< typename... Types > stored_pack save_pack( Types&&... types ) { return stored_pack( std::forward(types)... ); } #endif /* __TBB_CPP11_RVALUE_REF_PRESENT && __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT */ #if __TBB_CPP14_INTEGER_SEQUENCE_PRESENT using std::index_sequence; using std::make_index_sequence; #elif __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_TEMPLATE_ALIASES_PRESENT template class index_sequence {}; template struct make_index_sequence_impl : make_index_sequence_impl < N - 1, N - 1, S... > {}; template struct make_index_sequence_impl <0, S...> { using type = index_sequence; }; template using make_index_sequence = typename tbb::internal::make_index_sequence_impl::type; #endif /* __TBB_CPP14_INTEGER_SEQUENCE_PRESENT */ #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT template struct conjunction; template struct conjunction : std::conditional, First>::type {}; template struct conjunction : T {}; template<> struct conjunction<> : std::true_type {}; #endif #if __TBB_CPP11_PRESENT template< typename Iter > using iterator_value_t = typename std::iterator_traits::value_type; template< typename Iter > using iterator_key_t = typename std::remove_const::first_type>::type; template< typename Iter > using iterator_mapped_t = typename iterator_value_t::second_type; template< typename A > using value_type = typename A::value_type; template< typename A > using alloc_ptr_t = typename std::allocator_traits::pointer; template< typename A > using has_allocate = decltype(std::declval&>() = std::declval().allocate(0)); template< typename A > using has_deallocate = decltype(std::declval().deallocate(std::declval>(), 0)); // value_type should be checked first because it can be used in other checks (via allocator_traits) template< typename T > using is_allocator = supports; #if __TBB_CPP14_VARIABLE_TEMPLATES_PRESENT template< typename T > static constexpr bool is_allocator_v = is_allocator::value; #endif /*__TBB_CPP14_VARIABLE_TEMPLATES */ template< std::size_t N, typename... Args > struct pack_element { using type = void; }; template< std::size_t N, typename T, typename... Args > struct pack_element { using type = typename pack_element::type; }; template< typename T, typename... Args > struct pack_element<0, T, Args...> { using type = T; }; template< std::size_t N, typename... Args > using pack_element_t = typename pack_element::type; // Helper alias for heterogeneous lookup functions in containers // template parameter K and std::conditional are needed to provide immediate context // and postpone getting is_trasparent from the compare functor until method instantiation. template using is_transparent = typename std::conditional::type::is_transparent; #endif /* __TBB_CPP11_PRESENT */ template struct body_arg_detector; template struct body_arg_detector { typedef T arg_type; }; template struct body_arg_detector { typedef T arg_type; }; #if __TBB_CPP11_PRESENT using std::conditional; #else template struct conditional { typedef U type; }; template struct conditional { typedef T type; }; #endif } } // namespace internal, namespace tbb #endif /* __TBB_template_helpers_H */