Created
May 8, 2014 13:33
-
-
Save oteguro/c8ec15b4806a5c75eae7 to your computer and use it in GitHub Desktop.
Tiny "ScopeExit" macro on C++11.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ---------------------------------------------------------------------------- | |
| // scope_exit.h | |
| // ---------------------------------------------------------------------------- | |
| // Description : Tiny "ScopeExit" on C++11. | |
| #pragma once | |
| template <typename T> class ScopeExit | |
| { | |
| public: | |
| T m_func; | |
| ScopeExit(T func) | |
| : m_func(func) | |
| { | |
| } | |
| ~ScopeExit() | |
| { | |
| m_func(); | |
| } | |
| }; // class ScopeExit | |
| template <typename T> ScopeExit<T> MakeScopeExit(T func) | |
| { | |
| return ScopeExit<T>(func); | |
| } | |
| #define SCOPE_EXIT_CONCAT_IMPL(x,y) x ## y | |
| #define SCOPE_EXIT_CONCAT(x,y) SCOPE_EXIT_CONCAT_IMPL(x,y) | |
| #define SCOPE_EXIT(code) auto SCOPE_EXIT_CONCAT(scopeExit, __LINE__) = MakeScopeExit([&](){code;}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment