Language

The Free and Open Productivity Suite
Released: Apache OpenOffice 4.1.15

API

SDK

Tips ‘n’ Tricks

Miscellaneous


:: salhelper ::

template< typename SingletonClass >

class SingletonRef


Base Classes
None.
Known Derived Classes
None.

virtual abstract interface template
NO NO NO YES
Summary
template for implementing singleton classes.
Description
Such classes can be instanciated everytimes they are needed. But the internal wrapped object will be created one times only. Of course its 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!
To prevent the code against race conditions, its not allowed to start operations inside the ctor of the internal wrapped object - especialy operations which needs a reference to the same singleton too. The only chance to supress such strange constellations is a lazy-init mechanism.
  • a) The singleton class can provide a special init() method, which must be called as first after creation.
  • b) The singleton class can call a special impl_init() method implicit for every called interface method.
Note further that this singleton pattern can work only, if all user of such singleton are located inside the same library! Because static values cant be exported - e.g. from windows libraries.
File
singletonref.hxx

Public Members

Methods


SingletonRef( );
standard ctor.

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

Private Members

Structs

SingletonLockInit creates an own mutex for guarding static contents.

Methods

::osl::Mutex &
ownStaticLock( ) const;

Static Data

SingletonClass * m_pInstance; pointer to the internal wrapped singleton.
sal_Int32 m_nRef; ref count, which regulate creation and removing of m_pInstance.

Top of Page

Apache Software Foundation

Copyright & License | Privacy | Contact Us | Donate | Thanks

Apache, OpenOffice, OpenOffice.org and the seagull logo are registered trademarks of The Apache Software Foundation. The Apache feather logo is a trademark of The Apache Software Foundation. Other names appearing on the site may be trademarks of their respective owners.