Kaydet (Commit) a1f836e4 authored tarafından Xisco Fauli's avatar Xisco Fauli Kaydeden (comit) Noel Grandin

tdf#89329: use shared_ptr for pImpl in dynamicmenuoptions

Change-Id: I66bdeeee7f70e6ca16a39e8804aaf8a5f0d08205
Reviewed-on: https://gerrit.libreoffice.org/26327Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarNoel Grandin <noelgrandin@gmail.com>
üst f35b1397
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include <com/sun/star/uno/Sequence.h> #include <com/sun/star/uno/Sequence.h>
#include <com/sun/star/beans/PropertyValue.hpp> #include <com/sun/star/beans/PropertyValue.hpp>
#include <unotools/options.hxx> #include <unotools/options.hxx>
#include <memory>
/*-************************************************************************************************************ /*-************************************************************************************************************
@descr The method GetList() returns a list of property values. @descr The method GetList() returns a list of property values.
...@@ -63,17 +64,6 @@ class SvtDynamicMenuOptions_Impl; ...@@ -63,17 +64,6 @@ class SvtDynamicMenuOptions_Impl;
class SAL_WARN_UNUSED UNOTOOLS_DLLPUBLIC SvtDynamicMenuOptions : public utl::detail::Options class SAL_WARN_UNUSED UNOTOOLS_DLLPUBLIC SvtDynamicMenuOptions : public utl::detail::Options
{ {
public: public:
/*-****************************************************************************************************
@short standard constructor and destructor
@descr This will initialize an instance with default values.
We implement these class with a refcount mechanism! Every instance of this class increase it
at create and decrease it at delete time - but all instances use the same data container!
He is implemented as a static member ...
@seealso member m_nRefCount
@seealso member m_pDataContainer
*//*-*****************************************************************************************************/
SvtDynamicMenuOptions(); SvtDynamicMenuOptions();
virtual ~SvtDynamicMenuOptions(); virtual ~SvtDynamicMenuOptions();
...@@ -101,17 +91,7 @@ class SAL_WARN_UNUSED UNOTOOLS_DLLPUBLIC SvtDynamicMenuOptions : public utl::det ...@@ -101,17 +91,7 @@ class SAL_WARN_UNUSED UNOTOOLS_DLLPUBLIC SvtDynamicMenuOptions : public utl::det
UNOTOOLS_DLLPRIVATE static ::osl::Mutex& GetOwnStaticMutex(); UNOTOOLS_DLLPRIVATE static ::osl::Mutex& GetOwnStaticMutex();
private: private:
std::shared_ptr<SvtDynamicMenuOptions_Impl> m_pImpl;
/*Attention
Don't initialize these static members in these headers!
a) Double defined symbols will be detected ...
b) and unresolved externals exist at linking time.
Do it in your source only.
*/
static SvtDynamicMenuOptions_Impl* m_pDataContainer;
static sal_Int32 m_nRefCount;
}; // class SvtDynamicMenuOptions }; // class SvtDynamicMenuOptions
......
...@@ -566,44 +566,31 @@ void SvtDynamicMenuOptions_Impl::impl_SortAndExpandPropertyNames( const Sequence ...@@ -566,44 +566,31 @@ void SvtDynamicMenuOptions_Impl::impl_SortAndExpandPropertyNames( const Sequence
} }
} }
// initialize static member namespace {
// DON'T DO IT IN YOUR HEADER! // global
// see definition for further information std::weak_ptr<SvtDynamicMenuOptions_Impl> g_pDynamicMenuOptions;
}
SvtDynamicMenuOptions_Impl* SvtDynamicMenuOptions::m_pDataContainer = nullptr;
sal_Int32 SvtDynamicMenuOptions::m_nRefCount = 0;
// constructor
SvtDynamicMenuOptions::SvtDynamicMenuOptions() SvtDynamicMenuOptions::SvtDynamicMenuOptions()
{ {
// Global access, must be guarded (multithreading!). // Global access, must be guarded (multithreading!).
MutexGuard aGuard( GetOwnStaticMutex() ); MutexGuard aGuard( GetOwnStaticMutex() );
// Increase our refcount ...
++m_nRefCount; m_pImpl = g_pDynamicMenuOptions.lock();
// ... and initialize our data container only if it not already exist! if( !m_pImpl )
if( m_pDataContainer == nullptr )
{ {
m_pDataContainer = new SvtDynamicMenuOptions_Impl; m_pImpl = std::make_shared<SvtDynamicMenuOptions_Impl>();
g_pDynamicMenuOptions = m_pImpl;
ItemHolder1::holdConfigItem(E_DYNAMICMENUOPTIONS); ItemHolder1::holdConfigItem(E_DYNAMICMENUOPTIONS);
} }
} }
// destructor
SvtDynamicMenuOptions::~SvtDynamicMenuOptions() SvtDynamicMenuOptions::~SvtDynamicMenuOptions()
{ {
// Global access, must be guarded (multithreading!) // Global access, must be guarded (multithreading!)
MutexGuard aGuard( GetOwnStaticMutex() ); MutexGuard aGuard( GetOwnStaticMutex() );
// Decrease our refcount.
--m_nRefCount; m_pImpl.reset();
// If last instance was deleted ...
// we must destroy our static data container!
if( m_nRefCount <= 0 )
{
delete m_pDataContainer;
m_pDataContainer = nullptr;
}
} }
// public method // public method
...@@ -611,7 +598,7 @@ SvtDynamicMenuOptions::~SvtDynamicMenuOptions() ...@@ -611,7 +598,7 @@ SvtDynamicMenuOptions::~SvtDynamicMenuOptions()
Sequence< Sequence< PropertyValue > > SvtDynamicMenuOptions::GetMenu( EDynamicMenuType eMenu ) const Sequence< Sequence< PropertyValue > > SvtDynamicMenuOptions::GetMenu( EDynamicMenuType eMenu ) const
{ {
MutexGuard aGuard( GetOwnStaticMutex() ); MutexGuard aGuard( GetOwnStaticMutex() );
return m_pDataContainer->GetMenu( eMenu ); return m_pImpl->GetMenu( eMenu );
} }
namespace namespace
......
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