// Copyright (c) 2014 Marshall A. Greenblatt. Portions copyright (c) 2011 // Google Inc. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the name Chromium Embedded // Framework nor the names of its contributors may be used to endorse // or promote products derived from this software without specific prior // written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef CEF_INCLUDE_BASE_CEF_TEMPLATE_UTIL_H_ #define CEF_INCLUDE_BASE_CEF_TEMPLATE_UTIL_H_ #pragma once #if defined(USING_CHROMIUM_INCLUDES) // When building CEF include the Chromium header directly. #include "base/template_util.h" #else // !USING_CHROMIUM_INCLUDES // The following is substantially similar to the Chromium implementation. // If the Chromium implementation diverges the below implementation should be // updated to match. #include #include #include #include #include #include #include "include/base/cef_build.h" // Some versions of libstdc++ have partial support for type_traits, but misses // a smaller subset while removing some of the older non-standard stuff. Assume // that all versions below 5.0 fall in this category, along with one 5.0 // experimental release. Test for this by consulting compiler major version, // the only reliable option available, so theoretically this could fail should // you attempt to mix an earlier version of libstdc++ with >= GCC5. But // that's unlikely to work out, especially as GCC5 changed ABI. #define CR_GLIBCXX_5_0_0 20150123 #if (defined(__GNUC__) && __GNUC__ < 5) || \ (defined(__GLIBCXX__) && __GLIBCXX__ == CR_GLIBCXX_5_0_0) #define CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX #endif // This hacks around using gcc with libc++ which has some incompatibilies. // - is_trivially_* doesn't work: https://llvm.org/bugs/show_bug.cgi?id=27538 // TODO(danakj): Remove this when android builders are all using a newer version // of gcc, or the android ndk is updated to a newer libc++ that works with older // gcc versions. #if !defined(__clang__) && defined(_LIBCPP_VERSION) #define CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX #endif namespace base { template struct is_non_const_reference : std::false_type {}; template struct is_non_const_reference : std::true_type {}; template struct is_non_const_reference : std::false_type {}; namespace internal { // Implementation detail of base::void_t below. template struct make_void { using type = void; }; } // namespace internal // base::void_t is an implementation of std::void_t from C++17. // // We use |base::internal::make_void| as a helper struct to avoid a C++14 // defect: // http://en.cppreference.com/w/cpp/types/void_t // http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#1558 template using void_t = typename ::base::internal::make_void::type; namespace internal { // Uses expression SFINAE to detect whether using operator<< would work. template struct SupportsOstreamOperator : std::false_type {}; template struct SupportsOstreamOperator() << std::declval()))> : std::true_type {}; template struct SupportsToString : std::false_type {}; template struct SupportsToString().ToString()))> : std::true_type {}; // Used to detech whether the given type is an iterator. This is normally used // with std::enable_if to provide disambiguation for functions that take // templatzed iterators as input. template struct is_iterator : std::false_type {}; template struct is_iterator::iterator_category>> : std::true_type {}; // Helper to express preferences in an overload set. If more than one overload // are available for a given set of parameters the overload with the higher // priority will be chosen. template struct priority_tag : priority_tag {}; template <> struct priority_tag<0> {}; } // namespace internal // is_trivially_copyable is especially hard to get right. // - Older versions of libstdc++ will fail to have it like they do for other // type traits. This has become a subset of the second point, but used to be // handled independently. // - An experimental release of gcc includes most of type_traits but misses // is_trivially_copyable, so we still have to avoid using libstdc++ in this // case, which is covered by CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX. // - When compiling libc++ from before r239653, with a gcc compiler, the // std::is_trivially_copyable can fail. So we need to work around that by not // using the one in libc++ in this case. This is covered by the // CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX define, and is discussed in // https://llvm.org/bugs/show_bug.cgi?id=27538#c1 where they point out that // in libc++'s commit r239653 this is fixed by libc++ checking for gcc 5.1. // - In both of the above cases we are using the gcc compiler. When defining // this ourselves on compiler intrinsics, the __is_trivially_copyable() // intrinsic is not available on gcc before version 5.1 (see the discussion in // https://llvm.org/bugs/show_bug.cgi?id=27538#c1 again), so we must check for // that version. // - When __is_trivially_copyable() is not available because we are on gcc older // than 5.1, we need to fall back to something, so we use __has_trivial_copy() // instead based on what was done one-off in bit_cast() previously. // TODO(crbug.com/554293): Remove this when all platforms have this in the std // namespace and it works with gcc as needed. #if defined(CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX) || \ defined(CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX) template struct is_trivially_copyable { // TODO(danakj): Remove this when android builders are all using a newer version // of gcc, or the android ndk is updated to a newer libc++ that does this for // us. #if _GNUC_VER >= 501 static constexpr bool value = __is_trivially_copyable(T); #else static constexpr bool value = __has_trivial_copy(T) && __has_trivial_destructor(T); #endif }; #else template using is_trivially_copyable = std::is_trivially_copyable; #endif #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ <= 7 // Workaround for g++7 and earlier family. // Due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80654, without this // Optional> where T is non-copyable causes a compile error. // As we know it is not trivially copy constructible, explicitly declare so. template struct is_trivially_copy_constructible : std::is_trivially_copy_constructible {}; template struct is_trivially_copy_constructible> : std::false_type {}; #else // Otherwise use std::is_trivially_copy_constructible as is. template using is_trivially_copy_constructible = std::is_trivially_copy_constructible; #endif // base::in_place_t is an implementation of std::in_place_t from // C++17. A tag type used to request in-place construction in template vararg // constructors. // Specification: // https://en.cppreference.com/w/cpp/utility/in_place struct in_place_t {}; constexpr in_place_t in_place = {}; // base::in_place_type_t is an implementation of std::in_place_type_t from // C++17. A tag type used for in-place construction when the type to construct // needs to be specified, such as with base::unique_any, designed to be a // drop-in replacement. // Specification: // http://en.cppreference.com/w/cpp/utility/in_place template struct in_place_type_t {}; template struct is_in_place_type_t { static constexpr bool value = false; }; template struct is_in_place_type_t> { static constexpr bool value = true; }; // C++14 implementation of C++17's std::bool_constant. // // Reference: https://en.cppreference.com/w/cpp/types/integral_constant // Specification: https://wg21.link/meta.type.synop template using bool_constant = std::integral_constant; // C++14 implementation of C++17's std::conjunction. // // Reference: https://en.cppreference.com/w/cpp/types/conjunction // Specification: https://wg21.link/meta.logical#1.itemdecl:1 template struct conjunction : std::true_type {}; template struct conjunction : B1 {}; template struct conjunction : std::conditional_t(B1::value), conjunction, B1> { }; // C++14 implementation of C++17's std::disjunction. // // Reference: https://en.cppreference.com/w/cpp/types/disjunction // Specification: https://wg21.link/meta.logical#itemdecl:2 template struct disjunction : std::false_type {}; template struct disjunction : B1 {}; template struct disjunction : std::conditional_t(B1::value), B1, disjunction> { }; // C++14 implementation of C++17's std::negation. // // Reference: https://en.cppreference.com/w/cpp/types/negation // Specification: https://wg21.link/meta.logical#itemdecl:3 template struct negation : bool_constant(B::value)> {}; // Implementation of C++17's invoke_result. // // This implementation adds references to `Functor` and `Args` to work around // some quirks of std::result_of. See the #Notes section of [1] for details. // // References: // [1] https://en.cppreference.com/w/cpp/types/result_of // [2] https://wg21.link/meta.trans.other#lib:invoke_result template using invoke_result = std::result_of; // Implementation of C++17's std::invoke_result_t. // // Reference: https://wg21.link/meta.type.synop#lib:invoke_result_t template using invoke_result_t = typename invoke_result::type; namespace internal { // Base case, `InvokeResult` does not have a nested type member. This means `F` // could not be invoked with `Args...` and thus is not invocable. template struct IsInvocableImpl : std::false_type {}; // Happy case, `InvokeResult` does have a nested type member. Now check whether // `InvokeResult::type` is convertible to `R`. Short circuit in case // `std::is_void`. template struct IsInvocableImpl> : disjunction, std::is_convertible> {}; } // namespace internal // Implementation of C++17's std::is_invocable_r. // // Returns whether `F` can be invoked with `Args...` and the result is // convertible to `R`. // // Reference: https://wg21.link/meta.rel#lib:is_invocable_r template struct is_invocable_r : internal::IsInvocableImpl, R> {}; // Implementation of C++17's std::is_invocable. // // Returns whether `F` can be invoked with `Args...`. // // Reference: https://wg21.link/meta.rel#lib:is_invocable template struct is_invocable : is_invocable_r {}; namespace internal { // The indirection with std::is_enum is required, because instantiating // std::underlying_type_t when T is not an enum is UB prior to C++20. template ::value> struct IsScopedEnumImpl : std::false_type {}; template struct IsScopedEnumImpl::value=*/true> : negation>> {}; } // namespace internal // Implementation of C++23's std::is_scoped_enum // // Reference: https://en.cppreference.com/w/cpp/types/is_scoped_enum template struct is_scoped_enum : internal::IsScopedEnumImpl {}; // Implementation of C++20's std::remove_cvref. // // References: // - https://en.cppreference.com/w/cpp/types/remove_cvref // - https://wg21.link/meta.trans.other#lib:remove_cvref template struct remove_cvref { using type = std::remove_cv_t>; }; // Implementation of C++20's std::remove_cvref_t. // // References: // - https://en.cppreference.com/w/cpp/types/remove_cvref // - https://wg21.link/meta.type.synop#lib:remove_cvref_t template using remove_cvref_t = typename remove_cvref::type; // Simplified implementation of C++20's std::iter_value_t. // As opposed to std::iter_value_t, this implementation does not restrict // the type of `Iter` and does not consider specializations of // `indirectly_readable_traits`. // // Reference: https://wg21.link/readable.traits#2 template using iter_value_t = typename std::iterator_traits>::value_type; // Simplified implementation of C++20's std::iter_reference_t. // As opposed to std::iter_reference_t, this implementation does not restrict // the type of `Iter`. // // Reference: https://wg21.link/iterator.synopsis#:~:text=iter_reference_t template using iter_reference_t = decltype(*std::declval()); // Simplified implementation of C++20's std::indirect_result_t. As opposed to // std::indirect_result_t, this implementation does not restrict the type of // `Func` and `Iters`. // // Reference: https://wg21.link/iterator.synopsis#:~:text=indirect_result_t template using indirect_result_t = invoke_result_t...>; // Simplified implementation of C++20's std::projected. As opposed to // std::projected, this implementation does not explicitly restrict the type of // `Iter` and `Proj`, but rather does so implicitly by requiring // `indirect_result_t` is a valid type. This is required for SFINAE // friendliness. // // Reference: https://wg21.link/projected template > struct projected { using value_type = remove_cvref_t; IndirectResultT operator*() const; // not defined }; } // namespace base #undef CR_USE_FALLBACKS_FOR_GCC_WITH_LIBCXX #undef CR_USE_FALLBACKS_FOR_OLD_EXPERIMENTAL_GLIBCXX #endif // !USING_CHROMIUM_INCLUDES #endif // CEF_INCLUDE_BASE_CEF_TEMPLATE_UTIL_H_