Singleton template
2013-07-05 11:49
295 查看
共享一个Singleton template的实现:
Singleton.hpp如下
Resguard.hpp
Resguard_linux.hpp
Singleton.hpp如下
/************************************************************************ * Template of Singleton, threads safe. * by xiuleili 2013-7-5 ************************************************************************/ #pragma once #include <memory> #ifdef WIN32 #include "ResGuard.hpp" #else #include "ResGuard_linux.hpp" #endif template <class T> class Singleton { public: // return instance of the singleton static inline T* instance(){ if( 0 == _instance.get() ) { ResGuard::Lock gd(_rs); if( 0== _instance.get()) { _instance.reset ( new T); } } return _instance.get(); } private: Singleton(void){} ~Singleton(void){} Singleton(const Singleton&){} Singleton & operator= (const Singleton &){} static std::auto_ptr<T> _instance; // auto_ptr used for release the resource. static ResGuard _rs; // }; template <class T> std::auto_ptr<T> Singleton<T>::_instance; template <class T> ResGuard Singleton<T>::_rs; // use this macro to declare a singleton #define DECLARE_SINGLETON_CLASS( type ) \ friend class std::auto_ptr< type >;\ friend class Singleton< type >; // How to use: // class A{ // private: // A(){} // A(const A &){} // ~A(){} // A & operator=(const A &){} // DECLARE_SINGLETON_CLASS(A); // }; // // declare the singleton: // typedef Singleton<A> theA; // // call it somewhere like this: // theA::instance()->foo();
Resguard.hpp
/* * Implementation of ResGuard on Windows. * see <<windows kernal var cpp>> * by xiuleili 2013-7-5 */ #pragma once #ifndef _UNIX #include <windows.h> /////////////////////////////////////////////////////////////////////////////// // Instances of this class will be accessed by multiple threads. So, // all members of this class (except the constructor and destructor) // must be thread-safe. class ResGuard { public: ResGuard() { m_guardcount = 0; InitializeCriticalSection(&m_cs); } ~ResGuard() { DeleteCriticalSection(&m_cs); } // IsGuarded is used for debugging bool isgurded() const { return(m_guardcount > 0); } public: class Lock { public: Lock(ResGuard& rg) : m_rg(rg) { m_rg.Guard(); }; ~Lock() { m_rg.Unguard(); } private: ResGuard& m_rg; }; private: void Guard() { EnterCriticalSection(&m_cs); m_guardcount++; } void Unguard() { m_guardcount--; LeaveCriticalSection(&m_cs); } // Guard/Unguard can only be accessed by the nested CGuard class. friend class ResGuard::Lock; private: CRITICAL_SECTION m_cs; long m_guardcount; // # of EnterCriticalSection calls }; #endif
Resguard_linux.hpp
/* * implementation of ResGuard. * See <POSIX Muti-threads programming> * by xiuleili 2013-7-5 */ #pragma once #include <pthread.h> // Instances of this class will be accessed by multiple threads. So, // all members of this class (except the constructor and destructor) // must be thread-safe. class ResGuard { public: ResGuard() { m_guardcount = 0; pthread_mutex_init(&m_cs, NULL); } ~ResGuard() { pthread_mutex_destroy(&m_cs); } // IsGuarded is used for debugging bool isgurded() const { return(m_guardcount > 0); } public: class Lock { public: Lock(ResGuard& rg) : m_rg(rg) { m_rg.Guard(); }; ~Lock() { m_rg.Unguard(); } private: ResGuard& m_rg; }; private: void Guard() { pthread_mutex_lock(&m_cs); m_guardcount++; } void Unguard() { m_guardcount--; pthread_mutex_unlock(&m_cs); } // Guard/Unguard can only be accessed by the nested Guard class. friend class ResGuard::Lock; private: pthread_mutex_t m_cs; long m_guardcount; // # of EnterCriticalSection calls };
相关文章推荐
- Objective C ARC下的单例模版宏 ARC Singleton template
- 模板强化Template Method & Singleton 模式
- 关于Singleton Template与于static member initialization问题
- A singleton template-based approach
- Template + Strategy + SingleTon + Factory 处理排序问题
- Singleton Pattern combined with Template
- Singleton -- template
- java单例模式(Singleton)
- C++ 11 Template ... 与Decltype 测试
- PEAR里HTML_Template_IT摸板类的用法
- 在 C# 中实现 Singleton (来源MSDN技术文章)
- [问题]django_openstack.templatetags
- 实施和使用 Singleton 设计模式的有效方式
- 使用Antlr+Stringtemplate生成method chaining,一个不太简单的案例(5)
- TableView性能优化——动态cell适应工具(UITableView+FDTemplateLayoutCell)
- getSession - getHibernateTemplate - getSessionFactory
- template<class T>(转)
- 单例模式(singleton)解析例子
- WPF 获取 ListView DataTemplate 中控件值
- Spring中常用的hql查询方法 (getHibernateTemplate())