From: pius.lee Date: Fri, 16 Jan 2015 05:26:11 +0000 (+0900) Subject: [Common] add scope out macro for easy releasing X-Git-Tag: submit/tizen_tv/20150603.064601~1^2~610^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2d6dbd667ff989478f17458b670ccbec0630bda3;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Common] add scope out macro for easy releasing Change-Id: I5181036cc35c4cd78b9863254905f8ea73abe9ee Signed-off-by: pius.lee --- 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_