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

template for implementing singleton classes. More...

#include <singletonref.hxx>

Classes

struct  SingletonLockInit
 creates an own mutex for guarding static contents. More...
 

Public Member Functions

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

Private Member Functions

 SingletonRef (SingletonRef &) SAL_DELETED_FUNCTION
 
::osl::Mutex & ownStaticLock () const
 

Static Private Attributes

static SingletonClass * m_pInstance = NULL
 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 salhelper::SingletonRef< SingletonClass >

template for implementing singleton classes.

    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 73 of file singletonref.hxx.

Constructor & Destructor Documentation

◆ SingletonRef() [1/2]

template<class SingletonClass >
salhelper::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 99 of file singletonref.hxx.

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

◆ ~SingletonRef()

template<class SingletonClass >
salhelper::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 122 of file singletonref.hxx.

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

◆ SingletonRef() [2/2]

template<class SingletonClass >
salhelper::SingletonRef< SingletonClass >::SingletonRef ( SingletonRef< SingletonClass > &  )
private

Member Function Documentation

◆ operator*()

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

◆ operator->()

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

◆ ownStaticLock()

template<class SingletonClass >
::osl::Mutex & salhelper::SingletonRef< SingletonClass >::ownStaticLock ( ) const
inlineprivate

Member Data Documentation

◆ m_nRef

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

ref count, which regulate creation and removing of m_pInstance.

Definition at line 84 of file singletonref.hxx.

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

◆ m_pInstance

template<class SingletonClass >
SingletonClass * salhelper::SingletonRef< SingletonClass >::m_pInstance = NULL
staticprivate

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