| 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 | #include <memory> |
| 11 | #endif |
| 12 | |
| 13 | QT_BEGIN_NAMESPACE |
| 14 | |
| 15 | #if QT_CONFIG(thread) |
| 16 | |
| 17 | class Q_CORE_EXPORT QThreadStorageData |
| 18 | { |
| 19 | public: |
| 20 | explicit QThreadStorageData(void (*func)(void *)); |
| 21 | ~QThreadStorageData(); |
| 22 | |
| 23 | void** get() const; |
| 24 | void** set(void* p); |
| 25 | |
| 26 | static void finish(void**); |
| 27 | int id; |
| 28 | }; |
| 29 | |
| 30 | #if !defined(QT_MOC_CPP) |
| 31 | // MOC_SKIP_BEGIN |
| 32 | |
| 33 | // pointer specialization |
| 34 | template <typename T> |
| 35 | inline |
| 36 | T *&qThreadStorage_localData(QThreadStorageData &d, T **) |
| 37 | { |
| 38 | void **v = d.get(); |
| 39 | if (!v) v = d.set(nullptr); |
| 40 | return *(reinterpret_cast<T**>(v)); |
| 41 | } |
| 42 | |
| 43 | template <typename T> |
| 44 | inline |
| 45 | T *qThreadStorage_localData_const(const QThreadStorageData &d, T **) |
| 46 | { |
| 47 | void **v = d.get(); |
| 48 | return v ? *(reinterpret_cast<T**>(v)) : 0; |
| 49 | } |
| 50 | |
| 51 | template <typename T> |
| 52 | inline |
| 53 | void qThreadStorage_setLocalData(QThreadStorageData &d, T **t) |
| 54 | { (void) d.set(*t); } |
| 55 | |
| 56 | template <typename T> |
| 57 | inline |
| 58 | void qThreadStorage_deleteData(void *d, T **) |
| 59 | { delete static_cast<T *>(d); } |
| 60 | |
| 61 | // value-based specialization |
| 62 | template <typename T> |
| 63 | inline |
| 64 | T &qThreadStorage_localData(QThreadStorageData &d, T *) |
| 65 | { |
| 66 | void **v = d.get(); |
| 67 | if (!v) v = d.set(new T()); |
| 68 | return *(reinterpret_cast<T*>(*v)); |
| 69 | } |
| 70 | |
| 71 | template <typename T> |
| 72 | inline |
| 73 | T qThreadStorage_localData_const(const QThreadStorageData &d, T *) |
| 74 | { |
| 75 | void **v = d.get(); |
| 76 | return v ? *(reinterpret_cast<T*>(*v)) : T(); |
| 77 | } |
| 78 | |
| 79 | template <typename T> |
| 80 | inline |
| 81 | void qThreadStorage_setLocalData(QThreadStorageData &d, T *t) |
| 82 | { (void) d.set(new T(*t)); } |
| 83 | |
| 84 | template <typename T> |
| 85 | inline |
| 86 | void qThreadStorage_deleteData(void *d, T *) |
| 87 | { delete static_cast<T *>(d); } |
| 88 | |
| 89 | |
| 90 | // MOC_SKIP_END |
| 91 | #endif |
| 92 | |
| 93 | template <class T> |
| 94 | class QThreadStorage |
| 95 | { |
| 96 | private: |
| 97 | QThreadStorageData d; |
| 98 | |
| 99 | Q_DISABLE_COPY(QThreadStorage) |
| 100 | |
| 101 | static inline void deleteData(void *x) |
| 102 | { qThreadStorage_deleteData(x, reinterpret_cast<T*>(0)); } |
| 103 | |
| 104 | public: |
| 105 | inline QThreadStorage() : d(deleteData) { } |
| 106 | inline ~QThreadStorage() { } |
| 107 | |
| 108 | inline bool hasLocalData() const |
| 109 | { return d.get() != nullptr; } |
| 110 | |
| 111 | inline T& localData() |
| 112 | { return qThreadStorage_localData(d, reinterpret_cast<T*>(0)); } |
| 113 | inline T localData() const |
| 114 | { return qThreadStorage_localData_const(d, reinterpret_cast<T*>(0)); } |
| 115 | |
| 116 | inline void setLocalData(T t) |
| 117 | { qThreadStorage_setLocalData(d, &t); } |
| 118 | }; |
| 119 | |
| 120 | #else // !QT_CONFIG(thread) |
| 121 | |
| 122 | template <typename T, typename U> |
| 123 | inline bool qThreadStorage_hasLocalData(const std::unique_ptr<T, U> &data) |
| 124 | { |
| 125 | return !!data; |
| 126 | } |
| 127 | |
| 128 | template <typename T, typename U> |
| 129 | inline bool qThreadStorage_hasLocalData(const std::unique_ptr<T*, U> &data) |
| 130 | { |
| 131 | return !!data ? *data != nullptr : false; |
| 132 | } |
| 133 | |
| 134 | template <typename T> |
| 135 | inline void qThreadStorage_deleteLocalData(T *t) |
| 136 | { |
| 137 | delete t; |
| 138 | } |
| 139 | |
| 140 | template <typename T> |
| 141 | inline void qThreadStorage_deleteLocalData(T **t) |
| 142 | { |
| 143 | delete *t; |
| 144 | delete t; |
| 145 | } |
| 146 | |
| 147 | template <class T> |
| 148 | class QThreadStorage |
| 149 | { |
| 150 | private: |
| 151 | struct ScopedPointerThreadStorageDeleter |
| 152 | { |
| 153 | void operator()(T *t) const noexcept |
| 154 | { |
| 155 | if (t == nullptr) |
| 156 | return; |
| 157 | qThreadStorage_deleteLocalData(t); |
| 158 | } |
| 159 | }; |
| 160 | std::unique_ptr<T, ScopedPointerThreadStorageDeleter> data; |
| 161 | |
| 162 | public: |
| 163 | QThreadStorage() = default; |
| 164 | ~QThreadStorage() = default; |
| 165 | QThreadStorage(const QThreadStorage &rhs) = delete; |
| 166 | QThreadStorage &operator=(const QThreadStorage &rhs) = delete; |
| 167 | |
| 168 | inline bool hasLocalData() const |
| 169 | { |
| 170 | return qThreadStorage_hasLocalData(data); |
| 171 | } |
| 172 | |
| 173 | inline T &localData() |
| 174 | { |
| 175 | if (!data) |
| 176 | data.reset(new T()); |
| 177 | return *data; |
| 178 | } |
| 179 | |
| 180 | inline T localData() const |
| 181 | { |
| 182 | return !!data ? *data : T(); |
| 183 | } |
| 184 | |
| 185 | inline void setLocalData(T t) |
| 186 | { |
| 187 | data.reset(new T(t)); |
| 188 | } |
| 189 | }; |
| 190 | |
| 191 | #endif // QT_CONFIG(thread) |
| 192 | |
| 193 | QT_END_NAMESPACE |
| 194 | |
| 195 | #endif // QTHREADSTORAGE_H |
| 196 | |