I've hacked together something for this that kind of sucks. It needs some work probably, but it doesn't involve the use of an external program. But maybe this gives others an idea for a better solutions.
This is the code in "Enums.h":
Code:
#ifndef ENUMS_H_
#define ENUMS_H_
#include <string>
#include <map>
typedef std::map<unsigned int, std::string> EnumToName_;
/* Step 1: Add the enums to the project */
#define ENUM_START(Name) extern EnumToName_ Name##ToString; \
enum Name {
#define ENUM_END };
#define ENUM_ITEM(Name) Name
#include "Enums"
#undef ENUM_START
#undef ENUM_END
#undef ENUM_ITEM
/* Step 2: Generate the enum name lookup */
#ifdef ENUMS_H_INCLUDE_MAP
#define ENUM_START(Name) EnumToName_ Name##ToString; \
struct Name##EnumToStringInit_ { \
Name##EnumToStringInit_() { \
int ignore, ignore2 = 0; \
EnumToName_ &cmap = Name##ToString;
#define ENUM_ITEM(Name) cmap[Name] = #Name; ignore = ignore2
#define ENUM_END ; \
} \
} Name##EnumToStringInit__;
#include "Enums"
#undef ENUM_START
#undef ENUM_END
#undef ENUM_ITEM
#endif
#endif
To use it, create a file called "Enums" (no extension), and add as many enums as you like like this:
Code:
ENUM_START(NameOfEnum)
ENUM_ITEM(ItemName),
ENUM_ITEM(ItemName2),
ENUM_ITEM(ItemName3) = 500
ENUM_END
Now, include Enums.h from where ever you need any of the enums. Make sure that in exactly one of the files (Enum.cpp or the main file), you use:
Code:
#define ENUMS_H_INCLUDE_MAP
before you include Enums.h.
Now, everywhere, you can use the enum normally (though only in global scope, but I couldn't think of a better solution), or use the enum name with appended "ToString". For instance, in the above case, that would be "NameOfEnumToString".
As I said, it kind of sucks. Any better ideas for such a thing, so scoping doesn't matter (and there are a few other known flaws with this method)...
Anyways, it works like this: it defines some macro's that normally only make the enum work (adding enum { around it and stuff). When the ENUMS_H_INCLUDE_MAP is defined, it will include "Enums" twice, the second time it generates a struct that populates a map with the proper name...
Any better ideas for a hack method like this? ;-)