eis/inc/glob/SingletonTemplate.h

73 lines
1.1 KiB
C++

#ifndef H_SINGLETON_H
#define H_SINGLETON_H
#include <iostream>
using namespace std;
template <class T>
class SingletonTemplate
{
protected:
SingletonTemplate();
public:
virtual ~SingletonTemplate(void);
static T* GetInstancePtr();
static T& GetInstance();
static bool release(void);
private:
static T* mp_singleton;
};
template<class T>
T* SingletonTemplate<T>::mp_singleton = NULL;
template<class T>
SingletonTemplate<T>::SingletonTemplate(void)
{
//std::cout<<"run Singleton::Singleton(void)"<<std::endl;
}
template<class T>
SingletonTemplate<T>::~SingletonTemplate(void)
{
//std::cout<<"run Singleton::~Singleton(void)"<<std::endl;
}
template<class T>
T* SingletonTemplate<T>::GetInstancePtr()
{
if( mp_singleton == NULL ){
mp_singleton = new T();
}
return mp_singleton;
}
template<class T>
T& SingletonTemplate<T>::GetInstance()
{
if( mp_singleton == NULL ){
mp_singleton = new T();
}
return *mp_singleton;
}
template<class T>
bool SingletonTemplate<T>::release(void)
{
bool rtc = true;
if( mp_singleton != NULL ){
delete mp_singleton;
mp_singleton = NULL;
}
return rtc;
}
#endif