In order to make my code shorter and easier to change I want to replace something like
enum{ E_AAA, E_BBB, E_CCC };
static const char *strings{"AAA", "BBB", "CCC" };
With a macro, like INIT(AAA, BBB, CCC); but when I try doing a macro with variable arguments, and stringification I get an error as the arguments are not declared.
Any idea on how to do this?
Here a solution I learned a few days ago. The simplified version that attends your question is:
#define ENUM_MACRO(name, v1, v2, v3, v4, v5, v6, v7)\
enum name { v1, v2, v3, v4, v5, v6, v7};\
const char *name##Strings[] = { #v1, #v2, #v3, #v4, #v5, #v6, #v7};
ENUM_MACRO(Week, Sun, Mon, Tue, Wed, Thu, Fri, Sat);
But you can have an improved version, with a function call, like this:
#define ENUM_MACRO(name, v1, v2, v3, v4, v5, v6, v7)\
enum name { v1, v2, v3, v4, v5, v6, v7};\
const char *name##Strings[] = { #v1, #v2, #v3, #v4, #v5, #v6, #v7};\
const char *name##ToString(value) { return name##Strings[value]; }
ENUM_MACRO(Week, Sun, Mon, Tue, Wed, Thu, Fri, Sat);
This will grow to be:
enum Week { Sun, Mon, Tue, Wed, Thu, Fri, Sat};
const char *WeekStrings[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
const char *WeekToString(value) { return WeekStrings[value]; };
You can even use an offset for the first element, like this one:
#define ENUM_MACRO(name, offset, v1, v2, v3, v4, v5, v6, v7)\
enum name { v1 = offset, v2, v3, v4, v5, v6, v7};\
const char *name##Strings[] = { #v1, #v2, #v3, #v4, #v5, #v6, #v7};\
const char *name##ToString(value) { return name##Strings[value - offset ]; }
ENUM_MACRO(Week, 1, Sun, Mon, Tue, Wed, Thu, Fri, Sat);
I hope this helps.
Take care, Beco
Reference:
Print the month question, by Kush, answer by Danny Varod
You can do it with a bit of macro magic:
#define FRUITS \
etype(Unknown), \
etype(Apple), \
etype(Orange), \
etype(Banana), \
etype(Apricot), \
etype(Mango)
#define etype(x) F_##x
typedef enum { FRUITS } Fruit;
#undef etype
#define etype(x) #x
static const char *strFruit[] = { FRUITS };
Here is a test program:
#include <iostream>
#include <exception>
#include <vector>
#define FRUITS \
etype(Unknown), \
etype(Apple), \
etype(Orange), \
etype(Banana), \
etype(Apricot), \
etype(Mango)
#define etype(x) F_##x
typedef enum { FRUITS } Fruit;
#undef etype
#define etype(x) #x
static const char *strFruit[] = { FRUITS };
const char *enum2str (Fruit f)
{
return strFruit[static_cast<int>(f)];
}
Fruit str2enum (const char *f)
{
const int n = sizeof(strFruit) / sizeof(strFruit[0]);
for (int i = 0; i < n; ++i)
{
if (strcmp(strFruit[i], f) == 0)
return (Fruit) i;
}
return F_Unknown;
}
int main (int argc, char *argv[])
{
std::cout << "I like " << enum2str(F_Mango) << std::endl;
std::cout << "I do not like " << enum2str(F_Banana) << std::endl;
std::vector<char *> v;
v.push_back("Apple");
v.push_back("Mango");
v.push_back("Tomato");
for (int i = 0; i < v.size(); ++i)
{
const Fruit f = str2enum(v[i]);
if (f == F_Unknown)
std::cout << "Is " << v[i] << " a fruit?" << std::endl;
else
std::cout << v[i] << " is a fruit" << std::endl;
}
return 0;
}
It outputs:
I like Mango
I do not like Banana
Apple is a fruit
Mango is a fruit
Is Tomato a fruit?
Here is my solution:
#define FRUITS(fruit) \
fruit(Apple) \
fruit(Orange) \
fruit(Banana)
#define CREATE_ENUM(name) \
F_##name,
#define CREATE_STRINGS(name) \
#name,
The trick is that 'fruit' is an argument of the macro 'FRUITS' and will be replaced by what ever you pass to. For example:
FRUITS(CREATE_ENUM)
will expand to this:
F_Apple, F_Orange, F_Banana,
Lets create the enum and the string array:
enum fruit {
FRUITS(CREATE_ENUM)
};
const char* fruit_names[] = {
FRUITS(CREATE_STRINGS)
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With