I wanted to know if there any manipulator which will print the background_color as a string .
A simple answer: to the best of my knowledge there is no manipulator that will do this for you directly. However, there are plenty of tools and methods to achieve what you want. One had already suggested using an std::map
. That is one valid possible way and there are many others each having their own pros and cons. It is up to you to balance out the differences.
Instead of using std::map
I chose to use a static const std::array
. I also decided to place the enum inside of a struct that contains a constructor and two variables. The type it is and a string for its name. Then I created an overloaded operator<<()
to work on my struct for its output.
Here is what my code looks like:
Test.h
#include <array>
#inlucde <iostream>
#include <string>
// must match the size and order of
// the enumeration in the Color Struct
static const std::array<std::string, 8> name {
"Black",
"Red",
"Orange",
"Yellow",
"Green",
"Blue",
"White",
"Invalid"
};
struct Color {
enum Type {
BLACK,
RED,
ORANGE,
YELLOW,
GREEN,
BLUE,
WHITE,
INVALID,
} type_;
std::string name_;
explicit Color(Color::Type ty = INVALID) : type_(ty), name_(name[ty] ) {}
};
std::ostream& operator<<(std::ostream& os, const Color& color );
Test.cpp
#include "Test.h"
std::ostream& operator<<(std::ostream& os, const Color& color) {
return os << color.name_;
}
main.cpp
#include "Test.h"
int main() {
Color blank;
Color red(Color::RED);
Color white(Color::WHITE);
Color blue(Color::BLUE);
std::cout << blank << " "
<< red << " "
<< white << " "
<< blue << '\n';
return EXIT_SUCCESS;
}
Output
Invalid Red White Blue
I choose array over map for it has faster access time. Also I chose to make it a const static so that it would only have to be initialized once! These are the pros of my method.
The con is that the array is const and can not be modified, and you can not insert into an array. However because we are dealing with an enumeration, this shouldn't be an issue because you can not add to an enum after its been declared for it is an integral type and not a container.
Another pro to this method is that you do not have to worry about using a switch statement. The only con here is the storage space of two variables, an enum type and a string, but this should not create a large impact for a memory footprint unless if you are storing millions or billions of colors.
The design structure above is good for when you have a limited or a specified amount of objects that you know your application is going to support. Even if you declared 100 predefined colors, this approach is still manageable.
On the other hand if you are storing thousands or tens of thousands of different colors then an std::map might make more sense. In that case, you could declare a static map<unsigned, string>
and move it inside of the class, then have a static function that will initialize the map with all of the string names for each of the different object types you will support.