formula.hpp

Go to the documentation of this file.
00001 /* $Id: formula.hpp 24955 2008-03-21 23:11:31Z dragonking $ */
00002 /*
00003    Copyright (C) 2007 - 2008 by David White <dave.net>
00004    Part of the Silver Tree Project
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License version 2 or later.
00008    This program is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY.
00010 
00011    See the COPYING file for more details.
00012 */
00013 
00014 #ifndef FORMULA_HPP_INCLUDED
00015 #define FORMULA_HPP_INCLUDED
00016 
00017 #include <map>
00018 #include <string>
00019 
00020 #include "formula_fwd.hpp"
00021 #include "variant.hpp"
00022 
00023 namespace game_logic
00024 {
00025 
00026 class formula_callable;
00027 class formula_expression;
00028 class function_symbol_table;
00029 typedef boost::shared_ptr<formula_expression> expression_ptr;
00030 
00031 class formula {
00032 public:
00033     static variant evaluate(const const_formula_ptr& f,
00034                         const formula_callable& variables,
00035                         variant default_res=variant(0)) {
00036         if(f) {
00037             return f->execute(variables);
00038         } else {
00039             return default_res;
00040         }
00041     }
00042 
00043     // function which will create a formula that is a single string literal, 'str'.
00044     // 'str' should not be enclosed in quotes.
00045     static formula_ptr create_string_formula(const std::string& str);
00046     static formula_ptr create_optional_formula(const std::string& str, function_symbol_table* symbols=NULL);
00047     explicit formula(const std::string& str, function_symbol_table* symbols=NULL);
00048     variant execute(const formula_callable& variables) const;
00049     variant execute() const;
00050     const std::string& str() const { return str_; }
00051 
00052 private:
00053     formula() {}
00054     expression_ptr expr_;
00055     std::string str_;
00056 };
00057 
00058 struct formula_error
00059 {
00060 };
00061 
00062 }
00063 
00064 #endif

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs