From ac419786b3244d909901db053841862abf5e7a2f Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Fri, 26 Apr 2019 11:46:51 +0300 Subject: 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 --- include/comphelper/flagguard.hxx | 48 +++++++++++++++++++-------------------- include/comphelper/scopeguard.hxx | 32 +++++++++++++++++++------- 2 files changed, 47 insertions(+), 33 deletions(-) (limited to 'include') 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 { 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 - -#include +#include +#include namespace comphelper { /** ScopeGuard to ease writing exception-safe code. */ -class COMPHELPER_DLLPUBLIC ScopeGuard +template class ScopeGuard { public: /** @param func function object to be executed in dtor */ - template - 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 m_func; + Func m_func; + bool m_bDismissed = false; }; } // namespace comphelper -- cgit