std_pattern.h

来自「单件模块」· C头文件 代码 · 共 45 行

H
45
字号
#ifndef _GP_STDPATTERN_H
#define _GP_STDPATTERN_H

namespace GP
{
	//----------------------------------------------------------------------------------------------
	//Singleton<DataType>                          单件类,只会产生一个对象
	//SINGLETON(newClassName, orgClassName)        单件宏,从一个类(orgClassName)派生出一个单件类(newClassName),保持基类的功能
	//----------------------------------------------------------------------------------------------
	template <typename DataType>
	class Singleton
	{
		static Singleton<DataType> s;
		DataType * data;
		Singleton(DataType * x) : data(x){}
		Singleton(const Singleton<DataType> &);
		Singleton<DataType> & operator = (Singleton<DataType> &);
	public:
		static Singleton<DataType> & instance(){return s}	
		DataType getData(){return * data}
		void setData(DataType & theData){*data = theData}
	};


#define SINGLETON(newClassName, orgClassName)                  \
	class newClassName : public orgClassName                   \
	{                                                          \
		newClassName(const newClassName &);                    \
		newClassName & operator = (const newClassName &);      \
	protected:                                                 \
		newClassName(){};                                      \
	public:                                                    \
		static newClassName & instance()                       \
		{                                                      \
			static newClassName theInstance;                   \
			return theInstance;                                \
		}                                                      \
	};                                                        \
}
template <typename DataType>
GP::Singleton<DataType> GP::Singleton<DataType>::s(NULL);



#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?