OpenCurves  0.9
plotbinaryoperator.h
1 //
2 // author Kazys Stepanas
3 //
4 // Copyright (c) CSIRO 2015
5 //
6 #ifndef PLOTBINARYOPERATOR_H_
7 #define PLOTBINARYOPERATOR_H_
8 
9 #include "plotsconfig.h"
10 
11 #include "plotexpression.h"
12 
13 #include <QTextStream>
14 
22 {
23 public:
28  : _left(left), _right(right) {}
29 
32 
35  inline void setLeft(PlotExpression *left) { _left = left; }
36 
39  inline const PlotExpression *left() const { return _left; }
40 
43  inline void setRight(PlotExpression *right) { _right = right; }
44 
47  inline const PlotExpression *right() const { return _right; }
48 
52  virtual BindResult bind(const QList<PlotInstance *> &curves, PlotBindingTracker &bindTracker, PlotExpressionBindDomain &domain, bool repeatLastBinding = false);
53 
55  virtual void unbind();
56 
59  bool explicitTime() const override;
60 
61 private:
62  PlotExpression *_left;
63  PlotExpression *_right;
64 };
65 
66 
81 template <class Operator>
83 {
84 public:
86  inline PlotBinaryOperatorT() : PlotBinaryOperator(nullptr, nullptr) {}
87 
92  inline PlotBinaryOperatorT(PlotExpression *left, PlotExpression *right, const QString &opstr = QString())
93  : PlotBinaryOperator(left, right)
94  , _opStr(opstr) {}
95 
103  virtual inline double sample(double sampleTime) const
104  {
105  return Operator()(left()->sample(sampleTime), right()->sample(sampleTime));
106  }
107 
110  inline const QString &opStr() const { return _opStr; }
111 
114  inline void setOpStr(const QString &str) { _opStr = str; }
115 
117  virtual PlotExpression *clone() const
118  {
120  op->setOpStr(_opStr);
121  return op;
122  }
123 
124 private:
127  virtual QString stringExpression() const
128  {
129  QString str;
130  QTextStream stream(&str);
131  stream << left()->toString() << _opStr << right()->toString();
132  return str;
133  }
134 
135  QString _opStr;
136 };
137 
138 #endif // PLOTBINARYOPERATOR_H_
const PlotExpression * right() const
Get the right branch.
Definition: plotbinaryoperator.h:47
Supports multiple bindings of the same expression to different curves.
Definition: plotbindingtracker.h:34
virtual PlotExpression * clone() const
Deep clone.
Definition: plotbinaryoperator.h:117
void setLeft(PlotExpression *left)
Set the left branch.
Definition: plotbinaryoperator.h:35
virtual double sample(double sampleTime) const
Sample the branches and evaluate the results.
Definition: plotbinaryoperator.h:103
An extension of PlotExpression defining a binary operation.
Definition: plotbinaryoperator.h:21
virtual BindResult bind(const QList< PlotInstance * > &curves, PlotBindingTracker &bindTracker, PlotExpressionBindDomain &domain, bool repeatLastBinding=false)
Binds the left and right branch expressions.
Core data for binding a PlotExpression.
Definition: plotexpressionbinddomain.h:49
PlotBinaryOperatorT(PlotExpression *left, PlotExpression *right, const QString &opstr=QString())
Branched constructor.
Definition: plotbinaryoperator.h:92
bool explicitTime() const override
Is the generated expression sensitive to changes in the time domain?
virtual PlotExpression * clone() const =0
Performs a deep clone of the PlotExpression.
BindResult
Return values for the PlotExpression::bind() method.
Definition: plotexpressionbinddomain.h:15
PlotBinaryOperatorT()
Default constructor: left and right are null.
Definition: plotbinaryoperator.h:86
QString toString() const
Converts the PlotExpression into a parseable string form.
Definition: plotexpression.h:147
const PlotExpression * left() const
Get the left branch.
Definition: plotbinaryoperator.h:39
const QString & opStr() const
Return the string used to combine left and right branches.
Definition: plotbinaryoperator.h:110
A template binary operator, using a functional object to evaluate the expression. ...
Definition: plotbinaryoperator.h:82
virtual void unbind()
Unbind left and right branches.
virtual double sample(double sampleTime) const =0
Called to generate a sample at sampleTime.
void setOpStr(const QString &str)
Sets the string used to combine left and right branches.
Definition: plotbinaryoperator.h:114
A PlotExpression represents an operation in a plot equation.
Definition: plotexpression.h:91
~PlotBinaryOperator()
Destructor, destroying left and right.
PlotBinaryOperator(PlotExpression *left, PlotExpression *right)
Create a binary operator on the given left and right expressions.
Definition: plotbinaryoperator.h:27
void setRight(PlotExpression *right)
Set the right branch.
Definition: plotbinaryoperator.h:43