1// Copyright (C) 2023 The Qt Company Ltd.
2// Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#ifndef Q20MEMORY_H
6#define Q20MEMORY_H
7
8#include <QtCore/qtconfigmacros.h>
9
10#include <memory>
11
12#include <QtCore/q20type_traits.h>
13#include <utility>
14
15//
16// W A R N I N G
17// -------------
18//
19// This file is not part of the Qt API. Types and functions defined in this
20// file can reliably be replaced by their std counterparts, once available.
21// You may use these definitions in your own code, but be aware that we
22// will remove them once Qt depends on the C++ version that supports
23// them in namespace std. There will be NO deprecation warning, the
24// definitions will JUST go away.
25//
26// If you can't agree to these terms, don't use these definitions!
27//
28// We mean it.
29//
30
31QT_BEGIN_NAMESPACE
32
33// like std::construct_at (but not whitelisted for constexpr)
34namespace q20 {
35#ifdef __cpp_lib_constexpr_dynamic_alloc
36using std::construct_at;
37#else
38template <typename T,
39 typename... Args,
40 typename Enable = std::void_t<decltype(::new (std::declval<void *>()) T(std::declval<Args>()...))> >
41T *construct_at(T *ptr, Args && ... args)
42{
43 return ::new (const_cast<void *>(static_cast<const volatile void *>(ptr)))
44 T(std::forward<Args>(args)...);
45}
46#endif // __cpp_lib_constexpr_dynamic_alloc
47} // namespace q20
48
49// like std::make_unique_for_overwrite (excl. C++23-added constexpr)
50namespace q20 {
51#ifdef __cpp_lib_smart_ptr_for_overwrite
52using std::make_unique_for_overwrite;
53#else
54// https://eel.is/c++draft/unique.ptr.create#6
55template <typename T>
56std::enable_if_t<!std::is_array_v<T>, std::unique_ptr<T>>
57make_unique_for_overwrite()
58// https://eel.is/c++draft/unique.ptr.create#7
59{ return std::unique_ptr<T>(new T); }
60
61// https://eel.is/c++draft/unique.ptr.create#8
62template <typename T>
63std::enable_if_t<q20::is_unbounded_array_v<T>, std::unique_ptr<T>>
64make_unique_for_overwrite(std::size_t n)
65// https://eel.is/c++draft/unique.ptr.create#9
66{ return std::unique_ptr<T>(new std::remove_extent_t<T>[n]); }
67
68// https://eel.is/c++draft/unique.ptr.create#10
69template <typename T, typename...Args>
70std::enable_if_t<q20::is_bounded_array_v<T>>
71make_unique_for_overwrite(Args&&...) = delete;
72
73#endif // __cpp_lib_smart_ptr_for_overwrite
74} // namespace q20
75
76namespace q20 {
77// like std::to_address
78#ifdef __cpp_lib_to_address
79using std::to_address;
80#else
81// http://eel.is/c++draft/pointer.conversion
82template <typename T>
83constexpr T *to_address(T *p) noexcept {
84 // http://eel.is/c++draft/pointer.conversion#1:
85 // Mandates: T is not a function type.
86 static_assert(!std::is_function_v<T>, "to_address must not be used on function types");
87 return p;
88}
89
90template <typename Ptr, typename std::enable_if_t<!std::is_pointer_v<Ptr>, bool> = true>
91constexpr auto to_address(const Ptr &ptr) noexcept; // fwd declared
92
93namespace detail {
94 // http://eel.is/c++draft/pointer.conversion#3
95 template <typename Ptr, typename = void>
96 struct to_address_helper {
97 static auto get(const Ptr &ptr) noexcept
98 { return q20::to_address(ptr.operator->()); }
99 };
100 template <typename Ptr>
101 struct to_address_helper<Ptr, std::void_t<
102 decltype(std::pointer_traits<Ptr>::to_address(std::declval<const Ptr&>()))
103 >>
104 {
105 static auto get(const Ptr &ptr) noexcept
106 { return std::pointer_traits<Ptr>::to_address(ptr); }
107 };
108} // namespace detail
109
110template <typename Ptr, typename std::enable_if_t<!std::is_pointer_v<Ptr>, bool>>
111constexpr auto to_address(const Ptr &ptr) noexcept
112{ return detail::to_address_helper<Ptr>::get(ptr); }
113
114#endif // __cpp_lib_to_address
115} // namespace q20
116
117QT_END_NAMESPACE
118
119#endif /* Q20MEMORY_H */
120

Provided by KDAB

Privacy Policy
Learn Advanced QML with KDAB
Find out more

source code of qtbase/src/corelib/global/q20memory.h