#include <iostream>
#include <string>
#include <vector>
#include <map>
// http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c
std::vector<std::string> split(const std::string &text, char sep) {
std::vector<std::string> tokens;
size_t start = 0, end = 0;
while ((end = text.find(sep, start)) != std::string::npos) {
tokens.push_back(text.substr(start, end - start));
start = end + 1;
}
tokens.push_back(text.substr(start));
return tokens;
}
std::vector<std::string> parse(const char* args)
{
return split(args, ',');
}
std::vector<std::string> parse(const char* args);
template<typename T, typename ...Ts>
std::map<T, std::string> make_map(const char* text, Ts... args)
{
std::vector<T> keys{args...};
std::vector<std::string> vals = parse(text);
auto k = keys.cbegin();
auto v = vals.cbegin();
std::map<T, std::string> r;
for (; k != keys.cend(); k++, v++) {
r.emplace(*k, *v);
}
return r;
}
#define ENUM(name, ...) \
enum name \
{ \
__VA_ARGS__ \
}; \
static std::string to_string(const name v) { \
static std::map<name, std::string> m {make_map<name>(#__VA_ARGS__, __VA_ARGS__)};\
return m.at(v); \
}
ENUM(Color, Red,Green,Blue)
namespace ns {
ENUM(Alignment, Left,Right)
}
class X
{
public:
ENUM(Y,A,B,C)
};
int main(int , char**)
{
std::cout << to_string(Red) << to_string(Blue);
std::cout << to_string(ns::Left) << to_string(ns::Right);
std::cout << X::to_string(X::A) << X::to_string(X::B);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8bWFwPgoKCi8vIGh0dHA6Ly9zdGFja292ZXJmbG93LmNvbS9xdWVzdGlvbnMvMjM2MTI5L2hvdy10by1zcGxpdC1hLXN0cmluZy1pbi1jCnN0ZDo6dmVjdG9yPHN0ZDo6c3RyaW5nPiBzcGxpdChjb25zdCBzdGQ6OnN0cmluZyAmdGV4dCwgY2hhciBzZXApIHsKICAgIHN0ZDo6dmVjdG9yPHN0ZDo6c3RyaW5nPiB0b2tlbnM7CiAgICBzaXplX3Qgc3RhcnQgPSAwLCBlbmQ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gICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBcCnsgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBcCiAgICBfX1ZBX0FSR1NfXyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBcCn07ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBcCnN0YXRpYyBzdGQ6OnN0cmluZyB0b19zdHJpbmcoY29uc3QgbmFtZSB2KSB7ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBcCiAgICBzdGF0aWMgc3RkOjptYXA8bmFtZSwgc3RkOjpzdHJpbmc+IG0ge21ha2VfbWFwPG5hbWU+KCNfX1ZBX0FSR1NfXywgX19WQV9BUkdTX18pfTtcCiAgICByZXR1cm4gbS5hdCh2KTsgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBcCn0KCgpFTlVNKENvbG9yLCBSZWQsR3JlZW4sQmx1ZSkKbmFtZXNwYWNlIG5zIHsKRU5VTShBbGlnbm1lbnQsIExlZnQsUmlnaHQpCn0KCmNsYXNzIFgKewpwdWJsaWM6CiAgICBFTlVNKFksQSxCLEMpCn07CgoKaW50IG1haW4oaW50ICwgY2hhcioqKQp7CiAgICBzdGQ6OmNvdXQgPDwgdG9fc3RyaW5nKFJlZCkgPDwgdG9fc3RyaW5nKEJsdWUpOwogICAgc3RkOjpjb3V0IDw8IHRvX3N0cmluZyhuczo6TGVmdCkgPDwgdG9fc3RyaW5nKG5zOjpSaWdodCk7CiAgICBzdGQ6OmNvdXQgPDwgWDo6dG9fc3RyaW5nKFg6OkEpIDw8IFg6OnRvX3N0cmluZyhYOjpCKTsKICAgIHJldHVybiAwOwp9Cg==