Scan a struct/class for members/methods

Started by
2 comments, last by fastcall22 12 years, 1 month ago
Hey everyone smile.png

Is it possible to scan a class in code to obtain information about members in that class?

For example, lets pretend I want to make a function that writes all of the data members and member functions of a class to a text file, along with their types, sizes, values, etc.

My calling code would look something like this:
class SomeClass
{
public:
int CanYouSeeMe;
int IBetYouCant;

void MyMethod();
};

template <typename vtype>
void WriteClassInfoToFile(vtype &class_ref); // the parameter might be different

..... in some function somewhere:

SomeClass test;
WriteClassInfoToFile( test );


My resulting text file would look something like this:

Class: SomeClass
Size: 8 bytes

Members:
CanYouSeeMe - Type: int
IBetYouCant - Type int

Methods:
1. MyMethod() - return type: void


Is something similar to this possible? My real reason for wanting to do this is to be capable of adding a real game-code structure to a scripting engine more easily. My scripting engine is capable of defining instances of classes, using them to change members, etc, but each class must be defined into the scripting engine by manually defining each structure member.

Thanks for any information smile.png
Advertisement
It is not possible. You will have to manually register with the scripts whatever you want to be visible by the scripts.
You can make this easier via macros however.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

What you want is some kind of reflection system which is not in C++ natively (RTTI is not enough for your use).
There is some open source reflection system on the internet.
However, since you want to use in script binding only, did you check if there is C++ binding library there?
What language are you binding for?

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

If you're using Visual Studio 2010* or later, then you're in luck: The C++ Intellisense for Visual Studio 2010 and later uses SQL Server Compact to store Intellisense's analysis. You can connect to your project's SDF file and write T-SQL queries and do all sorts of fun stuff: Query members of a class/struct (even those generated by macros), query its methods (return types and parameters), query its parent types, and basically everything else the Intellisense can provide from inside the IDE. You could possibly write a program that, before building your program, extracts Intellisense information from your project and automagically generate/update code for reflection or serialization for all of the types (barring special corner cases) in your program.

Note that with this method, you will need to fallback to manually providing the code such a tool might generate if and when you start working on your project from a different IDE.

*Not sure if Visual Sudio 2008 uses SQL Server Compact for Intellisense -- it is worth noting intellisense for C++ in 2010 and later have improved significantly...

This topic is closed to new replies.

Advertisement