The Battle for Wesnoth  1.19.7+dev
variable_info.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2005 - 2024
3  by Philippe Plantier <ayin@anathas.org>
4  Copyright (C) 2003 by David White <dave@whitevine.net>
5  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY.
13 
14  See the COPYING file for more details.
15 */
16 
17 #pragma once
18 
19 #include "config.hpp"
20 
21 #include <string>
22 #include <type_traits>
23 
24 class invalid_variablename_exception : public std::exception
25 {
26 public:
27  invalid_variablename_exception() : std::exception() {}
28 
29  const char* what() const noexcept
30  {
31  return "invalid_variablename_exception";
32  }
33 };
34 
35 // NOTE: the detail file needs invalid_variablename_exception to be available,
36 // so include this after declaring it.
37 #include "variable_info_detail.hpp"
38 
39 /** Information on a WML variable. */
40 template<typename V>
42 {
43 public:
44  variable_info(const std::string& varname, maybe_const_t<config, V>& vars) noexcept;
45 
46  std::string get_error_message() const;
47 
48  bool explicit_index() const;
49 
50  /** @throws invalid_variablename_exception */
51  bool exists_as_attribute() const;
52 
53  /** @throws invalid_variablename_exception */
54  bool exists_as_container() const;
55 
56  /**
57  * If instantiated with vi_policy_const, the lifetime of the returned
58  * const attribute_value reference might end with the lifetime of this object.
59  *
60  * @throws invalid_variablename_exception
61  */
63 
64  /**
65  * If instantiated with vi_policy_const, the lifetime of the returned
66  * const attribute_value reference might end with the lifetime of this object.
67  *
68  * @throws invalid_variablename_exception
69  */
71 
72  /**
73  * If instantiated with vi_policy_const, the lifetime of the returned
74  * const attribute_value reference might end with the lifetime of this object.
75  *
76  * Range may be empty
77  * @throws invalid_variablename_exception
78  */
80 
81 protected:
82  std::string name_;
84  bool valid_;
85 
86  void throw_on_invalid() const;
87  void calculate_value();
88 };
89 
90 /**
91  * Additional functionality for a non-const variable_info.
92  * @todo: should these functions take a reference?
93  */
94 template<typename V>
96 {
97 public:
98  variable_info_mutable(const std::string& name, config& game_vars)
99  : variable_info<V>(name, game_vars)
100  {
101  static_assert(!std::is_same_v<
102  variable_info_implementation::vi_policy_const, std::remove_const_t<V>>,
103  "variable_info_mutable cannot be specialized with 'vi_policy_const'"
104  );
105  }
106 
107  /**
108  * @returns The new appended range.
109  * @throws invalid_variablename_exception
110  */
111  config::child_itors append_array(std::vector<config> children) const;
112 
113  /**
114  * @returns The new inserted range.
115  * @throws invalid_variablename_exception
116  */
117  config::child_itors insert_array(std::vector<config> children) const;
118 
119  /**
120  * @returns The new range.
121  * @throws invalid_variablename_exception
122  */
123  config::child_itors replace_array(std::vector<config> children) const;
124 
125  /**
126  * @throws invalid_variablename_exception
127  */
128  void merge_array(std::vector<config> children) const;
129 
130  /**
131  * Clears the value this object points to.
132  *
133  * @param only_tables If true, will not clear attribute values.
134  * @throws invalid_variablename_exception
135  */
136  void clear(bool only_tables = false) const;
137 };
138 
139 /** 'Create if nonexistent' access. */
141 
142 /** 'Throw if nonexistent' access. */
144 
145 /**
146  * Read-only access.
147  *
148  * NOTE: in order to easily mark certain types in this specialization as const we specify
149  * the policy as const here. This allows the use of const_clone.
150  */
152 
154 public:
155  virtual ~variable_set() {}
156  virtual config::attribute_value get_variable_const(const std::string &id) const = 0;
157  virtual variable_access_const get_variable_access_read(const std::string& varname) const = 0;
158 };
Variant for storing WML attributes.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
boost::iterator_range< child_iterator > child_itors
Definition: config.hpp:281
const char * what() const noexcept
Additional functionality for a non-const variable_info.
config::child_itors insert_array(std::vector< config > children) const
variable_info_mutable(const std::string &name, config &game_vars)
void merge_array(std::vector< config > children) const
config::child_itors replace_array(std::vector< config > children) const
void clear(bool only_tables=false) const
Clears the value this object points to.
config::child_itors append_array(std::vector< config > children) const
Information on a WML variable.
maybe_const_t< config::child_itors, V > as_array() const
If instantiated with vi_policy_const, the lifetime of the returned const attribute_value reference mi...
maybe_const_t< config::attribute_value, V > & as_scalar() const
If instantiated with vi_policy_const, the lifetime of the returned const attribute_value reference mi...
std::string get_error_message() const
void throw_on_invalid() const
std::string name_
bool exists_as_container() const
variable_info(const std::string &varname, maybe_const_t< config, V > &vars) noexcept
void calculate_value()
maybe_const_t< config, V > & as_container() const
If instantiated with vi_policy_const, the lifetime of the returned const attribute_value reference mi...
bool explicit_index() const
bool exists_as_attribute() const
variable_info_implementation::variable_info_state< V > state_
virtual config::attribute_value get_variable_const(const std::string &id) const =0
virtual ~variable_set()
virtual variable_access_const get_variable_access_read(const std::string &varname) const =0
Definitions for the interface to Wesnoth Markup Language (WML).
typename variable_info_implementation::maybe_const< T, V >::type maybe_const_t
Helper template alias for maybe_const, defined at global scope for convenience.