Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/QtCore/q20memory.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Copyright (C) 2023 The Qt Company Ltd.
0002 // Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
0003 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0004 // Qt-Security score:significant reason:default
0005 
0006 #ifndef Q20MEMORY_H
0007 #define Q20MEMORY_H
0008 
0009 #include <QtCore/qtconfigmacros.h>
0010 
0011 #include <QtCore/q17memory.h>
0012 
0013 #include <QtCore/q20type_traits.h>
0014 #include <utility>
0015 
0016 //
0017 //  W A R N I N G
0018 //  -------------
0019 //
0020 // This file is not part of the Qt API. Types and functions defined in this
0021 // file can reliably be replaced by their std counterparts, once available.
0022 // You may use these definitions in your own code, but be aware that we
0023 // will remove them once Qt depends on the C++ version that supports
0024 // them in namespace std. There will be NO deprecation warning, the
0025 // definitions will JUST go away.
0026 //
0027 // If you can't agree to these terms, don't use these definitions!
0028 //
0029 // We mean it.
0030 //
0031 
0032 QT_BEGIN_NAMESPACE
0033 
0034 // like std::construct_at (but not whitelisted for constexpr)
0035 namespace q20 {
0036 #ifdef __cpp_lib_constexpr_dynamic_alloc
0037 using std::construct_at;
0038 #else
0039 template <typename T,
0040           typename... Args,
0041           typename Enable = std::void_t<decltype(::new (std::declval<void *>()) T(std::declval<Args>()...))> >
0042 T *construct_at(T *ptr, Args && ... args)
0043 {
0044     return ::new (const_cast<void *>(static_cast<const volatile void *>(ptr)))
0045                                                                 T(std::forward<Args>(args)...);
0046 }
0047 #endif // __cpp_lib_constexpr_dynamic_alloc
0048 } // namespace q20
0049 
0050 // like std::make_unique_for_overwrite (excl. C++23-added constexpr)
0051 namespace q20 {
0052 #ifdef __cpp_lib_smart_ptr_for_overwrite
0053 using std::make_unique_for_overwrite;
0054 #else
0055 // https://eel.is/c++draft/unique.ptr.create#6
0056 template <typename T>
0057 std::enable_if_t<!std::is_array_v<T>, std::unique_ptr<T>>
0058 make_unique_for_overwrite()
0059 // https://eel.is/c++draft/unique.ptr.create#7
0060 { return std::unique_ptr<T>(new T); }
0061 
0062 // https://eel.is/c++draft/unique.ptr.create#8
0063 template <typename T>
0064 std::enable_if_t<q20::is_unbounded_array_v<T>, std::unique_ptr<T>>
0065 make_unique_for_overwrite(std::size_t n)
0066 // https://eel.is/c++draft/unique.ptr.create#9
0067 { return std::unique_ptr<T>(new std::remove_extent_t<T>[n]); }
0068 
0069 // https://eel.is/c++draft/unique.ptr.create#10
0070 template <typename T, typename...Args>
0071 std::enable_if_t<q20::is_bounded_array_v<T>>
0072 make_unique_for_overwrite(Args&&...) = delete;
0073 
0074 #endif // __cpp_lib_smart_ptr_for_overwrite
0075 } // namespace q20
0076 
0077 namespace q20 {
0078 // like std::to_address
0079 #ifdef __cpp_lib_to_address
0080 using std::to_address;
0081 #else
0082 // http://eel.is/c++draft/pointer.conversion
0083 template <typename T>
0084 constexpr T *to_address(T *p) noexcept {
0085     // http://eel.is/c++draft/pointer.conversion#1:
0086     //    Mandates: T is not a function type.
0087     static_assert(!std::is_function_v<T>, "to_address must not be used on function types");
0088     return p;
0089 }
0090 
0091 template <typename Ptr, typename std::enable_if_t<!std::is_pointer_v<Ptr>, bool> = true>
0092 constexpr auto to_address(const Ptr &ptr) noexcept; // fwd declared
0093 
0094 namespace detail {
0095     // http://eel.is/c++draft/pointer.conversion#3
0096     template <typename Ptr, typename = void>
0097     struct to_address_helper {
0098         static auto get(const Ptr &ptr) noexcept
0099         { return q20::to_address(ptr.operator->()); }
0100     };
0101     template <typename Ptr>
0102     struct to_address_helper<Ptr, std::void_t<
0103             decltype(std::pointer_traits<Ptr>::to_address(std::declval<const Ptr&>()))
0104         >>
0105     {
0106         static auto get(const Ptr &ptr) noexcept
0107         { return std::pointer_traits<Ptr>::to_address(ptr); }
0108     };
0109 } // namespace detail
0110 
0111 template <typename Ptr, typename std::enable_if_t<!std::is_pointer_v<Ptr>, bool>>
0112 constexpr auto to_address(const Ptr &ptr) noexcept
0113 { return detail::to_address_helper<Ptr>::get(ptr); }
0114 
0115 #endif // __cpp_lib_to_address
0116 } // namespace q20
0117 
0118 QT_END_NAMESPACE
0119 
0120 #endif /* Q20MEMORY_H */