Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/QtCore/qthreadstorage.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Copyright (C) 2016 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 
0004 #ifndef QTHREADSTORAGE_H
0005 #define QTHREADSTORAGE_H
0006 
0007 #include <QtCore/qglobal.h>
0008 
0009 #if QT_CONFIG(thread)
0010 
0011 QT_BEGIN_NAMESPACE
0012 
0013 
0014 class Q_CORE_EXPORT QThreadStorageData
0015 {
0016 public:
0017     explicit QThreadStorageData(void (*func)(void *));
0018     ~QThreadStorageData();
0019 
0020     void** get() const;
0021     void** set(void* p);
0022 
0023     static void finish(void**);
0024     int id;
0025 };
0026 
0027 #if !defined(QT_MOC_CPP)
0028 // MOC_SKIP_BEGIN
0029 
0030 // pointer specialization
0031 template <typename T>
0032 inline
0033 T *&qThreadStorage_localData(QThreadStorageData &d, T **)
0034 {
0035     void **v = d.get();
0036     if (!v) v = d.set(nullptr);
0037     return *(reinterpret_cast<T**>(v));
0038 }
0039 
0040 template <typename T>
0041 inline
0042 T *qThreadStorage_localData_const(const QThreadStorageData &d, T **)
0043 {
0044     void **v = d.get();
0045     return v ? *(reinterpret_cast<T**>(v)) : 0;
0046 }
0047 
0048 template <typename T>
0049 inline
0050 void qThreadStorage_setLocalData(QThreadStorageData &d, T **t)
0051 { (void) d.set(*t); }
0052 
0053 template <typename T>
0054 inline
0055 void qThreadStorage_deleteData(void *d, T **)
0056 { delete static_cast<T *>(d); }
0057 
0058 // value-based specialization
0059 template <typename T>
0060 inline
0061 T &qThreadStorage_localData(QThreadStorageData &d, T *)
0062 {
0063     void **v = d.get();
0064     if (!v) v = d.set(new T());
0065     return *(reinterpret_cast<T*>(*v));
0066 }
0067 
0068 template <typename T>
0069 inline
0070 T qThreadStorage_localData_const(const QThreadStorageData &d, T *)
0071 {
0072     void **v = d.get();
0073     return v ? *(reinterpret_cast<T*>(*v)) : T();
0074 }
0075 
0076 template <typename T>
0077 inline
0078 void qThreadStorage_setLocalData(QThreadStorageData &d, T *t)
0079 { (void) d.set(new T(*t)); }
0080 
0081 template <typename T>
0082 inline
0083 void qThreadStorage_deleteData(void *d, T *)
0084 { delete static_cast<T *>(d); }
0085 
0086 
0087 // MOC_SKIP_END
0088 #endif
0089 
0090 template <class T>
0091 class QThreadStorage
0092 {
0093 private:
0094     QThreadStorageData d;
0095 
0096     Q_DISABLE_COPY(QThreadStorage)
0097 
0098     static inline void deleteData(void *x)
0099     { qThreadStorage_deleteData(x, reinterpret_cast<T*>(0)); }
0100 
0101 public:
0102     inline QThreadStorage() : d(deleteData) { }
0103     inline ~QThreadStorage() { }
0104 
0105     inline bool hasLocalData() const
0106     { return d.get() != nullptr; }
0107 
0108     inline T& localData()
0109     { return qThreadStorage_localData(d, reinterpret_cast<T*>(0)); }
0110     inline T localData() const
0111     { return qThreadStorage_localData_const(d, reinterpret_cast<T*>(0)); }
0112 
0113     inline void setLocalData(T t)
0114     { qThreadStorage_setLocalData(d, &t); }
0115 };
0116 
0117 QT_END_NAMESPACE
0118 
0119 #else // !QT_CONFIG(thread)
0120 
0121 #include <QtCore/qscopedpointer.h>
0122 
0123 #include <type_traits>
0124 
0125 QT_BEGIN_NAMESPACE
0126 
0127 template <typename T, typename U>
0128 inline bool qThreadStorage_hasLocalData(const QScopedPointer<T, U> &data)
0129 {
0130     return !!data;
0131 }
0132 
0133 template <typename T, typename U>
0134 inline bool qThreadStorage_hasLocalData(const QScopedPointer<T*, U> &data)
0135 {
0136     return !!data ? *data != nullptr : false;
0137 }
0138 
0139 template <typename T>
0140 inline void qThreadStorage_deleteLocalData(T *t)
0141 {
0142     delete t;
0143 }
0144 
0145 template <typename T>
0146 inline void qThreadStorage_deleteLocalData(T **t)
0147 {
0148     delete *t;
0149     delete t;
0150 }
0151 
0152 template <class T>
0153 class QThreadStorage
0154 {
0155 private:
0156     struct ScopedPointerThreadStorageDeleter
0157     {
0158         static inline void cleanup(T *t)
0159         {
0160             if (t == nullptr)
0161                 return;
0162             qThreadStorage_deleteLocalData(t);
0163         }
0164     };
0165     QScopedPointer<T, ScopedPointerThreadStorageDeleter> data;
0166 
0167 public:
0168     QThreadStorage() = default;
0169     ~QThreadStorage() = default;
0170     QThreadStorage(const QThreadStorage &rhs) = delete;
0171     QThreadStorage &operator=(const QThreadStorage &rhs) = delete;
0172 
0173     inline bool hasLocalData() const
0174     {
0175         return qThreadStorage_hasLocalData(data);
0176     }
0177 
0178     inline T &localData()
0179     {
0180         if (!data)
0181             data.reset(new T());
0182         return *data;
0183     }
0184 
0185     inline T localData() const
0186     {
0187         return !!data ? *data : T();
0188     }
0189 
0190     inline void setLocalData(T t)
0191     {
0192         data.reset(new T(t));
0193     }
0194 };
0195 
0196 QT_END_NAMESPACE
0197 
0198 #endif // QT_CONFIG(thread)
0199 
0200 #endif // QTHREADSTORAGE_H