Files
o3de/Code/Framework/AzQtComponents/AzQtComponents/Utilities/ScopedCleanup.h
T
Steve Pham 38261d0800 Shorten copyright headers by splitting into 2 lines (#2213)
* Updated all copyright headers to split the longer original copyright line into 2 shorter lines

Signed-off-by: Steve Pham <spham@amazon.com>
2021-07-16 15:25:48 -07:00

72 lines
1.4 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#ifndef SCOPEDCLEANUP_H
#define SCOPEDCLEANUP_H
#include <QtCore/qglobal.h>
/**
* RAII-style class that executes a lambda when it goes out of scope.
* Ideal for cleanup in functions that have multiple return points.
*
* Usage:
* #include <AzQtComponents/Utilities/ScopedCleanup.h>
* (...)
* auto cleanup = scopedCleanup([]{ <my cleanup that should be executed at end of scope>});
*/
template <typename F> class ScopedCleanup;
template <typename F> ScopedCleanup<F> scopedCleanup(F f);
template <typename F>
class ScopedCleanup
{
public:
ScopedCleanup(ScopedCleanup&& other) Q_DECL_NOEXCEPT
: m_func(std::move(other.m_func))
, m_invoke(other.m_invoke)
{
other.dismiss();
}
~ScopedCleanup()
{
if (m_invoke)
{
m_func();
}
}
void dismiss() Q_DECL_NOEXCEPT
{
m_invoke = false;
}
private:
explicit ScopedCleanup(F f) Q_DECL_NOEXCEPT
: m_func(std::move(f))
{
}
Q_DISABLE_COPY(ScopedCleanup)
F m_func;
bool m_invoke = true;
friend ScopedCleanup scopedCleanup<F>(F);
};
template <typename F>
ScopedCleanup<F> scopedCleanup(F f)
{
return ScopedCleanup<F>(std::move(f));
}
#endif