Jump to content

View more

Image of the Day

The night is still, but the invasion brings chaos. #screenshotsaturday #hanako #indiegame #gameart #ue4 #samurai https://t.co/cgILXuokoS
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

Scan a struct/class for members/methods

4: Adsense

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   


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   Members   


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   


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   Moderators   


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...
zlib: eJzVVLsSAiEQ6/1qCwoK i7PxA/2S2zMOZljYB1TO ZG7OhUtiduH9egZQCJH9 KcJyo4Wq9t0/RXkKmjx+ cgU4FIMWHhKCU+o/Nx2R LEPgQWLtnfcErbiEl0u4 0UrMghhZewgYcptoEF42 YMj+Z1kg+bVvqxhyo17h nUf+h4b2W4bR4XO01TJ7 qFNzA7jjbxyL71Avh6Tv odnFk4hnxxAf4w6496Kd OgH7/RxC

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.