Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Formula.h

Go to the documentation of this file.
00001 /*
00002     liban - base function to do usefull things related to simulation and scientific work
00003     Copyright (C) 1999-2005 Stephane Magnenat <nct@ysagoon.com>
00004     Copyright (c) 2004-2005 Antoine Beyeler <antoine.beyeler@epfl.ch>
00005     Copyright (C) 2005 Laboratory of Intelligent Systems, EPFL, Lausanne
00006     See AUTHORS for details
00007 
00008     This program is free software. You can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     This program is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with this program; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 #ifndef __AN_FORMULA_H
00024 #define __AN_FORMULA_H
00025 
00026 #include <string>
00027 #include <vector>
00028 #include <stack>
00029 #include <map>
00030 #include <typeinfo>
00031 
00036 namespace An
00037 {
00039     class Formula
00040     {
00041     public:
00043         struct ParseResult
00044         {
00046             enum Result
00047             {
00048                 SUCCESS = 0, 
00049                 UNDEFINED_VARIABLE, 
00050                 SYNTAX_ERROR 
00051             } result;
00053             size_t errorPos;
00054             
00056             ParseResult() { result = SUCCESS; errorPos = 0; }
00058             operator bool () const { return result == SUCCESS; }
00059         };
00060         
00062         typedef std::stack<double> ResultStack;
00063         
00065         struct BytecodeToken
00066         {
00068             virtual bool eval(ResultStack &stack) = 0;
00070             virtual const char *getType(void) { return typeid(*this).name(); }
00071         };
00072         
00074         typedef double (*UnaryFunctionPtr)(double);
00075         
00077         typedef std::vector<BytecodeToken *> Bytecode;
00079         typedef std::map<std::string, const double *> VariableMap;
00081         typedef std::map<std::string, UnaryFunctionPtr> FunctionMap;
00082         
00083     protected:
00085         std::string formulaText;
00087         VariableMap variableMap;
00089         FunctionMap functionMap;
00091         Bytecode bytecode;
00093         bool parsed;
00094         
00095     public:
00097         Formula(const std::string &formula, bool useStandardFunctions = true);
00099         virtual ~Formula();
00101         void setVariable(const std::string &name, const double &value);
00103         void addUnaryFunction(const std::string &name, UnaryFunctionPtr funcPtr);
00105         ParseResult parse(void);
00107         double eval(void);
00108         
00109     protected:
00110         // standard functions
00112         static double sqr(double v) { return v*v; }
00114         static double sgn(double v) { return v > 0.0 ? 1.0 : (v < 0.0 ? -1.0 : 0.0); }
00115     };
00116 }
00117 
00118 #endif

Generated on Mon Oct 24 17:30:33 2005 for liban by  doxygen 1.4.2