2023-09-17 20:36:33 +08:00
|
|
|
#ifndef SINGLETON_H
|
|
|
|
#define SINGLETON_H
|
|
|
|
|
2024-03-06 00:34:43 +08:00
|
|
|
/**
|
|
|
|
* @brief The Singleton class
|
|
|
|
*/
|
2023-09-17 20:36:33 +08:00
|
|
|
template <typename T>
|
|
|
|
class Singleton {
|
|
|
|
public:
|
|
|
|
static T* getInstance();
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T* Singleton<T>::getInstance() {
|
2024-03-06 00:34:43 +08:00
|
|
|
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(); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // SINGLETON_H
|