c++之巧用type_info

注册博客有一段时间,一直很忙没有写技术文章,今天把近期心得写出来一起分享。

c++没有反射机制,我们可以通过c++的语言特性去实现同样的功能。本文介绍使用type_info实现的一些发射特性。

1 type_info简介:

class type_info {
public:
_CRTIMP virtual ~type_info();
_CRTIMP int operator==(const type_info& rhs) const;
_CRTIMP int operator!=(const type_info& rhs) const;
_CRTIMP int before(const type_info& rhs) const;
_CRTIMP const char* name() const;
_CRTIMP const char* raw_name() const;
private:
void *_m_data;
char _m_d_name[1];
type_info(const type_info& rhs);
type_info& operator=(const type_info& rhs);
};

(注: c++0x01增加了hash_code方法,获取类名对应的hash值)

使用type_info需要注意3点:

(1)type_info的构造函数和赋值操作符都为私有。因此不要试图去定义或复制一个type_info对象。创建type_info对象的唯一方法是使用typeid操作符。如果想使用type_info的引用,可以使用const type_info& info = typeid(foo)。

(2) type_info::name返回的是类似于"class CBase*"之类的名字, 而且c++标准只是告诉编译器需要实现type_info::name函数,不同编译器编译运行后输出不一样。

(3) 对于父类指针类型变量,typeid(ptr).name不具有多态性, 仍返回此父类的指针类名,如"class CBase*"。如果直接传入对象如typeid(*ptr).name则具有多态性,将返回"class CDevievd"类似的子类类名。  

2 巧用type_info

(1) 得到类名的一种方法是在类中实现class_name()方法,如 

class CDataType 
{
public:
virtual char* class_name() { return "CDataType"; }
};

这种方法的一个缺点就是强迫每一个类必须实现这个函数,否则后续的工作就无法完成。

 

下面是一个为数据注册创建者,并根据数据类型获取创建者的例子,看一下它是如何工作的。

// 数据
class CDataType
{
public:
virtual char* class_name() { return "CDataType"; }
};
class CDataTypeA : public CDataType
{
public:
virtual char* class_name() { return "CDataTypeA"; }
};
class CDataTypeB : public CDataType
{
public:
virtual char* class_name() { return "CDataTypeB"; }
}

// 数据创建者
class CreatorType {};
class CreatorForDataA : public CreatorType {};
class CreatorForDataB : public CreatorType {};

typedef std::string KeyType;
static std::map<std::string, CreatorType*> nameMap;

void RegistCreator(KeyType key, CreatorType* creator)
{
nameMap[key] = creator;
}

CreatorType* GetCreator( CDataType* data )
{
return nameMap[data->class_name()];
}

void main()
{
RegistCreator( "CDataTypeA", new CreatorForDataA );
RegistCreator( "CDataTypeB", new CreatorForDataB );

CDataTypeA* dataA = new CDataTypeA;

CreatorType* creator = GetCreator(dataA);
}

 

为每个数据强制加一个class_name虚函数看起来是否有点冗余?

下面我们用type_info来实现相同的功能。

// 数据
class CDataType {};
class CDataTypeA : public CDataType {};
class CDataTypeB : public CDataType {};

CreatorType* GetCreator( CDataType* data )
{
return nameMap[typeid(*data).name()];
}

void main()
{
RegistCreator( "CDataTypeA", new CreatorForDataA );
RegistCreator( "CDataTypeB", new CreatorForDataB );

CDataTypeA* dataA = new CDataTypeA;

CreatorType* creator = GetCreator(dataA);
}

比较一下,这个版本的代码要简单许多了,也有较好的扩展性,或许我们可以放松一下了。

可是没过多少天问题就来了,有的同事担心大量的字符串比较会影响速度,虽然map是很高效的。担心确实是不必要的,杀手锏还在你的手中——hash_code。

hash_code是把字符串映射到一个唯一整数,使用整数作为map键值效率要比string高许多。废话不说,上代码。

typedef int KeyType;
static std::map<KeyType, CreatorType*> nameMap;

void RegistCreator(KeyType key, CreatorType* creator)
{
nameMap[key] = creator;
}

CreatorType* GetCreator( CDataType* data )
{
return nameMap[typeid(*data).hash_code()];
}

void main()
{
RegistCreator( typeid(CDataTypeA).hash_code(), new CreatorForDataA );
RegistCreator( typeid(CDataTypeB).hash_code(), new CreatorForDataB );

CDataTypeA* dataA = new CDataTypeA;

CreatorType* creator = GetCreator(dataA);
}

如果你的c++版本不够,发现type_info根本没有hash_code方法,没有关系,可以使用下面的方法代替。

size_t hash_code( const type_info& info )
{
// hash name() to size_t value by pseudorandomizing transform
const char *_Keyval = info.name();
size_t _Val = 2166136261U;
size_t _First = 0;
size_t _Last = _CSTD strlen(_Keyval);
size_t _Stride = 1 + _Last / 10;

for(; _First < _Last; _First += _Stride)
_Val = 16777619U * _Val ^ (size_t)_Keyval[_First];
return (_Val);
}

 

本文只是抛砖引玉,希望各位朋友能有更多的发现。

 

2012-03-16

http://www.cnblogs.com/dario/

<转载请注明作者和出处>

 

原文链接: https://www.cnblogs.com/dario/archive/2012/03/16/2399864.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    c++之巧用type_info

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/44348

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 下午8:59
下一篇 2023年2月8日 下午8:59

相关推荐