File indexing completed on 2025-01-18 10:07:35
0001
0002
0003
0004
0005 #ifndef QSCOPEGUARD_H
0006 #define QSCOPEGUARD_H
0007
0008 #include <QtCore/qglobal.h>
0009
0010 #include <type_traits>
0011 #include <utility>
0012
0013 QT_BEGIN_NAMESPACE
0014
0015 template <typename F>
0016 class QScopeGuard
0017 {
0018 public:
0019 Q_NODISCARD_CTOR
0020 explicit QScopeGuard(F &&f) noexcept
0021 : m_func(std::move(f))
0022 {
0023 }
0024
0025 Q_NODISCARD_CTOR
0026 explicit QScopeGuard(const F &f) noexcept
0027 : m_func(f)
0028 {
0029 }
0030
0031 Q_NODISCARD_CTOR
0032 QScopeGuard(QScopeGuard &&other) noexcept
0033 : m_func(std::move(other.m_func))
0034 , m_invoke(std::exchange(other.m_invoke, false))
0035 {
0036 }
0037
0038 ~QScopeGuard() noexcept
0039 {
0040 if (m_invoke)
0041 m_func();
0042 }
0043
0044 void dismiss() noexcept
0045 {
0046 m_invoke = false;
0047 }
0048
0049 private:
0050 Q_DISABLE_COPY(QScopeGuard)
0051
0052 F m_func;
0053 bool m_invoke = true;
0054 };
0055
0056 template <typename F> QScopeGuard(F(&)()) -> QScopeGuard<F(*)()>;
0057
0058
0059 template <typename F>
0060 [[nodiscard]] QScopeGuard<typename std::decay<F>::type> qScopeGuard(F &&f)
0061 {
0062 return QScopeGuard<typename std::decay<F>::type>(std::forward<F>(f));
0063 }
0064
0065 QT_END_NAMESPACE
0066
0067 #endif