List structure members?

Is there a way to list structure elements (struct | class) in C ++ or C? I need to get the name, type and value of a member. I used to use the following code example in a small project where the variables were global. The problem that I have now is that the set of values ​​needs to be copied from the GUI to the environment of objects, files, and virtual machines. I could create another reflection system of β€œpoor men” or, hopefully, something better that I have not thought about. Anyone have any thoughts?

EDIT: I know that C ++ has no reflection.

union variant_t { unsigned int ui; int i; double d; char* s; }; struct pub_values_t { const char* name; union variant_t* addr; char type; // 'I' is int; 'U' is unsigned int; 'D' is double; 'S' is string }; #define pub_v(n,t) #n,(union variant_t*)&n,t struct pub_values_t pub_values[] = { pub_v(somemember, 'D'), pub_v(somemember2, 'D'), pub_v(somemember3, 'U'), ... }; const int no_of_pub_vs = sizeof(pub_values) / sizeof(struct pub_values_t); 
+4
source share
6 answers

To state the obvious, there is no reflection in C or C ++. Therefore, there is no reliable way to list member variables (by default).

If you have control over the data structure, you can try std::vector<boost::any> or std::map<std::string, boost::any> , and then add all the member variables to the vector / map .

Of course, this means that all of your variables are likely to be on the heap, so defeat will be achieved with this approach. Using the std :: map approach, this means that you will have a peculiar reflection of the "poor."

+4
source

You can specify your types in an intermediate file and generate C ++ code from this, something like COM classes that can be generated from idl files. The generated code provides reflection capabilities for these types.

I did something similar two different ways for different projects:

  • custom file processed by Ruby script to generate
  • define types as C # types, use C # reflection to get all the information and generate C ++ from this (it sounds confusing, but works surprisingly well, and type definition records are very similar to C ++ definition records)
+3
source

Boost has a Variant library ready to use that can fit your needs.

+2
source

The easiest way is to switch to Objective-C OR Objective-C ++. These languages ​​have good introspection and are fully compatible with C / C ++ sources.

You can also use m4 / cog / ... to simultaneously generate and describe it from some meta description.

+2
source

It looks like you are building some kind of debugger. I think this should be doable if you make sure that you create pdb files when you create your executable.

Not sure in what context you want to make this enumeration, but in your program you can call functions from Microsoft dbghelp.dll to get type information from variables, etc. (I'm assuming you are using windows, which of course cannot be the case)

Hope this helps you a little further.

Hooray!

0
source

Since C ++ has no built-in reflection, you can only get information about how you separately train your program about the contents of the structure.

This can be either by creating your structure from a format that you can use after that to find out information about strcture, or by parsing your .h file to extract information about the structure.

0
source

All Articles