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>() or type::of(const base& obj) type object associated type t or runtime type of obj respectively.
  • most importantly: ability check if type derived one, either using template parameter or type objects only, e.g. sometype.is<t>() or sometype.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

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

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -