From: Adam C. Emerson Date: Wed, 26 Jul 2017 22:09:27 +0000 (-0400) Subject: scope_guard: Support in-place construction and one-shots X-Git-Tag: v13.0.0~153^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=33a1da6eac1dbc338997c4950747cf5d0883820a;p=ceph.git scope_guard: Support in-place construction and one-shots Kefu Chai just pointed out this exists so I just rolled the in-place variants of the 'ward' function I'd written into it. Signed-off-by: Adam C. Emerson --- diff --git a/src/include/scope_guard.h b/src/include/scope_guard.h index ebffc5dfee5f..b60667f792e7 100644 --- a/src/include/scope_guard.h +++ b/src/include/scope_guard.h @@ -17,6 +17,8 @@ #include +#include "common/backport14.h" + template struct scope_guard { F f; @@ -25,9 +27,12 @@ struct scope_guard { scope_guard(scope_guard &&) = default; scope_guard & operator=(const scope_guard &) = delete; scope_guard & operator=(scope_guard &&) = default; + scope_guard(const F& f) : f(f) {} scope_guard(F &&f) : f(std::move(f)) {} + template + scope_guard(ceph::in_place_t, Args&& ...args) : f(std::forward(args)...) {} ~scope_guard() { - f(); + std::move(f)(); // Support at-most-once functions } }; @@ -36,4 +41,9 @@ scope_guard make_scope_guard(F &&f) { return scope_guard(std::forward(f)); } +template +scope_guard make_scope_guard(ceph::in_place_type_t, Args&& ...args) { + return { ceph::in_place, std::forward(args)... }; +} + #endif