Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
Evaluator.h
Go to the documentation of this file.
1// -*- C++ -*-
2// ---------------------------------------------------------------------------
3
4#ifndef HEP_EVALUATOR_H
5#define HEP_EVALUATOR_H
6
7#include <string>
8
9namespace HepTool {
10
11/**
12 * Evaluator of arithmetic expressions with an extendable dictionary.
13 * Example:
14 * @code
15 * #include "CLHEP/Evaluator/Evaluator.h"
16 * HepTool::Evaluator eval;
17 * eval.setStdMath();
18 * double res = eval.evaluate("sin(30*degree)");
19 * if (eval.status() != HepTool::Evaluator::OK) eval.print_error();
20 * @endcode
21 *
22 * @author Evgeni Chernyaev <[email protected]>
23 * @ingroup evaluator
24 */
25class Evaluator {
26 public:
27
28 /**
29 * List of possible statuses.
30 * Status of the last operation can be obtained with status().
31 * In case if status() is an ERROR the corresponding error message
32 * can be printed with print_error().
33 *
34 * @see status
35 * @see error_position
36 * @see print_error
37 */
38 enum {
39 OK, /**< Everything OK */
40 WARNING_EXISTING_VARIABLE, /**< Redefinition of existing variable */
41 WARNING_EXISTING_FUNCTION, /**< Redefinition of existing function */
42 WARNING_BLANK_STRING, /**< Empty input string */
43 ERROR_NOT_A_NAME, /**< Not allowed sysmbol in the name of variable or function */
44 ERROR_SYNTAX_ERROR, /**< Systax error */
45 ERROR_UNPAIRED_PARENTHESIS, /**< Unpaired parenthesis */
46 ERROR_UNEXPECTED_SYMBOL, /**< Unexpected sysbol */
47 ERROR_UNKNOWN_VARIABLE, /**< Non-existing variable */
48 ERROR_UNKNOWN_FUNCTION, /**< Non-existing function */
49 ERROR_EMPTY_PARAMETER, /**< Function call has empty parameter */
50 ERROR_CALCULATION_ERROR /**< Error during calculation */
51 };
52
53 /**
54 * Constructor.
55 */
56 Evaluator();
57
58 /**
59 * Destructor.
60 */
61 ~Evaluator();
62
63 /**
64 * Evaluates the arithmetic expression given as character string.
65 * The expression may consist of numbers, variables and functions
66 * separated by arithmetic (+, - , /, *, ^, **) and logical
67 * operators (==, !=, >, >=, <, <=, &&, ||).
68 *
69 * @param expression input expression.
70 * @return result of the evaluation.
71 * @see status
72 * @see error_position
73 * @see print_error
74 */
75 double evaluate(const char * expression);
76
77 /**
78 * Returns status of the last operation with the evaluator.
79 */
80 int status() const;
81
82 /**
83 * Returns position in the input string where the problem occured.
84 */
85 int error_position() const;
86
87 /**
88 * Prints error message if status() is an ERROR.
89 */
90 void print_error() const;
91 /**
92 * get a string defining the error name
93 */
94 std::string error_name() const;
95
96 /**
97 * Adds to the dictionary a variable with given value.
98 * If a variable with such a name already exist in the dictionary,
99 * then status will be set to WARNING_EXISTING_VARIABLE.
100 *
101 * @param name name of the variable.
102 * @param value value assigned to the variable.
103 */
104 void setVariable(const char * name, double value);
105
106 /**
107 * Adds to the dictionary a variable with an arithmetic expression
108 * assigned to it.
109 * If a variable with such a name already exist in the dictionary,
110 * then status will be set to WARNING_EXISTING_VARIABLE.
111 *
112 * @param name name of the variable.
113 * @param expression arithmetic expression.
114 */
115 void setVariable(const char * name, const char * expression);
116
117 /**
118 * Adds to the dictionary a function without parameters.
119 * If such a function already exist in the dictionary,
120 * then status will be set to WARNING_EXISTING_FUNCTION.
121 *
122 * @param name function name.
123 * @param fun pointer to the real function in the user code.
124 */
125 void setFunction(const char * name, double (*fun)());
126
127 /**
128 * Adds to the dictionary a function with one parameter.
129 * If such a function already exist in the dictionary,
130 * then status will be set to WARNING_EXISTING_FUNCTION.
131 *
132 * @param name function name.
133 * @param fun pointer to the real function in the user code.
134 */
135 void setFunction(const char * name, double (*fun)(double));
136
137 /**
138 * Adds to the dictionary a function with two parameters.
139 * If such a function already exist in the dictionary,
140 * then status will be set to WARNING_EXISTING_FUNCTION.
141 *
142 * @param name function name.
143 * @param fun pointer to the real function in the user code.
144 */
145 void setFunction(const char * name, double (*fun)(double,double));
146
147 /**
148 * Adds to the dictionary a function with three parameters.
149 * If such a function already exist in the dictionary,
150 * then status will be set to WARNING_EXISTING_FUNCTION.
151 *
152 * @param name function name.
153 * @param fun pointer to the real function in the user code.
154 */
155 void setFunction(const char * name, double (*fun)(double,double,double));
156
157 /**
158 * Adds to the dictionary a function with four parameters.
159 * If such a function already exist in the dictionary,
160 * then status will be set to WARNING_EXISTING_FUNCTION.
161 *
162 * @param name function name.
163 * @param fun pointer to the real function in the user code.
164 */
165 void setFunction(const char * name,
166 double (*fun)(double,double,double,double));
167
168 /**
169 * Adds to the dictionary a function with five parameters.
170 * If such a function already exist in the dictionary,
171 * then status will be set to WARNING_EXISTING_FUNCTION.
172 *
173 * @param name function name.
174 * @param fun pointer to the real function in the user code.
175 */
176 void setFunction(const char * name,
177 double (*fun)(double,double,double,double,double));
178
179 /**
180 * Finds the variable in the dictionary.
181 *
182 * @param name name of the variable.
183 * @return true if such a variable exists, false otherwise.
184 */
185 bool findVariable(const char * name) const;
186
187 /**
188 * Finds the function in the dictionary.
189 *
190 * @param name name of the function to be unset.
191 * @param npar number of parameters of the function.
192 * @return true if such a function exists, false otherwise.
193 */
194 bool findFunction(const char * name, int npar) const;
195
196 /**
197 * Removes the variable from the dictionary.
198 *
199 * @param name name of the variable.
200 */
201 void removeVariable(const char * name);
202
203 /**
204 * Removes the function from the dictionary.
205 *
206 * @param name name of the function to be unset.
207 * @param npar number of parameters of the function.
208 */
209 void removeFunction(const char * name, int npar);
210
211 /**
212 * Clear all settings.
213 */
214 void clear();
215
216 /**
217 * Sets standard mathematical functions and constants.
218 */
219 void setStdMath();
220
221 /**
222 * Sets system of units. Default is the SI system of units.
223 * To set the CGS (Centimeter-Gram-Second) system of units
224 * one should call:
225 * setSystemOfUnits(100., 1000., 1.0, 1.0, 1.0, 1.0, 1.0);
226 *
227 * To set system of units accepted in the GEANT4 simulation toolkit
228 * one should call:
229 * @code
230 * setSystemOfUnits(1.e+3, 1./1.60217733e-25, 1.e+9, 1./1.60217733e-10,
231 * 1.0, 1.0, 1.0);
232 * @endcode
233 *
234 * The basic units in GEANT4 are:
235 * @code
236 * millimeter (millimeter = 1.)
237 * nanosecond (nanosecond = 1.)
238 * Mega electron Volt (MeV = 1.)
239 * positron charge (eplus = 1.)
240 * degree Kelvin (kelvin = 1.)
241 * the amount of substance (mole = 1.)
242 * luminous intensity (candela = 1.)
243 * radian (radian = 1.)
244 * steradian (steradian = 1.)
245 * @endcode
246 */
247 void setSystemOfUnits(double meter = 1.0,
248 double kilogram = 1.0,
249 double second = 1.0,
250 double ampere = 1.0,
251 double kelvin = 1.0,
252 double mole = 1.0,
253 double candela = 1.0);
254
255private:
256 void * p; // private data
257 Evaluator(const Evaluator &); // copy constructor is not allowed
258 Evaluator & operator=(const Evaluator &); // assignment is not allowed
259};
260
261} // namespace HepTool
262
263#endif /* HEP_EVALUATOR_H */
int error_position() const
Definition Evaluator.cc:641
bool findFunction(const char *name, int npar) const
Definition Evaluator.cc:737
void setSystemOfUnits(double meter=1.0, double kilogram=1.0, double second=1.0, double ampere=1.0, double kelvin=1.0, double mole=1.0, double candela=1.0)
void print_error() const
Definition Evaluator.cc:646
double evaluate(const char *expression)
Definition Evaluator.cc:616
std::string error_name() const
Definition Evaluator.cc:655
void removeVariable(const char *name)
Definition Evaluator.cc:748
void removeFunction(const char *name, int npar)
Definition Evaluator.cc:757
void setFunction(const char *name, double(*fun)())
Definition Evaluator.cc:701
void setVariable(const char *name, double value)
Definition Evaluator.cc:692
int status() const
Definition Evaluator.cc:636
bool findVariable(const char *name) const
Definition Evaluator.cc:726