From 2d6dbd667ff989478f17458b670ccbec0630bda3 Mon Sep 17 00:00:00 2001 From: "pius.lee" Date: Fri, 16 Jan 2015 14:26:11 +0900 Subject: [PATCH] [Common] add scope out macro for easy releasing Change-Id: I5181036cc35c4cd78b9863254905f8ea73abe9ee Signed-off-by: pius.lee --- src/common/scope_exit.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/common/scope_exit.h b/src/common/scope_exit.h index e2ad9736..9de21928 100644 --- a/src/common/scope_exit.h +++ b/src/common/scope_exit.h @@ -72,6 +72,40 @@ ScopeExit MakeScopeExit(F f) { return ScopeExit(f); } +// Internal use for macro SCOPE_EXIT +template +ScopeExit::type> +operator+(int, F&& f) +{ + return ScopeExit::type> + {std::forward(f)}; +} + +/* + * This macro is for simple way to using ScopeExit + * + * Example: + * + * struct unit; + * struct unit* unit_new(); + * void unit_free(unit*); + * + * void ExampleFunctionScope() { + * struct unit* u = unit_new(); + * + * // automatic cleanup for all cases + * SCOPE_EXIT { + * unit_free(u); + * }; + * + * // do 1st operation with unit and possibly quit function + * // do 2nd operation with unit and possibly quit function + * // do 3rd operation with unit and possibly quit function + * } + */ +#define SCOPE_EXIT \ + auto SCOPE_EXIT_##__LINE__ = 0 + [&]() noexcept + } // namespace common #endif // COMMON_SCOPE_EXIT_H_ -- 2.34.1