📄 singleton.h
字号:
/***************************************************************
*Copyright (c) 金瀚海网络科技有限公司 All rights reserved
*文件名称: define.h
*模 块 名: 定义文件
*功能描述: 全局常量符号定义
*当前版本: 1.0
*作 者: 彭飞云
*完成日期: 2007-08-20
*修改记录:
* 日期 版本 作者 修改描述
****************************************************************
****************************************************************/#ifndef SINGLETON_H#define SINGLETON_H#include <stdexcept>#define DEBUG_SINGLETON#ifdef DEBUG_SINGLETON#define TEST_STRING(sentence) printf(sentence);#else #define TEST_STRING(sentence) #endifusing namespace std;template<class SubClass>class Singleton{private: static SubClass* _instance; static bool _create_by_instance;public: static SubClass * Instance();protected: Singleton() { if(!_create_by_instance) throw std::runtime_error ("can't create object by subclass"); }};template<class SubClass>SubClass * Singleton<SubClass>::Instance (){ if(false == _create_by_instance){ if(_instance == NULL){ _create_by_instance = true; try{ _instance = new SubClass(); } catch(...){ _instance = 0; _create_by_instance = false;// throw; } //_create_by_instance = true; return _instance; }else{ TEST_STRING("Singleton class error!\n") return NULL; } }else{//_create_by_instance == true; if(_instance == NULL) TEST_STRING("Singleton class error!\n") return _instance; }/* if( _instance == 0 ) { _create_by_instance = true; // try { _instance = new SubClass(); } catch(...) { _create_by_instance = false;// throw; } _create_by_instance = false;// }*/// return _instance;}template<class SubClass>SubClass * Singleton<SubClass>::_instance = 0;template<class SubClass>bool Singleton<SubClass>::_create_by_instance = 0;#endif //SINGELTON_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -