diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2019-04-26 11:46:51 +0300 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2019-04-26 22:47:05 +0200 |
commit | ac419786b3244d909901db053841862abf5e7a2f (patch) | |
tree | 5dba1043e19cc431fd11c727d167e88a9808287e /include | |
parent | 8a53befea7cfe75bbd946cef281b922c30c87386 (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')
-rw-r--r-- | include/comphelper/flagguard.hxx | 48 | ||||
-rw-r--r-- | include/comphelper/scopeguard.hxx | 32 |
2 files changed, 47 insertions, 33 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(); }; diff --git a/include/comphelper/scopeguard.hxx b/include/comphelper/scopeguard.hxx index cfe012acf340..60836a7661c7 100644 --- a/include/comphelper/scopeguard.hxx +++ b/include/comphelper/scopeguard.hxx @@ -21,34 +21,50 @@ #define INCLUDED_COMPHELPER_SCOPEGUARD_HXX #include <comphelper/comphelperdllapi.h> - -#include <functional> +#include <sal/log.hxx> +#include <com/sun/star/uno/Exception.hpp> namespace comphelper { /** ScopeGuard to ease writing exception-safe code. */ -class COMPHELPER_DLLPUBLIC ScopeGuard +template <class Func> class ScopeGuard { public: /** @param func function object to be executed in dtor */ - template <typename func_type> - explicit ScopeGuard( func_type const & func ) : m_func( func ) {} + explicit ScopeGuard( Func && func ) : m_func( std::move(func) ) {} - ~ScopeGuard(); + ~ScopeGuard() + { + if (m_bDismissed) + return; + try + { + m_func(); + } + catch (css::uno::Exception& exc) + { + SAL_WARN("comphelper", "UNO exception occurred: " << exc); + } + catch (...) + { + SAL_WARN("comphelper", "unknown exception occurred!"); + } + } /** Dismisses the scope guard, i.e. the function won't be executed. */ - void dismiss(); + void dismiss() { m_bDismissed = true; } private: // noncopyable until we have good reasons... ScopeGuard(const ScopeGuard&) = delete; ScopeGuard& operator=(const ScopeGuard&) = delete; - ::std::function<void ()> m_func; + Func m_func; + bool m_bDismissed = false; }; } // namespace comphelper |