Giri's C++ Support Library
C++ library providing everything you need to quickly create awesome applications.
giri::Singleton< T > Class Template Reference

Singleton Template Class. More...

#include <Singleton.h>

Inheritance diagram for giri::Singleton< T >:
Collaboration diagram for giri::Singleton< T >:

Static Public Member Functions

template<typename... Args>
static T * getInstance (Args... args)
 Generates a static Variable which can only be instanced once. Parameters depend on the inheriting class. More...
 
static void destroy ()
 Destroys the Object held by the Singleton.
 

Additional Inherited Members

- Public Types inherited from giri::Object< T >
using SPtr = std::shared_ptr< T >
 
using UPtr = std::unique_ptr< T >
 
using WPtr = std::weak_ptr< T >
 

Detailed Description

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

Singleton Template Class.

Example Usage:

#include <Singleton.h>
#include <iostream>
using namespace giri;
// Class Implementation
class MyClass : public Singleton<MyClass>
{
public:
// Class implementation belongs here
void identify(){
std::cout << "Hi, i am " << this << std::endl;
}
using UPtr = std::unique_ptr<MyClass>;
~MyClass(){
std::cout << "Destroyed!" << std::endl;
}
protected:
// protect ctor, only Singleton may call it
MyClass(){
std::cout << "Created!" << std::endl;
}
friend class Singleton<MyClass>;
};
// use Singleton
int main(int argc, char *argv[]){
MyClass::getInstance()->identify(); // Created!
MyClass::getInstance()->identify();
MyClass::destroy(); // Destroyed!
MyClass::getInstance()->identify(); // Created!
return EXIT_SUCCESS;
}
Singleton Pattern implementation.
Namespace for giri's C++ support library.
Definition: Base64.h:47

Member Function Documentation

◆ getInstance()

template<typename T >
template<typename... Args>
static T* giri::Singleton< T >::getInstance ( Args...  args)
inlinestatic

Generates a static Variable which can only be instanced once. Parameters depend on the inheriting class.

Returns
Pointer to the only existing instance.

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