一年半前,见过宏定义##的使用,实现类似反射机制,通过字符串创建类对象的代码,感觉很神奇。
两个月前,利用##的语法,实现一个小功能,通过机组字符串排列组合,调用一个万行代码,计算功能类的函数接口。
三四天前,简单概要的把代码提纯,记录下来,以备后用。代码量不多,但每次写都好费劲,至少写了5遍了,仍然费劲。
main.cpp
1 #include <iostream> 2 #include "register.h" 3 #include "registerfun.h" 4 5 using namespace std; 6 7 8 int main() 9 { 10 //1.使用##创建对象,创建同类,不同对象 11 cout << "1." <<endl; 12 Init(); 13 Create("CObjectA"); 14 Create("CObjectA"); 15 Create("CObjectB"); 16 17 //2.使用##调用对象函数 18 cout << "2." <<endl; 19 InitFun(); 20 CallFun("Welcome1"); 21 CallFun("Welcome2"); 22 23 return 0; 24 }
register.h
1 #ifndef _REGISTER_H_ 2 #define _REGISTER_H_ 3 4 #include <iostream> 5 #include <string> 6 7 #include "instance.h" 8 9 extern"C" bool Init(void); 10 extern"C" bool Clear(); 11 extern"C" IObj* Create(const string& name); 12 13 #endif
register.cpp
#include "register.h" #include <iostream> #include <string> #include <map> using namespace std; //宏定义的一种用法,可实现类似反射的 c++ 功能。宏定义的神奇,减少代码量,类似泛型的感觉 #define IMPLEMENT_CREATE_OBJ(classname) static IObj* _create_obj##classname() { return new classname(); } #define CREATE_OBJ_FUN(classname) _create_obj##classname //函数指针 typedef IObj* (*FCreateObj)(void); //函数指针的map类型 typedef map<string, FCreateObj> StrCreateFunMap; //通过宏定义实现各种函数指针 IMPLEMENT_CREATE_OBJ(CObjectA) IMPLEMENT_CREATE_OBJ(CObjectB) //定义一个函数指针的map StrCreateFunMap g_strCreateFunMap; bool Init(void) { g_strCreateFunMap["CObjectA"] = CREATE_OBJ_FUN(CObjectA); g_strCreateFunMap["CObjectB"] = CREATE_OBJ_FUN(CObjectB); return true; } bool Clear() { return true; } IObj* Create(const string& name) { return (*(g_strCreateFunMap[name]))(); }
registerfun.h
1 #ifndef _REGISTER_FUN_H_ 2 #define _REGISTER_FUN_H_ 3 4 #include <iostream> 5 #include <string> 6 #include <map> 7 8 #include "instance.h" 9 10 using namespace std; 11 12 extern"C" bool InitFun(void); 13 extern"C" bool ClearFun(void); 14 extern"C" bool CallFun(const string& name); 15 16 #endif
registerfun.cpp
#include "registerfun.h" #include <iostream> #include <string> #include <map> using namespace std; #define IMPLEMENT_CALL_FUN(funname) static void _call_instance##funname() { CInstance* p = CInstance::GetInstance(); p->funname(); } #define CALL_FUN(funname) _call_instance##funname typedef void(*FCallFun)(void); typedef map<string, FCallFun> StrCallFunMap; IMPLEMENT_CALL_FUN(Welcome1) IMPLEMENT_CALL_FUN(Welcome2) StrCallFunMap g_strCallFunMap; bool InitFun() { g_strCallFunMap["Welcome1"] = CALL_FUN(Welcome1); g_strCallFunMap["Welcome2"] = CALL_FUN(Welcome2); return true; } bool ClearFun() { return true; } bool CallFun(const string& name) { (*(g_strCallFunMap[name]))(); return true; }
instance.h
1 #ifndef _INSTANCE_H_ 2 #define _INSTANCE_H_ 3 4 #include <iostream> 5 #include <string> 6 7 using namespace std; 8 9 class IObj 10 { 11 public: 12 virtual void Welcome()=0; 13 }; 14 class CObjectA : public IObj 15 { 16 public: 17 CObjectA(); 18 virtual ~CObjectA(); 19 void Welcome(); 20 }; 21 class CObjectB : public IObj 22 { 23 public: 24 CObjectB(); 25 virtual ~CObjectB(); 26 void Welcome(); 27 }; 28 class CInstance 29 { 30 private: 31 CInstance(); 32 virtual ~CInstance(); 33 public: 34 static CInstance* GetInstance(); 35 void Welcome1(); 36 void Welcome2(); 37 private: 38 static CInstance* m_pInstance; 39 }; 40 41 #endif
instance.cpp
#include "instance.h" // CObjectA::CObjectA() { Welcome(); } CObjectA::~CObjectA() { } void CObjectA::Welcome() { cout << "CObjectA welcome " << this <<endl; } // CObjectB::CObjectB() { Welcome(); } CObjectB::~CObjectB() { } void CObjectB::Welcome() { cout << "CObjetcB Welcome " << this <<endl; } //静态私有类成员变量赋值 CInstance* CInstance::m_pInstance = NULL; CInstance::CInstance() { } CInstance::~CInstance() { } CInstance* CInstance::GetInstance() { if (m_pInstance == NULL) { m_pInstance = new CInstance(); } return m_pInstance; } void CInstance::Welcome1() { cout << "CInstance Welcome1"<<endl; } void CInstance::Welcome2() { cout << "CInstance Welcome2"<<endl; }
时间: 2024-12-19 16:30:44