Added MIJIN_SCOPE_EXIT and MIJIN_CONCAT macros.
This commit is contained in:
parent
e81fe345c9
commit
c484d0fa9a
10
source/mijin/util/common_macros.hpp
Normal file
10
source/mijin/util/common_macros.hpp
Normal 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)
|
||||
42
source/mijin/util/scope_guard.hpp
Normal file
42
source/mijin/util/scope_guard.hpp
Normal 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)
|
||||
Loading…
x
Reference in New Issue
Block a user