]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
scope_guard: Support in-place construction and one-shots
authorAdam C. Emerson <aemerson@redhat.com>
Wed, 26 Jul 2017 22:09:27 +0000 (18:09 -0400)
committerAdam C. Emerson <aemerson@redhat.com>
Wed, 9 Aug 2017 02:53:12 +0000 (22:53 -0400)
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 <aemerson@redhat.com>
src/include/scope_guard.h

index ebffc5dfee5fa3d954094df6b49e992ccdf69c87..b60667f792e72d5f3836048796ee092cdafeaebb 100644 (file)
@@ -17,6 +17,8 @@
 
 #include <utility>
 
+#include "common/backport14.h"
+
 template <typename F>
 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<typename... Args>
+  scope_guard(ceph::in_place_t, Args&& ...args) : f(std::forward<Args>(args)...) {}
   ~scope_guard() {
-    f();
+    std::move(f)(); // Support at-most-once functions
   }
 };
 
@@ -36,4 +41,9 @@ scope_guard<F> make_scope_guard(F &&f) {
   return scope_guard<F>(std::forward<F>(f));
 }
 
+template<typename F, typename... Args>
+scope_guard<F> make_scope_guard(ceph::in_place_type_t<F>, Args&& ...args) {
+  return { ceph::in_place, std::forward<Args>(args)... };
+}
+
 #endif