Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qtipccommon.h
Go to the documentation of this file.
1// Copyright (C) 2022 Intel Corporation.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QNATIVEIPCKEY_H
5#define QNATIVEIPCKEY_H
6
7#include <QtCore/qglobal.h>
8#include <QtCore/qtcore-config.h>
9
10#if QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore)
11# include <QtCore/qstring.h>
12# include <QtCore/qobjectdefs.h>
13
15
16class QNativeIpcKeyPrivate;
17class QNativeIpcKey
18{
19 Q_GADGET_EXPORT(Q_CORE_EXPORT)
20public:
21 enum class Type : quint16 {
22 // 0 is reserved for the invalid type
23 // keep 1 through 0xff free, except for SystemV
24 SystemV = 0x51, // 'Q'
25
26 PosixRealtime = 0x100,
27 Windows,
28 };
30
31 static constexpr Type DefaultTypeForOs =
32#ifdef Q_OS_WIN
33 Type::Windows
34#else
35 Type::PosixRealtime
36#endif
37 ;
38 static Type legacyDefaultTypeForOs() noexcept;
39
40 constexpr QNativeIpcKey() noexcept = default;
41
42 explicit constexpr QNativeIpcKey(Type type) noexcept
43 : typeAndFlags{type}
44 {
45 }
46
47 Q_IMPLICIT QNativeIpcKey(const QString &k, Type type = DefaultTypeForOs)
48 : key(k), typeAndFlags{type}
49 {
50 }
51
52 QNativeIpcKey(const QNativeIpcKey &other)
53 : d(other.d), key(other.key), typeAndFlags(other.typeAndFlags)
54 {
55 if (isSlowPath())
56 copy_internal(other);
57 }
58
59 QNativeIpcKey(QNativeIpcKey &&other) noexcept
60 : d(std::exchange(other.d, nullptr)), key(std::move(other.key)),
61 typeAndFlags(std::move(other.typeAndFlags))
62 {
63 if (isSlowPath())
64 move_internal(std::move(other));
65 }
66
67 ~QNativeIpcKey()
68 {
69 if (isSlowPath())
70 destroy_internal();
71 }
72
73 QNativeIpcKey &operator=(const QNativeIpcKey &other)
74 {
75 typeAndFlags = other.typeAndFlags;
76 key = other.key;
77 if (isSlowPath() || other.isSlowPath())
78 return assign_internal(other);
79 Q_ASSERT(!d);
80 return *this;
81 }
82
83 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QNativeIpcKey)
84 void swap(QNativeIpcKey &other) noexcept
85 {
86 std::swap(d, other.d);
87 key.swap(other.key);
88 typeAndFlags.swap(other.typeAndFlags);
89 }
90
91 bool isEmpty() const noexcept
92 {
93 return key.isEmpty();
94 }
95
96 bool isValid() const noexcept
97 {
98 return type() != Type{};
99 }
100
101 constexpr Type type() const noexcept
102 {
103 return typeAndFlags.type;
104 }
105
106 constexpr void setType(Type type)
107 {
108 if (isSlowPath())
109 return setType_internal(type);
110 typeAndFlags.type = type;
111 }
112
113 QString nativeKey() const noexcept
114 {
115 return key;
116 }
117 void setNativeKey(const QString &newKey)
118 {
119 key = newKey;
120 if (isSlowPath())
121 setNativeKey_internal(newKey);
122 }
123
124 Q_CORE_EXPORT QString toString() const;
125 Q_CORE_EXPORT static QNativeIpcKey fromString(const QString &string);
126
127private:
128 struct TypeAndFlags {
129 Type type = DefaultTypeForOs;
130 quint16 reserved1 = {};
131 quint32 reserved2 = {};
132
133 void swap(TypeAndFlags &other) noexcept
134 {
135 std::swap(type, other.type);
136 std::swap(reserved1, other.reserved1);
137 std::swap(reserved2, other.reserved2);
138 }
139
140 friend constexpr bool operator==(const TypeAndFlags &lhs, const TypeAndFlags &rhs) noexcept
141 {
142 return lhs.type == rhs.type &&
143 lhs.reserved1 == rhs.reserved1 &&
144 lhs.reserved2 == rhs.reserved2;
145 }
146 };
147
148 QNativeIpcKeyPrivate *d = nullptr;
149 QString key;
150 TypeAndFlags typeAndFlags;
151
152 friend class QNativeIpcKeyPrivate;
153 constexpr bool isSlowPath() const noexcept
154 { return Q_UNLIKELY(d); }
155
156 friend Q_CORE_EXPORT size_t qHash(const QNativeIpcKey &ipcKey, size_t seed) noexcept;
157 friend size_t qHash(const QNativeIpcKey &ipcKey) noexcept
158 { return qHash(ipcKey, 0); }
159
160 Q_CORE_EXPORT void copy_internal(const QNativeIpcKey &other);
161 Q_CORE_EXPORT void move_internal(QNativeIpcKey &&other) noexcept;
162 Q_CORE_EXPORT QNativeIpcKey &assign_internal(const QNativeIpcKey &other);
163 Q_CORE_EXPORT void destroy_internal() noexcept;
164 Q_CORE_EXPORT void setType_internal(Type);
165 Q_CORE_EXPORT void setNativeKey_internal(const QString &);
166 Q_DECL_PURE_FUNCTION Q_CORE_EXPORT static int
167 compare_internal(const QNativeIpcKey &lhs, const QNativeIpcKey &rhs) noexcept;
168
169#ifdef Q_OS_DARWIN
170 Q_DECL_CONST_FUNCTION Q_CORE_EXPORT static Type defaultTypeForOs_internal() noexcept;
171#endif
172 friend bool comparesEqual(const QNativeIpcKey &lhs, const QNativeIpcKey &rhs) noexcept
173 {
174 if (!(lhs.typeAndFlags == rhs.typeAndFlags))
175 return false;
176 if (lhs.key != rhs.key)
177 return false;
178 if (lhs.d == rhs.d)
179 return true;
180 return compare_internal(lhs, rhs) == 0;
181 }
182 Q_DECLARE_EQUALITY_COMPARABLE(QNativeIpcKey)
183};
184
185// not a shared type, exactly, but this works too
186Q_DECLARE_SHARED(QNativeIpcKey)
187
188inline auto QNativeIpcKey::legacyDefaultTypeForOs() noexcept -> Type
189{
190#if defined(Q_OS_WIN)
191 return Type::Windows;
192#elif defined(QT_POSIX_IPC)
193 return Type::PosixRealtime;
194#elif defined(Q_OS_DARWIN)
195 return defaultTypeForOs_internal();
196#else
197 return Type::SystemV;
198#endif
199}
200
202
203#endif // QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore)
204
205
206#endif // QNATIVEIPCKEY_H
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
Combined button and popup list for selecting options.
#define Q_DECLARE_EQUALITY_COMPARABLE(...)
#define Q_UNLIKELY(x)
#define Q_DECL_PURE_FUNCTION
#define Q_DECL_CONST_FUNCTION
#define Q_IMPLICIT
bool comparesEqual(const QDir &lhs, const QDir &rhs)
Definition qdir.cpp:1819
size_t qHash(const QFileSystemWatcherPathKey &key, size_t seed=0)
GLuint64 key
GLenum type
static bool fromString(const QMetaObject *mo, QString s, Allocate &&allocate)
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1220
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define Q_ENUM(x)
#define Q_GADGET_EXPORT(...)
unsigned int quint32
Definition qtypes.h:50
unsigned short quint16
Definition qtypes.h:48
#define explicit
QSharedPointer< T > other(t)
[5]
this swap(other)
proxy setType(QNetworkProxy::Socks5Proxy)
char * toString(const MyType &t)
[31]
Definition moc.h:23