LibreOffice Module comphelper (master) 1
|
Template for implementing singleton classes. More...
#include <singletonref.hxx>
Public Member Functions | |
SingletonRef () | |
standard ctor. More... | |
~SingletonRef () | |
standard dtor. More... | |
SingletonRef & | operator= (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::mutex & | ownStaticLock () |
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... | |
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.
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.
|
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().
|
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().
|
privatedelete |
|
inline |
Allows (*rSingle).someBodyOp().
Definition at line 134 of file singletonref.hxx.
References comphelper::SingletonRef< SingletonClass >::m_pInstance.
|
inline |
Allows rSingle->someBodyOp().
Definition at line 125 of file singletonref.hxx.
References comphelper::SingletonRef< SingletonClass >::m_pInstance.
|
default |
|
inlinestaticprivate |
Definition at line 146 of file singletonref.hxx.
References mutex.
Referenced by comphelper::SingletonRef< SingletonClass >::SingletonRef(), and comphelper::SingletonRef< SingletonClass >::~SingletonRef().
|
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().
|
staticprivate |
pointer to the internal wrapped singleton.
Definition at line 67 of file singletonref.hxx.
Referenced by comphelper::SingletonRef< SingletonClass >::operator*(), comphelper::SingletonRef< SingletonClass >::operator->(), comphelper::SingletonRef< SingletonClass >::SingletonRef(), and comphelper::SingletonRef< SingletonClass >::~SingletonRef().