Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
q20memory.h
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
5
6#ifndef Q20MEMORY_H
7#define Q20MEMORY_H
8
9#include <QtCore/qtconfigmacros.h>
10
11#include <QtCore/q17memory.h>
12
13#include <QtCore/q20type_traits.h>
14#include <utility>
15
16//
17// W A R N I N G
18// -------------
19//
20// This file is not part of the Qt API. Types and functions defined in this
21// file can reliably be replaced by their std counterparts, once available.
22// You may use these definitions in your own code, but be aware that we
23// will remove them once Qt depends on the C++ version that supports
24// them in namespace std. There will be NO deprecation warning, the
25// definitions will JUST go away.
26//
27// If you can't agree to these terms, don't use these definitions!
28//
29// We mean it.
30//
31
32QT_BEGIN_NAMESPACE
33
34// like std::construct_at (but not whitelisted for constexpr)
35namespace q20 {
36#ifdef __cpp_lib_constexpr_dynamic_alloc
37using std::construct_at;
38#else
39template <typename T,
40 typename... Args,
41 typename Enable = std::void_t<decltype(::new (std::declval<void *>()) T(std::declval<Args>()...))> >
42T *construct_at(T *ptr, Args && ... args)
43{
44 return ::new (const_cast<void *>(static_cast<const volatile void *>(ptr)))
45 T(std::forward<Args>(args)...);
46}
47#endif // __cpp_lib_constexpr_dynamic_alloc
48} // namespace q20
49
50// like std::make_unique_for_overwrite (excl. C++23-added constexpr)
51namespace q20 {
52#ifdef __cpp_lib_smart_ptr_for_overwrite
54#else
55// https://eel.is/c++draft/unique.ptr.create#6
56template <typename T>
59// https://eel.is/c++draft/unique.ptr.create#7
60{ return std::unique_ptr<T>(new T); }
61
62// https://eel.is/c++draft/unique.ptr.create#8
63template <typename T>
66// https://eel.is/c++draft/unique.ptr.create#9
67{ return std::unique_ptr<T>(new std::remove_extent_t<T>[n]); }
68
69// https://eel.is/c++draft/unique.ptr.create#10
70template <typename T, typename...Args>
73
74#endif // __cpp_lib_smart_ptr_for_overwrite
75} // namespace q20
76
77namespace q20 {
78// like std::to_address
79#ifdef __cpp_lib_to_address
80using std::to_address;
81#else
82// http://eel.is/c++draft/pointer.conversion
83template <typename T>
84constexpr T *to_address(T *p) noexcept {
85 // http://eel.is/c++draft/pointer.conversion#1:
86 // Mandates: T is not a function type.
87 static_assert(!std::is_function_v<T>, "to_address must not be used on function types");
88 return p;
89}
90
91template <typename Ptr, typename std::enable_if_t<!std::is_pointer_v<Ptr>, bool> = true>
92constexpr auto to_address(const Ptr &ptr) noexcept; // fwd declared
93
94namespace detail {
95 // http://eel.is/c++draft/pointer.conversion#3
96 template <typename Ptr, typename = void>
98 static auto get(const Ptr &ptr) noexcept
99 { return q20::to_address(ptr.operator->()); }
100 };
101 template <typename Ptr>
103 decltype(std::pointer_traits<Ptr>::to_address(std::declval<const Ptr&>()))
104 >>
105 {
106 static auto get(const Ptr &ptr) noexcept
107 { return std::pointer_traits<Ptr>::to_address(ptr); }
108 };
109} // namespace detail
110
111template <typename Ptr, typename std::enable_if_t<!std::is_pointer_v<Ptr>, bool>>
112constexpr auto to_address(const Ptr &ptr) noexcept
113{ return detail::to_address_helper<Ptr>::get(ptr); }
114
115#endif // __cpp_lib_to_address
116} // namespace q20
117
118QT_END_NAMESPACE
119
120#endif /* Q20MEMORY_H */
constexpr T * to_address(T *p) noexcept
Definition q20memory.h:84
T * construct_at(T *ptr, Args &&... args)
Definition q20memory.h:42
constexpr auto to_address(const Ptr &ptr) noexcept
Definition q20memory.h:112
static QString retrieveLabel(const QStorageInfoPrivate &d, int fd, quint64 deviceId)
static constexpr short MountId
static quint64 retrieveDeviceId(const QByteArray &device, quint64 deviceId=0)
static const char MountInfoPath[]
static constexpr short MountPoint
static constexpr short MountSource
static constexpr short FieldCount
static quint64 mountIdForPath(int fd)
static constexpr short MountOptions
std::vector< MountInfo > doParseMountInfo(const QByteArray &mountinfo, FilterMountInfo filter)
static constexpr short FsType
static std::optional< dev_t > deviceNumber(QByteArrayView devno)
static std::optional< QString > retrieveLabelViaIoctl(int fd)
#define FS_IOC_GETFSLABEL
static constexpr short DevNo
#define ST_RDONLY
static QByteArray parseMangledPath(QByteArrayView path)
static constexpr short SuperOptions
static QDirListing devicesByLabel()
static constexpr short FsRoot
static QString decodeFsEncString(QString &&str)
static std::vector< MountInfo > parseMountInfo(FilterMountInfo filter=FilterMountInfo::All)
static auto retrieveLabels()
static void tokenizeLine(std::array< QByteArrayView, FieldCount > &fields, QByteArrayView line)
static dev_t deviceIdForPath(const QString &device)
#define FSLABEL_MAX
static std::vector< MountInfo > doParseMountInfo(const QByteArray &mountinfo, FilterMountInfo filter=FilterMountInfo::All)
static auto get(const Ptr &ptr) noexcept
Definition q20memory.h:98