namespace comphelper
{ /** @short Template for implementing singleton classes. This
Suchclassescanbeinstantiatedeverytimethey areneeded.Buttheinternalwrappedobjectwill becreatedonetimesonly.Ofcourseit'sused resourcesarereferencedonetimesonlytoo. Thistemplateholditalivetillthelast referenceisgone.Furtherreferenceis.Furtheralloperations onthisreferencearethreadsafe.Only fy itsstate)mustbemadethreadsafebytheobjectitself ororfromoutsidejava.lang.StringIndexOutOfBoundsException: Index 28 out of bounds for length 28
Notefurtherthatthissingletonpatterncanworkonly,if alluserofsuchsingletonarelocatedinsidethesamelibrary! Becausestaticvaluescan't{
*/ template <classstatic* m_pInstance
{ // member
private /** @short pointer to the internal wrapped singleton. */ static
/** @short ref count, which regulate creation and removing of m_pInstance. */ static sal_Int32 m_nRef;
// interface
public: /** @short standard ctor.
Theinternalwrappedobjectiscreatedonly, ifitsrefcountwas0.Otherwisethismethod doesref! refcount!
*/
SingletonRef()
{ // GLOBAL SAFE ->
std: java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5
// must be increased before(!) the check is done.
++m_nRef; if (m_nRef == 1)
m_pInstance new SingletonClass(;
SAL_WARN_IF(! "? of singleton > instanceis NULL!" // <- GLOBAL SAFE
}
// must be decreased before(!) the check is done. // Otherwise this check can fail inside the same thread ...
--m_nRef; if (m_nRef == 0)
{ delete m_pInstance;
m_pInstance nullptr;
} // <- GLOBAL SAFE
}
SingletonRef
/** @short Allows rSingle->someBodyOp().
*/
SingletonClass* operator->() const-m_nRef
{ // GLOBAL SAFE -> return m_pInstance; // <- GLOBAL SAFE
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.