summaryrefslogtreecommitdiff
path: root/zen/scope_guard.h
diff options
context:
space:
mode:
authorDaniel Wilhelm <daniel@wili.li>2014-04-18 17:22:36 +0200
committerDaniel Wilhelm <daniel@wili.li>2014-04-18 17:22:36 +0200
commitecb1524f8da7901338b263384fed3c612f117b4c (patch)
treee7e06423fe27ea5ab45f27fc4b39ae597ba72490 /zen/scope_guard.h
parent5.10 (diff)
downloadFreeFileSync-ecb1524f8da7901338b263384fed3c612f117b4c.tar.gz
FreeFileSync-ecb1524f8da7901338b263384fed3c612f117b4c.tar.bz2
FreeFileSync-ecb1524f8da7901338b263384fed3c612f117b4c.zip
5.11
Diffstat (limited to 'zen/scope_guard.h')
-rw-r--r--zen/scope_guard.h15
1 files changed, 9 insertions, 6 deletions
diff --git a/zen/scope_guard.h b/zen/scope_guard.h
index 7d79e115..81f47f87 100644
--- a/zen/scope_guard.h
+++ b/zen/scope_guard.h
@@ -28,19 +28,20 @@ namespace zen
class ScopeGuardBase
{
public:
- void dismiss() const { dismissed_ = true; }
+ void dismiss() { dismissed_ = true; }
protected:
ScopeGuardBase() : dismissed_(false) {}
- ScopeGuardBase(const ScopeGuardBase& other) : dismissed_(other.dismissed_) { other.dismissed_ = true; } //take over responsibility
+ ScopeGuardBase(ScopeGuardBase&& other) : dismissed_(other.dismissed_) { other.dismiss(); } //take over responsibility
~ScopeGuardBase() {}
bool isDismissed() const { return dismissed_; }
private:
+ ScopeGuardBase(const ScopeGuardBase&); //delete
ScopeGuardBase& operator=(const ScopeGuardBase&); // = delete;
- mutable bool dismissed_;
+ bool dismissed_;
};
@@ -48,7 +49,9 @@ template <typename F>
class ScopeGuardImpl : public ScopeGuardBase
{
public:
- ScopeGuardImpl(F fun) : fun_(fun) {}
+ explicit ScopeGuardImpl(const F& fun) : fun_(fun) {}
+ explicit ScopeGuardImpl(F&& fun) : fun_(std::move(fun)) {}
+ ScopeGuardImpl(ScopeGuardImpl&& other) : ScopeGuardBase(std::move(other)), fun_(std::move(other.fun_)) {}
~ScopeGuardImpl()
{
@@ -64,10 +67,10 @@ private:
F fun_;
};
-typedef const ScopeGuardBase& ScopeGuard;
+typedef ScopeGuardBase&& ScopeGuard;
template <class F> inline
-ScopeGuardImpl<F> makeGuard(F fun) { return ScopeGuardImpl<F>(fun); }
+ScopeGuardImpl<typename std::decay<F>::type> makeGuard(F&& fun) { return ScopeGuardImpl<typename std::decay<F>::type>(std::forward<F>(fun)); }
}
#define ZEN_CONCAT_SUB(X, Y) X ## Y
bgstack15