// Copyright (C) 2016 The Qt Company Ltd. // Copyright (C) 2016 Intel Corporation. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #ifndef QTYPEINFO_H #define QTYPEINFO_H #include #include #include #include #include #include QT_BEGIN_NAMESPACE class QDebug; /* QTypeInfo - type trait functionality */ namespace QtPrivate { // A trivially copyable class must also have a trivial, non-deleted // destructor [class.prop/1.3], CWG1734. Some implementations don't // check for a trivial destructor, because of backwards compatibility // with C++98's definition of trivial copyability. // Since trivial copiability has implications for the ABI, implementations // can't "just fix" their traits. So, although formally redundant, we // explicitly check for trivial destruction here. template inline constexpr bool qIsRelocatable = std::is_trivially_copyable_v && std::is_trivially_destructible_v; // Denotes types that are trivially default constructible, and for which // value-initialization can be achieved by filling their storage with 0 bits. // There is no type trait we can use for this, so we hardcode a list of // possibilities that we know are OK on the architectures that we support. // The most notable exception are pointers to data members, which for instance // on the Itanium ABI are initialized to -1. template inline constexpr bool qIsValueInitializationBitwiseZero = std::is_scalar_v && !std::is_member_object_pointer_v; } /* The catch-all template. */ template class QTypeInfo { public: enum { isPointer [[deprecated("Use std::is_pointer instead")]] = std::is_pointer_v, isIntegral [[deprecated("Use std::is_integral instead")]] = std::is_integral_v, isComplex = !std::is_trivial_v, isRelocatable = QtPrivate::qIsRelocatable, isValueInitializationBitwiseZero = QtPrivate::qIsValueInitializationBitwiseZero, }; }; template<> class QTypeInfo { public: enum { isPointer [[deprecated("Use std::is_pointer instead")]] = false, isIntegral [[deprecated("Use std::is_integral instead")]] = false, isComplex = false, isRelocatable = false, isValueInitializationBitwiseZero = false, }; }; /*! \class QTypeInfoMerger \inmodule QtCore \internal \brief QTypeInfoMerger merges the QTypeInfo flags of T1, T2... and presents them as a QTypeInfo would do. Let's assume that we have a simple set of structs: \snippet code/src_corelib_global_qglobal.cpp 50 To create a proper QTypeInfo specialization for A struct, we have to check all sub-components; B, C and D, then take the lowest common denominator and call Q_DECLARE_TYPEINFO with the resulting flags. An easier and less fragile approach is to use QTypeInfoMerger, which does that automatically. So struct A would have the following QTypeInfo definition: \snippet code/src_corelib_global_qglobal.cpp 51 */ template class QTypeInfoMerger { static_assert(sizeof...(Ts) > 0); public: static constexpr bool isComplex = ((QTypeInfo::isComplex) || ...); static constexpr bool isRelocatable = ((QTypeInfo::isRelocatable) && ...); [[deprecated("Use std::is_pointer instead")]] static constexpr bool isPointer = false; [[deprecated("Use std::is_integral instead")]] static constexpr bool isIntegral = false; static constexpr bool isValueInitializationBitwiseZero = false; static_assert(!isRelocatable || std::is_copy_constructible_v || std::is_move_constructible_v, "All Ts... are Q_RELOCATABLE_TYPE, but T is neither copy- nor move-constructible, " "so cannot be Q_RELOCATABLE_TYPE. Please mark T as Q_COMPLEX_TYPE manually."); }; // QTypeInfo for std::pair: // std::pair is spec'ed to be struct { T1 first; T2 second; }, so, unlike tuple<>, // we _can_ specialize QTypeInfo for pair<>: template class QTypeInfo> : public QTypeInfoMerger, T1, T2> {}; #define Q_DECLARE_MOVABLE_CONTAINER(CONTAINER) \ template \ class QTypeInfo> \ { \ public: \ enum { \ isPointer [[deprecated("Use std::is_pointer instead")]] = false, \ isIntegral [[deprecated("Use std::is_integral instead")]] = false, \ isComplex = true, \ isRelocatable = true, \ isValueInitializationBitwiseZero = false, \ }; \ } Q_DECLARE_MOVABLE_CONTAINER(QList); Q_DECLARE_MOVABLE_CONTAINER(QQueue); Q_DECLARE_MOVABLE_CONTAINER(QStack); Q_DECLARE_MOVABLE_CONTAINER(QSet); Q_DECLARE_MOVABLE_CONTAINER(QMap); Q_DECLARE_MOVABLE_CONTAINER(QMultiMap); Q_DECLARE_MOVABLE_CONTAINER(QHash); Q_DECLARE_MOVABLE_CONTAINER(QMultiHash); Q_DECLARE_MOVABLE_CONTAINER(QCache); #undef Q_DECLARE_MOVABLE_CONTAINER /* Specialize a specific type with: Q_DECLARE_TYPEINFO(type, flags); where 'type' is the name of the type to specialize and 'flags' is logically-OR'ed combination of the flags below. */ enum { /* TYPEINFO flags */ Q_COMPLEX_TYPE = 0, Q_PRIMITIVE_TYPE = 0x1, Q_RELOCATABLE_TYPE = 0x2, Q_MOVABLE_TYPE = 0x2, Q_DUMMY_TYPE = 0x4, }; #define Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS) \ class QTypeInfo \ { \ public: \ enum { \ isComplex = (((FLAGS) & Q_PRIMITIVE_TYPE) == 0) && !std::is_trivial_v, \ isRelocatable = !isComplex || ((FLAGS) & Q_RELOCATABLE_TYPE) || QtPrivate::qIsRelocatable, \ isPointer [[deprecated("Use std::is_pointer instead")]] = std::is_pointer_v< TYPE >, \ isIntegral [[deprecated("Use std::is_integral instead")]] = std::is_integral< TYPE >::value, \ isValueInitializationBitwiseZero = QtPrivate::qIsValueInitializationBitwiseZero, \ }; \ static_assert(!isRelocatable || \ std::is_copy_constructible_v || \ std::is_move_constructible_v, \ #TYPE " is neither copy- nor move-constructible, so cannot be Q_RELOCATABLE_TYPE"); \ } #define Q_DECLARE_TYPEINFO(TYPE, FLAGS) \ template<> \ Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS) /* Specialize QTypeInfo for QFlags */ template class QFlags; template Q_DECLARE_TYPEINFO_BODY(QFlags, Q_PRIMITIVE_TYPE); namespace QTypeTraits { /* The templates below aim to find out whether one can safely instantiate an operator==() or operator<() for a type. This is tricky for containers, as most containers have unconstrained comparison operators, even though they rely on the corresponding operators for its content. This is especially true for all of the STL template classes that have a comparison operator defined, and leads to the situation, that the compiler would try to instantiate the operator, and fail if any of its template arguments does not have the operator implemented. The code tries to cover the relevant cases for Qt and the STL, by checking (recusrsively) the value_type of a container (if it exists), and checking the template arguments of pair, tuple and variant. */ namespace detail { // find out whether T is a conteiner // this is required to check the value type of containers for the existence of the comparison operator template struct is_container : std::false_type {}; template struct is_container().begin() != std::declval().end()), bool> >> : std::true_type {}; // Checks the existence of the comparison operator for the class itself QT_WARNING_PUSH QT_WARNING_DISABLE_FLOAT_COMPARE template struct has_operator_equal : std::false_type {}; template struct has_operator_equal() == std::declval()))>> : std::true_type {}; QT_WARNING_POP // Two forward declarations template::value> struct expand_operator_equal_container; template struct expand_operator_equal_tuple; // the entry point for the public method template using expand_operator_equal = expand_operator_equal_container; // if T isn't a container check if it's a tuple like object template struct expand_operator_equal_container : expand_operator_equal_tuple {}; // if T::value_type exists, check first T::value_type, then T itself template struct expand_operator_equal_container : std::conjunction< std::disjunction< std::is_same, // avoid endless recursion expand_operator_equal >, expand_operator_equal_tuple> {}; // recursively check the template arguments of a tuple like object template using expand_operator_equal_recursive = std::conjunction...>; template struct expand_operator_equal_tuple : has_operator_equal {}; template struct expand_operator_equal_tuple> : expand_operator_equal_recursive {}; template struct expand_operator_equal_tuple> : expand_operator_equal_recursive {}; template struct expand_operator_equal_tuple> : expand_operator_equal_recursive {}; template struct expand_operator_equal_tuple> : expand_operator_equal_recursive {}; // the same for operator<(), see above for explanations template struct has_operator_less_than : std::false_type{}; template struct has_operator_less_than() < std::declval()))>> : std::true_type{}; template::value> struct expand_operator_less_than_container; template struct expand_operator_less_than_tuple; template using expand_operator_less_than = expand_operator_less_than_container; template struct expand_operator_less_than_container : expand_operator_less_than_tuple {}; template struct expand_operator_less_than_container : std::conjunction< std::disjunction< std::is_same, expand_operator_less_than >, expand_operator_less_than_tuple > {}; template using expand_operator_less_than_recursive = std::conjunction...>; template struct expand_operator_less_than_tuple : has_operator_less_than {}; template struct expand_operator_less_than_tuple> : expand_operator_less_than_recursive {}; template struct expand_operator_less_than_tuple> : expand_operator_less_than_recursive {}; template struct expand_operator_less_than_tuple> : expand_operator_less_than_recursive {}; template struct expand_operator_less_than_tuple> : expand_operator_less_than_recursive {}; } template struct is_dereferenceable : std::false_type {}; template struct is_dereferenceable().operator->())> > : std::true_type {}; template inline constexpr bool is_dereferenceable_v = is_dereferenceable::value; template struct has_operator_equal : detail::expand_operator_equal {}; template inline constexpr bool has_operator_equal_v = has_operator_equal::value; template using has_operator_equal_container = std::disjunction, QTypeTraits::has_operator_equal>; template struct has_operator_less_than : detail::expand_operator_less_than {}; template inline constexpr bool has_operator_less_than_v = has_operator_less_than::value; template using has_operator_less_than_container = std::disjunction, QTypeTraits::has_operator_less_than>; template using compare_eq_result = std::enable_if_t...>, bool>; template using compare_eq_result_container = std::enable_if_t...>, bool>; template using compare_lt_result = std::enable_if_t...>, bool>; template using compare_lt_result_container = std::enable_if_t...>, bool>; namespace detail { template const T &const_reference(); template T &reference(); } template struct has_ostream_operator : std::false_type {}; template struct has_ostream_operator() << detail::const_reference())>> : std::true_type {}; template inline constexpr bool has_ostream_operator_v = has_ostream_operator::value; template using has_ostream_operator_container = std::disjunction, QTypeTraits::has_ostream_operator>; template struct has_istream_operator : std::false_type {}; template struct has_istream_operator() >> detail::reference())>> : std::true_type {}; template inline constexpr bool has_istream_operator_v = has_istream_operator::value; template using has_istream_operator_container = std::disjunction, QTypeTraits::has_istream_operator>; template inline constexpr bool has_stream_operator_v = has_ostream_operator_v && has_istream_operator_v; } QT_END_NAMESPACE #endif // QTYPEINFO_H