Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


rvkennedy

Member Since 13 Sep 2007
Offline Last Active Oct 22 2012 10:16 AM

Topics I've Started

Reflection and Introspection

19 August 2009 - 08:27 AM

Hello there, I'm curious about the level of interest in reflection/introspection for C++. What I'm talking about is a system that can take an arbitrary class - without special macros, templates, or base classes, and extract metadata - class names, function names, and so on. Then you can call the functions. An example would be an XML streaming system that can look for property get/set functions in a class, or public member variables, and save-off the instance's data or load it at a later time. Or a property sheet that can take an arbitrary class instance pointer, and look for get/set functions, so as to make a class's data editable. It's the sort of thing that you might use to make tools - level editors and so on. For example, calling a function might look like this:
    MetaClassInstance c=reflection::MakeClassInstance(instance_ptr);
    MetaFunction f=c->GetFunction("GetSize");
    float size=f();
In this case, a MetaClassInstance contains a pointer to an instance, and automatic meta-data saying what kind of class it points to. A MetaFunction contains the instance pointer, and a reference to the function we asked for - if it exists. Then we call the overloaded () operator of MetaFunction, and this invokes the function, which obviously knows what class instance it refers to. So we could just say:
    float f=MakeClassInstance(instance_ptr).GetFunction("GetSize")();
- although, obviously we want exceptions or some kind of error output if the class has no function GetSize(), or it takes one or more arguments, or doesn't return something that converts to float. Would this kind of system be worthwhile to the community? Would you use it, or roll your own? Have you already rolled your own, and if so, how did you get on? And for this kind of system what would be a good syntax for the meta functions?

PARTNERS