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
qthreadstorage.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
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 QTHREADSTORAGE_H
5#define QTHREADSTORAGE_H
6
7#include <QtCore/qglobal.h>
8
9#if QT_CONFIG(thread)
10
12
13
14class Q_CORE_EXPORT QThreadStorageData
15{
16public:
17 explicit QThreadStorageData(void (*func)(void *));
18 ~QThreadStorageData();
19
20 void** get() const;
21 void** set(void* p);
22
23 static void finish(void**);
24 int id;
25};
26
27#if !defined(QT_MOC_CPP)
28// MOC_SKIP_BEGIN
29
30// pointer specialization
31template <typename T>
32inline
33T *&qThreadStorage_localData(QThreadStorageData &d, T **)
34{
35 void **v = d.get();
36 if (!v) v = d.set(nullptr);
37 return *(reinterpret_cast<T**>(v));
38}
39
40template <typename T>
41inline
42T *qThreadStorage_localData_const(const QThreadStorageData &d, T **)
43{
44 void **v = d.get();
45 return v ? *(reinterpret_cast<T**>(v)) : 0;
46}
47
48template <typename T>
49inline
50void qThreadStorage_setLocalData(QThreadStorageData &d, T **t)
51{ (void) d.set(*t); }
52
53template <typename T>
54inline
55void qThreadStorage_deleteData(void *d, T **)
56{ delete static_cast<T *>(d); }
57
58// value-based specialization
59template <typename T>
60inline
61T &qThreadStorage_localData(QThreadStorageData &d, T *)
62{
63 void **v = d.get();
64 if (!v) v = d.set(new T());
65 return *(reinterpret_cast<T*>(*v));
66}
67
68template <typename T>
69inline
70T qThreadStorage_localData_const(const QThreadStorageData &d, T *)
71{
72 void **v = d.get();
73 return v ? *(reinterpret_cast<T*>(*v)) : T();
74}
75
76template <typename T>
77inline
78void qThreadStorage_setLocalData(QThreadStorageData &d, T *t)
79{ (void) d.set(new T(*t)); }
80
81template <typename T>
82inline
83void qThreadStorage_deleteData(void *d, T *)
84{ delete static_cast<T *>(d); }
85
86
87// MOC_SKIP_END
88#endif
89
90template <class T>
92{
93private:
94 QThreadStorageData d;
95
96 Q_DISABLE_COPY(QThreadStorage)
97
98 static inline void deleteData(void *x)
99 { qThreadStorage_deleteData(x, reinterpret_cast<T*>(0)); }
100
101public:
102 inline QThreadStorage() : d(deleteData) { }
103 inline ~QThreadStorage() { }
104
105 inline bool hasLocalData() const
106 { return d.get() != nullptr; }
107
108 inline T& localData()
109 { return qThreadStorage_localData(d, reinterpret_cast<T*>(0)); }
110 inline T localData() const
111 { return qThreadStorage_localData_const(d, reinterpret_cast<T*>(0)); }
112
113 inline void setLocalData(T t)
114 { qThreadStorage_setLocalData(d, &t); }
115};
116
118
119#else // !QT_CONFIG(thread)
120
121#include <QtCore/qscopedpointer.h>
122
123#include <type_traits>
124
126
127template <typename T, typename U>
128inline bool qThreadStorage_hasLocalData(const QScopedPointer<T, U> &data)
129{
130 return !!data;
131}
132
133template <typename T, typename U>
134inline bool qThreadStorage_hasLocalData(const QScopedPointer<T*, U> &data)
135{
136 return !!data ? *data != nullptr : false;
137}
138
139template <typename T>
141{
142 delete t;
143}
144
145template <typename T>
147{
148 delete *t;
149 delete t;
150}
151
152template <class T>
154{
155private:
156 struct ScopedPointerThreadStorageDeleter
157 {
158 static inline void cleanup(T *t)
159 {
160 if (t == nullptr)
161 return;
163 }
164 };
165 QScopedPointer<T, ScopedPointerThreadStorageDeleter> data;
166
167public:
168 QThreadStorage() = default;
169 ~QThreadStorage() = default;
170 QThreadStorage(const QThreadStorage &rhs) = delete;
172
173 inline bool hasLocalData() const
174 {
176 }
177
178 inline T &localData()
179 {
180 if (!data)
181 data.reset(new T());
182 return *data;
183 }
184
185 inline T localData() const
186 {
187 return !!data ? *data : T();
188 }
189
190 inline void setLocalData(T t)
191 {
192 data.reset(new T(t));
193 }
194};
195
197
198#endif // QT_CONFIG(thread)
199
200#endif // QTHREADSTORAGE_H
\inmodule QtCore
QThreadStorage()=default
Constructs a new per-thread data storage object.
QThreadStorage(const QThreadStorage &rhs)=delete
bool hasLocalData() const
If T is a pointer type, returns true if the calling thread has non-zero data available.
QThreadStorage & operator=(const QThreadStorage &rhs)=delete
T localData() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
~QThreadStorage()=default
Destroys the per-thread data storage object.
void setLocalData(T t)
Sets the local data for the calling thread to data.
T & localData()
Returns a reference to the data that was set by the calling thread.
Combined button and popup list for selecting options.
static void deleteData(QBrushData *d)
Definition qbrush.cpp:215
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
static QDBusError::ErrorType get(const char *name)
GLsizei const GLfloat * v
[13]
GLint GLint GLint GLint GLint x
[0]
GLenum GLuint id
[7]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum func
Definition qopenglext.h:663
GLdouble GLdouble t
Definition qopenglext.h:243
GLfloat GLfloat p
[1]
QT_BEGIN_NAMESPACE bool qThreadStorage_hasLocalData(const QScopedPointer< T, U > &data)
void qThreadStorage_deleteLocalData(T *t)
QFuture< QSet< QChar > > set
[10]
QObject::connect nullptr