The Battle for Wesnoth  1.19.10+dev
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
config_attribute_value.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2025
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 <iosfwd>
37 #include <string>
38 #include <vector>
39 #include <type_traits>
40 
41 /**
42  * Variant for storing WML attributes.
43  * The most efficient type is used when assigning a value. For instance,
44  * strings "yes", "no", "true", "false" will be detected and stored as boolean.
45  * @note The blank variant is only used when querying missing attributes.
46  * It is not stored in config objects.
47  */
49 {
50  /**
51  * A wrapper for bool to get the correct streaming ("true"/"false").
52  * Most visitors can simply treat this as bool.
53  */
54 public:
55  class true_false
56  {
57  bool value_;
58  public:
59  explicit true_false(bool value = false) : value_(value) {}
60  operator bool() const { return value_; }
61 
62  const std::string & str() const
63  {
65  }
66  };
67  friend std::ostream& operator<<(std::ostream &os, const true_false &v) { return os << v.str(); }
68 
69  /**
70  * A wrapper for bool to get the correct streaming ("yes"/"no").
71  * Most visitors can simply treat this as bool.
72  */
73  class yes_no
74  {
75  bool value_;
76  public:
77  explicit yes_no(bool value = false) : value_(value) {}
78  operator bool() const { return value_; }
79 
80  const std::string & str() const
81  {
83  }
84  };
85  friend std::ostream& operator<<(std::ostream &os, const yes_no &v) { return os << v.str(); }
86 private:
87  /** Visitor for checking equality. */
88  class equality_visitor;
89  /** Visitor for converting a variant to a string. */
90  class string_visitor;
91 
92  // Data will be stored in a variant, allowing for the possibility of
93  // boolean, numeric, and translatable data in addition to basic string
94  // data. For most purposes, int is the preferred type for numeric data
95  // as it is fast (often natural word size). While it is desirable to
96  // use few types (to keep the overhead low), we do have use cases for
97  // fractions (double) and huge numbers (up to the larger of LLONG_MAX
98  // and SIZE_MAX).
99  typedef utils::variant<utils::monostate,
100  true_false, yes_no,
101  int, unsigned long long, double,
102  std::string, t_string
104  /**
105  * The stored value will always use the first type from the variant
106  * definition that can represent it and that can be streamed to the
107  * correct string representation (if applicable).
108  * This is enforced upon assignment.
109  */
111 
112 public:
113  // Numeric assignments:
116  config_attribute_value& operator=(long v) { return operator=(static_cast<long long>(v)); }
117  config_attribute_value& operator=(long long v);
118  config_attribute_value& operator=(unsigned v) { return operator=(static_cast<unsigned long long>(v)); }
119  config_attribute_value& operator=(unsigned long v) { return operator=(static_cast<unsigned long long>(v)); }
120  config_attribute_value& operator=(unsigned long long v);
122 
123  // String assignments:
124  config_attribute_value& operator=(const char *v) { return operator=(std::string(v)); }
125  config_attribute_value& operator=(std::string&& v);
126  config_attribute_value& operator=(const std::string &v);
127  config_attribute_value& operator=(const std::string_view &v);
129 
130  //TODO: should this be a normal constructor?
131  template<typename T>
132  static config_attribute_value create(const T val)
133  {
135  res = val;
136  return res;
137  }
138 
139  template<typename... Args>
140  config_attribute_value& operator=(const std::chrono::duration<Args...>& v)
141  {
142  return this->operator=(v.count());
143  }
144 
145  /** Calls @ref operator=(const std::string&) if @a v is not empty. */
146  void write_if_not_empty(const std::string& v);
147  void write_if_not_empty(const t_string& v);
148 
149  // Extracting as a specific type:
150  bool to_bool(bool def = false) const;
151  int to_int(int def = 0) const;
152  long long to_long_long(long long def = 0) const;
153  unsigned to_unsigned(unsigned def = 0) const;
154  std::size_t to_size_t(std::size_t def = 0) const;
155  double to_double(double def = 0.) const;
156  std::string str(const std::string& fallback = "") const;
157  t_string t_str() const;
158 
159  bool to(const bool def) const { return to_bool(def); }
160  int to(int def) const { return to_int(def); }
161  unsigned to(unsigned def) const { return to_unsigned(def); }
162  double to(double def) const { return to_double(def); }
163  std::string to(const std::string& def) const { return str(def); }
164 
165  // Implicit conversions:
166  operator std::string() const { return str(); }
167  operator t_string() const { return t_str(); }
168 
169  /** Tests for an attribute that was never set. */
170  bool blank() const;
171  /** Tests for an attribute that either was never set or was set to "". */
172  bool empty() const;
173 
174  // Comparisons:
175  bool operator==(const config_attribute_value &other) const;
176  bool operator!=(const config_attribute_value &other) const
177  {
178  return !operator==(other);
179  }
180 
181  bool operator==(bool comp) const
182  {
183  const bool has_bool =
184  utils::holds_alternative<yes_no>(value_) ||
185  utils::holds_alternative<true_false>(value_);
186  return has_bool && to_bool() == comp;
187  }
188 
189  template<typename T>
190  bool operator==(const T& comp) const
191  {
192  if constexpr(std::is_convertible_v<T, std::string>) {
194  v = comp;
195  return *this == v;
196  } else {
197  return utils::holds_alternative<T>(value_) && this->to(T{}) == comp;
198  }
199  }
200 
201  template<typename T>
202  bool friend operator!=(const config_attribute_value& val, const T& str)
203  {
204  return !val.operator==(str);
205  }
206 
207  template<typename T>
208  bool friend operator!=(const T &str, const config_attribute_value& val)
209  {
210  return !val.operator==(str);
211  }
212 
213  // Streaming:
214  friend std::ostream& operator<<(std::ostream& os, const config_attribute_value& v);
215 
216  /**
217  * Visitor support:
218  * Applies a visitor to the underlying variant.
219  * (See the documentation for Boost.Variant.)
220  */
221  template <typename V>
222  auto apply_visitor(const V & visitor) const
223  {
224  return utils::visit(visitor, value_);
225  }
226 
227 private:
228  // Special strings.
229  static const std::string s_yes, s_no;
230  static const std::string s_true, s_false;
231 };
232 
233 #ifndef USING_BOOST_VARIANT
234 /** Specialize operator<< for monostate. Boost already does this, but the STL does not. */
235 inline std::ostream& operator<<(std::ostream& os, const std::monostate&) { return os; }
236 #endif
237 
238 namespace utils
239 {
240  std::vector<std::string> split(const config_attribute_value& val);
241 }
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::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)
static config_attribute_value create(const T val)
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....