How to easily map c++ enums to strings
If you want the enum names themselves as strings, see this post.
Otherwise, a std::map<MyEnum, char const*>
will work nicely. (No point in copying your string literals to std::strings in the map)
For extra syntactic sugar, here's how to write a map_init class. The goal is to allow
std::map<MyEnum, const char*> MyMap;
map_init(MyMap)
(eValue1, "A")
(eValue2, "B")
(eValue3, "C")
;
The function template <typename T> map_init(T&)
returns a map_init_helper<T>
.
map_init_helper<T>
stores a T&, and defines the trivial map_init_helper& operator()(typename T::key_type const&, typename T::value_type const&)
. (Returning *this
from operator()
allows the chaining of operator()
, like operator<<
on std::ostream
s)
template<typename T> struct map_init_helper
{
T& data;
map_init_helper(T& d) : data(d) {}
map_init_helper& operator() (typename T::key_type const& key, typename T::mapped_type const& value)
{
data[key] = value;
return *this;
}
};
template<typename T> map_init_helper<T> map_init(T& item)
{
return map_init_helper<T>(item);
}
Since the function and helper class are templated, you can use them for any map, or map-like structure. I.e. it can also add entries to std::unordered_map
If you don't like writing these helpers, boost::assign offers the same functionality out of the box.
MSalters solution is a good one but basically re-implements boost::assign::map_list_of
. If you have boost, you can use it directly:
#include <boost/assign/list_of.hpp>
#include <boost/unordered_map.hpp>
#include <iostream>
using boost::assign::map_list_of;
enum eee { AA,BB,CC };
const boost::unordered_map<eee,const char*> eeeToString = map_list_of
(AA, "AA")
(BB, "BB")
(CC, "CC");
int main()
{
std::cout << " enum AA = " << eeeToString.at(AA) << std::endl;
return 0;
}
Auto-generate one form from another.
Source:
enum {
VALUE1, /* value 1 */
VALUE2, /* value 2 */
};
Generated:
const char* enum2str[] = {
"value 1", /* VALUE1 */
"value 2", /* VALUE2 */
};
If enum values are large then a generated form could use unordered_map<> or templates as suggested by Constantin.
Source:
enum State{
state0 = 0, /* state 0 */
state1 = 1, /* state 1 */
state2 = 2, /* state 2 */
state3 = 4, /* state 3 */
state16 = 0x10000, /* state 16 */
};
Generated:
template <State n> struct enum2str { static const char * const value; };
template <State n> const char * const enum2str<n>::value = "error";
template <> struct enum2str<state0> { static const char * const value; };
const char * const enum2str<state0>::value = "state 0";
Example:
#include <iostream>
int main()
{
std::cout << enum2str<state16>::value << std::endl;
return 0;
}