Minsky
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 <ecolab.h>
23 #include <xml_pack_base.h>
24 #include <xml_unpack_base.h>
25 
26 // override EcoLab's default CLASSDESC_ACCESS macro
27 #include "classdesc_access.h"
28 
29 #include "item.h"
30 #include "slider.h"
31 
32 #include <vector>
33 #include <cairo/cairo.h>
34 
35 #include <arrays.h>
36 
37 #include "polyBase.h"
38 #include "polyPackBase.h"
39 #include <pack_base.h>
40 #include "operationType.h"
41 
42 namespace ecolab {class Pango;}
43 
44 namespace minsky
45 {
46  class OperationPtr;
47  class Group;
48 
49  class OperationBase: virtual public classdesc::PolyPackBase,
51  {
53  protected:
54  mutable classdesc::Exclude<std::shared_ptr<ecolab::Pango>> cachedPango;
56  void setCachedText(cairo_t*, const std::string&, double) const;
57  public:
58  static constexpr float l=-8, h=12, r=12;
60 
61  virtual std::size_t numPorts() const=0;
63  static OperationBase* create(Type type);
64  virtual Type type() const=0;
65 
66  const OperationBase* operationCast() const override {return this;}
67  OperationBase* operationCast() override {return this;}
68 
70  virtual void iconDraw(cairo_t *) const=0;
71 
73  std::string portValues() const;
74 
75  // returns true if multiple input wires are allowed.
76  bool multiWire() const;
77 
78  // manage the port structures associated with this operation
79  virtual void addPorts();
80 
81  void drawUserFunction(cairo_t* cairo) const;
82 
83  void draw(cairo_t*) const override;
84  void resize(const LassoBox& b) override;
85  float scaleFactor() const override;
86 
88  double value() const override;
89 
92  double arg=1;
93 
95  std::string axis;
96 
99  std::vector<std::string> dimensions() const;
100  Units units(bool check=false) const override;
101 
102  protected:
103 
104  friend struct EvalOpBase;
105  friend struct SchemaHelper;
106  private:
107  Units unitsBinOpCase(bool check) const;
108  };
109 
110 
114  class OperationPtr: public std::shared_ptr<OperationBase>
115  {
116  public:
117  typedef std::shared_ptr<OperationBase> PtrBase;
119  PtrBase(OperationBase::create(type)) {}
120  // reset pointer to a newly created operation
122  OperationPtr clone() const {return OperationPtr(ItemPtr(get()->clone()));}
123  std::size_t use_count() const {return classdesc::shared_ptr<OperationBase>::use_count();}
124  OperationPtr(const PtrBase& x): PtrBase(x) {}
125  OperationPtr& operator=(const PtrBase& x) {PtrBase::operator=(x); return *this;}
126  OperationPtr(const ItemPtr& x):
127  PtrBase(std::dynamic_pointer_cast<OperationBase>(x)) {}
128  };
129 }
130 
131  // for TCL interfacing
132 inline std::ostream& operator<<(std::ostream& x, const std::vector<int>& y)
133 {
134  for (std::size_t i=0; i<y.size(); ++i)
135  x<<(i==0?"":" ")<<y[i];
136  return x;
137 }
138 
139 
140 inline void pack(classdesc::pack_t&,const classdesc::string&,classdesc::ref<ecolab::urand>&) {}
141 inline void unpack(classdesc::pack_t&,const classdesc::string&,classdesc::ref<ecolab::urand>&) {}
142 inline void xml_pack(classdesc::xml_pack_t&,const classdesc::string&,classdesc::ref<ecolab::urand>&) {}
143 inline void xml_unpack(classdesc::xml_unpack_t&,const classdesc::string&,classdesc::ref<ecolab::urand>&) {}
144 
145 
146 
147 #include "operationBase.cd"
148 #include "operationBase.xcd"
149 #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:67
OperationPtr(OperationType::Type type=OperationType::numOps)
classdesc::Exclude< std::shared_ptr< ecolab::Pango > > cachedPango
Definition: operationBase.h:54
void draw(cairo_t *) const override
draw this item into a cairo context
Definition: operation.cc:230
void xml_unpack(classdesc::xml_unpack_t &, const classdesc::string &, classdesc::ref< ecolab::urand > &)
std::shared_ptr< OperationBase > PtrBase
void pack(classdesc::pack_t &, const classdesc::string &, classdesc::ref< ecolab::urand > &)
const OperationBase * operationCast() const override
Definition: operationBase.h:66
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:92
OperationPtr clone() const
void unpack(classdesc::pack_t &, const classdesc::string &, classdesc::ref< ecolab::urand > &)
void xml_pack(classdesc::xml_pack_t &, const classdesc::string &, classdesc::ref< ecolab::urand > &)
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:57
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:58
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
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:58
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:95
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:58
OperationType::Type Type
Definition: operationBase.h:59
double value() const override
current value of output port
Definition: operation.cc:329
CLASSDESC_ACCESS(OperationBase)