tdf#89329: use shared_ptr for pImpl in addonsoptions

Change-Id: I159ba04fa70f324d2a7f5b3c592337b92c7f097d
Reviewed-on: https://gerrit.libreoffice.org/26560
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Noel Grandin <noelgrandin@gmail.com>
This commit is contained in:
Xisco Fauli
2016-06-22 02:30:43 +02:00
committed by Noel Grandin
parent 08f2993b09
commit 3bdc5063f9
2 changed files with 27 additions and 68 deletions

View File

@@ -203,7 +203,6 @@ class AddonsOptions_Impl : public ConfigItem
const MergeStatusbarInstructionContainer& GetMergeStatusbarInstructions() const { return m_aCachedStatusbarMergingInstructions;} const MergeStatusbarInstructionContainer& GetMergeStatusbarInstructions() const { return m_aCachedStatusbarMergingInstructions;}
void ReadConfigurationData(); void ReadConfigurationData();
private: private:
enum ImageSize enum ImageSize
{ {
@@ -470,7 +469,6 @@ bool AddonsOptions_Impl::GetMergeToolbarInstructions(
return false; return false;
} }
// public method // public method
static Image ScaleImage( const Image &rImage, bool bBig ) static Image ScaleImage( const Image &rImage, bool bBig )
@@ -1471,43 +1469,30 @@ Sequence< OUString > AddonsOptions_Impl::GetPropertyNamesImages( const OUString&
return lResult; return lResult;
} }
// initialize static member namespace{
// DON'T DO IT IN YOUR HEADER! //global
// see definition for further information std::weak_ptr<AddonsOptions_Impl> g_pAddonsOptions;
}
AddonsOptions_Impl* AddonsOptions::m_pDataContainer = nullptr;
sal_Int32 AddonsOptions::m_nRefCount = 0;
// constructor
AddonsOptions::AddonsOptions() AddonsOptions::AddonsOptions()
{ {
// 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_pAddonsOptions.lock();
// ... and initialize our data container only if it not already exist! if( !m_pImpl )
if( m_pDataContainer == nullptr )
{ {
m_pDataContainer = new AddonsOptions_Impl; m_pImpl = std::make_shared<AddonsOptions_Impl>();
g_pAddonsOptions = m_pImpl;
} }
} }
// destructor
AddonsOptions::~AddonsOptions() AddonsOptions::~AddonsOptions()
{ {
// 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
@@ -1515,7 +1500,7 @@ AddonsOptions::~AddonsOptions()
bool AddonsOptions::HasAddonsMenu() const bool AddonsOptions::HasAddonsMenu() const
{ {
MutexGuard aGuard( GetOwnStaticMutex() ); MutexGuard aGuard( GetOwnStaticMutex() );
return m_pDataContainer->HasAddonsMenu(); return m_pImpl->HasAddonsMenu();
} }
// public method // public method
@@ -1523,7 +1508,7 @@ bool AddonsOptions::HasAddonsMenu() const
sal_Int32 AddonsOptions::GetAddonsToolBarCount() const sal_Int32 AddonsOptions::GetAddonsToolBarCount() const
{ {
MutexGuard aGuard( GetOwnStaticMutex() ); MutexGuard aGuard( GetOwnStaticMutex() );
return m_pDataContainer->GetAddonsToolBarCount(); return m_pImpl->GetAddonsToolBarCount();
} }
// public method // public method
@@ -1531,7 +1516,7 @@ sal_Int32 AddonsOptions::GetAddonsToolBarCount() const
const Sequence< Sequence< PropertyValue > >& AddonsOptions::GetAddonsMenu() const const Sequence< Sequence< PropertyValue > >& AddonsOptions::GetAddonsMenu() const
{ {
MutexGuard aGuard( GetOwnStaticMutex() ); MutexGuard aGuard( GetOwnStaticMutex() );
return m_pDataContainer->GetAddonsMenu(); return m_pImpl->GetAddonsMenu();
} }
// public method // public method
@@ -1539,7 +1524,7 @@ const Sequence< Sequence< PropertyValue > >& AddonsOptions::GetAddonsMenu() cons
const Sequence< Sequence< PropertyValue > >& AddonsOptions::GetAddonsMenuBarPart() const const Sequence< Sequence< PropertyValue > >& AddonsOptions::GetAddonsMenuBarPart() const
{ {
MutexGuard aGuard( GetOwnStaticMutex() ); MutexGuard aGuard( GetOwnStaticMutex() );
return m_pDataContainer->GetAddonsMenuBarPart(); return m_pImpl->GetAddonsMenuBarPart();
} }
// public method // public method
@@ -1547,7 +1532,7 @@ const Sequence< Sequence< PropertyValue > >& AddonsOptions::GetAddonsMenuBarPart
const Sequence< Sequence< PropertyValue > >& AddonsOptions::GetAddonsToolBarPart( sal_uInt32 nIndex ) const const Sequence< Sequence< PropertyValue > >& AddonsOptions::GetAddonsToolBarPart( sal_uInt32 nIndex ) const
{ {
MutexGuard aGuard( GetOwnStaticMutex() ); MutexGuard aGuard( GetOwnStaticMutex() );
return m_pDataContainer->GetAddonsToolBarPart( nIndex ); return m_pImpl->GetAddonsToolBarPart( nIndex );
} }
// public method // public method
@@ -1555,7 +1540,7 @@ const Sequence< Sequence< PropertyValue > >& AddonsOptions::GetAddonsToolBarPart
const OUString AddonsOptions::GetAddonsToolbarResourceName( sal_uInt32 nIndex ) const const OUString AddonsOptions::GetAddonsToolbarResourceName( sal_uInt32 nIndex ) const
{ {
MutexGuard aGuard( GetOwnStaticMutex() ); MutexGuard aGuard( GetOwnStaticMutex() );
return m_pDataContainer->GetAddonsToolbarResourceName( nIndex ); return m_pImpl->GetAddonsToolbarResourceName( nIndex );
} }
// public method // public method
@@ -1563,7 +1548,7 @@ const OUString AddonsOptions::GetAddonsToolbarResourceName( sal_uInt32 nIndex )
const Sequence< Sequence< PropertyValue > >& AddonsOptions::GetAddonsHelpMenu() const const Sequence< Sequence< PropertyValue > >& AddonsOptions::GetAddonsHelpMenu() const
{ {
MutexGuard aGuard( GetOwnStaticMutex() ); MutexGuard aGuard( GetOwnStaticMutex() );
return m_pDataContainer->GetAddonsHelpMenu(); return m_pImpl->GetAddonsHelpMenu();
} }
// public method // public method
@@ -1571,7 +1556,7 @@ const Sequence< Sequence< PropertyValue > >& AddonsOptions::GetAddonsHelpMenu()
const MergeMenuInstructionContainer& AddonsOptions::GetMergeMenuInstructions() const const MergeMenuInstructionContainer& AddonsOptions::GetMergeMenuInstructions() const
{ {
MutexGuard aGuard( GetOwnStaticMutex() ); MutexGuard aGuard( GetOwnStaticMutex() );
return m_pDataContainer->GetMergeMenuInstructions(); return m_pImpl->GetMergeMenuInstructions();
} }
// public method // public method
@@ -1581,14 +1566,14 @@ bool AddonsOptions::GetMergeToolbarInstructions(
MergeToolbarInstructionContainer& rToolbarInstructions ) const MergeToolbarInstructionContainer& rToolbarInstructions ) const
{ {
MutexGuard aGuard( GetOwnStaticMutex() ); MutexGuard aGuard( GetOwnStaticMutex() );
return m_pDataContainer->GetMergeToolbarInstructions( return m_pImpl->GetMergeToolbarInstructions(
rToolbarName, rToolbarInstructions ); rToolbarName, rToolbarInstructions );
} }
const MergeStatusbarInstructionContainer& AddonsOptions::GetMergeStatusbarInstructions() const const MergeStatusbarInstructionContainer& AddonsOptions::GetMergeStatusbarInstructions() const
{ {
MutexGuard aGuard( GetOwnStaticMutex() ); MutexGuard aGuard( GetOwnStaticMutex() );
return m_pDataContainer->GetMergeStatusbarInstructions(); return m_pImpl->GetMergeStatusbarInstructions();
} }
// public method // public method
@@ -1596,7 +1581,7 @@ const MergeStatusbarInstructionContainer& AddonsOptions::GetMergeStatusbarInstru
Image AddonsOptions::GetImageFromURL( const OUString& aURL, bool bBig, bool bNoScale ) const Image AddonsOptions::GetImageFromURL( const OUString& aURL, bool bBig, bool bNoScale ) const
{ {
MutexGuard aGuard( GetOwnStaticMutex() ); MutexGuard aGuard( GetOwnStaticMutex() );
return m_pDataContainer->GetImageFromURL( aURL, bBig, bNoScale ); return m_pImpl->GetImageFromURL( aURL, bBig, bNoScale );
} }
// public method // public method
@@ -1628,10 +1613,10 @@ Mutex& AddonsOptions::GetOwnStaticMutex()
return *pMutex; return *pMutex;
} }
IMPL_STATIC_LINK_NOARG_TYPED( AddonsOptions, Notify, void*, void ) IMPL_LINK_NOARG_TYPED( AddonsOptions, Notify, void*, void )
{ {
MutexGuard aGuard( GetOwnStaticMutex() ); MutexGuard aGuard( GetOwnStaticMutex() );
m_pDataContainer->ReadConfigurationData(); m_pImpl->ReadConfigurationData();
} }
} }

