The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
callable.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #ifndef FORMULA_CALLABLE_HPP_INCLUDED
16 #define FORMULA_CALLABLE_HPP_INCLUDED
17 
19 #include "formula/variant.hpp"
20 
21 namespace game_logic
22 {
23 
25 struct formula_input {
29  : name(name), access(access)
30  {}
31 };
32 
33 //interface for objects that can have formulae run on them
35 public:
36  explicit formula_callable(bool has_self=true) : type_(FORMULA_C), has_self_(has_self)
37  {}
38 
39  variant query_value(const std::string& key) const {
40  if(has_self_ && key == "self") {
41  return variant(this);
42  }
43  return get_value(key);
44  }
45 
46  void mutate_value(const std::string& key, const variant& value) {
47  set_value(key, value);
48  }
49 
50  std::vector<formula_input> inputs() const {
51  std::vector<formula_input> res;
52  get_inputs(&res);
53  return res;
54  }
55 
56  bool equals(const formula_callable* other) const {
57  return do_compare(other) == 0;
58  }
59 
60  bool less(const formula_callable* other) const {
61  return do_compare(other) < 0;
62  }
63 
64  virtual void get_inputs(std::vector<formula_input>* /*inputs*/) const {}
65 
66  //note: this function should NOT overwrite str, but append text to it!
67  void serialize(std::string& str) const {
69  }
70 
71  bool has_key(const std::string& key) const
72  { return !query_value(key).is_null(); }
73 
74 protected:
75  virtual ~formula_callable() {}
76 
77  virtual void set_value(const std::string& key, const variant& value);
78  virtual int do_compare(const formula_callable* callable) const {
79  if( type_ < callable->type_ )
80  return -1;
81 
82  if( type_ > callable->type_ )
83  return 1;
84 
85  return this < callable ? -1 : (this == callable ? 0 : 1);
86  }
87 
88  //note: this function should NOT overwrite str, but append text to it!
89  virtual void serialize_to_string(std::string& /*str*/) const {
90  throw type_error("Tried to serialize type which cannot be serialized");
91  }
92 
93  //priority for objects that are derived from this class, used in do_compare
94  //when comparing objects of different types
95  //for example: formula_callable < terrain_callable < unit_type_callable ...
98 
100 private:
101  virtual variant get_value(const std::string& key) const = 0;
102  bool has_self_;
103 };
104 
106 public:
109  }
111 };
112 
116  variant get_value(const std::string& key) const {
117  variant var = main_.query_value(key);
118  if(var.is_null()) {
119  return backup_.query_value(key);
120  }
121 
122  return var;
123  }
124 
125  void get_inputs(std::vector<formula_input>* inputs) const {
126  main_.get_inputs(inputs);
127  backup_.get_inputs(inputs);
128  }
129 public:
130  formula_callable_with_backup(const formula_callable& main, const formula_callable& backup) : formula_callable(false), main_(main), backup_(backup)
131  {}
132 };
133 
137  variant get_value(const std::string& key) const {
138  variant var = var_.get_member(key);
139  if(var.is_null()) {
140  return backup_.query_value(key);
141  }
142 
143  return var;
144  }
145 
146  void get_inputs(std::vector<formula_input>* inputs) const {
147  backup_.get_inputs(inputs);
148  }
149 
150 public:
151  formula_variant_callable_with_backup(const variant& var, const formula_callable& backup) : formula_callable(false), var_(var), backup_(backup)
152  {}
153 };
154 
156 public:
157  explicit map_formula_callable(const formula_callable* fallback=nullptr);
158  map_formula_callable& add(const std::string& key, const variant& value);
159  void set_fallback(const formula_callable* fallback) { fallback_ = fallback; }
160  bool empty() const { return values_.empty(); }
161  void clear() { values_.clear(); }
162 
163  typedef std::map<std::string,variant>::const_iterator const_iterator;
164 
165  const_iterator begin() const { return values_.begin(); }
166  const_iterator end() const { return values_.end(); }
167 
168 private:
169  variant get_value(const std::string& key) const;
170  void get_inputs(std::vector<formula_input>* inputs) const;
171  void set_value(const std::string& key, const variant& value);
172  std::map<std::string,variant> values_;
174 };
175 
178 
179 }
180 
181 #endif
FORMULA_ACCESS_TYPE access
Definition: callable.hpp:27
formula_callable_with_backup(const formula_callable &main, const formula_callable &backup)
Definition: callable.hpp:130
const formula_callable & main_
Definition: callable.hpp:114
void get_inputs(std::vector< formula_input > *inputs) const
Definition: callable.hpp:125
variant get_value(const std::string &key) const
Definition: callable.hpp:116
boost::intrusive_ptr< const map_formula_callable > const_map_formula_callable_ptr
Definition: callable.hpp:177
formula_variant_callable_with_backup(const variant &var, const formula_callable &backup)
Definition: callable.hpp:151
bool is_null() const
Definition: variant.hpp:80
virtual void serialize_to_string(std::string &) const
Definition: callable.hpp:89
variant query_value(const std::string &key) const
Definition: callable.hpp:39
variant get_value(const std::string &key) const
Definition: callable.hpp:137
boost::intrusive_ptr< map_formula_callable > map_formula_callable_ptr
Definition: callable.hpp:176
void get_inputs(std::vector< formula_input > *inputs) const
Definition: formula.cpp:60
void serialize(std::string &str) const
Definition: callable.hpp:67
GLsizei const GLfloat * value
Definition: glew.h:1817
variant get_value(const std::string &key) const
Definition: formula.cpp:48
void set_value(const std::string &key, const variant &value)
Definition: formula.cpp:70
formula_callable(bool has_self=true)
Definition: callable.hpp:36
const_iterator begin() const
Definition: callable.hpp:165
bool less(const formula_callable *other) const
Definition: callable.hpp:60
bool equals(const formula_callable *other) const
Definition: callable.hpp:56
void mutate_value(const std::string &key, const variant &value)
Definition: callable.hpp:46
std::vector< formula_input > inputs() const
Definition: callable.hpp:50
GLuint res
Definition: glew.h:9258
virtual variant get_value(const std::string &key) const =0
FORMULA_ACCESS_TYPE
Definition: callable.hpp:24
bool has_key(const std::string &key) const
Definition: callable.hpp:71
const_iterator end() const
Definition: callable.hpp:166
const formula_callable * fallback_
Definition: callable.hpp:173
map_formula_callable & add(const std::string &key, const variant &value)
Definition: formula.cpp:41
int main(int argc, char **argv)
virtual int do_compare(const formula_callable *callable) const
Definition: callable.hpp:78
formula_input(const std::string &name, FORMULA_ACCESS_TYPE access=FORMULA_READ_WRITE)
Definition: callable.hpp:28
variant get_member(const std::string &str) const
Definition: variant.cpp:545
std::map< std::string, variant > values_
Definition: callable.hpp:172
virtual void set_value(const std::string &key, const variant &value)
Definition: formula.cpp:29
map_formula_callable(const formula_callable *fallback=nullptr)
Definition: formula.cpp:35
const formula_callable & backup_
Definition: callable.hpp:115
void get_inputs(std::vector< formula_input > *inputs) const
Definition: callable.hpp:146
std::map< std::string, variant >::const_iterator const_iterator
Definition: callable.hpp:163
virtual void get_inputs(std::vector< formula_input > *) const
Definition: callable.hpp:64
GLsizei const GLcharARB ** string
Definition: glew.h:4503
void set_fallback(const formula_callable *fallback)
Definition: callable.hpp:159