2013-04-02 20:06:43 +03:00
|
|
|
/*
|
|
|
|
* ScopeGuard.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2013-04-02 20:06:43 +03:00
|
|
|
|
|
|
|
namespace vstd
|
|
|
|
{
|
|
|
|
template<typename Func>
|
|
|
|
class ScopeGuard
|
|
|
|
{
|
|
|
|
bool fire;
|
|
|
|
Func f;
|
|
|
|
|
|
|
|
explicit ScopeGuard(ScopeGuard&);
|
|
|
|
ScopeGuard& operator=(ScopeGuard&);
|
|
|
|
public:
|
|
|
|
ScopeGuard(ScopeGuard &&other):
|
|
|
|
fire(false),
|
|
|
|
f(other.f)
|
|
|
|
{
|
|
|
|
std::swap(fire, other.fire);
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit ScopeGuard(Func && f):
|
|
|
|
fire(true),
|
2024-02-12 12:33:09 +02:00
|
|
|
f(std::move(f))
|
2013-04-02 20:06:43 +03:00
|
|
|
{}
|
|
|
|
~ScopeGuard()
|
|
|
|
{
|
2023-04-02 13:46:56 +02:00
|
|
|
if(fire)
|
|
|
|
f();
|
2013-04-02 20:06:43 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Func>
|
|
|
|
ScopeGuard<Func> makeScopeGuard(Func&& exitScope)
|
|
|
|
{
|
|
|
|
return ScopeGuard<Func>(std::forward<Func>(exitScope));
|
|
|
|
}
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|