贴一个singleton

luopengxo 2004-11-11 12:28:58
#ifndef _singleton_h
#define _singleton_h

// STL
#include <list>
#include <algorithm>
#include <stdexcept>

#ifndef _PTLIB_H
#include <ptlib.h>
#endif

using std::list;

//
// a list of pointers that would delete all objects
// referred by the pointers in the list on destruction
//
template<class T> class listptr : public list<void *> {
public:
listptr() : clear_list(false) {}
~listptr();
bool clear_list;

private:
static void delete_obj(void *t) { delete static_cast<T *>(t); }
};

template<class T> listptr<T>::~listptr()
{
clear_list=true;
std::for_each(begin(), end(), delete_obj);
}


// Base class for all singletons
class SingletonBase {
public:
SingletonBase();
virtual ~SingletonBase();

private:
// Note the SingletonBase instance is not singleton itself :p
// However, list of singletons *are* singleton
// But we can't put the singleton into the list :(
static listptr<SingletonBase> _instance_list;
};

//
// A singleton class should be derived from this template.
// class Ts : public Singleton<Ts> {
// ...
// };
//
// If the class is instantiated more than once,
// a runtime error would be thrown
//
// I provide two ways to access the singleton:
// (since I'm not sure which is better)
// T::Instance() or InstanceOf<T>
//
template<class T> class Singleton : public SingletonBase {
public:
static T *Instance();
template<class U> static T *Instance(const U &);

protected:
Singleton();
~Singleton();

#ifdef WIN32
public:
#else
private:
friend T *InstanceOf<T>();
#endif
static SingletonBase *m_Instance;
static PMutex m_CreationLock;
};

template<class T> Singleton<T>::Singleton()
{
if (m_Instance != NULL)
throw std::runtime_error("Duplicate instances");
}

template<class T> Singleton<T>::~Singleton()
{
PWaitAndSignal lock(m_CreationLock);
m_Instance = NULL;
}

// Function to access the singleton
template<class T> T *Singleton<T>::Instance()
{
if (m_Instance == NULL) {
PWaitAndSignal lock(m_CreationLock);
// We have to check it again after we got the lock
if (m_Instance == NULL)
m_Instance = new T;
}
return dynamic_cast<T *>(m_Instance);
}

#ifndef WIN32 // VC++ doesn't support nested template?
template<class T> template <class U> T *Singleton<T>::Instance(const U &u)
{
if (m_Instance == NULL) {
PWaitAndSignal lock(m_CreationLock);
// We have to check it again after we got the lock
if (m_Instance == NULL)
m_Instance = new T(u);
}
return dynamic_cast<T *>(m_Instance);
}
#endif

// Function to access the singleton
template<class T> T *InstanceOf()
{
if (Singleton<T>::m_Instance == NULL) {
PWaitAndSignal lock(Singleton<T>::m_CreationLock);
// We have to check it again after we got the lock
if (Singleton<T>::m_Instance == NULL)
Singleton<T>::m_Instance = new T;
}
return dynamic_cast<T *>(Singleton<T>::m_Instance);
}

// static members
template<class T> SingletonBase *Singleton<T>::m_Instance=NULL;
template<class T> PMutex Singleton<T>::m_CreationLock;


#endif
...全文
267 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dot99 2004-12-04
  • 打赏
  • 举报
回复
public class UI
{
private UI() {}
private static UI s_ = new UI();
public UI static instance() {
return s_;
}
}

郁闷的代码......被逼无奈阿。。。。
jingrunx 2004-11-28
  • 打赏
  • 举报
回复
////////////////////////////////////////////////////////////////////////////////
// class template SingletonHolder
// Provides Singleton amenities for a type T
// To protect that type from spurious instantiations, you have to protect it
// yourself.
////////////////////////////////////////////////////////////////////////////////

template
<
typename T,
template <class> class CreationPolicy = CreateUsingNew,
template <class> class LifetimePolicy = DefaultLifetime,
template <class> class ThreadingModel = SingleThreaded
>
class SingletonHolder
{
public:
static T& Instance();

private:
// Helpers
static void MakeInstance();
static void DestroySingleton();

// Protection
SingletonHolder();

// Data
typedef typename ThreadingModel<T*>::VolatileType PtrInstanceType;
static PtrInstanceType pInstance_;
static bool destroyed_;
};
MPU 2004-11-12
  • 打赏
  • 举报
回复
软件设计师考题!!!Singleton Design Pattern

#include <iostream>
#include <cstdlib>
#define for if(0);else for // VC++ 6.0
using std::cout;
using std::cin;
using std::endl;

class Configure
{
protected:
Configure() {} ; ///// 原题掉了 {} .......所以编译通不过!
public:
static Configure* Instance();
public:
int GetConfigureData() { return data;}
int SetConfigureData(int m_data) { data = m_data; return data; }

private:
static Configure* _instance;
int data;
};

Configure* Configure::_instance = NULL;

Configure* Configure::Instance()
{
if ( _instance == NULL)
{
_instance = new Configure();
}
return _instance;
}

int main( )
{

Configure* t = NULL;
t = t->Instance();
int d = t->GetConfigureData();

system("pause");
return 0;
}

luopengxo 2004-11-11
  • 打赏
  • 举报
回复
#include "singleton.h"

#if PTRACING
static int singleton_cnt=0;
#endif

listptr<SingletonBase> SingletonBase::_instance_list;

SingletonBase::SingletonBase()
{
#if PTRACING
++singleton_cnt;
PTRACE(5,"Create instance: "<<singleton_cnt<<endl);
#endif
_instance_list.push_back(this);
}

SingletonBase::~SingletonBase()
{
#if PTRACING
--singleton_cnt;
PTRACE(5,"Delete instance: "<<singleton_cnt<<" objects left"<<endl);
#endif
if (!_instance_list.clear_list)
_instance_list.remove(this);
}

5,530

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 模式及实现
社区管理员
  • 模式及实现社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