2024-04-11 14:51:43 +08:00
|
|
|
#pragma once
|
2023-09-17 20:36:33 +08:00
|
|
|
|
2024-03-06 00:34:43 +08:00
|
|
|
/**
|
|
|
|
* @brief The Singleton class
|
|
|
|
*/
|
2024-04-11 14:51:43 +08:00
|
|
|
template<typename T>
|
2023-09-17 20:36:33 +08:00
|
|
|
class Singleton {
|
|
|
|
public:
|
2024-04-11 14:51:43 +08:00
|
|
|
static T *getInstance();
|
2023-09-17 20:36:33 +08:00
|
|
|
};
|
|
|
|
|
2024-04-11 14:51:43 +08:00
|
|
|
template<typename T>
|
|
|
|
T *Singleton<T>::getInstance() {
|
|
|
|
static T *instance = new T();
|
2023-09-17 20:36:33 +08:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2024-01-04 14:28:51 +08:00
|
|
|
#define SINGLETON(Class) \
|
|
|
|
private: \
|
2023-09-17 20:36:33 +08:00
|
|
|
friend class Singleton<Class>; \
|
2024-01-04 14:28:51 +08:00
|
|
|
public: \
|
|
|
|
static Class* getInstance() { \
|
2023-09-17 20:36:33 +08:00
|
|
|
return Singleton<Class>::getInstance(); \
|
2024-04-11 20:29:59 +08:00
|
|
|
} \
|