The Battle for Wesnoth  1.19.0-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 <climits>
35 #include <ctime>
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  /** Default implementation, but defined out-of-line for efficiency reasons. */
115  /** Default implementation, but defined out-of-line for efficiency reasons. */
117  /** Default implementation, but defined out-of-line for efficiency reasons. */
119  /** Default implementation, but defined out-of-line for efficiency reasons. */
121 
122  // Numeric assignments:
125  config_attribute_value& operator=(long v) { return operator=(static_cast<long long>(v)); }
126  config_attribute_value& operator=(long long v);
127  config_attribute_value& operator=(unsigned v) { return operator=(static_cast<unsigned long long>(v)); }
128  config_attribute_value& operator=(unsigned long v) { return operator=(static_cast<unsigned long long>(v)); }
129  config_attribute_value& operator=(unsigned long long v);
131 
132  // String assignments:
133  config_attribute_value& operator=(const char *v) { return operator=(std::string(v)); }
134  config_attribute_value& operator=(const std::string &v);
135  config_attribute_value& operator=(const std::string_view &v);
137 
138  /** Calls @ref operator=(const std::string&) if @a v is not empty. */
139  void write_if_not_empty(const std::string& v);
140  void write_if_not_empty(const t_string& v);
141 
142  // Extracting as a specific type:
143  bool to_bool(bool def = false) const;
144  int to_int(int def = 0) const;
145  long long to_long_long(long long def = 0) const;
146  unsigned to_unsigned(unsigned def = 0) const;
147  std::size_t to_size_t(std::size_t def = 0) const;
148  std::time_t to_time_t(std::time_t def = 0) const;
149  double to_double(double def = 0.) const;
150  std::string str(const std::string& fallback = "") const;
151  t_string t_str() const;
152 
153  // Implicit conversions:
154  operator int() const { return to_int(); }
155  operator std::string() const { return str(); }
156  operator t_string() const { return t_str(); }
157  // This is to prevent int conversion being used when an attribute value is tested in an if statement
158  explicit operator bool() const {return to_bool(); }
159 
160  /** Tests for an attribute that was never set. */
161  bool blank() const;
162  /** Tests for an attribute that either was never set or was set to "". */
163  bool empty() const;
164 
165 
166  // Comparisons:
167  bool operator==(const config_attribute_value &other) const;
168  bool operator!=(const config_attribute_value &other) const
169  {
170  return !operator==(other);
171  }
172 
173  bool equals(const std::string& str) const;
174  // These function prevent t_string creation in case of c["a"] == "b" comparisons.
175  // The templates are needed to prevent using these function in case of c["a"] == 0 comparisons.
176  template<typename T>
177  std::enable_if_t<std::is_same_v<const std::string, std::add_const_t<T>>, bool>
178  friend operator==(const config_attribute_value &val, const T &str)
179  {
180  return val.equals(str);
181  }
182 
183  template<typename T>
184  std::enable_if_t<std::is_same_v<const char*, T>, bool>
185  friend operator==(const config_attribute_value& val, T str)
186  {
187  return val.equals(std::string(str));
188  }
189 
190  template<typename T>
191  bool friend operator==(const T& str, const config_attribute_value& val)
192  {
193  return val == str;
194  }
195 
196  template<typename T>
197  bool friend operator!=(const config_attribute_value& val, const T& str)
198  {
199  return !(val == str);
200  }
201 
202  template<typename T>
203  bool friend operator!=(const T &str, const config_attribute_value& val)
204  {
205  return !(val == str);
206  }
207 
208  // Streaming:
209  friend std::ostream& operator<<(std::ostream& os, const config_attribute_value& v);
210 
211  /**
212  * Visitor support:
213  * Applies a visitor to the underlying variant.
214  * (See the documentation for Boost.Variant.)
215  */
216  template <typename V>
217  auto apply_visitor(const V & visitor) const
218  {
219  return utils::visit(visitor, value_);
220  }
221 
222 private:
223  // Special strings.
224  static const std::string s_yes, s_no;
225  static const std::string s_true, s_false;
226 };
227 
228 #ifndef USING_BOOST_VARIANT
229 /** Specialize operator<< for monostate. Boost already does this, but the STL does not. */
230 inline std::ostream& operator<<(std::ostream& os, const std::monostate&) { return os; }
231 #endif
232 
233 namespace utils
234 {
235  std::vector<std::string> split(const config_attribute_value& val);
236 }
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.
config_attribute_value & operator=(const config_attribute_value &)
Default implementation, but defined out-of-line for efficiency reasons.
bool equals(const std::string &str) const
Checks for equality of the attribute values when viewed as strings.
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...
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
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
std::enable_if_t< std::is_same_v< const char *, T >, bool > friend operator==(const config_attribute_value &val, T str)
bool blank() const
Tests for an attribute that was never set.
static const std::string s_no
config_attribute_value & operator=(long v)
config_attribute_value & operator=(unsigned long v)
config_attribute_value()
Default implementation, but defined out-of-line for efficiency reasons.
std::enable_if_t< std::is_same_v< const std::string, std::add_const_t< T > >, bool > friend operator==(const config_attribute_value &val, const T &str)
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=(const char *v)
~config_attribute_value()
Default implementation, but defined out-of-line for efficiency reasons.
utils::variant< utils::monostate, true_false, yes_no, int, unsigned long long, double, std::string, t_string > value_type
bool friend operator==(const T &str, const config_attribute_value &val)
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 "".
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....