summaryrefslogtreecommitdiff
path: root/include/comphelper/flagguard.hxx
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2019-04-26 11:46:51 +0300
committerMike Kaganski <mike.kaganski@collabora.com>2019-04-26 22:47:05 +0200
commitac419786b3244d909901db053841862abf5e7a2f (patch)
tree5dba1043e19cc431fd11c727d167e88a9808287e /include/comphelper/flagguard.hxx
parent8a53befea7cfe75bbd946cef281b922c30c87386 (diff)
Don't use std::function in scope guard for performance reasons
Change-Id: I1d2f0307c0bf9ff5abde74d3326899a1aaa69c40 Reviewed-on: https://gerrit.libreoffice.org/71346 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'include/comphelper/flagguard.hxx')
-rw-r--r--include/comphelper/flagguard.hxx48
1 files changed, 23 insertions, 25 deletions
diff --git a/include/comphelper/flagguard.hxx b/include/comphelper/flagguard.hxx
index 17c78ec5ddc6..44d2f4f73ab4 100644
--- a/include/comphelper/flagguard.hxx
+++ b/include/comphelper/flagguard.hxx
@@ -28,46 +28,44 @@ namespace comphelper
//= FlagRestorationGuard
- class COMPHELPER_DLLPUBLIC FlagRestorationGuard : public ScopeGuard
+ // note: can't store the originalValue in a FlagRestorationGuard member,
+ // because it will be used from base class dtor
+ struct FlagRestorationGuard_Impl
+ {
+ bool & rFlag;
+ bool const originalValue;
+ FlagRestorationGuard_Impl(bool & i_flagRef)
+ : rFlag(i_flagRef), originalValue(i_flagRef) {}
+ void operator()()
+ {
+ rFlag = originalValue;
+ }
+ };
+
+ class FlagRestorationGuard : public ScopeGuard<FlagRestorationGuard_Impl>
{
public:
FlagRestorationGuard( bool& i_flagRef, bool i_temporaryValue )
- : ScopeGuard(RestoreFlag(i_flagRef))
+ : ScopeGuard(FlagRestorationGuard_Impl(i_flagRef))
{
i_flagRef = i_temporaryValue;
}
-
- ~FlagRestorationGuard();
-
- private:
- // note: can't store the originalValue in a FlagRestorationGuard member,
- // because it will be used from base class dtor
- struct RestoreFlag
- {
- bool & rFlag;
- bool const originalValue;
- RestoreFlag(bool & i_flagRef)
- : rFlag(i_flagRef), originalValue(i_flagRef) {}
- void operator()()
- {
- rFlag = originalValue;
- }
- };
};
//= FlagGuard
- class COMPHELPER_DLLPUBLIC FlagGuard : public ScopeGuard
+ // Guarantees that the flag is true within the scope of the guard, and is set to false after
+ // its destruction, regardless of initial flag value
+ class FlagGuard : public FlagRestorationGuard
{
public:
- explicit FlagGuard( bool& i_flagRef )
- : ScopeGuard( [&i_flagRef] () { i_flagRef = false; } )
+ // Set flag to false before passing its reference to base class ctor, so that it would be
+ // reset back to false in base class dtor
+ explicit FlagGuard(bool& i_flagRef)
+ : FlagRestorationGuard((i_flagRef = false), true)
{
- i_flagRef = true;
}
-
- ~FlagGuard();
};