LibreOffice Module comphelper (master) 1
Public Member Functions | Private Member Functions | Static Private Member Functions | Static Private Attributes | List of all members
comphelper::SingletonRef< SingletonClass > Class Template Reference

Template for implementing singleton classes. More...

#include <singletonref.hxx>

Inheritance diagram for comphelper::SingletonRef< SingletonClass >:
[legend]

Public Member Functions

 SingletonRef ()
 standard ctor. More...
 
 ~SingletonRef ()
 standard dtor. More...
 
SingletonRefoperator= (SingletonRef const &)=default
 
SingletonClass * operator-> () const
 Allows rSingle->someBodyOp(). More...
 
SingletonClass & operator* () const
 Allows (*rSingle).someBodyOp(). More...
 

Private Member Functions

 SingletonRef (SingletonRef &)=delete
 

Static Private Member Functions

static std::mutexownStaticLock ()
 

Static Private Attributes

static SingletonClass * m_pInstance = nullptr
 pointer to the internal wrapped singleton. More...
 
static sal_Int32 m_nRef = 0
 ref count, which regulate creation and removing of m_pInstance. More...
 

Detailed Description

template<class SingletonClass>
class comphelper::SingletonRef< SingletonClass >

Template for implementing singleton classes.

This is a replacement for salhelper::SingletonRef, but which uses std::mutex instead of osl::Mutex.

Such classes can be instantiated every time they are needed. But the internal wrapped object will be created one times only. Of course it's used resources are referenced one times only too. This template hold it alive till the last reference is gone. Further all operations on this reference are threadsafe. Only calls directly to the internal object (which modify its state) must be made threadsafe by the object itself or from outside.

Attention: \n To prevent the code against race conditions, it's not
allowed to start operations inside the ctor of the internal wrapped object - especially operations which needs a reference to the same singleton too.

The only chance to suppress such strange constellations is a lazy-init mechanism.

Note further that this singleton pattern can work only, if all user of such singleton are located inside the same library! Because static values can't be exported - e.g. from windows libraries.

Definition at line 61 of file singletonref.hxx.

Constructor & Destructor Documentation

◆ SingletonRef() [1/2]

template<class SingletonClass >
comphelper::SingletonRef< SingletonClass >::SingletonRef ( )
inline

standard ctor.

        The internal wrapped object is created only,
        if its ref count was 0. Otherwise this method
        does nothing ... except increasing of the internal
        ref count!

Definition at line 82 of file singletonref.hxx.

References comphelper::SingletonRef< SingletonClass >::m_nRef, comphelper::SingletonRef< SingletonClass >::m_pInstance, and comphelper::SingletonRef< SingletonClass >::ownStaticLock().

◆ ~SingletonRef()

template<class SingletonClass >
comphelper::SingletonRef< SingletonClass >::~SingletonRef ( )
inline

standard dtor.

        The internal wrapped object is removed only,
        if its ref count will be 0. Otherwise this method
        does nothing ... except decreasing of the internal
        ref count!

Definition at line 105 of file singletonref.hxx.

References comphelper::SingletonRef< SingletonClass >::m_nRef, comphelper::SingletonRef< SingletonClass >::m_pInstance, and comphelper::SingletonRef< SingletonClass >::ownStaticLock().

◆ SingletonRef() [2/2]

template<class SingletonClass >
comphelper::SingletonRef< SingletonClass >::SingletonRef ( SingletonRef< SingletonClass > &  )
privatedelete

Member Function Documentation

◆ operator*()

template<class SingletonClass >
SingletonClass & comphelper::SingletonRef< SingletonClass >::operator* ( ) const
inline

Allows (*rSingle).someBodyOp().

Definition at line 134 of file singletonref.hxx.

References comphelper::SingletonRef< SingletonClass >::m_pInstance.

◆ operator->()

template<class SingletonClass >
SingletonClass * comphelper::SingletonRef< SingletonClass >::operator-> ( ) const
inline

Allows rSingle->someBodyOp().

Definition at line 125 of file singletonref.hxx.

References comphelper::SingletonRef< SingletonClass >::m_pInstance.

◆ operator=()

template<class SingletonClass >
SingletonRef & comphelper::SingletonRef< SingletonClass >::operator= ( SingletonRef< SingletonClass > const &  )
default

◆ ownStaticLock()

template<class SingletonClass >
static std::mutex & comphelper::SingletonRef< SingletonClass >::ownStaticLock ( )
inlinestaticprivate

Member Data Documentation

◆ m_nRef

template<class SingletonClass >
sal_Int32 comphelper::SingletonRef< SingletonClass >::m_nRef = 0
staticprivate

ref count, which regulate creation and removing of m_pInstance.

Definition at line 70 of file singletonref.hxx.

Referenced by comphelper::SingletonRef< SingletonClass >::SingletonRef(), and comphelper::SingletonRef< SingletonClass >::~SingletonRef().

◆ m_pInstance

template<class SingletonClass >
SingletonClass * comphelper::SingletonRef< SingletonClass >::m_pInstance = nullptr
staticprivate

The documentation for this class was generated from the following file: