Ocean
Ocean::Singleton< T > Class Template Reference

This template class is the base class for all singleton objects. More...

Inheritance diagram for Ocean::Singleton< T >:

Static Public Member Functions

static T & get ()
 Returns a reference to the unique object. More...
 

Protected Member Functions

 Singleton ()=default
 Default constructor. More...
 

Private Member Functions

 Singleton (const Singleton< T > &singleton)=delete
 Disabled copy constructor. More...
 
Singletonoperator= (const Singleton< T > &singleton)=delete
 Disabled assign operator. More...
 

Static Private Member Functions

static T * internalGet ()
 Returns a pointer to the unique object. More...
 
static void releaseSingleton ()
 Releases the singleton object. More...
 

Detailed Description

template<typename T>
class Ocean::Singleton< T >

This template class is the base class for all singleton objects.

Each object derived from this class can only have at most one instance at the same time within the same process unless standalone dynamic libraries attached to the process.
By default, in the case a standalone dynamic library is attached, this library has it's own global variables and thus also provides own singleton objects.
If only one unique process-wide singleton is desired for applications with standalone dynamic libraries, the OceanManager object needs to be initialized accordingly directly at process beginning and directly during initialization of the dynamic library.

Beware: The derived class must not be accomplished entirely with inline functions.
Further, the derived class has to defined the Singleton base class as friend class.

Template Parameters
TClass type that will be extended as singleton
See also
OceanManager.

See this tutorial:

// Any class using the thread class as base class.
class DerivedClass : public Singleton<DerivedClass>
{
friend class Singleton<DerivedClass>;
public:
// Any function.
void anyFunction()
{
// do something here
}
private:
// Private constructor ensuring that this object can only be created by the singleton mechanism.
DerivedClass()
{
// initialize something here
}
// Private destructor ensuring that this object can only be disposed by the singleton mechanism.
~DerivedClass()
{
// release something here
}
};
void anywhereInYourCode()
{
// accessing the singleton object anywhere in your code
DerivedClass::get().anyFunction();
}
Singleton()=default
Default constructor.

Constructor & Destructor Documentation

◆ Singleton() [1/2]

template<typename T >
Ocean::Singleton< T >::Singleton ( )
protecteddefault

Default constructor.

◆ Singleton() [2/2]

template<typename T >
Ocean::Singleton< T >::Singleton ( const Singleton< T > &  singleton)
privatedelete

Disabled copy constructor.

Parameters
singletonThe singleton object that would be copied

Member Function Documentation

◆ get()

template<typename T >
T & Ocean::Singleton< T >::get
static

Returns a reference to the unique object.

Returns
Reference to the object

◆ internalGet()

template<typename T >
T * Ocean::Singleton< T >::internalGet
staticprivate

Returns a pointer to the unique object.

Returns
Pointer to the object

◆ operator=()

template<typename T >
Singleton& Ocean::Singleton< T >::operator= ( const Singleton< T > &  singleton)
privatedelete

Disabled assign operator.

Parameters
singletonThe singleton object that would be assigned

◆ releaseSingleton()

template<typename T >
void Ocean::Singleton< T >::releaseSingleton
staticprivate

Releases the singleton object.

Beware: This function should be call by the OceanManager only.


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