Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-03-29 08:28:49

0001 // Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Sérgio Martins <sergio.martins@kdab.com>
0002 // Copyright (C) 2019 The Qt Company Ltd.
0003 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0004 
0005 #ifndef QSCOPEGUARD_H
0006 #define QSCOPEGUARD_H
0007 
0008 #include <QtCore/qtclasshelpermacros.h>
0009 #include <QtCore/qcompilerdetection.h>
0010 #include <QtCore/qtconfigmacros.h>
0011 
0012 #include <type_traits>
0013 #include <utility>
0014 
0015 QT_BEGIN_NAMESPACE
0016 
0017 template <typename F>
0018 class QScopeGuard
0019 {
0020 public:
0021     Q_NODISCARD_CTOR
0022     explicit QScopeGuard(F &&f) noexcept
0023         : m_func(std::move(f))
0024     {
0025     }
0026 
0027     Q_NODISCARD_CTOR
0028     explicit QScopeGuard(const F &f) noexcept
0029         : m_func(f)
0030     {
0031     }
0032 
0033     Q_NODISCARD_CTOR
0034     QScopeGuard(QScopeGuard &&other) noexcept
0035         : m_func(std::move(other.m_func))
0036         , m_invoke(std::exchange(other.m_invoke, false))
0037     {
0038     }
0039 
0040     ~QScopeGuard() noexcept
0041     {
0042         if (m_invoke)
0043             m_func();
0044     }
0045 
0046     void dismiss() noexcept
0047     {
0048         m_invoke = false;
0049     }
0050 
0051 private:
0052     Q_DISABLE_COPY(QScopeGuard)
0053 
0054     F m_func;
0055     bool m_invoke = true;
0056 };
0057 
0058 template <typename F> QScopeGuard(F(&)()) -> QScopeGuard<F(*)()>;
0059 
0060 //! [qScopeGuard]
0061 template <typename F>
0062 [[nodiscard]] QScopeGuard<typename std::decay<F>::type> qScopeGuard(F &&f)
0063 {
0064     return QScopeGuard<typename std::decay<F>::type>(std::forward<F>(f));
0065 }
0066 
0067 QT_END_NAMESPACE
0068 
0069 #endif // QSCOPEGUARD_H