aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2021-01-26 10:21:39 +0100
committerUlf Hermann <ulf.hermann@qt.io>2021-01-26 13:19:31 +0100
commit48b6e192bbb023bb19c9de81c1e3d3ac3bca238c (patch)
treeb8866afc35098a3f5d254a42ce1e87f2ab508628
parent74060990bc83b8714d017da6b2238b00e6aba0e4 (diff)
QDeferredSharedPointer: lazy-load to determine isNull()
If the content is not loaded, yet, the data is always null. If it is loaded, the result might be null. So, in order to know, we have to load. The same holds for deferred weak pointers, the hash functions, and the equality operators. Change-Id: I20eec58efe5da604187c34578ee40f769090c910 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-rw-r--r--src/qmlcompiler/qdeferredpointer_p.h58
1 files changed, 42 insertions, 16 deletions
diff --git a/src/qmlcompiler/qdeferredpointer_p.h b/src/qmlcompiler/qdeferredpointer_p.h
index 6125b90afd..56672b2a9f 100644
--- a/src/qmlcompiler/qdeferredpointer_p.h
+++ b/src/qmlcompiler/qdeferredpointer_p.h
@@ -77,11 +77,7 @@ public:
operator QSharedPointer<T>() const
{
- if (m_factory && m_factory->isValid()) {
- Factory localFactory;
- std::swap(localFactory, *m_factory); // Swap before executing, to avoid recursion
- const_cast<std::remove_const_t<T> &>(*m_data) = localFactory.create();
- }
+ lazyLoad();
return m_data;
}
@@ -90,7 +86,12 @@ public:
T &operator*() const { return QSharedPointer<T>(*this).operator*(); }
T *operator->() const { return QSharedPointer<T>(*this).operator->(); }
- bool isNull() const { return m_data.isNull(); }
+ bool isNull() const
+ {
+ lazyLoad();
+ return m_data.isNull();
+ }
+
explicit operator bool() const noexcept { return !isNull(); }
bool operator !() const noexcept { return isNull(); }
@@ -98,11 +99,14 @@ public:
friend size_t qHash(const QDeferredSharedPointer &ptr, size_t seed = 0)
{
+ ptr.lazyLoad();
return qHashMulti(seed, ptr.m_factory, ptr.m_data);
}
friend bool operator==(const QDeferredSharedPointer &a, const QDeferredSharedPointer &b)
{
+ a.lazyLoad();
+ b.lazyLoad();
return a.m_factory == b.m_factory && a.m_data == b.m_data;
}
@@ -114,6 +118,15 @@ public:
private:
friend class QDeferredWeakPointer<T>;
+ void lazyLoad() const
+ {
+ if (m_factory && m_factory->isValid()) {
+ Factory localFactory;
+ std::swap(localFactory, *m_factory); // Swap before executing, to avoid recursion
+ const_cast<std::remove_const_t<T> &>(*m_data) = localFactory.create();
+ }
+ }
+
QSharedPointer<T> m_data;
QSharedPointer<Factory> m_factory;
};
@@ -137,15 +150,7 @@ public:
operator QWeakPointer<T>() const
{
- if (m_factory) {
- auto factory = m_factory.toStrongRef();
- if (factory->isValid()) {
- Factory localFactory;
- std::swap(localFactory, *factory); // Swap before executing, to avoid recursion
- const_cast<std::remove_const_t<T> &>(*(m_data.toStrongRef()))
- = localFactory.create();
- }
- }
+ lazyLoad();
return m_data;
}
@@ -161,7 +166,12 @@ public:
return QWeakPointer<T>(*this).toStrongRef();
}
- bool isNull() const { return m_data.isNull(); }
+ bool isNull() const
+ {
+ lazyLoad();
+ return m_data.isNull();
+ }
+
explicit operator bool() const noexcept { return !isNull(); }
bool operator !() const noexcept { return isNull(); }
@@ -169,11 +179,14 @@ public:
friend size_t qHash(const QDeferredWeakPointer &ptr, size_t seed = 0)
{
+ ptr.lazyLoad();
return qHashMulti(seed, ptr.m_factory, ptr.m_data);
}
friend bool operator==(const QDeferredWeakPointer &a, const QDeferredWeakPointer &b)
{
+ a.lazyLoad();
+ b.lazyLoad();
return a.m_factory == b.m_factory && a.m_data == b.m_data;
}
@@ -183,6 +196,19 @@ public:
}
private:
+ void lazyLoad() const
+ {
+ if (m_factory) {
+ auto factory = m_factory.toStrongRef();
+ if (factory->isValid()) {
+ Factory localFactory;
+ std::swap(localFactory, *factory); // Swap before executing, to avoid recursion
+ const_cast<std::remove_const_t<T> &>(*(m_data.toStrongRef()))
+ = localFactory.create();
+ }
+ }
+ }
+
QWeakPointer<T> m_data;
QWeakPointer<Factory> m_factory;
};