c++运行时类型识别(rtti)

运行时类型识别机制
本文介绍了一种简单的运行时类型识别(RTTI)机制实现。通过模板元编程定义了一个通用的类型信息基类,该基类能够判断对象是否为特定类型或者是否为另一类型的派生。文中还展示了如何为具体的类注册类型信息,并提供了测试代码。

一个简单运行时类型识别

namespace rtti_ex {

	/*
	 *	类型信息基类
	 */
	class i_type_info {
	public:
		// 判断是否是指定类型
		bool is(const char* _name) const { return name() == _name; }
		template<class T> bool is() const { return is(T::name()); }

		// 判断是否是派生类型
		bool is_kind_of(const char* _name) const { return on_is_kind_of(_name); }
		template<class T> bool is_kind_of() const { return is_kind_of(T::name()); }

	protected:
		virtual bool on_is_kind_of(const char* name) const = 0;
		virtual const std::string& name() const = 0;
	};


	/**	类型信息
	 *	@_This	当前类
	 *	@_Base	基类
	 *	@_name_	返回当前类名称的函数
	 */
	template<class _This, class _Base, const char*(*_name_)()>
	class type_info : public _Base::type_info_t {
	public:
		type_info() : _name(_name_()) {}

	protected:
		virtual bool on_is_kind_of(const char* name) const {
			if (_name == name) {
				return true;
			}
			return _Base::type_info_t::on_is_kind_of(name);
		}
		virtual const std::string& name() const { return _name; };

	private:
		const std::string _name;
	};

	/**	偏特化,最开始的基类类型(即没有继承的类)
	 *	
	 */
	template<class _This, const char*(*_name_)()>
	class type_info<_This, void, _name_> : public i_type_info{
	public:
		type_info() : _name(_name_()) {}
	protected:
		virtual bool on_is_kind_of(const char* name) const {
			if (_name == name) {
				return true;
			}
			return false;
		}
		virtual const std::string& name() const { return _name; };
	private:
		const std::string _name;
	};

}


#define REGIST_INFO_CLASS(ThisClass, BaseClass) \
public: \
	static const char* name() { return #ThisClass; }\
	typedef rtti_ex::type_info<ThisClass, BaseClass, name> type_info_t; \
	virtual const rtti_ex::i_type_info& getinfo() { \
	static type_info_t* s_info = nullptr; \
		if (!s_info) { \
			type_info_t* p = new type_info_t(); \
			s_info = p; \
		} \
		return *s_info; \
	}

测试:

class A {
	REGIST_INFO_CLASS(A, void)
};



class B : public A {
	REGIST_INFO_CLASS(B, A)
};


class C : public B{
	REGIST_INFO_CLASS(C, B)
};

	C c;

	A* a = &c;
	bool b;

	b = a->getinfo().is<A>();
	b = a->getinfo().is<B>();
	b = a->getinfo().is<C>();

	b = a->getinfo().is_kind_of<A>();
	b = a->getinfo().is_kind_of<B>();
	b = a->getinfo().is_kind_of<C>();



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值