• Advertisement
Sign in to follow this  

Scan a struct/class for members/methods

This topic is 2232 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement