Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 09:09:05

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