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 
0005 #ifndef Q20MEMORY_H
0006 #define Q20MEMORY_H
0007 
0008 #include <QtCore/qtconfigmacros.h>
0009 
0010 #include <memory>
0011 
0012 #include <type_traits>
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 
0050 namespace q20 {
0051 // like std::to_address
0052 #ifdef __cpp_lib_to_address
0053 using std::to_address;
0054 #else
0055 // http://eel.is/c++draft/pointer.conversion
0056 template <typename T>
0057 constexpr T *to_address(T *p) noexcept {
0058     // http://eel.is/c++draft/pointer.conversion#1:
0059     //    Mandates: T is not a function type.
0060     static_assert(!std::is_function_v<T>, "to_address must not be used on function types");
0061     return p;
0062 }
0063 
0064 template <typename Ptr, typename std::enable_if_t<!std::is_pointer_v<Ptr>, bool> = true>
0065 constexpr auto to_address(const Ptr &ptr) noexcept; // fwd declared
0066 
0067 namespace detail {
0068     // http://eel.is/c++draft/pointer.conversion#3
0069     template <typename Ptr, typename = void>
0070     struct to_address_helper {
0071         static auto get(const Ptr &ptr) noexcept
0072         { return q20::to_address(ptr.operator->()); }
0073     };
0074     template <typename Ptr>
0075     struct to_address_helper<Ptr, std::void_t<
0076             decltype(std::pointer_traits<Ptr>::to_address(std::declval<const Ptr&>()))
0077         >>
0078     {
0079         static auto get(const Ptr &ptr) noexcept
0080         { return std::pointer_traits<Ptr>::to_address(ptr); }
0081     };
0082 } // namespace detail
0083 
0084 template <typename Ptr, typename std::enable_if_t<!std::is_pointer_v<Ptr>, bool>>
0085 constexpr auto to_address(const Ptr &ptr) noexcept
0086 { return detail::to_address_helper<Ptr>::get(ptr); }
0087 
0088 #endif // __cpp_lib_to_address
0089 } // namespace q20
0090 
0091 QT_END_NAMESPACE
0092 
0093 #endif /* Q20MEMORY_H */