// class.h
#ifndef CLASS_H
#define CLASS_H
#include<iostream>
#include<functional>
#include <memory>
#include <map>
#include <stdarg.h>
using namespace std;
class ClassMappingObject
{
public:
virtual void classinit()=0;
virtual ~ClassMappingObject()=default;
};
using RegisterFunc = std::function<std::unique_ptr<ClassMappingObject>()>;
inline std::map<std::string ,RegisterFunc> objectMap;
static void registerObject(const std::string& name,RegisterFunc func) {
std::cout << "registerObject:" << &objectMap << std::endl;
objectMap[name] = func;
}
template <typename T>
T* createObject(const std::string& name) {
for(auto m: objectMap)
{
// std::cout<<"keys: "<<m.first<<std::endl;
}
if (objectMap.find(name) == objectMap.end()) {
return nullptr;
}
std::cout << "createObject:" << &objectMap << std::endl;
auto ptr = objectMap[name]();
// 从基类动态转换为外部需要的类型
return dynamic_cast<T*>(ptr.release());
}
template <typename T1>
std::unique_ptr<ClassMappingObject> class_make_unique() {
auto* ptr = new T1;
return std::unique_ptr<ClassMappingObject>(dynamic_cast<ClassMappingObject*>(ptr));
}
struct RegisterTask {
RegisterTask( string a) {
std::cout<<a<<std::endl;
}
static string registerfun(const std::string& name,
std::function<std::unique_ptr<ClassMappingObject>()> func) {
registerObject(name,func);
return name;
}
};
#ifndef REGISTER_CLASS_2
#define REGISTER_CLASS_2(Type)\
static RegisterTask task##Type(RegisterTask::registerfun(#Type,class_make_unique<Type>));
#endif
#endif
// F.h
#ifndef F_H
#define F_H
class F
{
public:
virtual void fun()=0;
};
#endif
// Per.h
#include"class.h"
#include"F.h"
class Per:public F,public ClassMappingObject
{
public:
void classinit() //实现ClassMappingObject中函数进行初始化
{
tem=10;
std::cout<<"Per classinit"<<std::endl;
}
virtual void fun() //实现F中虚函数进行执行业务
{
std::cout<<"Per fun:"<<tem<<std::endl;
}
private:
int tem=0;
};
//进行注册
// REGISTER_CLASS(Per);
REGISTER_CLASS_2(Per)
// Per1.h
#include"class.h"
#include"F.h"
class Per1:public F,public ClassMappingObject
{
public:
void classinit() //实现ClassMappingObject中函数进行初始化
{
tem=10;
std::cout<<"Per1 classinit"<<std::endl;
}
virtual void fun() //实现F中虚函数进行执行业务
{
std::cout<<"Per1 fun:"<<tem<<std::endl;
}
private:
int tem=0;
};
//进行注册
// REGISTER_CLASS(Per1);
REGISTER_CLASS_2(Per1);
// Per2.h
#include"class.h"
#include<vector>
#include"F.h"
class Per2 :public F, public ClassMappingObject
{
public:
Student(int a=10){tem = a;}
virtual void fun()
{
std::cout<<"Per2 fun"<<tem<<std::endl;
}
public:
void classinit()
{
tem=80;
std::cout<<"Per2 classinit"<<std::endl;
}
private:
float tem;
};
// REGISTER_CLASS(Student);
REGISTER_CLASS_2(Student);
// main.cpp
#include "Per.h"
#include "Per1.h"
#include "Per2.h"
#include "F.h"
int main(int argc, char const *argv[])
{
/* code */
F* ptr = createObject<F>("Per"); //获取基类类型,实现多态
ptr->fun();
F* ptr1 = createObject<F>("Per1"); //获取基类类型,实现多态
ptr1->fun();
return 0;
}
标签:std,反射,实现,ptr,C++,public,ClassMappingObject,include,class
From: https://www.cnblogs.com/xiaohaigegede/p/16904695.html