理了一下思路,实现动态识别和创建对象

心流涌现 心流涌现 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
今天又整理了一下思路,重新简要写了一下实现动态识别的机制
include "targetver.h"

#include <stdio.h>
#include <tchar.h>


// TODO: 在此处引用程序需要的其他头文件

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

#include <functional>
#include <iostream>
#include <unordered_map>

#include <codecvt>

using namespace std;


class CMyObject;
typedef CMyObject * (* FP)();

class B;
class C;
class D;

struct reg_map;
typedef reg_map DynamicManageClass;


struct reg_map
{
static std::unordered_map<std::wstring , FP> className_To_Construct;

reg_map( const std::wstring & classNameStr , const FP & pConstructFunc )
{
if (classNameStr.empty())
return;

reg_map::className_To_Construct[classNameStr] = pConstructFunc;
}

static CMyObject * ConstructInst(const wchar_t * nameStr)
{
if ( nameStr == nullptr )
return nullptr;

if ( reg_map::className_To_Construct.count( nameStr ) <= 0 )
{
wcout << L"\n" << nameStr << L" is not a class name! Construct Instance is failed!";
return nullptr;
}

FP pf = reg_map::className_To_Construct[nameStr];
if ( pf == nullptr )
{
wcout << L"\nclass " << nameStr << L" does not support dynamic creation (only dynamic identification)! Construct Instance is failed!" ;
return nullptr;
}

return (* pf)();
}

};
std::unordered_map<std::wstring , FP> reg_map::className_To_Construct;


template <typename Type>
class dy
{
public:
// typedef Type _classType;
static const reg_map _initMap;
};

template <typename Type>
class dyc
{
public:
// typedef Type _classType;
static CMyObject * ConstructInst(){ return new Type(); }
static const reg_map _initMap;
};


#define DECLARE_DYNAMIC()\
virtual bool isKindof(const wchar_t * nameStr);\
virtual std::wstring getClassName();\
virtual std::wstring getBaseClassName();\
virtual std::wstring getRootBasetClassName();


#define IMPLEMENT_DYNAMIC(class_name, baseclass_name)\
class class_name;\
template class dy<class_name>;\
const reg_map dy<class_name>::_initMap(L#class_name , nullptr);\
bool class_name::isKindof(const wchar_t * nameStr)\
{\
if ( nameStr == nullptr )\
return false;\
if ( std::wstring(L#class_name).compare( nameStr ) == 0 )\
{ \
wcout << L"\nobject is kind of " << L#class_name;\
return true;\
}\
else \
{\
return baseclass_name::isKindof( nameStr );\
}\
}\
std::wstring class_name::getClassName(){ return L#class_name; }\
std::wstring class_name::getBaseClassName(){ return baseclass_name::getClassName(); }\
std::wstring class_name::getRootBasetClassName(){ return baseclass_name::getRootBasetClassName(); }


#define IMPLEMENE_DYCREATE(class_name, baseclass_name)\
class class_name;\
template class dyc<class_name>;\
const reg_map dyc<class_name>::_initMap(L#class_name , dyc<class_name>::ConstructInst);\
bool class_name::isKindof(const wchar_t * nameStr)\
{\
if ( nameStr == nullptr )\
return false;\
if ( std::wstring(L#class_name).compare( nameStr ) == 0 )\
{ \
wcout << L"\nobject is kind of " << L#class_name;\
return true;\
}\
else \
{\
return baseclass_name::isKindof( nameStr );\
}\
}\
std::wstring class_name::getClassName(){ return L#class_name; }\
std::wstring class_name::getBaseClassName(){ return baseclass_name::getClassName(); }\
std::wstring class_name::getRootBasetClassName(){ return baseclass_name::getRootBasetClassName(); }


class CMyObject
{
public:
virtual ~CMyObject(){ ; }

public:
virtual bool isKindof(const wchar_t * nameStr)
{
if ( nameStr == nullptr )
{
return false;
}

if ( std::wstring(L"CMyObject").compare( nameStr ) == 0 )
{
wcout << L"\nobject is kind of CMyObject.";
return true;
}

wcout << L"\nobject is not kind of " << nameStr;
return false;
}
virtual std::wstring getClassName(){ return L"CMyObject"; }
//类里没有了static 的construct()函数,放到外部一个模板实例类里了。
//省字节,被模板实例占字节了,平衡
virtual std::wstring getBasetClassName(){ return L"\0"; }
virtual std::wstring getRootBasetClassName(){ return getClassName(); }

virtual void fun() = 0; //注意派生类内必须定义,才能动态创建对象实例
};
template class dy<CMyObject>;
const reg_map dy<CMyObject>::_initMap(L"CMyObject" , nullptr);

class B : public CMyObject
{
public:
virtual ~B(){ ; }

public:

DECLARE_DYNAMIC()
};
IMPLEMENT_DYNAMIC(B, CMyObject)//动态识别


class C : public B
{
public:
virtual ~C(){ ; }

public:
virtual void fun() { ; }

DECLARE_DYNAMIC()
};
IMPLEMENE_DYCREATE(C, B) //动态创建



class D : public C
{
public:
virtual ~D(){ ; }

public:

DECLARE_DYNAMIC()
};
IMPLEMENE_DYCREATE(D, C) //动态创建


int _tmain(int argc, _TCHAR* argv[])
{
CMyObject * pObj = DynamicManageClass::ConstructInst(L"CMyObject");
if ( pObj != nullptr )
{
pObj->isKindof( L"CMyObject" );
pObj->isKindof( L"B" );
pObj->isKindof( L"C" );
pObj->isKindof( L"D" );
delete pObj;
}

cout << "\n";

pObj = DynamicManageClass::ConstructInst(L"B");
if ( pObj != nullptr )
{
pObj->isKindof( L"CMyObject" );
pObj->isKindof( L"B" );
pObj->isKindof( L"C" );
pObj->isKindof( L"D" );
delete pObj;
}

cout << "\n";

pObj = DynamicManageClass::ConstructInst(L"C");
if ( pObj != nullptr )
{
pObj->isKindof( L"CMyObject" );
pObj->isKindof( L"B" );
pObj->isKindof( L"C" );
pObj->isKindof( L"D" );
delete pObj;
}

cout << "\n";

pObj = DynamicManageClass::ConstructInst(L"D");
if ( pObj != nullptr )
{
pObj->isKindof( L"CMyObject" );
pObj->isKindof( L"B" );
pObj->isKindof( L"C" );
pObj->isKindof( L"D" );
delete pObj;
}

cout << "\n";

return 0;
}

...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
心流涌现
C++ 语言 65,214 成员 250,514 内容