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


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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#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: 4456


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

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.