From 815153d4a453855bb528f0fa9cb7e5a77d589a11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Thu, 31 May 2018 10:36:11 +0200 Subject: Add QThreadStorage stub implementation Add implementation for the no-thread configuration: Assume access will only happen from one thread and use a QScopedPointer to hold the data. Unlike the real implementation this version will delete the stored data on destruction, as opposed to on QApplication destruction. Change-Id: I9d8e89e7da18f967f463e2db7b50549c962acc84 Reviewed-by: Lorn Potter --- src/corelib/thread/qthreadstorage.h | 75 +++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) (limited to 'src/corelib/thread') 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 -- cgit v1.2.3