inheritance - C++: Building a simple custom RTTI system on top of the existing one -
for few days have been trying build simple custom rtti system in c++. using templates , typeid operator have tried different approaches, work in following fashion
- manual registration of types during static initialization using
type::add<t>(name)t derived common base class, e.g.base. - using static methods
type::of<t>()ortype::of(const base& obj)typeobject associated typetor runtime type ofobjrespectively. - most importantly: ability check if type derived one, either using template parameter or
typeobjects only, e.g.sometype.is<t>()orsometype.is(const type& base).
while of features implemented quickly, haven't been able figure out proper/efficient solution last one, i.e. checking if 1 type derived another, when given 2 type objects, example like
// check if 'typea' base class of 'typeb' bool isbaseof(const type& typea, const type& typeb) { return typeb.is(typea); // <- how implement that? } one obvious solution store base classes of every registered type , traverse hierarchy on every type::is(const type&) call. while work, can't imagine being efficient. prefer use built-in rtti system, if it's somehow possible. if stored respective std::type_info in every of type objects, use former check inheritance. unfortunately, i've read in
- c++: using typeinfo test class inheritance
- can tell if std::type_info object equal or class derived other?
that not directly possible, @ least not in portable way. so, finally, here come(s) question(s): there way want to, without storing , traversing inheritance hierarchy? know article / example of simple c++ rtti system implementing aforementioned features? have found either articles how rtti supposed work, without actual implementation, or complicated implementations additional features such automatic (de)serialization etc. i'd happy share code, if work properly, in case else struggling same problem.
Comments
Post a Comment