Qt 6.x
The Qt SDK
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 : quintptr {
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 explicit constexpr QNativeIpcKey(Type type = DefaultTypeForOs) noexcept
41 : d()
42 {
43 typeAndFlags.type = type;
44 }
45
46 Q_IMPLICIT QNativeIpcKey(const QString &k, Type type = DefaultTypeForOs)
47 : d(), key(k)
48 {
49 typeAndFlags.type = type;
50 }
51
52 QNativeIpcKey(const QNativeIpcKey &other)
53 : d(other.d), key(other.key)
54 {
55 if (isSlowPath())
56 copy_internal(other);
57 }
58
59 QNativeIpcKey(QNativeIpcKey &&other) noexcept
60 : d(std::exchange(other.d, 0)), key(std::move(other.key))
61 {
62 if (isSlowPath())
63 move_internal(std::move(other));
64 }
65
66 ~QNativeIpcKey()
67 {
68 if (isSlowPath())
69 destroy_internal();
70 }
71
72 QNativeIpcKey &operator=(const QNativeIpcKey &other)
73 {
74 key = other.key;
75 if (isSlowPath() || other.isSlowPath())
76 return assign_internal(other);
77 d = other.d;
78 return *this;
79 }
80
81 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QNativeIpcKey)
82 void swap(QNativeIpcKey &other)
83 {
84 std::swap(d, other.d);
85 key.swap(other.key);
86 }
87
88 bool isEmpty() const
89 {
90 return key.isEmpty();
91 }
92
93 bool isValid() const
94 {
95 return type() != Type{};
96 }
97
98 constexpr Type type() const noexcept
99 {
100 if (isSlowPath())
101 return type_internal();
102 return typeAndFlags.type;
103 }
104
105 constexpr void setType(Type type)
106 {
107 if (isSlowPath())
108 return setType_internal(type);
109 typeAndFlags.type = type;
110 }
111
112 QString nativeKey() const noexcept
113 { return key; }
114 void setNativeKey(const QString &newKey)
115 {
116 key = newKey;
117 if (isSlowPath())
118 setNativeKey_internal(newKey);
119 }
120
121 Q_CORE_EXPORT QString toString() const;
122 Q_CORE_EXPORT static QNativeIpcKey fromString(const QString &string);
123
124private:
125 struct TypeAndFlags {
126#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
127 // this is the LSB
128 quintptr isExtended : 1;
129 Type type : 15;
130#endif
131
132 quintptr reserved : sizeof(quintptr) * 8 - 16;
133
134#if Q_BYTE_ORDER == Q_BIG_ENDIAN
135 Type type : 15;
136 quint16 isExtended : 1;
137 // this was the LSB
138#endif
139 };
140
141 // Bit 0: if set, holds a pointer (with the LSB set); if clear, holds the
142 // the TypeAndFlags structure.
143 union {
144 quintptr d = 0;
145 TypeAndFlags typeAndFlags;
146 static_assert(sizeof(typeAndFlags) == sizeof(d));
147 };
148
149 QString key;
150
151 friend class QNativeIpcKeyPrivate;
152 QNativeIpcKeyPrivate *d_func();
153 const QNativeIpcKeyPrivate *d_func() const;
154 constexpr bool isSlowPath() const noexcept
155 { return Q_UNLIKELY(typeAndFlags.isExtended); }
156
157 friend bool operator==(const QNativeIpcKey &lhs, const QNativeIpcKey &rhs) noexcept
158 {
159 if (lhs.key != rhs.key)
160 return false;
161 if (lhs.d == rhs.d)
162 return true;
163 if (lhs.isSlowPath() && rhs.isSlowPath())
164 return compare_internal(lhs, rhs) == 0;
165 return lhs.d == rhs.d;
166 }
167 friend bool operator!=(const QNativeIpcKey &lhs, const QNativeIpcKey &rhs) noexcept
168 {
169 return !(lhs == rhs);
170 }
171
172 Q_CORE_EXPORT void copy_internal(const QNativeIpcKey &other);
173 Q_CORE_EXPORT void move_internal(QNativeIpcKey &&other) noexcept;
174 Q_CORE_EXPORT QNativeIpcKey &assign_internal(const QNativeIpcKey &other);
175 Q_CORE_EXPORT void destroy_internal() noexcept;
176 Q_DECL_PURE_FUNCTION Q_CORE_EXPORT Type type_internal() const noexcept;
177 Q_CORE_EXPORT void setType_internal(Type);
178 Q_CORE_EXPORT void setNativeKey_internal(const QString &);
179 Q_DECL_PURE_FUNCTION Q_CORE_EXPORT static int
180 compare_internal(const QNativeIpcKey &lhs, const QNativeIpcKey &rhs) noexcept;
181
182#ifdef Q_OS_DARWIN
183 Q_DECL_CONST_FUNCTION Q_CORE_EXPORT static Type defaultTypeForOs_internal() noexcept;
184#endif
185};
186
187// not a shared type, exactly, but this works too
188Q_DECLARE_SHARED(QNativeIpcKey)
189
190inline auto QNativeIpcKey::legacyDefaultTypeForOs() noexcept -> Type
191{
192#if defined(Q_OS_WIN)
193 return Type::Windows;
194#elif defined(QT_POSIX_IPC)
195 return Type::PosixRealtime;
196#elif defined(Q_OS_DARWIN)
197 return defaultTypeForOs_internal();
198#else
199 return Type::SystemV;
200#endif
201}
202
203#endif // QT_CONFIG(sharedmemory) || QT_CONFIG(systemsemaphore)
204
206
207#endif // QNATIVEIPCKEY_H
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
Combined button and popup list for selecting options.
#define Q_UNLIKELY(x)
#define Q_DECL_PURE_FUNCTION
#define Q_DECL_CONST_FUNCTION
#define Q_IMPLICIT
constexpr bool operator!=(const timespec &t1, const timespec &t2)
GLuint64 key
GLenum type
static bool fromString(const QMetaObject *mo, QString s, Allocate &&allocate)
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1219
#define Q_ENUM(x)
#define Q_GADGET_EXPORT(...)
unsigned short quint16
Definition qtypes.h:43
size_t quintptr
Definition qtypes.h:72
#define explicit
QSharedPointer< T > other(t)
[5]
this swap(other)
proxy setType(QNetworkProxy::Socks5Proxy)
char * toString(const MyType &t)
[31]
Definition moc.h:24