5#ifndef QTHREADSTORAGE_H
6#define QTHREADSTORAGE_H
8#include <QtCore/qglobal.h>
18template <
bool ShouldWarn>
struct QThreadStorageTraits
20 static constexpr void warnAboutTrivial() {}
22template <>
struct QThreadStorageTraits<
true>
24#ifndef Q_NO_THREAD_STORAGE_TRIVIAL_WARNING
25 Q_DECL_DEPRECATED_X(
"QThreadStorage used with a trivial non-pointer type; consider using thread_local")
27 static constexpr void warnAboutTrivial()
noexcept {}
30class Q_CORE_EXPORT QThreadStorageData
33 using DeleterFn =
void (*)(
void *);
34 explicit QThreadStorageData(DeleterFn func);
35 ~QThreadStorageData();
46T *&qThreadStorage_localData(QThreadStorageData &d, T **)
49 if (!v) v = d.set(
nullptr);
50 return *(
reinterpret_cast<T**>(v));
55T *qThreadStorage_localData_const(
const QThreadStorageData &d, T **)
58 return v ? *(
reinterpret_cast<T**>(v)) : 0;
63void qThreadStorage_setLocalData(QThreadStorageData &d, T **t)
68void qThreadStorage_deleteData(
void *d, T **)
69{
delete static_cast<T *>(d); }
74T &qThreadStorage_localData(QThreadStorageData &d, T *)
77 if (!v) v = d.set(
new T());
78 return *(
reinterpret_cast<T*>(*v));
83T qThreadStorage_localData_const(
const QThreadStorageData &d, T *)
86 return v ? *(
reinterpret_cast<T*>(*v)) : T();
91void qThreadStorage_setLocalData(QThreadStorageData &d, T *t)
92{ (
void) d.set(
new T(*t)); }
96void qThreadStorage_deleteData(
void *d, T *)
97{
delete static_cast<T *>(d); }
103 using Trait = QThreadStorageTraits<std::is_trivially_default_constructible_v<T> &&
104 std::is_trivially_copyable_v<T> && !std::is_pointer_v<T>>;
105 QThreadStorageData d;
107 Q_DISABLE_COPY(QThreadStorage)
109 static inline void deleteData(
void *x)
110 { qThreadStorage_deleteData(x,
reinterpret_cast<T*>(0)); }
112 static inline QThreadStorageData::DeleterFn deleter()
114 if constexpr (std::is_trivially_destructible_v<T> && !std::is_pointer_v<T>)
121 inline QThreadStorage() : d(deleter()) { Trait::warnAboutTrivial(); }
122 inline ~QThreadStorage() { }
124 inline bool hasLocalData()
const
125 {
return d.get() !=
nullptr; }
127 inline T& localData()
128 {
return qThreadStorage_localData(d,
reinterpret_cast<T*>(0)); }
129 inline T localData()
const
130 {
return qThreadStorage_localData_const(d,
reinterpret_cast<T*>(0)); }
132 inline void setLocalData(T t)
133 { qThreadStorage_setLocalData(d, &t); }
138template <
typename T,
typename U>
139inline bool qThreadStorage_hasLocalData(
const std::unique_ptr<T, U> &data)
144template <
typename T,
typename U>
147 return !!data ? *data !=
nullptr :
false;
167 struct ScopedPointerThreadStorageDeleter
169 void operator()(T *t)
const noexcept
173 qThreadStorage_deleteLocalData(t);
176 std::unique_ptr<T, ScopedPointerThreadStorageDeleter> data;
186 return qThreadStorage_hasLocalData(data);
198 return !!data ? *data : T();
203 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)
void QT_MANGLE_NAMESPACE qt_startup_hook()
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