CASM  1.1.0
A Clusters Approach to Statistical Mechanics
io_traits.hh
Go to the documentation of this file.
1 #ifndef CASM_support_enum_io_traits
2 #define CASM_support_enum_io_traits
3 
4 #include <iostream>
5 #include <map>
6 #include <set>
7 #include <sstream>
8 #include <stdexcept>
9 #include <string>
10 #include <vector>
11 
12 #include "casm/casm_io/Help.hh"
13 
14 namespace CASM {
15 
16 class jsonParser;
17 template <typename T>
18 struct traits;
19 
23 
29 
30 template <typename ENUM>
31 std::string to_string(ENUM val);
32 
46 template <typename ENUM>
47 std::string multiline_enum_help() {
48  std::stringstream ss;
49  ss << "Options are:\n";
50  for (auto it = traits<ENUM>::strval.begin(); it != traits<ENUM>::strval.end();
51  ++it) {
52  ss << " ";
53  for (auto sit = it->second.begin(); sit != it->second.end(); sit++) {
54  if (sit != it->second.begin()) {
55  ss << " or " << *sit;
56  } else {
57  ss << *sit;
58  }
59  }
60  ss << "\n";
61  }
62  return ss.str();
63 }
64 
82 template <typename StringContainer>
83 std::string standard_singleline_help(StringContainer options,
84  std::string _default = "") {
85  std::stringstream ss;
86  ss << "Options are: {";
87  for (auto it = options.begin(); it != options.end(); ++it) {
88  if (it != options.begin()) {
89  ss << ", ";
90  }
91  ss << "'" << *it << "'";
92  if (*it == _default) {
93  ss << " (default)";
94  }
95  }
96  ss << "}";
97  return ss.str();
98 }
99 
114 template <typename ENUM>
115 std::string standard_singleline_enum_help(std::string _default = "",
116  std::string other = "") {
117  std::vector<std::string> options;
118  if (!other.empty()) {
119  options.push_back(std::string("<") + other + ">");
120  }
121  for (auto it = traits<ENUM>::strval.begin(); it != traits<ENUM>::strval.end();
122  ++it) {
123  options.push_back(to_string(it->first));
124  }
125  return standard_singleline_help(options, _default);
126 }
127 
138 template <typename ENUM>
139 std::string singleline_enum_help() {
140  return standard_singleline_enum_help<ENUM>(
141  traits<ENUM>::strval.begin()->second[0]);
142 }
143 
157 template <typename ENUM>
158 void invalid_enum_string(std::string val) {
159  std::stringstream s;
160  s << "Invalid " << traits<ENUM>::name << ": " << val << ". "
161  << singleline_help<ENUM>();
162  // serr << s.str() << "\n";
163  // serr << help<ENUM>();
164  throw std::invalid_argument(std::string("ERROR: ") + s.str());
165 }
166 
171 template <typename ENUM>
172 std::string to_string(ENUM val) {
173  return traits<ENUM>::strval.find(val)->second[0];
174 }
175 
183 template <typename ENUM>
184 std::set<ENUM> matches(const std::string &val) {
185  std::set<ENUM> res;
186  for (auto it = traits<ENUM>::strval.begin(); it != traits<ENUM>::strval.end();
187  ++it) {
188  for (auto sit = it->second.begin(); sit != it->second.end(); sit++) {
189  if (sit->substr(0, val.size()) == val) {
190  res.insert(it->first);
191  }
192  }
193  }
194  return res;
195 }
196 
201 template <typename ENUM>
202 ENUM from_string(const std::string &val) {
203  std::set<ENUM> _matches = matches<ENUM>(val);
204 
205  if (_matches.size() == 1) {
206  return *_matches.begin();
207  }
208 
209  invalid_enum_string<ENUM>(val); // throws
210  return traits<ENUM>::strval.begin()->first; // never reached
211 }
212 
213 #define ENUM_TRAITS(ENUM) \
214  template <> \
215  struct traits<ENUM> { \
216  static const std::string name; \
217  \
218  static const std::multimap<ENUM, std::vector<std::string> > strval; \
219  }; \
220  template <> \
221  inline std::string singleline_help<ENUM>() { \
222  return singleline_enum_help<ENUM>(); \
223  } \
224  template <> \
225  inline std::string multiline_help<ENUM>() { \
226  return multiline_enum_help<ENUM>(); \
227  }
228 
229 } // namespace CASM
230 
231 #endif
std::set< std::string > & s
std::string singleline_enum_help()
Print short help message describing recognized strings for allowed enum values.
Definition: io_traits.hh:139
void invalid_enum_string(std::string val)
Throw invalid_argument error for unrecognized strings.
Definition: io_traits.hh:158
ENUM from_string(const std::string &val)
Return enum class object from string representation.
Definition: io_traits.hh:202
std::set< ENUM > matches(const std::string &val)
Return all matching enum class members from string representation.
Definition: io_traits.hh:184
std::string to_string(ENUM val)
Return string representation of enum class.
Definition: io_traits.hh:172
std::string standard_singleline_enum_help(std::string _default="", std::string other="")
Print short help message describing recognized strings for allowed enum values.
Definition: io_traits.hh:115
std::string multiline_enum_help()
Print help message describing recognized strings for allowed enum values.
Definition: io_traits.hh:47
std::string standard_singleline_help(StringContainer options, std::string _default="")
Print short help message describing recognized strings for allowed enum values.
Definition: io_traits.hh:83
Main CASM namespace.
Definition: APICommand.hh:8