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>
This commit is contained in:
Morten Johan Sørvig 2018-05-31 10:36:11 +02:00 committed by Lorn Potter
parent 07f6eff58d
commit 815153d4a4

View File

@ -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