Skip to content

Instantly share code, notes, and snippets.

@0xa
Created March 16, 2013 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xa/5176306 to your computer and use it in GitHub Desktop.
Save 0xa/5176306 to your computer and use it in GitHub Desktop.
#ifndef COMMON_SINGLETON_H
#define COMMON_SINGLETON_H
#include <cstdlib>
template<typename T>
class Singleton {
public:
Singleton() {
m_Instance = static_cast<T*>(this);
}
static T* GetInstancePtr() {
if (m_Instance == NULL) {
m_Instance = new T();
}
return m_Instance;
}
static T& GetInstance() {
return *GetInstancePtr();
}
static void DeleteInstance() {
delete m_Instance;
}
private:
static T* m_Instance;
Singleton(const Singleton<T> &);
Singleton& operator=(const Singleton<T> &);
};
template<typename T>
T* Singleton<T>::m_Instance = NULL;
#endif // COMMON_SINGLETON_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment