1

I have a huge C++ solution created as a "win32 console application" project in MVS2008, the only Additional Options during the creation having been : "precompiled header".

In this solution I now have a huge amount of enums defined in various ways : they are always defined in a .h file, but they can be defined either within a namespace, either within a class, either within a class within a namespace.

I would like to define in more elegant way (I know how to do it in a bruteforce way, but it wouldn't be DRY, right ?) a function:

void myfunc( const char * )

Which would print all "stringed" values of an enum when I pass to it the "string value" of this enum.

To be precise, if I have somewhere in my code:

enum Place
{
    Home,
    Work
};

I would like to be able to pass to my function the string "Place", and my function to print the strings "Home" and "Work".

Having really A LOT of enums, I would like, if possible, to avoid modifying them a lot, ideally not at all. I thought of a small parser, but I don't think this optimal at all.

By the way, I am RESTRICTED not to use external things different from boost, but I can use macros. I would like to be able to somehow define the concepts of "loop" over enums, and of "loop within the "strings of enums"" as well, but don't see how to do it at all.

All ideas are welcome.

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
  • C++ does not have reflection. These names exist only for the convenience of the programmer when the code is being created. At runtime, after compilation, they do not exist. – Lightness Races in Orbit Nov 07 '12 at 16:21
  • [C++: Print out enum value as text](http://stackoverflow.com/questions/3342726/c-print-out-enum-value-as-text) – Anonymous Coward Nov 07 '12 at 16:34
  • I know that there's no reflection in c++, even if in some cases one can mimic it through appropriate macros. For printing out the stuff, it is the bruteforce way, which, as precised in the question, I'd like to avoid. – Rockinsquat2 Nov 07 '12 at 17:25
  • I thought writing a parsing script somehow in MVS, that would parse the appopriate headers and create code in the appropriate cpp file before building the solution. How can one do such scripts ? (I mean not how can one parse, but how can one add whatever script to a solution, script that would for instance add a simple line somewhere in some header file ?) – Rockinsquat2 Nov 07 '12 at 17:30
  • You can dig this out of the PDB file with the Debug API. But I suppose that fits the "external things" restriction. – Hans Passant Nov 07 '12 at 18:26
  • The solution of parsing the source code for enums doesn't seem to difficult to handle. – J. Chomel Aug 03 '16 at 11:58

0 Answers0