/**************************************************************************** ** ** Copyright (C) 2020 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the tools applications of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3 as published by the Free Software ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT ** included in the packaging of this file. Please review the following ** information to ensure the GNU General Public License requirements will ** be met: https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef QDEFERREDPOINTER_P_H #define QDEFERREDPOINTER_P_H // // W A R N I N G // ------------- // // This file is not part of the Qt API. It exists purely as an // implementation detail. This header file may change from version to // version without notice, or even be removed. // // We mean it. #include #include QT_BEGIN_NAMESPACE template class QDeferredFactory { public: T create() const; bool isValid() const; }; template class QDeferredWeakPointer; template class QDeferredSharedPointer { public: using Factory = QDeferredFactory>; QDeferredSharedPointer() = default; QDeferredSharedPointer(QSharedPointer data) : m_data(data) {} QDeferredSharedPointer(QWeakPointer data) : m_data(data) {} QDeferredSharedPointer(QSharedPointer data, QSharedPointer factory) : m_data(data), m_factory(factory) {} operator QSharedPointer() const { if (m_factory && m_factory->isValid()) { Factory localFactory; std::swap(localFactory, *m_factory); // Swap before executing, to avoid recursion const_cast &>(*m_data) = localFactory.create(); } return m_data; } operator QDeferredSharedPointer() const { return { m_data, m_factory }; } T &operator*() const { return QSharedPointer(*this).operator*(); } T *operator->() const { return QSharedPointer(*this).operator->(); } bool isNull() const { return m_data.isNull(); } explicit operator bool() const noexcept { return !isNull(); } bool operator !() const noexcept { return isNull(); } T *data() const { return QSharedPointer(*this).data(); } friend size_t qHash(const QDeferredSharedPointer &ptr, size_t seed = 0) { return qHashMulti(seed, ptr.m_factory, ptr.m_data); } friend bool operator==(const QDeferredSharedPointer &a, const QDeferredSharedPointer &b) { return a.m_factory == b.m_factory && a.m_data == b.m_data; } friend bool operator!=(const QDeferredSharedPointer &a, const QDeferredSharedPointer &b) { return !(a == b); } private: friend class QDeferredWeakPointer; QSharedPointer m_data; QSharedPointer m_factory; }; template class QDeferredWeakPointer { public: using Factory = QDeferredFactory>; QDeferredWeakPointer() = default; QDeferredWeakPointer(const QDeferredSharedPointer &strong) : m_data(strong.m_data), m_factory(strong.m_factory) { } QDeferredWeakPointer(QWeakPointer data, QWeakPointer factory) : m_data(data), m_factory(factory) {} operator QWeakPointer() 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 &>(*(m_data.toStrongRef())) = localFactory.create(); } } return m_data; } operator QDeferredSharedPointer() const { return QDeferredSharedPointer(m_data.toStrongRef(), m_factory.toStrongRef()); } operator QDeferredWeakPointer() const { return {m_data, m_factory}; } QSharedPointer toStrongRef() const { return QWeakPointer(*this).toStrongRef(); } bool isNull() const { return m_data.isNull(); } explicit operator bool() const noexcept { return !isNull(); } bool operator !() const noexcept { return isNull(); } T *data() const { return QWeakPointer(*this).data(); } friend size_t qHash(const QDeferredWeakPointer &ptr, size_t seed = 0) { return qHashMulti(seed, ptr.m_factory, ptr.m_data); } friend bool operator==(const QDeferredWeakPointer &a, const QDeferredWeakPointer &b) { return a.m_factory == b.m_factory && a.m_data == b.m_data; } friend bool operator!=(const QDeferredWeakPointer &a, const QDeferredWeakPointer &b) { return !(a == b); } private: QWeakPointer m_data; QWeakPointer m_factory; }; QT_END_NAMESPACE #endif // QDEFERREDPOINTER_P_H