根据字符串动态创建类对象

标签: 笔记 c++

【目的】

根据字符串运行时刻动态产生类对象

【编译运行环境】

Windows/ VC6
Linux / g++


【使用方法】

1. 首先需要定义类,比如 B 从 A 派生出来

2. 调用以下语句注册B类:

DYN_DECLARE(B);

3. 此后只需用类名对应的字符串就可以创建该类的对象:

A * p1 = (A *) DYN_CREATE("B");

此时创建的是 B 的对象(downcast)

4. 如果某个类未注册(通过DYN_DECLARE),DYN_CREATE 返回NULL指针

【实现】

/***************************************************************************/
/* dynclass.h */
/***************************************************************************/

#ifndef __DYNAMIC_H__

#define __DYNAMIC_H__

#include < cstdio >
#include < string >
#include < typeinfo >

#if !defined ( DYN_DECLARE )
#define DYN_DECLARE(class_name) DYN_CLASS::CFactory class_name
#endif

#if !defined ( DYN_CREATE )
#define DYN_CREATE(class_name) DYN_CLASS::Create(class_name)
#endif


namespace DYN_CLASS
{
/* create object by class name */
void * Create( const char * class_name );

/* interface of class factory*/
class CAbstractFactory
{
public:

virtual void * Create( const char * class_name ) = 0;
};

/* list of class factory */
class CFactoryList
{
friend void * Create( const char * class_name );

static CFactoryList * _head;
CFactoryList * m_next;
CAbstractFactory * m_item;

public:

CFactoryList( CAbstractFactory * fact );
virtual ~CFactoryList( void );

};

/* ctor of CFactoryList, add a class factory to list */
inline CFactoryList::CFactoryList( CAbstractFactory * fact )
: m_item( fact )
{
m_next = _head;
_head = this;
}

#if defined ( _MSC_VER )
/* disable warning for the following line : CFactory( void ): m_item( this ) {} */
#pragma warning(disable : 4355)
#endif

/* realization of class factory */
template 
class CFactory: public CAbstractFactory
{
static t _object;
CFactoryList m_item;

public:

/* add itself to list of class factory when constructed */
CFactory( void ) : m_item( this ) {}

virtual ~CFactory() {}

/* create object of this class if matched */
void * Create( const char * class_name )
{
std::string strClassName;

#if defined (WIN32 )
strClassName = ( "class " );
#else
char szSize[4] = {0};
sprintf(szSize, "%d", strlen(class_name) );
strClassName = szSize;
#endif
strClassName += class_name;

/* RTTI support */
return !strcmp( typeid(_object).name(), strClassName.c_str() )
? (void*)( new t ) : 0 ;

}
};
}
#endif /* __DYNAMIC_H__ */


/***************************************************************************/
/* dynclass.cpp */
/***************************************************************************/

#include " dynclass.h "

namespace DYN_CLASS
{
CFactoryList * CFactoryList::_head = 0;

void *Create( const char * class_name )
{
void * new_object = 0;
const CFactoryList * cur = CFactoryList::_head;

for( ; cur ; cur = cur->m_next )
{
/* if class_name matched, object will then be created and returned */
if( new_object = cur->m_item->Create(class_name) )
{
break;
}
}

return new_object;
}

/* delete linkage from CFactoryList when some class factory destroyed */
CFactoryList::~CFactoryList( void )
{
CFactoryList ** m_nextp = &CFactoryList::_head;

for( ; *m_nextp ; m_nextp = &(*m_nextp)->m_next )
if( *m_nextp == this )
{
*m_nextp = (*m_nextp)->m_next;
break;
}
}
}

/***************************************************************************/
/* main.cpp */
/***************************************************************************/


#include < iostream >
#include " dynclass.h "

class CBase
{

public:
CBase(){ std::cout << "CBase ctor" << std::endl; }
virtual ~CBase(){ std::cout << "CBase dtor" << std::endl; }
};

class CDerived1 : public CBase
{
public:
CDerived1(){ std::cout << "CDerived1 ctor" << std::endl; }
virtual ~CDerived1(){ std::cout << "CDerived1 dtor" << std::endl; }
};

class CDerived2 : public CBase
{
public:
CDerived2(){ std::cout << "CDerived2 ctor" << std::endl; }
virtual ~CDerived2(){ std::cout << "CDerived2 dtor" << std::endl; }
};


int main(void)
{

DYN_DECLARE(CDerived1);
DYN_DECLARE(CDerived2);


CBase * p1 = (CBase *) DYN_CREATE("CDerived1");
CBase * p2 = (CBase *) DYN_CREATE("CDerived2");

std::cout << std::endl;

if ( p1 != NULL )
{
delete p1;
}

if ( p2 != NULL )
{
delete p2;
}


return 0;
}

/* result:

CBase ctor
CDerived1 ctor
CBase ctor
CDerived2 ctor

CDerived1 dtor
CBase dtor
CDerived2 dtor
CBase dtor
*/


转载于:https://my.oschina.net/u/1450061/blog/204563

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值