View File

@@ -27,6 +27,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 <framework/fwedllapi.h> #include <framework/fwedllapi.h>
#include <memory>
/*-************************************************************************************************************ /*-************************************************************************************************************
@descr The method GetAddonsMenu() returns a list of property values. @descr The method GetAddonsMenu() returns a list of property values.
@@ -97,18 +98,6 @@ class AddonsOptions_Impl;
class FWE_DLLPUBLIC AddonsOptions class FWE_DLLPUBLIC AddonsOptions
{ {
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
*//*-*****************************************************************************************************/
AddonsOptions(); AddonsOptions();
~AddonsOptions(); ~AddonsOptions();
@@ -116,7 +105,6 @@ class FWE_DLLPUBLIC AddonsOptions
@short returns if an addons menu is available @short returns if an addons menu is available
@descr Call to retrieve if a addons menu is available @descr Call to retrieve if a addons menu is available
@return true if there is a menu otherwise false @return true if there is a menu otherwise false
*//*-*****************************************************************************************************/ *//*-*****************************************************************************************************/
@@ -126,7 +114,6 @@ class FWE_DLLPUBLIC AddonsOptions
@short returns number of addons toolbars @short returns number of addons toolbars
@descr Call to retrieve the number of addons toolbars @descr Call to retrieve the number of addons toolbars
@return number of addons toolbars @return number of addons toolbars
*//*-*****************************************************************************************************/ *//*-*****************************************************************************************************/
sal_Int32 GetAddonsToolBarCount() const ; sal_Int32 GetAddonsToolBarCount() const ;
@@ -208,10 +195,8 @@ class FWE_DLLPUBLIC AddonsOptions
Image GetImageFromURL( const OUString& aURL, bool bBig, bool bNoScale ) const; Image GetImageFromURL( const OUString& aURL, bool bBig, bool bNoScale ) const;
Image GetImageFromURL( const OUString& aURL, bool bBig ) const; Image GetImageFromURL( const OUString& aURL, bool bBig ) const;
// private methods // private methods
/*-**************************************************************************************************** /*-****************************************************************************************************
@short return a reference to a static mutex @short return a reference to a static mutex
@descr These class is partially threadsafe (for de-/initialization only). @descr These class is partially threadsafe (for de-/initialization only).
@@ -229,21 +214,10 @@ class FWE_DLLPUBLIC AddonsOptions
We create a static mutex only for one ime and use at different times. We create a static mutex only for one ime and use at different times.
@return A reference to a static mutex member. @return A reference to a static mutex member.
*//*-*****************************************************************************************************/ *//*-*****************************************************************************************************/
DECL_STATIC_LINK_TYPED( AddonsOptions, Notify, void*, void ); DECL_LINK_TYPED( Notify, void*, void );
private: private:
std::shared_ptr<AddonsOptions_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 AddonsOptions_Impl* m_pDataContainer ;
static sal_Int32 m_nRefCount ;
}; };
} }