Devel Lib
Devel::CSingleton< T > Class Template Reference

A class template for creating singleton classes. More...

#include <Singleton.h>

Static Public Member Functions

static T * instance ()
 Get the instance of the Singleton class. More...
 

Protected Member Functions

 CSingleton ()=default
 Constructor. More...
 
 CSingleton (const CSingleton &)=delete
 Copy constructor. More...
 
CSingletonoperator= (const CSingleton &)=delete
 Copy assignment operator. More...
 
virtual ~CSingleton ()=default
 Destructor. More...
 

Detailed Description

template<typename T>
class Devel::CSingleton< T >

A class template for creating singleton classes.

A singleton is a design pattern that restricts the instantiation of a class to a single instance. This is useful when exactly one object is needed to coordinate actions across the system.

Template Parameters
TThe type that will be a singleton.

Example

A class must derive from this class in order to become a singleton.

class CSingletonClass : public Devel::CSingleton<CSingletonClass> // Derive the Singleton class
{
friend class CSingleton<Devel::CSingletonClass>; // For access to constructor
private:
CSingletonClass() {} // Private constructor!
virtual ~CSingletonClass() {}
public:
bool initialize(); // Example function!
};
// Using the singleton class:
CSingletonClass::instance()->initialize(); // Function initialize just an example!
A class template for creating singleton classes.
Definition: Singleton.h:31
CSingleton()=default
Constructor.
A namespace for development related classes and functions.

Constructor & Destructor Documentation

◆ CSingleton() [1/2]

template<typename T >
Devel::CSingleton< T >::CSingleton ( )
protecteddefault

Constructor.

This constructor is protected to prevent direct instantiation of the class.

◆ CSingleton() [2/2]

template<typename T >
Devel::CSingleton< T >::CSingleton ( const CSingleton< T > &  )
protecteddelete

Copy constructor.

This constructor is deleted to prevent copying of the instance.

◆ ~CSingleton()

template<typename T >
virtual Devel::CSingleton< T >::~CSingleton ( )
protectedvirtualdefault

Destructor.

This destructor is virtual and does nothing, allowing derived classes to clean up if needed.

Member Function Documentation

◆ instance()

template<typename T >
static T* Devel::CSingleton< T >::instance ( )
inlinestatic

Get the instance of the Singleton class.

Returns
Instance of the Singleton class

This function provides access to the single instance of the class. If the instance does not exist, it is created.

◆ operator=()

template<typename T >
CSingleton& Devel::CSingleton< T >::operator= ( const CSingleton< T > &  )
protecteddelete

Copy assignment operator.

This operator is deleted to prevent assignment of the instance.


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