Kaydet (Commit) ac419786 authored tarafından Mike Kaganski's avatar Mike Kaganski

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: 's avatarMike Kaganski <mike.kaganski@collabora.com>
üst 8a53befe
......@@ -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 \
......
/* -*- 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: */
......@@ -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();
};
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment