The Battle for Wesnoth  1.19.5+dev
config_attribute_value.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * @file
18  * Definitions for the interface to Wesnoth Markup Language (WML).
19  *
20  * This module defines the interface to Wesnoth Markup Language (WML). WML is
21  * a simple hierarchical text-based file format. The format is defined in
22  * Wiki, under BuildingScenariosWML
23  *
24  * All configuration files are stored in this format, and data is sent across
25  * the network in this format. It is thus used extensively throughout the
26  * game.
27  */
28 
29 #pragma once
30 
31 #include "tstring.hpp"
32 #include "utils/variant.hpp"
33 
34 #include <chrono>
35 #include <climits>
36 #include <ctime>
37 #include <iosfwd>
38 #include <string>
39 #include <vector>
40 #include <type_traits>
41 
42 /**
43  * Variant for storing WML attributes.
44  * The most efficient type is used when assigning a value. For instance,
45  * strings "yes", "no", "true", "false" will be detected and stored as boolean.
46  * @note The blank variant is only used when querying missing attributes.
47  * It is not stored in config objects.
48  */
50 {
51  /**
52  * A wrapper for bool to get the correct streaming ("true"/"false").
53  * Most visitors can simply treat this as bool.
54  */
55 public:
56  class true_false
57  {
58  bool value_;
59  public:
60  explicit true_false(bool value = false) : value_(value) {}
61  operator bool() const { return value_; }
62 
63  const std::string & str() const
64  {
66  }
67  };
68  friend std::ostream& operator<<(std::ostream &os, const true_false &v) { return os << v.str(); }
69 
70  /**
71  * A wrapper for bool to get the correct streaming ("yes"/"no").
72  * Most visitors can simply treat this as bool.
73  */
74  class yes_no
75  {
76  bool value_;
77  public:
78  explicit yes_no(bool value = false) : value_(value) {}
79  operator bool() const { return value_; }
80 
81  const std::string & str() const
82  {
84  }
85  };
86  friend std::ostream& operator<<(std::ostream &os, const yes_no &v) { return os << v.str(); }
87 private:
88  /** Visitor for checking equality. */
89  class equality_visitor;
90  /** Visitor for converting a variant to a string. */
91  class string_visitor;
92 
93  // Data will be stored in a variant, allowing for the possibility of
94  // boolean, numeric, and translatable data in addition to basic string
95  // data. For most purposes, int is the preferred type for numeric data
96  // as it is fast (often natural word size). While it is desirable to
97  // use few types (to keep the overhead low), we do have use cases for
98  // fractions (double) and huge numbers (up to the larger of LLONG_MAX
99  // and SIZE_MAX).
100  typedef utils::variant<utils::monostate,
101  true_false, yes_no,
102  int, unsigned long long, double,
103  std::string, t_string
105  /**
106  * The stored value will always use the first type from the variant
107  * definition that can represent it and that can be streamed to the
108  * correct string representation (if applicable).
109  * This is enforced upon assignment.
110  */
112 
113 public:
114  // Numeric assignments:
117  config_attribute_value& operator=(long v) { return operator=(static_cast<long long>(v)); }
118  config_attribute_value& operator=(long long v);
119  config_attribute_value& operator=(unsigned v) { return operator=(static_cast<unsigned long long>(v)); }
120  config_attribute_value& operator=(unsigned long v) { return operator=(static_cast<unsigned long long>(v)); }
121  config_attribute_value& operator=(unsigned long long v);
123 
124  // String assignments:
125  config_attribute_value& operator=(const char *v) { return operator=(std::string(v)); }
126  config_attribute_value& operator=(std::string&& v);
127  config_attribute_value& operator=(const std::string &v);
128  config_attribute_value& operator=(const std::string_view &v);
130 
131  template<typename... Args>
132  config_attribute_value& operator=(const std::chrono::duration<Args...>& v)
133  {
134  return this->operator=(v.count());
135  }
136 
137  /** Calls @ref operator=(const std::string&) if @a v is not empty. */
138  void write_if_not_empty(const std::string& v);
139  void write_if_not_empty(const t_string& v);
140 
141  // Extracting as a specific type:
142  bool to_bool(bool def = false) const;
143  int to_int(int def = 0) const;
144  long long to_long_long(long long def = 0) const;
145  unsigned to_unsigned(unsigned def = 0) const;
146  std::size_t to_size_t(std::size_t def = 0) const;
147  std::time_t to_time_t(std::time_t def = 0) const;
148  double to_double(double def = 0.) const;
149  std::string str(const std::string& fallback = "") const;
150  t_string t_str() const;
151 
152  bool to(const bool def) const { return to_bool(def); }
153  int to(int def) const { return to_int(def); }
154  unsigned to(unsigned def) const { return to_unsigned(def); }
155  double to(double def) const { return to_double(def); }
156  std::string to(const std::string& def) const { return str(def); }
157 
158  // Implicit conversions:
159  operator std::string() const { return str(); }
160  operator t_string() const { return t_str(); }
161 
162  /** Tests for an attribute that was never set. */
163  bool blank() const;
164  /** Tests for an attribute that either was never set or was set to "". */
165  bool empty() const;
166 
167  // Comparisons:
168  bool operator==(const config_attribute_value &other) const;
169  bool operator!=(const config_attribute_value &other) const
170  {
171  return !operator==(other);
172  }
173 
174  bool operator==(bool comp) const
175  {
176  const bool has_bool =
177  utils::holds_alternative<yes_no>(value_) ||
178  utils::holds_alternative<true_false>(value_);
179  return has_bool && to_bool() == comp;
180  }
181 
182  template<typename T>
183  bool operator==(const T& comp) const
184  {
185  if constexpr(std::is_convertible_v<T, std::string>) {
187  v = comp;
188  return *this == v;
189  } else {
190  return utils::holds_alternative<T>(value_) && this->to(T{}) == comp;
191  }
192  }
193 
194  template<typename T>
195  bool friend operator!=(const config_attribute_value& val, const T& str)
196  {
197  return !val.operator==(str);
198  }
199 
200  template<typename T>
201  bool friend operator!=(const T &str, const config_attribute_value& val)
202  {
203  return !val.operator==(str);
204  }
205 
206  // Streaming:
207  friend std::ostream& operator<<(std::ostream& os, const config_attribute_value& v);
208 
209  /**
210  * Visitor support:
211  * Applies a visitor to the underlying variant.
212  * (See the documentation for Boost.Variant.)
213  */
214  template <typename V>
215  auto apply_visitor(const V & visitor) const
216  {
217  return utils::visit(visitor, value_);
218  }
219 
220 private:
221  // Special strings.
222  static const std::string s_yes, s_no;
223  static const std::string s_true, s_false;
224 };
225 
226 #ifndef USING_BOOST_VARIANT
227 /** Specialize operator<< for monostate. Boost already does this, but the STL does not. */
228 inline std::ostream& operator<<(std::ostream& os, const std::monostate&) { return os; }
229 #endif
230 
231 namespace utils
232 {
233  std::vector<std::string> split(const config_attribute_value& val);
234 }
A wrapper for bool to get the correct streaming ("true"/"false").
A wrapper for bool to get the correct streaming ("yes"/"no").
const std::string & str() const
Variant for storing WML attributes.
friend std::ostream & operator<<(std::ostream &os, const true_false &v)
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...
bool operator==(bool comp) const
config_attribute_value & operator=(unsigned v)
std::string str(const std::string &fallback="") const
std::time_t to_time_t(std::time_t def=0) const
std::string to(const std::string &def) 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
unsigned to(unsigned def) const
bool blank() const
Tests for an attribute that was never set.
static const std::string s_no
bool operator==(const T &comp) const
config_attribute_value & operator=(long v)
config_attribute_value & operator=(unsigned long v)
bool to(const bool def) const
bool friend operator!=(const config_attribute_value &val, const T &str)
bool operator!=(const config_attribute_value &other) const
bool to_bool(bool def=false) const
bool friend operator!=(const T &str, const config_attribute_value &val)
config_attribute_value & operator=(bool v)
config_attribute_value & operator=(const char *v)
utils::variant< utils::monostate, true_false, yes_no, int, unsigned long long, double, std::string, t_string > value_type
double to(double def) const
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 "".
config_attribute_value & operator=(const std::chrono::duration< Args... > &v)
friend std::ostream & operator<<(std::ostream &os, const yes_no &v)
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
std::ostream & operator<<(std::ostream &os, const std::monostate &)
Specialize operator<< for monostate.
std::vector< std::string > split(const config_attribute_value &val)
MacOS doesn't support std::visit when targing MacOS < 10.14 (currently we target 10....