PicoScenes API Docs
 
Loading...
Searching...
No Matches
Singleton.hxx
Go to the documentation of this file.
1//
2// Singleton.hpp
3//
4// Created by JiangZhping on 2017/2/1.
5// Copyright © 2017年 JiangZhping. All rights reserved.
6//
7
8#ifndef Singleton_hpp
9#define Singleton_hpp
10
11#include <cstdio>
12
22template<class T>
23class Singleton {
24public:
35 template<typename... Args>
36 static T &getInstance(Args... args) {
37 if (!m_pInstance) [[unlikely]] {
38 m_pInstance = new T(std::forward<Args>(args)...);
39 }
40
41 return *m_pInstance;
42 }
43
44protected:
49
54
55private:
59 Singleton(Singleton const &);
60
64 Singleton &operator=(Singleton const &);
65
66 static T *m_pInstance;
67};
68
69template<class T> T *Singleton<T>::m_pInstance = nullptr;
70
71#endif /* Singleton_hpp */
A template class for creating singleton instances.
Definition Singleton.hxx:23
static T & getInstance(Args... args)
Gets the singleton instance of the class.
Definition Singleton.hxx:36
Singleton()
Protected constructor to prevent direct instantiation.
~Singleton()
Protected destructor to prevent direct deletion.