Can you give me the code so I can learn something different
You can add something like the following to something like `BOOST::PP_FOR_EACH'. (I don't remember what it is called.) With the above primitive you'd have something like:
Code:
STDX__ENUMERATOR__ENUMERATE(my_enum, (string1, 1) (string2, 2) (string5, 5))
for the interface. I'm just to lazy to write those primitives for such a trivial example.
I provided a means of getting a reference to a vector of all enum value names, without typing up all the enum values again.
Exactly. Hence the statement: "generate a string/value association from a string/value association".
no one has yet posted an example of a truly better method of what my program does
That could be because your program doesn't do anything. Or rather, there are a bazillion ways to do it. It isn't difficult. It is extremely trivial. This is elementary parsing and generation. It is a good stepping stone. You probably learned some good things writing it, but it isn't something to trump the very simple ideas that have been posted several times.
Have a look at this for my simple version, it ain't brilliant so it'll barf if you use value assignment in the enum list.
That's not bad at all.
Soma
Code:
#define STDX__APPLY(FUNCTION) FUNCTION
#define STDX__ENUMERATOR__DEFINITION_INITIALIZER(NAME) \
enum NAME \
{
#define STDX__ENUMERATOR__DEFINITION_CALLBACK(VARIABLE, VALUE) \
VARIABLE = VALUE,
#define STDX__ENUMERATOR__DEFINITION_FINALIZER(NAME) \
NAME##__UUIDB49527AC_8FBC_4DCD_93C7_56D122ADF592 \
};
#define STDX__ENUMERATOR__MAP_INITIALIZER(NAME) \
const char * NAME##__GET_STRING_FROM_VALUE (NAME value) \
{ \
switch(value) \
{
#define STDX__ENUMERATOR__MAP_CALLBACK(VARIABLE, VALUE) \
case VALUE: \
{ \
return(#VARIABLE); \
}
#define STDX__ENUMERATOR__MAP_FINALIZER(NAME) \
} \
return("(null)"); \
}
#define STDX__ENUMERATOR__APPLY(NAME) \
(STDX__APPLY(STDX__ENUMERATOR__##NAME##_INITIALIZER), STDX__APPLY(STDX__ENUMERATOR__##NAME##_CALLBACK), STDX__APPLY(STDX__ENUMERATOR__##NAME##_FINALIZER))
#define STDX__ENUMERATOR__ENUMERATE(NAME) \
STDX__APPLY(STDX__APPLY(NAME##__VALUE_DEFINITIONS) STDX__ENUMERATOR__APPLY(DEFINITION)) \
STDX__APPLY(STDX__APPLY(NAME##__VALUE_DEFINITIONS) STDX__ENUMERATOR__APPLY(MAP))
#include <iostream>
#define my_enum__VALUE_DEFINITIONS(INITIALIZER, CALLBACK, FINALIZER) \
INITIALIZER(my_enum) \
CALLBACK(string1, 1) \
CALLBACK(string2, 2) \
CALLBACK(string5, 5) \
FINALIZER(my_enum)
STDX__ENUMERATOR__ENUMERATE(my_enum)
int main()
{
int test;
std::cin >> test;
// too lazy to write the stream operator
std::cout << my_enum__GET_STRING_FROM_VALUE(static_cast<my_enum>(test)) << '\n';
return(0);
}