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 <type_traits>
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
50namespace q20 {
51// like std::to_address
52#ifdef __cpp_lib_to_address
53using std::to_address;
54#else
55// http://eel.is/c++draft/pointer.conversion
56template <typename T>
57constexpr T *to_address(T *p) noexcept {
58 // http://eel.is/c++draft/pointer.conversion#1:
59 // Mandates: T is not a function type.
60 static_assert(!std::is_function_v<T>, "to_address must not be used on function types");
61 return p;
62}
63
64template <typename Ptr, typename std::enable_if_t<!std::is_pointer_v<Ptr>, bool> = true>
65constexpr auto to_address(const Ptr &ptr) noexcept; // fwd declared
66
67namespace detail {
68 // http://eel.is/c++draft/pointer.conversion#3
69 template <typename Ptr, typename = void>
70 struct to_address_helper {
71 static auto get(const Ptr &ptr) noexcept
72 { return q20::to_address(ptr.operator->()); }
73 };
74 template <typename Ptr>
75 struct to_address_helper<Ptr, std::void_t<
76 decltype(std::pointer_traits<Ptr>::to_address(std::declval<const Ptr&>()))
77 >>
78 {
79 static auto get(const Ptr &ptr) noexcept
80 { return std::pointer_traits<Ptr>::to_address(ptr); }
81 };
82} // namespace detail
83
84template <typename Ptr, typename std::enable_if_t<!std::is_pointer_v<Ptr>, bool>>
85constexpr auto to_address(const Ptr &ptr) noexcept
86{ return detail::to_address_helper<Ptr>::get(ptr); }
87
88#endif // __cpp_lib_to_address
89} // namespace q20
90
91QT_END_NAMESPACE
92
93#endif /* Q20MEMORY_H */
94

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