CuteHMI - CuteHMI (CuteHMI.2)
cutehmi::Singleton< C > Class Template Reference

Singleton template. More...

#include <cutehmi/Singleton.hpp>

Inheritance diagram for cutehmi::Singleton< C >:
cutehmi::NonCopyable cutehmi::NonMovable

Static Public Member Functions

static C & Instance ()
 Get instance. More...
 
static void Destroy ()
 Destroy instance. More...
 

Protected Member Functions

 Singleton ()
 Default constructor. More...
 
virtual ~Singleton ()
 Destructor. More...
 
- Protected Member Functions inherited from cutehmi::NonCopyable
 NonCopyable ()=default
 
 NonCopyable (NonCopyable &&other)=default
 
 ~NonCopyable ()=default
 
NonCopyableoperator= (NonCopyable &&other)=default
 
- Protected Member Functions inherited from cutehmi::NonMovable
 NonMovable ()=default
 

Static Protected Member Functions

static std::unique_ptr< C > & InstancePtr ()
 Get instance pointer. More...
 

Detailed Description

template<class C>
class cutehmi::Singleton< C >

Singleton template.

It may be necessary to make derived class a friend of inheriting class to grant Singleton<C> access to its constructor.

Snippet below shows sample class, which uses Singleton template.

class MySingleton:
public cutehmi::Singleton<MySingleton>
{
friend class cutehmi::Singleton<MySingleton>;
public:
void saySomething()
{
qInfo() << "I am not married!";
}
protected:
MySingleton() = default;
};

Singleton instance can be obtained through Instance() function.

MySingleton::Instance().saySomething();

Singleton can be a subclass of QObject, providing that QObject is its first base class, as mentioned in Qt docs.

class QObjectSingleton:
public QObject,
public cutehmi::Singleton<QObjectSingleton>
{
Q_OBJECT
friend class cutehmi::Singleton<QObjectSingleton>;
public:
void sayQObject()
{
qInfo() << "QObject!";
emit qobjectSaid();
}
signals:
void qobjectSaid();
protected:
QObjectSingleton() = default;
};

Constructor & Destructor Documentation

◆ Singleton()

template<class C >
cutehmi::Singleton< C >::Singleton
protected

Default constructor.

◆ ~Singleton()

template<class C >
cutehmi::Singleton< C >::~Singleton
protectedvirtual

Destructor.

Member Function Documentation

◆ Destroy()

template<class C >
void cutehmi::Singleton< C >::Destroy
static

Destroy instance.

This function is provided to satisfy the requirement that QApplication has to be first created and last destroyed QObject. Once this function is called singleton becomes unusable.

Note
Only frontend applications, which instantiate QApplication should take care about this function.
Typically it is better to use destroySingletonInstances() function, which destroys all singletons, that inherit after this class, at once.
Assumption:
cutehmi::Singleton-singleton_class_will_not_call_Destroy_from_destructor
Class that inherits after Singleton will not call Destroy() functions from its destructor (neither its own nor of another class).
See also
destroySingletonInstances().

◆ Instance()

template<class C >
C & cutehmi::Singleton< C >::Instance
static

Get instance.

Returns a reference to the instance of the singleton class. Object is instantiated upon first call of this function.

Returns
a reference to the instance of the singleton class.
Warning
because this class can be inherited by QObject, destroySingletonInstances() function should be used by front-end application to satisfy the requirement that QApplication has to be first created and last destroyed QObject.
if neither Destroy() function nor destroySingletonInstances() has been used, then destructor will be called on static de-initialization. Beware of using singleton instance in destructor of some other static object. If that's unavoidable, assure that singletons or any static objects are constructed in the reverse order they are going to be destructed.

◆ InstancePtr()

template<class C >
std::unique_ptr< C > & cutehmi::Singleton< C >::InstancePtr
staticprotected

Get instance pointer.

Returns
instance pointer.
Note
This is provided as a workaround (CuteHMI-2.workaround).
QObject
cutehmi::Singleton
Singleton template.
Definition: Singleton.hpp:29