31 #define ERR_CF LOG_STREAM(err, log_config)
32 #define DBG_CF LOG_STREAM(debug, log_config)
56 return *
this =
static_cast<unsigned long long>(v);
59 if(v >= std::numeric_limits<int>::min()) {
61 return *
this =
static_cast<int>(v);
69 value_ =
static_cast<double>(v);
76 if(v <= std::numeric_limits<int>::max()) {
77 return *
this =
static_cast<int>(v);
89 unsigned long long ull =
static_cast<unsigned long long>(v);
90 if(
static_cast<double>(ull) == v) {
95 int i =
static_cast<int>(v);
96 if(
static_cast<double>(
i) == v) {
116 template<
typename To>
117 bool from_string_verify(
const std::string& source, To& res)
120 std::istringstream in_str(source);
121 if(!(in_str >> res)) {
126 std::ostringstream out_str;
128 return out_str.str() == source;
162 double d = strtod(v.c_str(), &eptr);
169 unsigned long long ull = 0;
170 if(from_string_verify<unsigned long long>(v, ull)) {
176 if(from_string_verify<int>(v,
i)) {
184 std::ostringstream tester;
186 if(tester.str() == v) {
212 return *
this = v.
str();
248 class attribute_numeric_visitor
249 #ifdef USING_BOOST_VARIANT
250 :
public boost::static_visitor<T>
255 attribute_numeric_visitor(T def) : def_(def) {}
257 T operator()(
const utils::monostate&)
const {
return def_; }
258 T operator()(
bool)
const {
return def_; }
259 T operator()(
int i)
const {
return static_cast<T
>(
i); }
260 T operator()(
unsigned long long u)
const {
return static_cast<T
>(u); }
261 T operator()(
double d)
const {
return static_cast<T
>(
d); }
262 T operator()(
const std::string&
s)
const {
return lexical_cast_default<T>(
s, def_); }
263 T operator()(
const t_string&)
const {
return def_; }
277 return apply_visitor(attribute_numeric_visitor<long long>(def));
282 return apply_visitor(attribute_numeric_visitor<unsigned>(def));
287 return apply_visitor(attribute_numeric_visitor<std::size_t>(def));
292 return apply_visitor(attribute_numeric_visitor<std::time_t>(def));
297 return apply_visitor(attribute_numeric_visitor<double>(def));
302 #ifdef USING_BOOST_VARIANT
303 :
public boost::static_visitor<std::string>
315 std::string
operator()(
unsigned long long u)
const {
return std::to_string(u); }
316 std::string
operator()(
double d)
const {
return lexical_cast<std::string>(
d); }
340 return utils::holds_alternative<utils::monostate>(
value_);
352 if(
const std::string*
p = utils::get_if<std::string>(&
value_)) {
361 #ifdef USING_BOOST_VARIANT
362 :
public boost::static_visitor<bool>
367 template<
typename T,
typename U>
383 return bool(lhs) == bool(rhs);
388 return bool(lhs) == bool(rhs);
Visitor handling equality checks.
bool operator()(const yes_no &lhs, const true_false &rhs) const
bool operator()(const T &, const U &) const
bool operator()(const true_false &lhs, const yes_no &rhs) const
bool operator()(const T &lhs, const T &rhs) const
Visitor for converting a variant to a string.
std::string operator()(unsigned long long u) const
std::string operator()(int i) const
const std::string default_
std::string operator()(const utils::monostate &) const
std::string operator()(const t_string &s) const
string_visitor(const std::string &fallback)
std::string operator()(const true_false &b) const
std::string operator()(const yes_no &b) const
std::string operator()(double d) const
std::string operator()(const std::string &s) const
A wrapper for bool to get the correct streaming ("true"/"false").
A wrapper for bool to get the correct streaming ("yes"/"no").
Variant for storing WML attributes.
static const std::string s_false
value_type value_
The stored value will always use the first type from the variant definition that can represent it and...
std::string str(const std::string &fallback="") const
std::time_t to_time_t(std::time_t def=0) const
int to_int(int def=0) const
static const std::string s_true
auto apply_visitor(const V &visitor) const
Visitor support: Applies a visitor to the underlying variant.
unsigned to_unsigned(unsigned def=0) const
static const std::string s_yes
bool blank() const
Tests for an attribute that was never set.
static const std::string s_no
bool to_bool(bool def=false) const
config_attribute_value & operator=(bool v)
bool operator==(const config_attribute_value &other) const
Checks for equality of the attribute values when viewed as strings.
void write_if_not_empty(const std::string &v)
Calls operator=(const std::string&) if v is not empty.
bool empty() const
Tests for an attribute that either was never set or was set to "".
long long to_long_long(long long def=0) const
std::size_t to_size_t(std::size_t def=0) const
double to_double(double def=0.) const
bool translatable() const
const std::string & str() const
std::ostream & operator<<(std::ostream &os, const config_attribute_value &v)
static lg::log_domain log_config("config")
Definitions for the interface to Wesnoth Markup Language (WML).
New lexcical_cast header.
Standard logging facilities (interface).
std::vector< std::string > split(const config_attribute_value &val)
static map_location::direction s