The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
variable.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 by David White <[email protected]>
3  Copyright (C) 2005 - 2016 by Philippe Plantier <[email protected]>
4 
5  Part of the Battle for Wesnoth Project http://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 #ifndef VARIABLE_H_INCLUDED
17 #define VARIABLE_H_INCLUDED
18 
19 #include "config.hpp"
20 
21 #include <boost/shared_ptr.hpp>
22 
23 #include <utility>
24 
25 class unit_map;
26 
27 /**
28  * A variable-expanding proxy for the config class. This class roughly behaves
29  * as a constant config object, but automatically expands variables.
30  *
31  * When dealing with a vconfig, keep in mind its lifetime. By default, vconfigs
32  * do not maintain a copy their data; if you need a vconfig to stick around,
33  * either construct it with manage_memory=true or call make_safe(). This will
34  * cause the vconfig to make a copy of the underlying config object.
35  */
36 class vconfig
37 {
38 private:
39 
40 /*
41  * Starting with the rc of gcc 4.6 the code failed to compile due to the
42  * missing default constructor for vconfig. Not entirely sure whether it's a
43  * bug in gcc or not. For now make the code conditional.
44  */
45 #if __GNUC__ == 4 && __GNUC_MINOR__ == 6 && defined(__GXX_EXPERIMENTAL_CXX0X__)
46  template<class T1, class T2>
47  friend class std::pair;
48 #endif
49 
50  vconfig();
52 public:
53  /// Constructor from a config.
54  /// Equivalent to vconfig(cfg, false).
55  /// Do not use if the vconfig will persist after @a cfg is destroyed!
56  explicit vconfig(const config &cfg) : cache_(), cfg_(&cfg) {}
57  vconfig(const config &cfg, bool manage_memory);
58  ~vconfig();
59 
60  static vconfig empty_vconfig(); // Valid to dereference. Contains nothing
61  static vconfig unconstructed_vconfig(); // Must not be dereferenced
62 
63  /// A vconfig evaluates to true iff it can be dereferenced.
64  explicit operator bool() const { return !null(); }
65 
66  bool null() const { assert(cfg_); return cfg_ == &default_empty_config; }
67  void make_safe() const; //!< instruct the vconfig to make a private copy of its underlying data.
68  const config& get_config() const { return *cfg_; }
69  config get_parsed_config() const;
70 
71  typedef std::vector<vconfig> child_list;
72  child_list get_children(const std::string& key) const;
73  size_t count_children(const std::string& key) const;
74  vconfig child(const std::string& key) const;
75  bool has_child(const std::string& key) const;
76 
77  /**
78  * Note: vconfig::operator[] returns const, and this should not be changed
79  * because vconfig is often used as a drop-in replacement for config, and
80  * this const will properly warn you if you try to assign vcfg["key"]=val;
81  *
82  * Note: The following construction is unsafe:
83  * const std::string& temp = vcfg["foo"];
84  * This binds temp to a member of a temporary t_string. The lifetime of the
85  * temporary is not extended by this reference binding and the temporary's
86  * lifetime ends which causes UB. Instead use:
87  * const std::string temp = vcfg["foo"];
88  */
90  { return expand(key); }
91  config::attribute_value expand(const std::string&) const; /** < Synonym for operator[] */
92  bool has_attribute(const std::string& key) const { return cfg_->has_attribute(key); }
93  bool empty() const { return (null() || cfg_->empty()); }
94 
96  {
97  struct pointer_proxy;
98 
99  typedef std::pair<std::string, vconfig> value_type;
100  typedef std::forward_iterator_tag iterator_category;
101  typedef int difference_type;
102  typedef const pointer_proxy pointer;
103  typedef const value_type reference;
105  explicit all_children_iterator(const Itor &i);
106  all_children_iterator(const Itor &i, const boost::shared_ptr<const config> & cache);
107 
110 
111  reference operator*() const;
112  pointer operator->() const;
113 
114  std::string get_key() const;
115  vconfig get_child() const;
117 
118  bool operator==(const all_children_iterator &i) const;
119  bool operator!=(const all_children_iterator &i) const
120  { return !operator==(i); }
121 
122  private:
123  Itor i_;
124  /*
125  if wa have game variables
126  [a] b = 1 [/a]
127  [a] b = 4 [/a]
128  [a] b = 6 [/a]
129  we want to expand [insert_tag] variable = a name = "u" [/insert_tag] to
130  [u] b = 1 [/u]
131  [u] b = 4 [/u]
132  [u] b = 6 [/u]
133  in this case inner_index_ points to the index in 'a' we are currently processing.
134  */
137  };
138 
139  struct recursion_error : public config::error {
141  };
142 
143  /** In-order iteration over all children. */
144  all_children_iterator ordered_begin() const;
145  all_children_iterator ordered_end() const;
146  std::pair<all_children_iterator,all_children_iterator> all_ordered() const {
147  return std::make_pair(ordered_begin(), ordered_end());
148  }
149 
150 private:
151  /// Returns true if *this has made a copy of its config.
152  bool memory_managed() const { return static_cast<bool>(cache_); }
153 
154  /// Keeps a copy of our config alive when we manage our own memory.
155  /// If this is not null, then cfg_ points to *cache_ or a child of *cache_.
157  /// Used to access our config (original or copy, as appropriate).
158  mutable const config* cfg_;
160 };
161 
163 {
165  const value_type *operator->() const { return &p; }
166 };
167 
168 
170 {
171 public:
172  scoped_wml_variable(const std::string& var_name);
173  virtual ~scoped_wml_variable();
174  const std::string& name() const { return var_name_; }
175  virtual void activate() = 0;
176  config &store(const config &var_value = config());
177  bool activated() const { return activated_; }
178 private:
182 };
183 
185 {
186 public:
187  scoped_weapon_info(const std::string& var_name, const config &data)
188  : scoped_wml_variable(var_name), data_(data) {}
189  void activate();
190 private:
191  config const &data_;
192 };
193 
195 {
196 public:
197  scoped_xy_unit(const std::string& var_name, const int x, const int y, const unit_map& umap)
198  : scoped_wml_variable(var_name), x_(x), y_(y), umap_(umap) {}
199  void activate();
200 private:
201  const int x_, y_;
202  const unit_map& umap_;
203 };
204 
206 {
207 public:
208  scoped_recall_unit(const std::string& var_name, const std::string& player,
209  unsigned int recall_index) : scoped_wml_variable(var_name), player_(player),
210  recall_index_(recall_index) {}
211  void activate();
212 private:
214  unsigned int recall_index_;
215 };
216 
217 #endif
void activate()
Definition: variable.cpp:461
boost::shared_ptr< const config > cache_
Definition: variable.hpp:136
recursion_error(const std::string &msg)
Definition: variable.hpp:140
size_t count_children(const std::string &key) const
Definition: variable.cpp:211
vconfig()
Definition: variable.cpp:63
vconfig(const config &cfg)
Constructor from a config.
Definition: variable.hpp:56
vconfig get_child() const
Definition: variable.cpp:374
vconfig child(const std::string &key) const
Returns a child of *this whose key is key.
Definition: variable.cpp:243
config get_parsed_config() const
Definition: variable.cpp:131
const std::string & name() const
Definition: variable.hpp:174
all_children_iterator ordered_end() const
Definition: variable.cpp:403
~vconfig()
Default destructor, but defined here for possibly faster compiles (templates sometimes can be rough o...
Definition: variable.cpp:93
all_children_iterator(const Itor &i)
Definition: variable.cpp:311
std::forward_iterator_tag iterator_category
Definition: variable.hpp:100
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
const pointer_proxy pointer
Definition: variable.hpp:102
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
scoped_recall_unit(const std::string &var_name, const std::string &player, unsigned int recall_index)
Definition: variable.hpp:208
bool empty() const
Definition: config.cpp:1105
std::pair< std::string, vconfig > value_type
Definition: variable.hpp:97
Definitions for the interface to Wesnoth Markup Language (WML).
scoped_wml_variable(const std::string &var_name)
Definition: variable.cpp:408
const int x_
Definition: variable.hpp:201
Variant for storing WML attributes.
Definition: config.hpp:223
bool memory_managed() const
Returns true if *this has made a copy of its config.
Definition: variable.hpp:152
unsigned int recall_index_
Definition: variable.hpp:214
const config * cfg_
Used to access our config (original or copy, as appropriate).
Definition: variable.hpp:158
bool null() const
Definition: variable.hpp:66
child_list get_children(const std::string &key) const
Definition: variable.cpp:181
bool has_child(const std::string &key) const
Returns whether or not *this has a child whose key is key.
Definition: variable.cpp:270
virtual void activate()=0
std::pair< all_children_iterator, all_children_iterator > all_ordered() const
Definition: variable.hpp:146
config & store(const config &var_value=config())
Definition: variable.cpp:417
const value_type * operator->() const
Definition: variable.hpp:165
const int y_
Definition: variable.hpp:201
config::attribute_value expand(const std::string &) const
Definition: variable.cpp:303
boost::shared_ptr< const config > cache_
Keeps a copy of our config alive when we manage our own memory.
Definition: variable.hpp:156
const config & get_config() const
Definition: variable.hpp:68
config const & data_
Definition: variable.hpp:191
bool empty() const
Definition: variable.hpp:93
static tcache cache
Definition: minimap.cpp:139
size_t i
Definition: function.cpp:1057
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
bool activated() const
Definition: variable.hpp:177
const config::attribute_value operator[](const std::string &key) const
Note: vconfig::operator[] returns const, and this should not be changed because vconfig is often used...
Definition: variable.hpp:89
scoped_weapon_info(const std::string &var_name, const config &data)
Definition: variable.hpp:187
static vconfig unconstructed_vconfig()
This is just a wrapper for the default constructor; it exists for historical reasons and to make it c...
Definition: variable.cpp:108
reference operator*() const
Definition: variable.cpp:353
const std::string player_
Definition: variable.hpp:213
config::all_children_iterator Itor
Definition: variable.hpp:104
bool has_attribute(const std::string &key) const
Definition: config.cpp:514
bool operator==(const all_children_iterator &i) const
Definition: variable.cpp:393
A variable-expanding proxy for the config class.
Definition: variable.hpp:36
pointer operator->() const
Definition: variable.cpp:358
Container associating units to locations.
Definition: map.hpp:90
static vconfig empty_vconfig()
Definition: variable.cpp:97
static const config default_empty_config
Definition: variable.hpp:159
std::vector< vconfig > child_list
Definition: variable.hpp:71
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
void make_safe() const
instruct the vconfig to make a private copy of its underlying data.
Definition: variable.cpp:119
const std::string var_name_
Definition: variable.hpp:180
bool operator!=(const all_children_iterator &i) const
Definition: variable.hpp:119
GLsizei const GLcharARB ** string
Definition: glew.h:4503
virtual ~scoped_wml_variable()
Definition: variable.cpp:438
std::string get_key() const
Definition: variable.cpp:365
bool has_attribute(const std::string &key) const
< Synonym for operator[]
Definition: variable.hpp:92
all_children_iterator ordered_begin() const
In-order iteration over all children.
Definition: variable.cpp:398
const value_type reference
Definition: variable.hpp:103
const unit_map & umap_
Definition: variable.hpp:202
all_children_iterator & operator++()
Definition: variable.cpp:321
scoped_xy_unit(const std::string &var_name, const int x, const int y, const unit_map &umap)
Definition: variable.hpp:197