Jump to content

  • Log In with Google      Sign In   
  • Create Account

Scan a struct/class for members/methods

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Kest   Members   -  Reputation: 547


Posted 16 March 2012 - 07:05 PM

Hey everyone Posted Image

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
	 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

CanYouSeeMe - Type: int
IBetYouCant - Type int

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 Posted Image

#2 L. Spiro   Crossbones+   -  Reputation: 23975


Posted 16 March 2012 - 07:15 PM

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

#3 wqking   Members   -  Reputation: 756


Posted 16 March 2012 - 08:53 PM

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?

cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.

#4 fastcall22   Crossbones+   -  Reputation: 8836


Posted 16 March 2012 - 10:32 PM

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...
gzip: H4sIAAAAAAAEAG1QTUvEMBC991e8nvaiFfYoS7yo sLCo6MnjtJ1ugmkiyWRL/72z3T1YEQIJ8z4zA2Xp yPvt1qBpGrRFIJZkk9FyRyUzHCbKIHgn4hnZOrm1 TD0mG0HCCs+QGDGWziKXI6Wm2n++GYwUVH2mrGEE PnGCVQ8K8+JYfXA6URDEQfMZh5h6g5eoAlWJdeEI bbH2qYZf7XMUfw8f/Q0oMeZYNL9/WHF0uFEshvMr XYujd9SycFb+F18QcSOvlJauZ8ejqevdnV7/d550 e0t6prmunh73Bu+vz4c/XUeOQXfJgvKNkhf95U8/ Dtgmy5IBAAA=

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.