You are hereBlogs / hogliux's blog / Class Introspection in C++? Ask the physicists!
Class Introspection in C++? Ask the physicists!
In my experience as a programmer, I have noticed that there always has been demand for a good way to achieve class introspection. Then the arguing starts: some languages already have class and object introspection, like Obj-C or Python, which, though becoming increasingly more popular, are still the minority of used languages. Then we have C++, which doesn't have class introspection by default, but most people use a lot. Is there a solution to this problem? Surprisingly, physicists have developed a great tool!
Just think about having a big open source project, like a game engine, where you would like many people to contribute with a modular way to extend classes in the Game API. Most people know C++ and wouldn't have to learn a new programming language to contribute to the project. So a goal of mine has always been to find a good way to implement class introspection in C++. Of course, you can achieve this with a ton of macros, but this approach makes your code pretty ugly. Of course you can write a C++ parser which automatically inserts the macros in the right position, but writing a C++ parser seems to be a real tough job: even after searching the web for a good C++ parser, I couldn't find an open-source C++ parser without any major limitations, like the lack of parsing template classes. Physicists have had a similar problem when building the LHC at CERN. Most physicists learn to program C++ in the course of their studies, but in general are really bad at any other programming language. The hundreds of detectors at CERN needed to be programmed and the large software needed was, of course, programmed in a huge collaboration. But the software also needed to be somewhat modular, so contributors could load-in classes during runtime without disturbing other experiments. Some extremely talented programmers at CERN created a software package called root, which basically is huge collection of very powerful math and graphing libraries, but with one huge advantage: the C++ code can be interpreted! Starting root is like starting python: you enter a "shell like" command environment. Except the input is C++. So you can for instance type in stuff like
for (int i; i<10;i++) { std::cout << i << std::endl; }Of course you can interpret whole .cpp files containing classes and then call their methods from the command line. The best thing is, that you can perfectly mix compiled shared libraries and interpreted source code and more: the root C++ interpreter can also compile C++ files to a byte-code form similar to python for faster interpretation or it can automatically compile C++ with GCC to a dynamic library, This is all done in a transparent way. CERN released the C++ interpreter of root, called "CINT", in an individual package, without all the mathematical, physics and GUI libraries. Some game APIs already use the CINT, for example emergency 4. And the best of all: it's open-source! So have fun checking out CINT from root!