~ubuntu-branches/ubuntu/precise/frogatto/precise

« back to all changes in this revision

Viewing changes to src/formula.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Dmitry E. Oboukhov
  • Date: 2010-07-21 16:21:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100721162145-zid0u93fm3xz73gh
Tags: upstream-1.0+dfsg1
ImportĀ upstreamĀ versionĀ 1.0+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: formula.hpp 24955 2008-03-21 23:11:31Z dragonking $ */
 
2
/*
 
3
   Copyright (C) 2007 - 2008 by David White <dave@whitevine.net>
 
4
   Part of the Silver Tree Project
 
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 version 2 or later.
 
8
   This program is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY.
 
10
 
 
11
   See the COPYING file for more details.
 
12
*/
 
13
 
 
14
#ifndef FORMULA_HPP_INCLUDED
 
15
#define FORMULA_HPP_INCLUDED
 
16
 
 
17
#include <map>
 
18
#include <string>
 
19
 
 
20
#include "formula_callable_definition_fwd.hpp"
 
21
#include "formula_fwd.hpp"
 
22
#include "formula_function.hpp"
 
23
#include "variant.hpp"
 
24
#include "wml_value.hpp"
 
25
 
 
26
namespace game_logic
 
27
{
 
28
 
 
29
class formula_callable;
 
30
class formula_expression;
 
31
class function_symbol_table;
 
32
typedef boost::shared_ptr<formula_expression> expression_ptr;
 
33
 
 
34
class formula {
 
35
public:
 
36
        static variant evaluate(const const_formula_ptr& f,
 
37
                            const formula_callable& variables,
 
38
                                                variant default_res=variant(0)) {
 
39
                if(f) {
 
40
                        return f->execute(variables);
 
41
                } else {
 
42
                        return default_res;
 
43
                }
 
44
        }
 
45
 
 
46
        // function which will create a formula that is a single string literal, 'str'.
 
47
        // 'str' should not be enclosed in quotes.
 
48
        static formula_ptr create_string_formula(const std::string& str);
 
49
        static formula_ptr create_optional_formula(const wml::value& str, function_symbol_table* symbols=NULL, const formula_callable_definition* def=NULL);
 
50
        explicit formula(const wml::value& val, function_symbol_table* symbols=NULL, const formula_callable_definition* def=NULL);
 
51
        ~formula();
 
52
        variant execute(const formula_callable& variables) const;
 
53
        variant execute() const;
 
54
        const std::string& str() const { return str_; }
 
55
 
 
56
        void output_debug_info() const;
 
57
 
 
58
private:
 
59
        formula() {}
 
60
        expression_ptr expr_;
 
61
        std::string str_;
 
62
        const std::string* filename_;
 
63
        int line_;
 
64
};
 
65
 
 
66
struct formula_error
 
67
{
 
68
        formula_error();
 
69
};
 
70
 
 
71
}
 
72
 
 
73
#endif