CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
EnumIO.hh
Go to the documentation of this file.
1 #ifndef CASM_EnumIO
2 #define CASM_EnumIO
3 
4 #include <iostream>
5 #include <string>
6 #include <sstream>
7 #include <stdexcept>
9 
10 namespace CASM {
11 
15 
21 
22  template <typename T>
23  struct traits {};
24 
37  template<typename ENUM>
38  std::string help() {
39  std::stringstream ss;
40  ss << "Options are:\n";
41  for(auto it = traits<ENUM>::strval.begin(); it != traits<ENUM>::strval.end(); ++it) {
42  ss << " ";
43  for(auto sit = it->second.begin(); sit != it->second.end(); sit++) {
44  if(sit != it->second.begin()) {
45  ss << " or " << *sit;
46  }
47  else {
48  ss << *sit;
49  }
50  }
51  ss << "\n";
52  }
53  return ss.str();
54  }
55 
69  template<typename ENUM>
70  void invalid_enum_string(std::string val, std::ostream &serr) {
71  std::stringstream s;
72  s << "Invalid " << traits<ENUM>::name << ": " << val;
73  serr << s.str() << "\n";
74  serr << help<ENUM>();
75  throw std::invalid_argument(std::string("ERROR: ") + s.str());
76  }
77 
82  template<typename ENUM>
83  std::string to_string(ENUM val) {
84  return traits<ENUM>::strval.find(val)->second[0];
85  }
86 
91  template<typename ENUM>
92  ENUM from_string(const std::string &val) {
93  for(auto it = traits<ENUM>::strval.begin(); it != traits<ENUM>::strval.end(); ++it) {
94  for(auto sit = it->second.begin(); sit != it->second.end(); sit++) {
95  if(*sit == val) {
96  return it->first;
97  }
98  }
99  }
100 
101  invalid_enum_string<ENUM>(val, std::cerr);
102  return traits<ENUM>::strval.begin()->first;
103  }
104 
105 #define ENUM_TRAITS(ENUM) \
106  template<> \
107  struct traits<ENUM> { \
108  \
109  static const std::string name; \
110  \
111  static const std::multimap<ENUM, std::vector<std::string> > strval; \
112  \
113  }; \
114 
115 #define ENUM_IO(ENUM) \
116  inline std::ostream &operator<<(std::ostream &sout, const ENUM& val) { \
117  sout << to_string<ENUM>(val); \
118  return sout; \
119  } \
120  \
121  inline std::istream &operator>>(std::istream &sin, ENUM& val) { \
122  std::string s; \
123  sin >> s; \
124  val = from_string<ENUM>(s); \
125  return sin; \
126  } \
127  \
128  inline jsonParser &to_json(const ENUM &val, jsonParser &json) { \
129  return to_json(to_string<ENUM>(val), json); \
130  } \
131  \
132  inline void from_json(ENUM& val, const jsonParser& json) { \
133  val = from_string<ENUM>(json.get<std::string>()); \
134  } \
135 
136 
137 }
138 
139 #endif
std::string help()
Print help message describing recognized strings for allowed enum values.
Definition: EnumIO.hh:38
ENUM from_string(const std::string &val)
Return enum class object from string representation.
Definition: EnumIO.hh:92
Main CASM namespace.
Definition: complete.cpp:8
std::string to_string(ENUM val)
Return string representation of enum class.
Definition: EnumIO.hh:83
void invalid_enum_string(std::string val, std::ostream &serr)
Throw invalid_argument error for unrecognized strings.
Definition: EnumIO.hh:70