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