18 #include "exception.h" 22 #include <initializer_list> 26 #include <type_traits> 27 #include <unordered_map> 47 virtual string toString(
const T& value)
const = 0;
50 virtual T
fromString(
const string& str)
const = 0;
61 string toString(
const T& value)
const override {
63 ss << std::boolalpha << value;
70 ss >> std::boolalpha >> value;
75 if (ss.fail() || !ss.eof())
76 throw Exception(
"invalid value string: '%s'", str.c_str());
88 for (
const auto& value_str : known_values) {
89 CHECK(value_to_string_.insert(value_str).second);
90 CHECK(string_to_value_.insert({ value_str.second, value_str.first }).second);
94 string toString(
const T& value)
const override {
95 auto it = value_to_string_.find(value);
96 if (it == value_to_string_.end())
102 auto it = string_to_value_.find(str);
103 if (it == string_to_value_.end()) {
105 msg <<
"Unknown value: '" << str <<
"'\n" 106 <<
" Valid values: [";
108 for (
const auto& kv : string_to_value_) {
112 msg <<
"'" << kv.first <<
"'";
122 vector<string> values;
123 for (
const auto& kv : string_to_value_)
124 values.push_back(kv.first);
129 unordered_map<T, string> value_to_string_;
130 map<string, T> string_to_value_;
137 string toString(
const string& value)
const override {
return value; }
139 string fromString(
const string& str)
const override {
return str; }
146 auto customStringify(core::TypeTag<T>) {
147 static auto stringify =
new GenericStringify<T>();
151 inline auto customStringify(core::TypeTag<bool>) {
152 static auto stringify =
new StringifyKnownValues<bool>{
159 inline auto customStringify(core::TypeTag<string>) {
160 static auto stringify =
new StringifyStrings();
167 return customStringify(TypeTag<T>{});
173 return stringify<T>()->
toString(value);
string toString(const T &value) const override
Value to string conversion.
Definition: stringify.h:94
T fromString(const string &str) const override
String to value conversion.
Definition: stringify.h:101
vector< string > knownValues() const override
The list of known values for type T, if available (for example enumerations)
Definition: stringify.h:121
The base for exception types in the Darwin framework.
Definition: exception.h:27
Maps strings unmodified (going through I/O operators doesn't handle strings with embedded whitespace)...
Definition: stringify.h:135
Generic utilities.
Definition: exception.h:24
T fromString(const string &str)
Convenience helper to parse a string value.
Definition: stringify.h:178
Interface for converting values to/from strings (the default implementation uses iostream I/O operato...
Definition: stringify.h:42
Use iostream I/O operators for the argument type.
Definition: stringify.h:59
T fromString(const string &str) const override
String to value conversion.
Definition: stringify.h:67
string toString(const T &value)
Convenience helper to stringify a value.
Definition: stringify.h:172
Handles types with a fixed, known set of values (enumerations for example)
Definition: stringify.h:85
vector< string > knownValues()
Convenience helper to return the set of known values.
Definition: stringify.h:184
const Stringify< T > * stringify()
Returns the stringifier for type T.
Definition: stringify.h:166
vector< string > knownValues() const override
The list of known values for type T, if available (for example enumerations)
Definition: stringify.h:141
string toString(const T &value) const override
Value to string conversion.
Definition: stringify.h:61
vector< string > knownValues() const override
The list of known values for type T, if available (for example enumerations)
Definition: stringify.h:80