NcEngine
ScopeExit.h
Go to the documentation of this file.
1
5#pragma once
6
8namespace nc::detail
9{
10template <class F>
11class ScopeExit
12{
13public:
14 explicit ScopeExit(const F& onExit) noexcept
15 : m_func{onExit}
16 {
17 }
18
19 explicit ScopeExit(F&& onExit) noexcept
20 : m_func{std::move(onExit)}
21 {
22 }
23
24 ~ScopeExit() noexcept
25 {
26 m_func();
27 }
28
29 ScopeExit(ScopeExit&&) = delete;
30 ScopeExit(const ScopeExit&) = delete;
31 void operator=(const ScopeExit&) = delete;
32 void operator=(ScopeExit&&) = delete;
33
34private:
35 F m_func;
36};
37} // namespace nc::detail
38
39#define UNIQUE_NAME_HELPER(name, lineNumber) name ## lineNumber
40#define UNIQUE_NAME(name, lineNumber) UNIQUE_NAME_HELPER(name, lineNumber)
44#define SCOPE_EXIT(body) auto UNIQUE_NAME(localScopeExitFunc, __LINE__) = nc::detail::ScopeExit([&](){body;});