Minsky: 3.17.0
operationBase.h
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2012
3  @author Russell Standish
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifndef OPERATIONBASE_H
20 #define OPERATIONBASE_H
21 
22 #include "classdesc_access.h"
23 
24 #include "item.h"
25 #include "slider.h"
26 
27 #include <vector>
28 #include <cairo/cairo.h>
29 
30 #include <arrays.h>
31 
32 #include "polyBase.h"
33 #include "polyPackBase.h"
34 #include <pack_base.h>
35 #include "operationType.h"
36 
37 namespace ecolab {class Pango;}
38 
39 namespace minsky
40 {
41  class OperationPtr;
42  class Group;
43 
44  class OperationBase: virtual public classdesc::PolyPackBase,
46  {
48  protected:
49  mutable classdesc::Exclude<std::shared_ptr<ecolab::Pango>> cachedPango;
51  void setCachedText(cairo_t*, const std::string&, double) const;
52  public:
53  static constexpr float l=-8, h=12, r=12;
55 
56  virtual std::size_t numPorts() const=0;
58  static OperationBase* create(Type type);
59  virtual Type type() const=0;
60 
61  const OperationBase* operationCast() const override {return this;}
62  OperationBase* operationCast() override {return this;}
63 
65  virtual void iconDraw(cairo_t *) const=0;
66 
68  std::string portValues() const;
69 
70  // returns true if multiple input wires are allowed.
71  bool multiWire() const;
72 
73  // manage the port structures associated with this operation
74  virtual void addPorts();
75 
76  void drawUserFunction(cairo_t* cairo) const;
77 
78  void draw(cairo_t*) const override;
79  void resize(const LassoBox& b) override;
80  float scaleFactor() const override;
81 
83  double value() const override;
84 
87  double arg=1;
88 
90  std::string axis;
91 
94  std::vector<std::string> dimensions() const;
95  Units units(bool check=false) const override;
96 
97  protected:
98 
99  friend struct EvalOpBase;
100  friend struct SchemaHelper;
101  private:
102  Units unitsBinOpCase(bool check) const;
103  };
104 
105 
109  class OperationPtr: public std::shared_ptr<OperationBase>
110  {
111  public:
112  typedef std::shared_ptr<OperationBase> PtrBase;
114  PtrBase(OperationBase::create(type)) {}
115  // reset pointer to a newly created operation
117  OperationPtr clone() const {return OperationPtr(ItemPtr(get()->clone()));}
118  std::size_t use_count() const {return classdesc::shared_ptr<OperationBase>::use_count();}
119  OperationPtr(const PtrBase& x): PtrBase(x) {}
120  OperationPtr& operator=(const PtrBase& x) {PtrBase::operator=(x); return *this;}
121  OperationPtr(const ItemPtr& x):
122  PtrBase(std::dynamic_pointer_cast<OperationBase>(x)) {}
123  };
124 }
125 
126  // for TCL interfacing
127 inline std::ostream& operator<<(std::ostream& x, const std::vector<int>& y)
128 {
129  for (std::size_t i=0; i<y.size(); ++i)
130  x<<(i==0?"":" ")<<y[i];
131  return x;
132 }
133 
134 
135 #include "operationBase.cd"
136 #include "operationBase.xcd"
137 #endif
void setCachedText(cairo_t *, const std::string &, double) const
check if cachedPango is up to date, and if not recreate
Definition: operation.cc:221
OperationBase * operationCast() override
Definition: operationBase.h:62
OperationPtr(OperationType::Type type=OperationType::numOps)
classdesc::Exclude< std::shared_ptr< ecolab::Pango > > cachedPango
Definition: operationBase.h:49
void draw(cairo_t *) const override
draw this item into a cairo context
Definition: operation.cc:230
std::shared_ptr< OperationBase > PtrBase
const OperationBase * operationCast() const override
Definition: operationBase.h:61
std::vector< std::string > dimensions() const
return dimension names of tensor object attached to input if binary op, then the union of dimension n...
Definition: operation.cc:348
double arg
operation argument. For example, the offset used in a difference operator, or binsize in a binning op...
Definition: operationBase.h:87
OperationPtr clone() const
shared_ptr class for polymorphic operation objects. Note, you may assume that this pointer is always ...
OperationPtr & operator=(const PtrBase &x)
represents rectangular region of a lasso operation
Definition: lasso.h:28
void drawUserFunction(cairo_t *cairo) const
Definition: operation.cc:143
STL namespace.
std::string portValues() const
returns a list of values the ports currently have
Definition: operation.cc:561
std::shared_ptr< Item > ItemPtr
Definition: item.h:55
OperationPtr(const PtrBase &x)
Units units(bool check=false) const override
compute the dimensional units
Definition: operation.cc:467
virtual std::size_t numPorts() const =0
OperationPtr(OperationBase *op)
static constexpr float l
Definition: operationBase.h:53
std::size_t use_count() const
float scaleFactor() const override
factor by which item has been resized
Definition: operation.cc:135
static constexpr float h
Definition: operationBase.h:53
represents the units (in sense of dimensional analysis) of a variable.
Definition: units.h:34
static OperationBase * create(Type type)
factory method.
Definition: operation.cc:545
void resize(const LassoBox &b) override
resize this item on the canvas
Definition: operation.cc:321
bool multiWire() const
Definition: operation.cc:85
OperationPtr(const ItemPtr &x)
std::string axis
axis selector in tensor operations
Definition: operationBase.h:90
virtual Type type() const =0
Units unitsBinOpCase(bool check) const
Definition: operation.cc:394
virtual void addPorts()
Definition: operation.cc:99
virtual void iconDraw(cairo_t *) const =0
visual representation of operation on the canvas
static constexpr float r
Definition: operationBase.h:53
OperationType::Type Type
Definition: operationBase.h:54
double value() const override
current value of output port
Definition: operation.cc:329
CLASSDESC_ACCESS(OperationBase)