July 12, 2009

C++ Reflection

Posted in Computer Graphics tagged , , , , , , , , , , , at 10:48 pm by sagito

Today I faced a different problem while trying to build an external interface for the Conspiracy Engine… As it was built on C++, which is a compiled (not interpreted) language, the compiler does not have the concept of reflection. In the first place, what is reflection? Reflection is a technique that makes a language able to know, observe and modify its own structure and behaviour. For example, if I have a class A that wants to know which methods and variables are in class B, I just ask the compiler to tell me. This happens, for example in C#, where we can ask the compiler for class methods, variables, and even execute the methods if we want.

Problem is: There is no direct support for this in C++… However, I need that, because my intention is to build an external editor that can interact directly with the engine like the CryEditor, for example. So, I’m trying to find a solution! There are several ways of attempting such a thing. The first one is to read through the debug information… This is not viable because different compilers use different approaches for debug and the program would have to be always built with debug information (which is huge).

Second solution would be to create a compiler that supported reflection for C++… I have already create one (not for the C++ language!) and I guarantee that it is not something neither pretty to see nor easy to implement. Third solution: Create a specialized pre-processor that could build over the class descriptions… This solution could be viable… However, parsing the C++ language is not trivial and would require a two-pass-process which would introduce a huge overhead.

So, there is no solution for this problem? What if the engine had a specialized class that would store the data for reflection? Every class that derived from this base would register their variables and methods in a global storage that could be accessed from any other class. This is not the real reflection that we are to, but it seems like a nice approach. And it contemplates the cases in which a variable (or class) is sealed, i.e., not accessible through reflection. In that case, the class simply does not register its variables or methods! This approach seems to be the best one so far and I have already implemented it on Conspiracy.

So far it is working fine! šŸ˜€ I should give you some news very soon about the new editor for Conspiracy! šŸ˜‰

Advertisements

2 Comments »

  1. Guilherme said,

    Your fourth solution seems to be a good roundabout for your issue!
    It’s like an emulation of the reflection, but as usually happens with emulations, wouldn’t it require additional processing power to register and access the data? Or it isn’t even notorious, being lightweight?

    Regarding your first option, is the size really an issue? Isn’t there a way to compress the debug file, but still being usable?

    I’m a bit out of my field of expertise, and probably I didn’t make much sense, but I’m just trying to learn (:

    By the way, I’ve opened a blog, and refered yours on it.
    (I”ve put it on the URL field, when commenting). Feel free to visit it (:

    • sagito said,

      Well, the big problem about the debug information is that it is really big and above all… Kind of ininteligible… šŸ˜› Could be parsed, but it wouldn’t be easy and it would also require a two pass processing (one for parsing and other for applying).

      Comparing with the process of just saving two strings (the class and the variable names) and a pointer (to the specific variable to save), that would be a huge overhead of processing… With this approach, you don’t even notice! šŸ˜€


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: