65,208
社区成员
发帖
与我相关
我的任务
分享
/************************************************************************/
/* 单例模版 */
/************************************************************************/
#ifndef _SINGLETON_H_
#define _SINGLETON_H_
template<class T>
class Singleton
{
private:
static T * iInstance;
public:
static T * getInstance();
static void FreeInstance();
};
template<class T>
T * Singleton<T>::iInstance=NULL;
template<class T>
T * Singleton<T>::getInstance()
{
if(iInstance == 0)
{
iInstance=new T();
}
return iInstance;
}
template<class T>
void Singleton<T>::FreeInstance()
{
if(iInstance)
{
delete iInstance;
iInstance=NULL;
}
}
#endif
template <typename T>
class SingletonBase
{
public:
static T& Instance()
{
if (!is_alive_) {
fprintf(stderr, "Singleton has been destroyed\n");
abort();
}
if (!instance_)
{
ScopedLocker<Mutex> locker(lock_);
if (!instance_)
{
T* p = new T();
MemoryWriteBarrier();
instance_ = p;
atexit(Destroy);
}
}
return *instance_;
}
template <typename A1>
static T& Instance(A1& a1)
{
if (!is_alive_) {
fprintf(stderr, "Singleton has been destroyed\n");
abort();
}
// double check locking optimize
if (!instance_)
{
ScopedLocker<Mutex> locker(lock_);
if (!instance_)
{
T* p = new T(a1);
MemoryWriteBarrier();
instance_ = p;
atexit(Destroy);
}
}
return *instance_;
}
static bool IsAlive()
{
return instance_ != NULL;
}
protected:
SingletonBase() {}
~SingletonBase() {}
private:
static void Destroy()
{
is_alive_ = false;
// need not locking
if (instance_)
{
delete instance_;
instance_ = NULL;
}
}
private:
static Mutex lock_;
static T* volatile instance_;
static bool volatile is_alive_;
DECLARE_UNCOPYABLE(SingletonBase);
};
template <typename T>
Mutex SingletonBase<T>::lock_;
template <typename T>
T* volatile SingletonBase<T>::instance_;
template <typename T>
bool volatile SingletonBase<T>::is_alive_ = true;
template <typename T>
class Singleton
: public SingletonBase<T>{ };
class YouClass
{
friend Singleton<YouClass>;
private:
YouClass(){};
YouClass(const YouClass&);
};
//main.cpp
YouClass* a = Singleton<YouClass>::getInstance();
#ifndef _SINGLETON_H_
#define _SINGLETON_H_
#include "CommonHeader.h"
#include <crtdbg.h>
#include "thread/LocalMutex.h"
#include "thread/AutoLock.h"
//
// Template that realize singleton pattern which does not supports "lazy"
// initialization.
//
// Remark:
// To use this pattern you must create singleton instance by your own hands,
// it means that this realization of singleton does not support "lazy" initialization.
//
template<class T> class Singleton {
public:
Singleton() {
AutoLock l(&m_instanceMutex);
if (s_instance == 0) {
s_instance = (T*)this;
} else {
_ASSERT(FALSE);
}
}
virtual ~Singleton() {
AutoLock l(&m_instanceMutex);
s_instance = 0;
}
static T* getInstance() {
{
AutoLock l(&m_instanceMutex);
if (s_instance == 0) {
_ASSERT(FALSE);
}
}
return (T*)s_instance;
}
private:
static LocalMutex m_instanceMutex;
static void* s_instance;
};
template<class T> LocalMutex Singleton<T>::m_instanceMutex;
template<class T> void *Singleton<T>::s_instance = 0;
#endif