Isis Developer Reference
InlineCalculator.h
Go to the documentation of this file.
1#ifndef InlineCalculator_h
2#define InlineCalculator_h
8/* SPDX-License-Identifier: CC0-1.0 */
9
10// parent class
11#include "Calculator.h"
12
13#include <QList>
14#include <QMap>
15#include <QString>
16#include <QVector>
17
18class QVariant;
19
20namespace Isis {
21
22 class CalculatorVariablePool;
23 class FxBinder;
24 class ParamaterFx;
25 class VoidFx;
26
31 #define CALL_MEMBER_FN(object, ptrToMember) ((object).*(ptrToMember))
32
50
51 public:
52
54 InlineCalculator(const QString &equation);
55 virtual ~InlineCalculator();
56
57 int size() const;
58
59 QString equation() const;
60 bool compile(const QString &equation);
61
62 QVector<double> evaluate(CalculatorVariablePool *variablePool);
63 QVector<double> evaluate();
64
65 protected:
68
69 // Implementations of local/new functions
70 void scalar(const QVariant &scalar);
71 void variable(const QVariant &variable);
72
73 void floatModulus();
74 void radians();
75 void degrees();
76 void logicalOr();
77 void logicalAnd();
78 void pi();
79 void eConstant();
80
81 virtual QString toPostfix(const QString &equation) const;
82 bool isScalar(const QString &scalar);
83 bool isVariable(const QString &str);
84
85 // Derived classes can be added with these methods for customization.
86 // See FxBinder().
87 bool fxExists(const QString &fxname) const;
89
90 virtual bool orphanTokenHandler(const QString &token);
91
92 private:
94 typedef QVector<FxTypePtr> FxEqList;
96 typedef QMap<QString, FxTypePtr> FxPoolType;
97
98 void pushVariables(CalculatorVariablePool *variablePool);
99 CalculatorVariablePool *variables();
100 void popVariables();
101
102 FxTypePtr find(const QString &fxname);
103 void initialize();
104 void destruct();
105
106 FxEqList m_functions;
107 FxPoolType m_fxPool;
108 QString m_equation;
109 QList<CalculatorVariablePool *> m_variablePoolList;
110
111 };
112
113
122 public:
125
126 virtual bool exists(const QString &variable) const;
127 virtual QVector<double> value(const QString &variable,
128 const int &index = 0) const;
129 virtual void add(const QString &key, QVector<double> &values);
130 };
131
132
140 class FxBinder {
141 public:
142 FxBinder(const QString &name);
143 virtual ~FxBinder();
144
145 QString name() const;
146 void execute();
147 void operator()();
148
153 virtual void dispatch() = 0;
154 virtual QVariant args();
155
156 private:
157 QString m_name;
158 };
159
160
169 class InlineVoidFx : public FxBinder {
170 public:
172 typedef void (InlineCalculator::*calcOp)();
173
174 InlineVoidFx(const QString &name, calcOp function,
175 InlineCalculator *calculator);
176 virtual ~InlineVoidFx();
177 void dispatch();
178
179 private:
180 calcOp m_func;
181 InlineCalculator *m_calc;
182 };
183
184
193 class ParameterFx : public FxBinder {
194 public:
196 typedef void (InlineCalculator::*calcOp)(const QVariant &arg);
197
198 ParameterFx(const QString &name, calcOp function,
199 InlineCalculator *calculator);
200 virtual ~ParameterFx();
201 void dispatch();
202
203 private:
204 calcOp m_func;
205 InlineCalculator *m_calc;
206 };
207
208
217 class VoidFx : public FxBinder {
218 public:
220 typedef void (Calculator::*calcOp)();
221
222 VoidFx(const QString &name, calcOp function,
223 InlineCalculator *calculator);
224 virtual ~VoidFx();
225 void dispatch();
226
227 private:
228 calcOp m_func;
229 InlineCalculator *m_calc;
230 };
231
232 // this is a global method, outside of all classes.
233 double floatModulusOperator(double a, double b);
234
235} // Namespace Isis
236
237#endif
Calculator for arrays.
Definition Calculator.h:55
This is a simple class to model a Calculator Variable Pool.
Definition InlineCalculator.h:121
CalculatorVariablePool()
Constructs a CalculatorVariablePool object.
Definition InlineCalculator.cpp:659
virtual void add(const QString &key, QVector< double > &values)
Add a parameter to the variable pool.
Definition InlineCalculator.cpp:711
virtual QVector< double > value(const QString &variable, const int &index=0) const
Return vector of doubles for Calculator functions.
Definition InlineCalculator.cpp:691
virtual bool exists(const QString &variable) const
Returns true so the real error can be reported.
Definition InlineCalculator.cpp:676
~CalculatorVariablePool()
Destroys the CalculatorVariablePool object.
Definition InlineCalculator.cpp:666
This is the parent class to the various function classes.
Definition InlineCalculator.h:140
void operator()()
Executes the function.
Definition InlineCalculator.cpp:757
virtual ~FxBinder()
Destroys the FxBinder object.
Definition InlineCalculator.cpp:730
void execute()
Executes the function.
Definition InlineCalculator.cpp:748
FxBinder(const QString &name)
Constructs a function binder given a name.
Definition InlineCalculator.cpp:723
virtual void dispatch()=0
This method defines how to execute this function.
virtual QVariant args()
Accesses the arguments for this function.
Definition InlineCalculator.cpp:768
QString name() const
The name assigned to this function binder.
Definition InlineCalculator.cpp:739
Provides a calculator for inline equations.
Definition InlineCalculator.h:49
virtual QString toPostfix(const QString &equation) const
Converts the given string from infix to postfix format.
Definition InlineCalculator.cpp:241
void degrees()
Pops the top vector off the current stack and converts from radians to degrees.
Definition InlineCalculator.cpp:375
QVector< double > evaluate()
Evaluate compiled equation with existing variable pool.
Definition InlineCalculator.cpp:218
bool isVariable(const QString &str)
Determines whether the given string is a variable.
Definition InlineCalculator.cpp:275
void logicalOr()
Pops the top two vectors off the current stack and performs a logical or on each pair.
Definition InlineCalculator.cpp:393
void scalar(const QVariant &scalar)
Pushes the given value onto the stack as a scalar.
Definition InlineCalculator.cpp:294
virtual bool orphanTokenHandler(const QString &token)
Default token handler if it is undefined during parsing/compilation.
Definition InlineCalculator.cpp:512
void floatModulus()
Pops the top two vectors off the current stack and performs the floatModulusOperator() on the corresp...
Definition InlineCalculator.cpp:345
void pi()
Pushes the PI constant onto the current stack.
Definition InlineCalculator.cpp:437
void logicalAnd()
Pops the top two vectors off the current stack and performs a logical and on each pair.
Definition InlineCalculator.cpp:417
void eConstant()
Pushes the Euler constant (e) onto the current stack.
Definition InlineCalculator.cpp:446
virtual ~InlineCalculator()
Destroys the InlineCalculator object.
Definition InlineCalculator.cpp:61
void variable(const QVariant &variable)
Pushes the given value onto the stack as a variable.
Definition InlineCalculator.cpp:309
FxBinder * FxTypePtr
Defintion for a FxTypePtr, a pointer to a function binder (FxBinder)
Definition InlineCalculator.h:67
int size() const
Accesses the number of functions, operators, variables, and scalars to be executed.
Definition InlineCalculator.cpp:73
void radians()
Pops the top vector off the current stack and converts from degrees to radians.
Definition InlineCalculator.cpp:360
InlineCalculator()
Constructs an InlineCalculator object by initializing the operator lookup list.
Definition InlineCalculator.cpp:41
bool compile(const QString &equation)
Compiles the given equation for evaluation.
Definition InlineCalculator.cpp:106
bool fxExists(const QString &fxname) const
Determines whether the given function name exists in the current function pool.
Definition InlineCalculator.cpp:461
QString equation() const
Accesses the string representation of the current equation, in postfix format.
Definition InlineCalculator.cpp:85
FxTypePtr addFunction(FxTypePtr function)
Adds a function to the function pool.
Definition InlineCalculator.cpp:478
bool isScalar(const QString &scalar)
Determines whether the given string contains a scalar value (i.e.
Definition InlineCalculator.cpp:255
This class is used to bind function names with corresponding InlineCalculator functions that do not t...
Definition InlineCalculator.h:169
void(InlineCalculator::* calcOp)()
Defines an InlineCalculator function that takes no arguments.
Definition InlineCalculator.h:172
InlineVoidFx(const QString &name, calcOp function, InlineCalculator *calculator)
Constructs an InlineVoid function from the given name, InlineCalculator operator, and InlineCalculato...
Definition InlineCalculator.cpp:781
void dispatch()
Calls the function corresponding to this object using its stored InlineCalculator and InlineCalculato...
Definition InlineCalculator.cpp:799
virtual ~InlineVoidFx()
Destroys the InlineVoidFx object.
Definition InlineCalculator.cpp:791
This class is used to bind function names with corresponding Calculator functions that take a paramet...
Definition InlineCalculator.h:193
virtual ~ParameterFx()
Destroys the ParameterFx object.
Definition InlineCalculator.cpp:825
void dispatch()
Calls the function corresponding to this object using its stored InlineCalculator,...
Definition InlineCalculator.cpp:834
void(InlineCalculator::* calcOp)(const QVariant &arg)
Defines an InlineCalculator function that takes arguments.
Definition InlineCalculator.h:196
ParameterFx(const QString &name, calcOp function, InlineCalculator *calculator)
Constructs a Parameter function from the given name (containing the appropriate parameters),...
Definition InlineCalculator.cpp:815
This class is used to bind function names with corresponding Calculator functions that do not take pa...
Definition InlineCalculator.h:217
void(Calculator::* calcOp)()
Defines a Calculator function that takes no arguments.
Definition InlineCalculator.h:220
virtual ~VoidFx()
Destroys the VoidFx object.
Definition InlineCalculator.cpp:857
void dispatch()
Calls the function corresponding to this object using its stored Calculator and Calculator operator.
Definition InlineCalculator.cpp:865
VoidFx(const QString &name, calcOp function, InlineCalculator *calculator)
Constructs a Void function from the given name, Calculator operator, and Calculator.
Definition InlineCalculator.cpp:847
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
double floatModulusOperator(double a, double b)
Determines the remainder of the quotient a/b whose sign is the same as that of a.
Definition InlineCalculator.cpp:335