www.pudn.com > insoructions-nitm.zip > SrvAppFactory.h, change:2004-04-14,size:2592b


// SrvAppFactory.h: interface for the CSrvAppFactory class. 
// 
/******************************************************************** 
	created:	2004/12/03 
	author:		Robin Liu 
	email:		robinliu@tsinghua.org.cn 
	purpose:	Create a NT service framework. 
*********************************************************************/ 
// 
////////////////////////////////////////////////////////////////////// 
 
#if !defined(AFX_SRVAPPFACTORY_H__E7E0362F_490B_4FC9_A1E5_736E4A996EF3__INCLUDED_) 
#define AFX_SRVAPPFACTORY_H__E7E0362F_490B_4FC9_A1E5_736E4A996EF3__INCLUDED_ 
 
#if _MSC_VER > 1000 
#pragma once 
#endif // _MSC_VER > 1000 
 
template <class CServiceApplication> 
class CSrvAppFactory 
{ 
public: 
	static CServiceApplication* Instance(); 
protected: 
	CSrvAppFactory(); 
	~CSrvAppFactory(); 
private: 
	static CServiceApplication* m_Instance; 
	friend CServiceApplication* InstanceOf(); 
	// friend CServiceApplication* InstanceOf<CServiceApplication>(); 
}; 
 
 
template <class CServiceApplication> 
CSrvAppFactory<CServiceApplication>::CSrvAppFactory() 
{ 
} 
 
 
template <class CServiceApplication> 
CSrvAppFactory<CServiceApplication>::~CSrvAppFactory() 
{ 
		delete m_Instance; 
		m_Instance = NULL; 
} 
 
template <class CServiceApplication> 
CServiceApplication* CSrvAppFactory<CServiceApplication>::Instance() 
{ 
	if (NULL == m_Instance) 
	{ 
		m_Instance = new CServiceApplication; 
	} 
	return dynamic_cast<CServiceApplication*>(m_Instance); 
} 
 
template<class CServiceApplication> CServiceApplication* CSrvAppFactory<CServiceApplication>::m_Instance=NULL; 
 
template <class CServiceApplication> 
CServiceApplication* InstanceOf() 
{ 
	if (CSrvAppFactory<CServiceApplication>::m_Instance == NULL) { 
		CSrvAppFactory<CServiceApplication>::m_Instance = new CServiceApplication; 
	} 
	return dynamic_cast<CServiceApplication*>(CSrvAppFactory<CServiceApplication>::m_Instance); 
} 
 
#define CREATE_SERVICEAPP(class_name) \ 
int APIENTRY WinMain(HINSTANCE hInstance, \ 
                     HINSTANCE hPrevInstance, \ 
                     LPSTR     lpCmdLine, \ 
                     int       nCmdShow) \ 
{ \ 
	static class_name* __srvApp; \ 
	__srvApp = CSrvAppFactory<class_name>::Instance(); \ 
	__srvApp->Main(hInstance, lpCmdLine); \ 
	return 0; \ 
} \ 
 
#define DECLARE_SERVICE(class_name) \ 
public: \ 
static class_name* Instance(); \ 
 
#define IMPLEMENT_SERVICE(class_name) \ 
class_name* class_name::Instance() \ 
{ \ 
	return ::InstanceOf<class_name>(); \ 
} \ 
 
#endif // !defined(AFX_SRVAPPFACTORY_H__E7E0362F_490B_4FC9_A1E5_736E4A996EF3__INCLUDED_)