Problem:
I have a structure that represent RGB colors. I would like to define an enum to be able to directly assign the colors to a variable.
1) Desired enum of struct:
What I would like is to define an enum that directly maps a color name to its value, just like I would do with an integer parameter or a list of items.
#include <iostream>
#include <array>
typedef struct _Color_3u8
{
std::array<unsigned char,3> rgb;
} Color_3u8;
std::ostream& operator<<(std::ostream& os, const Color_3u8& dt)
{
os << "R: " << (int)dt.rgb[0] << " | G: " << (int)dt.rgb[1] << " | B: " << (int)dt.rgb[2] << "\n";
return os;
}
typedef enum _Default_color
{
RED = (Color_3u8){255,0,0},
GREEN = (Color_3u8){0,255,0},
BLUE = (Color_3u8){0,0,255},
} Default_color;
int main()
{
Color_3u8 my_color = Default_color::RED;
std::cout << my_color;
return 0;
}
Now, the code above clearly doesn't work because enums are limited to integers.
2) constant array + enum:
One way to make it work is to use a constant array filled with the vaules and use the enum to index that array. It looks a lot clunkier since there are two separate definitions.
#include <iostream>
#include <array>
typedef struct _Color_3u8
{
std::array<unsigned char,3> rgb;
} Color_3u8;
std::ostream& operator<<(std::ostream& os, const Color_3u8& dt)
{
os << "R: " << (int)dt.rgb[0] << " | G: " << (int)dt.rgb[1] << " | B: " << (int)dt.rgb[2] << "\n";
return os;
}
typedef enum _Default_color
{
RED = 0,
GREEN = 1,
BLUE = 2,
NUM_COLORS = 3,
} Default_color;
const std::array<Color_3u8,Default_color::NUM_COLORS> c_default_colors=
{
(Color_3u8){255,0,0},
(Color_3u8){0,255,0},
(Color_3u8){0,0,255},
};
int main()
{
Color_3u8 my_color = c_default_colors[Default_color::BLUE];
std::cout << my_color;
return 0;
}
Output: "R: 0 | G: 0 | B: 255"
3) struct class:
Another way I can think of is to move the constant array from code 2) inside the structure and add a method to initialize the structure to a default color. It would work and it would be very clean, but like the 2), it would be more effort to add a color than I feels is necessary... I feel there should be a way to make code 1) work in some form.
4) Hack struct into integer
In this case since my structure is three bytes, I can convert a color to its integer representation and use that for the enum. I would like to find a way that works with structures bigger than an int as well.
Solution) namespace+constexpr
constexpr is used to declare a compile time variable ofthe right type with the right constructor. namespace wraps around the definitions and scope them.
struct Color_3u8
{
std::array<unsigned char,3> rgb;
};
std::ostream& operator<<(std::ostream& os, const Color_3u8& dt)
{
os << "R: " << (int)dt.rgb[0] << " | G: " << (int)dt.rgb[1] << " | B: " << (int)dt.rgb[2] << "\n";
return os;
}
namespace Default_color
{
constexpr Color_3u8 RED{255,0,0};
constexpr Color_3u8 GREEN{0,255,0};
constexpr Color_3u8 BLUE{0,0,255};
};
int main()
{
Color_3u8 my_color = Default_color::RED;
std::cout << my_color;
return 0;
}
Question
Is there a way in C++ to make something similar to code 1)? I would like to map a name to a structure content with just one entry in one table, like the enum does.