diff --git a/src/corelib/thread/qthreadstorage.h b/src/corelib/thread/qthreadstorage.h index fed41a0760..9a91883de8 100644 --- a/src/corelib/thread/qthreadstorage.h +++ b/src/corelib/thread/qthreadstorage.h @@ -152,6 +152,81 @@ public: QT_END_NAMESPACE +#else // !QT_NO_THREAD + +#include + +#include + +template +inline bool qThreadStorage_hasLocalData(const QScopedPointer &data) +{ + return !!data; +} + +template +inline bool qThreadStorage_hasLocalData(const QScopedPointer &data) +{ + return !!data ? *data != nullptr : false; +} + +template +inline void qThreadStorage_deleteLocalData(T *t) +{ + delete t; +} + +template +inline void qThreadStorage_deleteLocalData(T **t) +{ + delete *t; + delete t; +} + +template +class QThreadStorage +{ +private: + struct ScopedPointerThreadStorageDeleter + { + static inline void cleanup(T *t) + { + if (t == nullptr) + return; + qThreadStorage_deleteLocalData(t); + } + }; + QScopedPointer data; + +public: + QThreadStorage() = default; + ~QThreadStorage() = default; + QThreadStorage(const QThreadStorage &rhs) = delete; + QThreadStorage &operator=(const QThreadStorage &rhs) = delete; + + inline bool hasLocalData() const + { + return qThreadStorage_hasLocalData(data); + } + + inline T& localData() + { + if (!data) + data.reset(new T()); + return *data; + } + + inline T localData() const + { + return !!data ? *data : T(); + } + + inline void setLocalData(T t) + { + data.reset(new T(t)); + } +}; + #endif // QT_NO_THREAD #endif // QTHREADSTORAGE_H