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 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | |
14 | class Q_CORE_EXPORT QThreadStorageData |
15 | { |
16 | public: |
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 |
31 | template <typename T> |
32 | inline |
33 | T *&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 | |
40 | template <typename T> |
41 | inline |
42 | T *qThreadStorage_localData_const(const QThreadStorageData &d, T **) |
43 | { |
44 | void **v = d.get(); |
45 | return v ? *(reinterpret_cast<T**>(v)) : 0; |
46 | } |
47 | |
48 | template <typename T> |
49 | inline |
50 | void qThreadStorage_setLocalData(QThreadStorageData &d, T **t) |
51 | { (void) d.set(*t); } |
52 | |
53 | template <typename T> |
54 | inline |
55 | void qThreadStorage_deleteData(void *d, T **) |
56 | { delete static_cast<T *>(d); } |
57 | |
58 | // value-based specialization |
59 | template <typename T> |
60 | inline |
61 | T &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 | |
68 | template <typename T> |
69 | inline |
70 | T qThreadStorage_localData_const(const QThreadStorageData &d, T *) |
71 | { |
72 | void **v = d.get(); |
73 | return v ? *(reinterpret_cast<T*>(*v)) : T(); |
74 | } |
75 | |
76 | template <typename T> |
77 | inline |
78 | void qThreadStorage_setLocalData(QThreadStorageData &d, T *t) |
79 | { (void) d.set(new T(*t)); } |
80 | |
81 | template <typename T> |
82 | inline |
83 | void qThreadStorage_deleteData(void *d, T *) |
84 | { delete static_cast<T *>(d); } |
85 | |
86 | |
87 | // MOC_SKIP_END |
88 | #endif |
89 | |
90 | template <class T> |
91 | class QThreadStorage |
92 | { |
93 | private: |
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 | |
101 | public: |
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 | |
117 | QT_END_NAMESPACE |
118 | |
119 | #else // !QT_CONFIG(thread) |
120 | |
121 | #include <QtCore/qscopedpointer.h> |
122 | |
123 | #include <type_traits> |
124 | |
125 | QT_BEGIN_NAMESPACE |
126 | |
127 | template <typename T, typename U> |
128 | inline bool qThreadStorage_hasLocalData(const QScopedPointer<T, U> &data) |
129 | { |
130 | return !!data; |
131 | } |
132 | |
133 | template <typename T, typename U> |
134 | inline bool qThreadStorage_hasLocalData(const QScopedPointer<T*, U> &data) |
135 | { |
136 | return !!data ? *data != nullptr : false; |
137 | } |
138 | |
139 | template <typename T> |
140 | inline void qThreadStorage_deleteLocalData(T *t) |
141 | { |
142 | delete t; |
143 | } |
144 | |
145 | template <typename T> |
146 | inline void qThreadStorage_deleteLocalData(T **t) |
147 | { |
148 | delete *t; |
149 | delete t; |
150 | } |
151 | |
152 | template <class T> |
153 | class QThreadStorage |
154 | { |
155 | private: |
156 | struct ScopedPointerThreadStorageDeleter |
157 | { |
158 | static inline void cleanup(T *t) |
159 | { |
160 | if (t == nullptr) |
161 | return; |
162 | qThreadStorage_deleteLocalData(t); |
163 | } |
164 | }; |
165 | QScopedPointer<T, ScopedPointerThreadStorageDeleter> data; |
166 | |
167 | public: |
168 | QThreadStorage() = default; |
169 | ~QThreadStorage() = default; |
170 | QThreadStorage(const QThreadStorage &rhs) = delete; |
171 | QThreadStorage &operator=(const QThreadStorage &rhs) = delete; |
172 | |
173 | inline bool hasLocalData() const |
174 | { |
175 | return qThreadStorage_hasLocalData(data); |
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 | |
196 | QT_END_NAMESPACE |
197 | |
198 | #endif // QT_CONFIG(thread) |
199 | |
200 | #endif // QTHREADSTORAGE_H |
201 | |