4#ifndef QTHREADSTORAGE_H
5#define QTHREADSTORAGE_H
7#include <QtCore/qglobal.h>
17template <
bool ShouldWarn>
struct QThreadStorageTraits
19 static constexpr void warnAboutTrivial() {}
21template <>
struct QThreadStorageTraits<
true>
23#ifndef Q_NO_THREAD_STORAGE_TRIVIAL_WARNING
24 Q_DECL_DEPRECATED_X(
"QThreadStorage used with a trivial non-pointer type; consider using thread_local")
26 static constexpr void warnAboutTrivial()
noexcept {}
29class Q_CORE_EXPORT QThreadStorageData
32 explicit QThreadStorageData(
void (*func)(
void *));
33 ~QThreadStorageData();
38 static void finish(
void**);
45T *&qThreadStorage_localData(QThreadStorageData &d, T **)
48 if (!v) v = d.set(
nullptr);
49 return *(
reinterpret_cast<T**>(v));
54T *qThreadStorage_localData_const(
const QThreadStorageData &d, T **)
57 return v ? *(
reinterpret_cast<T**>(v)) : 0;
62void qThreadStorage_setLocalData(QThreadStorageData &d, T **t)
67void qThreadStorage_deleteData(
void *d, T **)
68{
delete static_cast<T *>(d); }
73T &qThreadStorage_localData(QThreadStorageData &d, T *)
76 if (!v) v = d.set(
new T());
77 return *(
reinterpret_cast<T*>(*v));
82T qThreadStorage_localData_const(
const QThreadStorageData &d, T *)
85 return v ? *(
reinterpret_cast<T*>(*v)) : T();
90void qThreadStorage_setLocalData(QThreadStorageData &d, T *t)
91{ (
void) d.set(
new T(*t)); }
95void qThreadStorage_deleteData(
void *d, T *)
96{
delete static_cast<T *>(d); }
102 using Trait = QThreadStorageTraits<std::is_trivial_v<T> && !std::is_pointer_v<T>>;
103 QThreadStorageData d;
105 Q_DISABLE_COPY(QThreadStorage)
107 static inline void deleteData(
void *x)
108 { qThreadStorage_deleteData(x,
reinterpret_cast<T*>(0)); }
111 inline QThreadStorage() : d(deleteData) { Trait::warnAboutTrivial(); }
112 inline ~QThreadStorage() { }
114 inline bool hasLocalData()
const
115 {
return d.get() !=
nullptr; }
117 inline T& localData()
118 {
return qThreadStorage_localData(d,
reinterpret_cast<T*>(0)); }
119 inline T localData()
const
120 {
return qThreadStorage_localData_const(d,
reinterpret_cast<T*>(0)); }
122 inline void setLocalData(T t)
123 { qThreadStorage_setLocalData(d, &t); }
128template <
typename T,
typename U>
129inline bool qThreadStorage_hasLocalData(
const std::unique_ptr<T, U> &data)
134template <
typename T,
typename U>
137 return !!data ? *data !=
nullptr :
false;
157 struct ScopedPointerThreadStorageDeleter
159 void operator()(T *t)
const noexcept
163 qThreadStorage_deleteLocalData(t);
166 std::unique_ptr<T, ScopedPointerThreadStorageDeleter> data;
176 return qThreadStorage_hasLocalData(data);
188 return !!data ? *data : T();
193 data.reset(
new T(t));
QDeferredDeleteEvent(int loopLevel, int scopeLevel)
Constructs a deferred delete event with the given loop and scope level.
Q_CORE_EXPORT ~QEventLoopLocker()
Destroys this event loop locker object.
void setLocalData(T t)
Sets the local data for the calling thread to data.
~QThreadStorage()=default
Destroys the per-thread data storage object.
QThreadStorage()=default
Constructs a new per-thread data storage object.
T localData() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
T & localData()
Returns a reference to the data that was set by the calling thread.
QThreadStorage(const QThreadStorage &rhs)=delete
bool hasLocalData() const
If T is a pointer type, returns true if the calling thread has non-zero data available.
QThreadStorage & operator=(const QThreadStorage &rhs)=delete
Combined button and popup list for selecting options.
Q_TRACE_POINT(qtcore, QCoreApplication_notify_exit, bool consumed, bool filtered)
static QString qAppFileName()
QList< QtStartUpFunction > QStartUpFuncList
Q_TRACE_METADATA(qtcore, "ENUM { AUTO, RANGE User ... MaxUser } QEvent::Type;")
QString qtTrId(const char *id, int n)
Q_TRACE_POINT(qtcore, QCoreApplication_postEvent_exit)
static void qt_call_pre_routines()
qsizetype qGlobalPostedEventsCount()
static Q_CONSTINIT bool preRoutinesCalled
QList< QtCleanUpFunction > QVFuncList
static void replacePercentN(QString *result, int n)
Q_TRACE_POINT(qtcore, QCoreApplication_postEvent_event_compressed, QObject *receiver, QEvent *event)
Q_TRACE_POINT(qtcore, QCoreApplication_postEvent_entry, QObject *receiver, QEvent *event, QEvent::Type type)
Q_TRACE_PREFIX(qtcore, "#include <qcoreevent.h>")
Q_CORE_EXPORT void qAddPostRoutine(QtCleanUpFunction)
void(* QtCleanUpFunction)()
void(* QtStartUpFunction)()
Q_CORE_EXPORT void qAddPreRoutine(QtStartUpFunction)
Q_CORE_EXPORT void qRemovePostRoutine(QtCleanUpFunction)
#define Q_EVENT_DISABLE_COPY(Class)
#define Q_DECL_EVENT_COMMON(Class)
Q_GLOBAL_STATIC(QReadWriteLock, g_updateMutex)
void qThreadStorage_deleteLocalData(T *t)
bool qThreadStorage_hasLocalData(const std::unique_ptr< T *, U > &data)
void qThreadStorage_deleteLocalData(T **t)
QString applicationVersion
QCoreApplicationData() noexcept
bool applicationVersionSet