[Libreoffice-commits] core.git: comphelper/Library_comphelper.mk comphelper/source include/comphelper

Mike Kaganski (via logerrit) logerrit at kemper.freedesktop.org
Fri Apr 26 20:48:26 UTC 2019


 comphelper/Library_comphelper.mk      |    1 
 comphelper/source/misc/scopeguard.cxx |   58 ----------------------------------
 include/comphelper/flagguard.hxx      |   48 +++++++++++++---------------
 include/comphelper/scopeguard.hxx     |   32 ++++++++++++++----
 4 files changed, 47 insertions(+), 92 deletions(-)

New commits:
commit ac419786b3244d909901db053841862abf5e7a2f
Author:     Mike Kaganski <mike.kaganski at collabora.com>
AuthorDate: Fri Apr 26 11:46:51 2019 +0300
Commit:     Mike Kaganski <mike.kaganski at collabora.com>
CommitDate: Fri Apr 26 22:47:05 2019 +0200

    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 at collabora.com>

diff --git a/comphelper/Library_comphelper.mk b/comphelper/Library_comphelper.mk
index a5275f971be4..62b59e0f72de 100644
--- a/comphelper/Library_comphelper.mk
+++ b/comphelper/Library_comphelper.mk
@@ -124,7 +124,6 @@ $(eval $(call gb_Library_add_exception_objects,comphelper,\
     comphelper/source/misc/profilezone \
     comphelper/source/misc/proxyaggregation \
     comphelper/source/misc/random \
-    comphelper/source/misc/scopeguard \
     comphelper/source/misc/SelectionMultiplex \
     comphelper/source/misc/sequenceashashmap \
     comphelper/source/misc/sequence \
diff --git a/comphelper/source/misc/scopeguard.cxx b/comphelper/source/misc/scopeguard.cxx
deleted file mode 100644
index 04c697c940be..000000000000
--- a/comphelper/source/misc/scopeguard.cxx
+++ /dev/null
@@ -1,58 +0,0 @@
-/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
-/*
- * This file is part of the LibreOffice project.
- *
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/.
- *
- * This file incorporates work covered by the following license notice:
- *
- *   Licensed to the Apache Software Foundation (ASF) under one or more
- *   contributor license agreements. See the NOTICE file distributed
- *   with this work for additional information regarding copyright
- *   ownership. The ASF licenses this file to you under the Apache
- *   License, Version 2.0 (the "License"); you may not use this file
- *   except in compliance with the License. You may obtain a copy of
- *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
- */
-
-
-#include <comphelper/flagguard.hxx>
-#include <osl/diagnose.h>
-#include <sal/log.hxx>
-#include <com/sun/star/uno/Exception.hpp>
-
-namespace comphelper {
-
-ScopeGuard::~ScopeGuard()
-{
-    if (!m_func)
-        return;
-    try {
-        m_func();
-    }
-    catch (css::uno::Exception & exc) {
-        SAL_WARN( "comphelper", "UNO exception occurred: " << exc );
-    }
-    catch (...) {
-        OSL_FAIL( "unknown exception occurred!" );
-    }
-}
-
-void ScopeGuard::dismiss()
-{
-    m_func = nullptr;
-}
-
-FlagGuard::~FlagGuard()
-{
-}
-
-FlagRestorationGuard::~FlagRestorationGuard()
-{
-}
-
-} // namespace comphelper
-
-/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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


More information about the Libreoffice-commits mailing list