|
||||
File indexing completed on 2025-01-18 09:14:30
0001 //========================================================================== 0002 // AIDA Detector description implementation 0003 //-------------------------------------------------------------------------- 0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN) 0005 // All rights reserved. 0006 // 0007 // For the licensing terms see $DD4hepINSTALL/LICENSE. 0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS. 0009 // 0010 //========================================================================== 0011 0012 #ifndef EVALUATOR_EVALUATOR_H 0013 #define EVALUATOR_EVALUATOR_H 0014 0015 /// C/C++ include files 0016 #include <ostream> 0017 #include <string> 0018 0019 /// Namespace for the AIDA detector description toolkit 0020 namespace dd4hep { 0021 0022 /// Namespace containing XML tools. 0023 namespace tools { 0024 0025 /// Evaluator of arithmetic expressions with an extendable dictionary. 0026 /** 0027 * Taken from CLHEP 1.9.2.1 0028 * 0029 * Example: 0030 * @code 0031 * #include "XmlTools/Evaluator.h" 0032 * XmlTools::Evaluator eval; 0033 * eval.setStdMath(); 0034 * double res = eval.evaluate("sin(30*degree)"); 0035 * if (eval.status() != XmlTools::Evaluator::OK) eval.print_error(); 0036 * @endcode 0037 * 0038 * @author Evgeni Chernyaev <Evgueni.Tcherniaev@cern.ch> 0039 * @ingroup evaluator 0040 */ 0041 class Evaluator { 0042 public: 0043 0044 /** 0045 * List of possible statuses. 0046 * Status of the last operation can be obtained with status(). 0047 * In case if status() is an ERROR the corresponding error message 0048 * can be printed with print_error(). 0049 * 0050 * @see status 0051 * @see error_position 0052 * @see print_error 0053 */ 0054 enum { 0055 OK, /**< Everything OK */ 0056 WARNING_EXISTING_VARIABLE, /**< Redefinition of existing variable */ 0057 WARNING_EXISTING_FUNCTION, /**< Redefinition of existing function */ 0058 WARNING_BLANK_STRING, /**< Empty input string */ 0059 ERROR_NOT_A_NAME, /**< Not allowed symbol in the name of variable or function */ 0060 ERROR_SYNTAX_ERROR, /**< Syntax error */ 0061 ERROR_UNPAIRED_PARENTHESIS, /**< Unpaired parenthesis */ 0062 ERROR_UNEXPECTED_SYMBOL, /**< Unexpected symbol */ 0063 ERROR_UNKNOWN_VARIABLE, /**< Non-existing variable */ 0064 ERROR_UNKNOWN_FUNCTION, /**< Non-existing function */ 0065 ERROR_EMPTY_PARAMETER, /**< Function call has empty parameter */ 0066 ERROR_CALCULATION_ERROR /**< Error during calculation */ 0067 }; 0068 0069 /** 0070 * Constructor. 0071 */ 0072 Evaluator(double meter = 1.0, double kilogram = 1.0, double second = 1.0, double ampere = 1.0, double kelvin = 1.0 0073 , double mole = 1.0, double candela = 1.0, double radians = 1.0); 0074 0075 /** 0076 * MoveConstructor. 0077 */ 0078 Evaluator(Evaluator&&); 0079 0080 /** 0081 * Destructor. 0082 */ 0083 ~Evaluator(); 0084 0085 /** 0086 * Evaluates the arithmetic expression given as character string. 0087 * The expression may consist of numbers, variables and functions 0088 * separated by arithmetic (+, - , /, *, ^, **) and logical 0089 * operators (==, !=, >, >=, <, <=, &&, ||). 0090 * 0091 * @param expression input expression. 0092 * @return pair(status,result) of the evaluation. 0093 */ 0094 std::pair<int,double> evaluate(const std::string& expression) const; 0095 0096 /** 0097 * Evaluates the arithmetic expression given as character string. 0098 * The expression may consist of numbers, variables and functions 0099 * separated by arithmetic (+, - , /, *, ^, **) and logical 0100 * operators (==, !=, >, >=, <, <=, &&, ||). 0101 * 0102 * @param expression input expression. 0103 * @param Possible stream identifier for error message 0104 * @return pair(status,result) of the evaluation. 0105 */ 0106 std::pair<int,double> evaluate(const std::string& expression, std::ostream& os) const; 0107 0108 /** 0109 * Adds to the dictionary a function without parameters. 0110 * If such a function already exist in the dictionary, 0111 * then status will be set to WARNING_EXISTING_FUNCTION. 0112 * 0113 * @param name function name. 0114 * @param fun pointer to the real function in the user code. 0115 * @return result of the evaluation. 0116 */ 0117 int setFunction(const std::string& name, double (*fun)()) const; 0118 0119 /** 0120 * Adds to the dictionary a function with one parameter. 0121 * If such a function already exist in the dictionary, 0122 * then status will be set to WARNING_EXISTING_FUNCTION. 0123 * 0124 * @param name function name. 0125 * @param fun pointer to the real function in the user code. 0126 * @return result of the evaluation. 0127 */ 0128 int setFunction(const std::string& name, double (*fun)(double)) const; 0129 0130 /** 0131 * Adds to the dictionary a function with two parameters. 0132 * If such a function already exist in the dictionary, 0133 * then status will be set to WARNING_EXISTING_FUNCTION. 0134 * 0135 * @param name function name. 0136 * @param fun pointer to the real function in the user code. 0137 * @return result of the evaluation. 0138 */ 0139 int setFunction(const std::string& name, double (*fun)(double, double)) const; 0140 0141 /** 0142 * Adds to the dictionary a function with three parameters. 0143 * If such a function already exist in the dictionary, 0144 * then status will be set to WARNING_EXISTING_FUNCTION. 0145 * 0146 * @param name function name. 0147 * @param fun pointer to the real function in the user code. 0148 * @return result of the evaluation. 0149 */ 0150 int setFunction(const std::string& name, double (*fun)(double, double, double)) const; 0151 0152 /** 0153 * Adds to the dictionary a function with four parameters. 0154 * If such a function already exist in the dictionary, 0155 * then status will be set to WARNING_EXISTING_FUNCTION. 0156 * 0157 * @param name function name. 0158 * @param fun pointer to the real function in the user code. 0159 * @return result of the evaluation. 0160 */ 0161 int setFunction(const std::string& name, double (*fun)(double, double, double, double)) const; 0162 0163 /** 0164 * Adds to the dictionary a function with five parameters. 0165 * If such a function already exist in the dictionary, 0166 * then status will be set to WARNING_EXISTING_FUNCTION. 0167 * 0168 * @param name function name. 0169 * @param fun pointer to the real function in the user code. 0170 * @return result of the evaluation. 0171 */ 0172 int setFunction(const std::string& name, double (*fun)(double, double, double, double, double)) const; 0173 0174 /** 0175 * Adds to the dictionary a string constant 0176 * 0177 * @param name name of the variable. 0178 * @param value value assigned to the variable. 0179 * @return result of the evaluation. 0180 */ 0181 int setEnviron(const std::string& name, const std::string& value) const; 0182 0183 /** 0184 * Lookup the dictionary for a string constant 0185 * 0186 * @param name name of the variable. 0187 * @return pair(status,result) of the evaluation. 0188 */ 0189 std::pair<int,std::string> getEnviron(const std::string& name) const; 0190 0191 /** 0192 * Lookup the dictionary for a string constant 0193 * 0194 * @param name name of the variable. 0195 * @param stream identifier for error message 0196 * @return pair(status,result) of the evaluation. 0197 */ 0198 std::pair<int,std::string> getEnviron(const std::string& name, std::ostream& os) const; 0199 0200 /** 0201 * Adds to the dictionary a variable with given value. 0202 * If a variable with such a name already exist in the dictionary, 0203 * then status will be set to WARNING_EXISTING_VARIABLE. 0204 * 0205 * @param name name of the variable. 0206 * @param value value assigned to the variable. 0207 * @return result of the evaluation. 0208 */ 0209 int setVariable(const std::string& name, double value) const; 0210 0211 /** 0212 * Adds to the dictionary a variable with given value. 0213 * If a variable with such a name already exist in the dictionary, 0214 * then status will be set to WARNING_EXISTING_VARIABLE. 0215 * 0216 * @param name name of the variable. 0217 * @param value value assigned to the variable. 0218 * @return result of the evaluation. 0219 */ 0220 int setVariable(const std::string& name, double value, std::ostream& os) const; 0221 0222 /** 0223 * Adds to the dictionary a variable with an arithmetic expression 0224 * assigned to it. 0225 * If a variable with such a name already exist in the dictionary, 0226 * then status will be set to WARNING_EXISTING_VARIABLE. 0227 * 0228 * @param name name of the variable. 0229 * @param expression arithmetic expression. 0230 * @return result of the evaluation. 0231 */ 0232 int setVariable(const std::string& name, const std::string& expression) const; 0233 0234 /** 0235 * Adds to the dictionary a variable with an arithmetic expression 0236 * assigned to it. 0237 * If a variable with such a name already exist in the dictionary, 0238 * then status will be set to WARNING_EXISTING_VARIABLE. 0239 * 0240 * @param name name of the variable. 0241 * @param expression arithmetic expression. 0242 * @param stream identifier for error message 0243 * @return result of the evaluation. 0244 */ 0245 int setVariable(const std::string& name, const std::string& expression, std::ostream& os) const; 0246 0247 /** 0248 * Finds the variable in the dictionary. 0249 * 0250 * @param name name of the variable. 0251 * @return true if such a variable exists, false otherwise. 0252 */ 0253 bool findVariable(const std::string& name) const; 0254 0255 /** 0256 * Finds the function in the dictionary. 0257 * 0258 * @param name name of the function to be unset. 0259 * @param npar number of parameters of the function. 0260 * @return true if such a function exists, false otherwise. 0261 */ 0262 bool findFunction(const std::string& name, int npar) const; 0263 0264 class Object; 0265 0266 private: 0267 Object* object = 0; // internal data 0268 Evaluator(const Evaluator &) = delete; // copy constructor is not allowed 0269 Evaluator & operator=(const Evaluator &) = delete; // assignment is not allowed 0270 }; 0271 0272 } // namespace tools 0273 } // namespace dd4hep 0274 0275 #endif // EVALUATOR_EVALUATOR_H
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |