The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
function.hpp
Go to the documentation of this file.
1 
2 /*
3  Copyright (C) 2008 - 2016 by David White <[email protected]>
4  Part of the Battle for Wesnoth Project http://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 #ifndef FORMULA_FUNCTION_HPP_INCLUDED
17 #define FORMULA_FUNCTION_HPP_INCLUDED
18 
19 #include "formula/formula.hpp"
20 #include "formula/callable.hpp"
21 
22 namespace game_logic {
23 
25 public:
27  virtual ~formula_expression() {}
28  variant evaluate(const formula_callable& variables, formula_debugger *fdb = nullptr) const {
29  call_stack_manager manager(name_);
30  if (fdb!=nullptr) {
31  return evaluate_arg_callback(*fdb,*this,variables);
32  } else {
33  return execute(variables,fdb);
34  }
35  }
36  void set_name(const char* name) { name_ = name; }
37 
38  const char* get_name() const { return name_; }
39  virtual std::string str() const = 0;
40 private:
41  virtual variant execute(const formula_callable& variables, formula_debugger *fdb = nullptr) const = 0;
42  const char* name_;
43  friend class formula_debugger;
44 };
45 
47 
49 public:
50  typedef std::vector<expression_ptr> args_list;
52  const std::string& name,
53  const args_list& args,
54  int min_args=-1, int max_args=-1)
55  : name_(name), args_(args)
56  {
57  set_name(name.c_str());
58  if(min_args >= 0 && args_.size() < static_cast<size_t>(min_args)) {
59  throw formula_error("Too few arguments", "", "", 0);
60  }
61 
62  if(max_args >= 0 && args_.size() > static_cast<size_t>(max_args)) {
63  throw formula_error("Too many arguments", "", "", 0);
64  }
65  }
66  virtual std::string str() const;
67 protected:
68  const args_list& args() const { return args_; }
69 private:
71  args_list args_;
72 };
73 
77 
78  variant get_value(const std::string& key) const;
79 
80  void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
81 public:
82  explicit key_value_pair(const variant& key, const variant& value) : key_(key), value_(value) {}
83 
84  void serialize_to_string(std::string& str) const;
85 };
86 
88 public:
89  explicit formula_function_expression(const std::string& name, const args_list& args, const_formula_ptr formula, const_formula_ptr precondition, const std::vector<std::string>& arg_names);
90 private:
91  variant execute(const formula_callable& variables, formula_debugger *fdb) const;
94  std::vector<std::string> arg_names_;
95  int star_arg_;
96 };
97 
99 
101 protected:
103 public:
104  formula_function(const std::string name) : name_(name) {}
105  virtual function_expression_ptr generate_function_expression(const std::vector<expression_ptr>& args) const = 0;
106  virtual ~formula_function() {}
107 };
108 
112  std::vector<std::string> args_;
113 public:
114  user_formula_function(const std::string& name, const_formula_ptr formula, const_formula_ptr precondition, const std::vector<std::string>& args)
115  : formula_function(name)
116  , formula_(formula)
117  , precondition_(precondition)
118  , args_(args)
119  {}
120 
121  function_expression_ptr generate_function_expression(const std::vector<expression_ptr>& args) const;
122 };
123 
124 template<typename T>
126 public:
128  function_expression_ptr generate_function_expression(const std::vector<expression_ptr>& args) const {
129  return function_expression_ptr(new T(args));
130  }
131 };
132 
134 typedef std::map<std::string, formula_function_ptr> functions_map;
135 
137  functions_map custom_formulas_;
138 public:
139  void add_function(const std::string& name, formula_function_ptr fcn);
140  expression_ptr create_function(const std::string& fn, const std::vector<expression_ptr>& args) const;
141  std::vector<std::string> get_function_names() const;
142  bool empty() {return custom_formulas_.empty();}
143 };
144 
145 expression_ptr create_function(const std::string& fn,
146  const std::vector<expression_ptr>& args,
147  const function_symbol_table* symbols);
148 std::vector<std::string> builtin_function_names();
149 
150 
152 public:
154  : arg_()
155  {
156  }
157 
158  wrapper_formula(expression_ptr arg)
159  : arg_(arg)
160  {
161  }
162 
164  {
165  }
166 
167  virtual std::string str() const
168  {
169  if (arg_) {
170  return arg_->str();
171  } else {
172  return "";
173  }
174  }
175 private:
176  virtual variant execute(const formula_callable& variables, formula_debugger *fdb = nullptr) const
177  {
178  if (arg_) {
179  return arg_->evaluate(variables,fdb);
180  } else {
181  return variant();
182  }
183  }
184  expression_ptr arg_;
185 };
186 
187 }
188 
189 #endif
variant get_value(const std::string &key) const
Definition: function.cpp:1454
variant execute(const formula_callable &variables, formula_debugger *fdb) const
Definition: function.cpp:1494
virtual std::string str() const
Definition: function.hpp:167
void add_function(const std::string &name, formula_function_ptr fcn)
Definition: function.cpp:1531
variant evaluate_arg_callback(formula_debugger &fdb, const formula_expression &expression, const formula_callable &variables)
std::vector< std::string > args_
Definition: function.hpp:112
builtin_formula_function(const std::string &name)
Definition: function.hpp:127
void set_name(const char *name)
Definition: function.hpp:36
expression_ptr create_function(const std::string &fn, const std::vector< expression_ptr > &args, const function_symbol_table *symbols)
Definition: function.cpp:1632
void get_inputs(std::vector< game_logic::formula_input > *inputs) const
Definition: function.cpp:1467
variant evaluate(const formula_callable &variables, formula_debugger *fdb=nullptr) const
Definition: function.hpp:28
wrapper_formula(expression_ptr arg)
Definition: function.hpp:158
function_expression(const std::string &name, const args_list &args, int min_args=-1, int max_args=-1)
Definition: function.hpp:51
std::vector< std::string > get_function_names() const
Definition: function.cpp:1546
function_expression_ptr generate_function_expression(const std::vector< expression_ptr > &args) const
Definition: function.hpp:128
virtual function_expression_ptr generate_function_expression(const std::vector< expression_ptr > &args) const =0
virtual variant execute(const formula_callable &variables, formula_debugger *fdb=nullptr) const =0
std::vector< std::string > builtin_function_names()
Definition: function.cpp:1651
virtual variant execute(const formula_callable &variables, formula_debugger *fdb=nullptr) const
Definition: function.hpp:176
const char * get_name() const
Definition: function.hpp:38
GLsizei const GLfloat * value
Definition: glew.h:1817
std::vector< formula_input > inputs() const
Definition: callable.hpp:50
boost::shared_ptr< function_expression > function_expression_ptr
Definition: function.hpp:98
std::vector< std::string > arg_names_
Definition: function.hpp:94
std::map< std::string, formula_function_ptr > functions_map
Definition: function.hpp:134
boost::shared_ptr< formula_expression > expression_ptr
Definition: formula.hpp:26
void serialize_to_string(std::string &str) const
Definition: function.cpp:1473
virtual std::string str() const =0
const args_list & args() const
Definition: function.hpp:68
function_expression_ptr generate_function_expression(const std::vector< expression_ptr > &args) const
Definition: function.cpp:1526
formula_function_expression(const std::string &name, const args_list &args, const_formula_ptr formula, const_formula_ptr precondition, const std::vector< std::string > &arg_names)
Definition: function.cpp:1481
const_formula_ptr precondition_
Definition: function.hpp:111
GLuint const GLchar * name
Definition: glew.h:1782
virtual std::string str() const
Definition: function.cpp:42
formula_function(const std::string name)
Definition: function.hpp:104
expression_ptr create_function(const std::string &fn, const std::vector< expression_ptr > &args) const
Definition: function.cpp:1536
key_value_pair(const variant &key, const variant &value)
Definition: function.hpp:82
boost::shared_ptr< formula_function > formula_function_ptr
Definition: function.hpp:133
user_formula_function(const std::string &name, const_formula_ptr formula, const_formula_ptr precondition, const std::vector< std::string > &args)
Definition: function.hpp:114
GLsizei const GLcharARB ** string
Definition: glew.h:4503
std::vector< expression_ptr > args_list
Definition: function.hpp:50