RefClassHelper.h
/******************************************************************** author : Clark/陈泽丹 created : 2013-5-8 purpose : 反射辅助类 *********************************************************************/ #pragma once //反射类型 struct RefClassType { //获取类型 virtual long GetType() = 0; }; //获得反射类型 template< class Base, int TYPE_ID > struct GetClassTypeEx: public Base { //获取类型 virtual long GetType(){ return TYPE_ID; } }; //实例ID struct RefClassID { //获取类型 virtual long GetID() = 0; }; //获得类型ID template< class Base > struct GetClassIDEx: public Base { public: GetClassIDEx(const long _id):ID(_id){} //获取类型 virtual long GetID(){ return ID; } private: const long ID; };
ServerDllHelper.h
/******************************************************************** author : Clark/陈泽丹 created : 2013-5-8 purpose : DLL辅助类 *********************************************************************/ #pragma once #include <Windows.h> template< class _Ret, class _IUserServer, class _IGlobalServer > class CServerDllHelper { private: typedef _Ret (*ProcDLLFun)(_IUserServer **_ppIUserServer, _IGlobalServer *_pIGlobalServer); public: CServerDllHelper():m_hDll(NULL),m_pUserServer(NULL){ m_szErrMsg[0] = 0; } ~CServerDllHelper() { Close(); if ( NULL != m_hDll ) { ::FreeLibrary(m_hDll); m_hDll = NULL; } } //销毁接口对象 void Close() { if ( NULL != m_pUserServer ) { //m_pUserServer->Release(); m_pUserServer = NULL; } } // 描 述:创建服务器 // 返回值:BOOL - 创建成功则返回TRUE,否则返回FALSE BOOL Create(LPCSTR _dll_name, LPCSTR _create_fun_name, _IGlobalServer *_pServerGlobal) { Close(); try { if ( NULL == m_hDll ) { //第一次调用时未加载dll,下面进行加载dll m_hDll = ::LoadLibrary(_dll_name); if (m_hDll == NULL) { lstrcpy(m_szErrMsg,"Can't load "); lstrcat(m_szErrMsg, _dll_name); throw m_szErrMsg; } } //下面获得dll的输出函数,即接口对象的创建函数 ProcDLLFun proc = NULL; proc = (ProcDLLFun)::GetProcAddress(m_hDll,_create_fun_name); if ( NULL == proc ) { lstrcpy(m_szErrMsg,"Can't GetProcAddress("); lstrcat(m_szErrMsg, _create_fun_name); lstrcat(m_szErrMsg, ")"); throw m_szErrMsg; } if (!proc( &m_pUserServer, _pServerGlobal )) { lstrcpy(m_szErrMsg,_create_fun_name); lstrcat(m_szErrMsg, " error!"); throw m_szErrMsg; } return TRUE;//接口对象创建成功 } catch (LPCSTR szMsg) { throw szMsg; return FALSE; } catch(...) { lstrcpyn(m_szErrMsg, "Unknown Error!", sizeof(m_szErrMsg)); return FALSE; } } //重载->,将helper对象的引用直接转为接口的引用 _IUserServer* operator ->(){ return m_pUserServer; } //获得相关服务 _IUserServer* GetServer(){ return m_pUserServer; } //判断接口指针是否有效 BOOL IsValid(){ return ( NULL != m_pUserServer ); } //获得错误信息 char* GetErrMsg(){ return m_szErrMsg; } private: _IUserServer *m_pUserServer; //接口对象 HINSTANCE m_hDll; //动态链接库加载与否标志 char m_szErrMsg[128]; //错误信息 };
UtilHelper.h
/******************************************************************** author : Clark/陈泽丹 created : 2013-5-8 purpose : 应用工具类 *********************************************************************/ #pragma once #include <iostream> using namespace std; #define TRACELN( MSG ) do{ cout<<MSG<<endl; } while(0) #define TRACEERR( MSG ) do{ cout<<"Error "<<MSG<<endl; } while(0) #define UTIL_TRUE_NORETURN( CONDITION ) do{ if( CONDITION ) return; } while(0) #define UTIL_TRUE_RETURN( CONDITION, RET_VAL ) do{ if( CONDITION ) return RET_VAL; } while(0) #define UTIL_TRUE_TRACE_NORETURN( CONDITION, MSG ) do{ if( CONDITION ) { cout<<MSG<<endl; return; } } while(0) #define UTIL_TRUE_TRACE_RETURN( CONDITION, MSG, RET_VAL ) do{ if( CONDITION ) { cout<<MSG<<endl; return RET_VAL; } } while(0)
ITrace.h
#pragma once #include <Windows.h> // Trace接口 struct ITrace { // 销毁Trace virtual void Release(void) = 0; // Trace普通信息(带回车换行功能, 线程安全, 蓝色) virtual BOOL TraceLn(LPCSTR lpszFormat, ...) = 0; // Trace错误信息(带回车换行功能, 线程安全, 红色) virtual BOOL TraceErrorLn(LPCSTR lpszFormat, ...) = 0; // Trace信息(带回车换行功能, 线程安全, 颜色自定义) virtual BOOL TraceColorLn(COLORREF color, LPCSTR lpszFormat, ...) = 0; };
#include "ITrace.h" #include "UtilHelper.h" class ServerTrace : public ITrace { public: // 销毁Trace virtual void Release(void){} // Trace普通信息(带回车换行功能, 线程安全, 蓝色) virtual BOOL TraceLn(LPCSTR lpszFormat, ...) { va_list args; va_start(args, lpszFormat); _vsnprintf_s(m_msg, sizeof(m_msg) - 1, lpszFormat, args); TRACELN(m_msg); va_end(args); return TRUE; } // Trace错误信息(带回车换行功能, 线程安全, 红色) virtual BOOL TraceErrorLn(LPCSTR lpszFormat, ...) { va_list args; va_start(args, lpszFormat); _vsnprintf_s(m_msg, sizeof(m_msg) - 1, lpszFormat, args); TRACELN(m_msg); va_end(args); return TRUE; } // Trace信息(带回车换行功能, 线程安全, 颜色自定义) virtual BOOL TraceColorLn(COLORREF color, LPCSTR lpszFormat, ...) { va_list args; va_start(args, lpszFormat); _vsnprintf_s(m_msg, sizeof(m_msg) - 1, lpszFormat, args); TRACELN(m_msg); va_end(args); return TRUE; } private: enum { BUF_MAX = 1024 }; char m_msg[BUF_MAX]; }; struct IServerGlobal; extern "C" __declspec(dllexport) BOOL CreateServerTrace(ITrace **_ppServerTrace, IServerGlobal *_pServerGlobal) { UTIL_TRUE_TRACE_RETURN( NULL == _pServerGlobal, "Error, _pServerGlobal is null!", FALSE ); ITrace* p_trace = new ServerTrace(); UTIL_TRUE_TRACE_RETURN( (NULL == p_trace), "Error, CreateServerTrace!", FALSE ); *_ppServerTrace = p_trace; return TRUE; }