Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Singleton.hxx
Go to the documentation of this file.
1
36#ifndef _UTILS_SINGLETON_HXX_
37#define _UTILS_SINGLETON_HXX_
38
39#include "utils/macros.h"
40
65template<class T> class Singleton {
66public:
67 Singleton() {
68 HASSERT(instance_ == nullptr);
69 instance_ = static_cast<T*>(this);
70 }
71
72 ~Singleton() {
73 instance_ = nullptr;
74 }
75
77 static T* instance() {
78 HASSERT(instance_ != nullptr);
79 return instance_;
80 }
81
83 static bool exists() {
84 return (instance_ != nullptr);
85 }
86
87private:
89 static T* instance_;
90};
91
92template<class T> T* Singleton<T>::instance_ = nullptr;
93
98#define DEFINE_SINGLETON_INSTANCE(T) template<> T* Singleton<T>::instance_ = nullptr
99
100#endif // _UTILS_SINGLETON_HXX_
101
Singleton class.
Definition Singleton.hxx:65
static T * instance()
Definition Singleton.hxx:77
static T * instance_
The singleton instance pointer.
Definition Singleton.hxx:89
static bool exists()
Definition Singleton.hxx:83
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138