Added MIJIN_SCOPE_EXIT and MIJIN_CONCAT macros.

This commit is contained in:
Patrick 2023-06-29 23:07:42 +02:00
parent e81fe345c9
commit c484d0fa9a
2 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,10 @@
#pragma once
#if !defined(MIJIN_UTIL_COMMON_MACROS_HPP_INCLUDED)
#define MIJIN_UTIL_COMMON_MACROS_HPP_INCLUDED 1
#define MIJIN_CONCAT2(a, b) a ## b
#define MIJIN_CONCAT(a, b) MIJIN_CONCAT2(a, b)
#endif // defined(MIJIN_UTIL_COMMON_MACROS_HPP_INCLUDED)

View File

@ -0,0 +1,42 @@
#pragma once
#if !defined(MIJIN_UTIL_SCOPE_GUARD_HPP_INCLUDED)
#define MIJIN_UTIL_SCOPE_GUARD_HPP_INCLUDED 1
#include <functional>
#include "./common_macros.hpp"
#define MIJIN_SCOPE_EXIT_NAMED(name) \
mijin::ScopeExitGuard name = [&]()
#define MIJIN_SCOPE_EXIT MIJIN_SCOPE_EXIT_NAMED(MIJIN_CONCAT(MIJIN_CONCAT(scope_guard_, __LINE__), __))
namespace mijin
{
class ScopeExitGuard
{
private:
std::function<void(void)> func;
public:
template<typename TFunc>
inline ScopeExitGuard(TFunc&& func_) noexcept : func(std::forward<TFunc>(func_)) {}
ScopeExitGuard(const ScopeExitGuard&) noexcept = delete;
ScopeExitGuard(ScopeExitGuard&&) noexcept = delete;
inline ~ScopeExitGuard() noexcept
{
if (func) {
func();
}
}
ScopeExitGuard& operator=(const ScopeExitGuard&) noexcept = delete;
ScopeExitGuard& operator=(ScopeExitGuard&&) noexcept = delete;
inline void reset() noexcept
{
func = {};
}
};
}
#endif // defined(MIJIN_UTIL_SCOPE_GUARD_HPP_INCLUDED)