summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorMorten Johan Sørvig <morten.sorvig@qt.io>2018-05-31 10:36:11 +0200
committerLorn Potter <lorn.potter@gmail.com>2018-08-17 00:34:33 +0000
commit815153d4a453855bb528f0fa9cb7e5a77d589a11 (patch)
treea6719e8ce61888654250a716d09aaa4fc1215111 /src/corelib
parent07f6eff58d5ffd8d148cd848141be1ba11c6c5da (diff)
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 <lorn.potter@gmail.com>
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/thread/qthreadstorage.h75
1 files changed, 75 insertions, 0 deletions
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 <qscopedpointer.h>
+
+#include <type_traits>
+
+template <typename T, typename U>
+inline bool qThreadStorage_hasLocalData(const QScopedPointer<T, U> &data)
+{
+ return !!data;
+}
+
+template <typename T, typename U>
+inline bool qThreadStorage_hasLocalData(const QScopedPointer<T*, U> &data)
+{
+ return !!data ? *data != nullptr : false;
+}
+
+template <typename T>
+inline void qThreadStorage_deleteLocalData(T *t)
+{
+ delete t;
+}
+
+template <typename T>
+inline void qThreadStorage_deleteLocalData(T **t)
+{
+ delete *t;
+ delete t;
+}
+
+template <class T>
+class QThreadStorage
+{
+private:
+ struct ScopedPointerThreadStorageDeleter
+ {
+ static inline void cleanup(T *t)
+ {
+ if (t == nullptr)
+ return;
+ qThreadStorage_deleteLocalData(t);
+ }
+ };
+ QScopedPointer<T, ScopedPointerThreadStorageDeleter> 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