00001 // -*- Mode: C++; tab-width: 2; -*- 00002 // vi: set ts=2: 00003 // 00004 // $Id: parsedFunction.h,v 1.9 2003/12/19 14:54:08 anne Exp $ 00005 // 00006 00007 #ifndef BALL_MATHS_PARSEDFUNCTION_H 00008 #define BALL_MATHS_PARSEDFUNCTION_H 00009 00010 #ifndef BALL_DATATYPE_STRINGHASHMAP_H 00011 # include <BALL/DATATYPE/stringHashMap.h> 00012 #endif 00013 00014 #include <numeric> 00015 00016 using std::unary_function; 00017 00018 extern double ParsedFunctionResult; 00019 extern int ParsedFunctionparse(); 00020 extern void ParsedFunction_initBuffer(const char*); 00021 extern void ParsedFunction_delBuffer(); 00022 00023 namespace BALL 00024 { 00028 extern StringHashMap<double*> *ParsedFunctionConstants; 00029 extern StringHashMap<double (*)(double)> *ParsedFunctionFunctions; 00030 00038 template <typename arg> 00039 class ParsedFunction 00040 : public unary_function<arg, double> 00041 { 00042 public: 00046 00048 ParsedFunction(); 00049 00051 ParsedFunction(const String& expression); 00052 00054 ParsedFunction(const ParsedFunction& func); 00055 00057 ~ParsedFunction(); 00058 00060 00066 double operator () (arg p) throw(Exception::ParseError); 00067 00069 00075 void initTable(); 00077 00080 StringHashMap<double*> constants_; 00081 00084 StringHashMap<double (*)(double)> functions_; 00085 00086 protected: 00087 String expression_; 00088 }; 00089 00090 template <typename arg> 00091 ParsedFunction<arg>::ParsedFunction() 00092 : constants_(), 00093 functions_(), 00094 expression_("") 00095 { 00096 initTable(); 00097 } 00098 00099 template <typename arg> 00100 ParsedFunction<arg>::ParsedFunction(const String& expression) 00101 : constants_(), 00102 functions_(), 00103 expression_(expression) 00104 { 00105 initTable(); 00106 } 00107 00108 template <typename arg> 00109 ParsedFunction<arg>::ParsedFunction(const ParsedFunction& func) 00110 { 00111 constants_ = func.constants_; 00112 functions_ = func.functions_; 00113 expression_ = func.expression_; 00114 initTable(); 00115 } 00116 00117 template <typename arg> 00118 ParsedFunction<arg>::~ParsedFunction() 00119 { 00120 } 00121 00122 template <typename arg> 00123 double ParsedFunction<arg>::operator () (arg argument) 00124 throw(Exception::ParseError) 00125 { 00126 constants_["X"] = (double*)&argument; 00127 ParsedFunctionConstants = &constants_; 00128 ParsedFunctionFunctions = &functions_; 00129 ParsedFunction_initBuffer(expression_.c_str()); 00130 ParsedFunctionparse(); 00131 ParsedFunction_delBuffer(); 00132 00133 return ParsedFunctionResult; 00134 } 00135 00136 template <> 00137 BALL_EXPORT double ParsedFunction<float>::operator () (float argument) 00138 throw(Exception::ParseError); 00139 00140 template <> 00141 BALL_EXPORT double ParsedFunction<double>::operator () (double argument) 00142 throw(Exception::ParseError); 00143 00144 template <typename arg> 00145 void ParsedFunction<arg>::initTable() 00146 { 00147 // initialize the functions table 00148 functions_["sin"] = (double(*)(double))&sin; 00149 functions_["cos"] = (double(*)(double))&cos; 00150 functions_["asin"] = (double(*)(double))&asin; 00151 functions_["acos"] = (double(*)(double))&acos; 00152 functions_["tan"] = (double(*)(double))&tan; 00153 functions_["atan"] = (double(*)(double))&atan; 00154 functions_["ln"] = (double(*)(double))&log; 00155 functions_["exp"] = (double(*)(double))&exp; 00156 functions_[""] = 0; 00157 } 00158 00160 } 00161 00162 #endif // BALL_MATHS_PARSEDFUNCTION_H