Minsky
port.h
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2015
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 PORT_H
20 #define PORT_H
21 #include "classdesc_access.h"
22 #include "units.h"
23 #include <error.h>
24 #include <vector>
25 #include <memory>
26 
27 namespace minsky
28 {
29  class Item;
30  class Wire;
31  class Group;
33  typedef std::shared_ptr<Group> GroupPtr;
34 
36  struct PortExclude
37  {
38  // const_cast hack required to work around classdesc::Exclude API
39  PortExclude(const Item& item): m_item(const_cast<Item&>(item)) {}
40  virtual ~PortExclude()=default;
42  void setVariableValue(const std::shared_ptr<VariableValue>& v);
44  std::shared_ptr<VariableValue> getVariableValue() const;
45  virtual bool input() const=0;
46  GroupPtr group() const;
47  protected:
49  std::weak_ptr<VariableValue> variableValue; //refers to variable value representing this port
50  std::vector<Wire*> m_wires;
51  };
52 
53  class Port: public classdesc::Exclude<PortExclude>
54  {
55  public:
56  private:
57  float m_x{0}, m_y{0};
59  friend struct SchemaHelper;
60  Port(const Port&)=delete;
61  void operator=(const Port&)=delete;
62  friend class Wire;
63  public:
65  // this is an accessor to prevent serialisation infinite loops
66  Item& item() {return m_item;};
67  const Item& item() const {return m_item;}
69 
71  std::vector<Wire*> const& wires() const {return m_wires;}
72  std::size_t numWires() const {return m_wires.size();}
73 
75  void eraseWire(Wire*);
77  void deleteWires();
78 
80  virtual bool input() const {return false;}
81 
85  virtual bool multiWireAllowed() const {return false;}
89  virtual void combineInput(double& x, double y) const {x=y;}
91  virtual double identity() const {return 0;}
92 
93  float x() const;
94  float y() const;
95  void moveTo(float x, float y);
97 
98  // destruction of this port must also destroy all attached wires
100 
102  double value() const;
103  Units units(bool) const;
105  Units checkUnits() const {return units(true);}
106  };
107 
108  struct InputPort: public Port
109  {
110  bool input() const override {return true;}
112  };
113 
115  {
116  bool multiWireAllowed() const override {return true;}
118  };
119 }
120 
121 #ifdef CLASSDESC
122 #pragma omit RESTProcess minsky::PortExclude
123 #endif
124 
125 #include "port.cd"
126 #endif
virtual bool input() const
true if input port
Definition: port.h:80
Port(Item &item)
Definition: port.h:96
std::vector< Wire * > const & wires() const
returns a vector of weak references to the wires attached to this port
Definition: port.h:71
Port(const Port &)=delete
void moveTo(float x, float y)
Definition: port.cc:49
MultiWireInputPort(Item &item)
Definition: port.h:117
~Port()
Definition: port.h:99
virtual bool input() const =0
virtual void combineInput(double &x, double y) const
combine two input wires
Definition: port.h:89
Units units(bool) const
Definition: port.cc:107
std::shared_ptr< VariableValue > getVariableValue() const
returns the variableValue associated with this port. May be null if not applicable ...
Definition: port.cc:114
void setVariableValue(const std::shared_ptr< VariableValue > &v)
sets the VariableValue associated with this port. Only for output ports
Definition: port.cc:86
Item & m_item
Definition: port.h:48
void deleteWires()
delete all attached wires
Definition: port.cc:70
double value() const
value associated with this port
Definition: port.cc:92
Units checkUnits() const
dimensional analysis with consistency check
Definition: port.h:105
CLASSDESC_ACCESS(Port)
float m_y
Definition: port.h:57
std::vector< Wire * > m_wires
Definition: port.h:50
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
float m_x
Definition: port.h:57
represents the units (in sense of dimensional analysis) of a variable.
Definition: units.h:34
GroupPtr group() const
Definition: port.cc:55
bool input() const override
true if input port
Definition: port.h:110
float y() const
Definition: port.cc:44
components of Port excluded from reflection
Definition: port.h:36
PortExclude(const Item &item)
Definition: port.h:39
float x() const
Definition: port.cc:39
virtual bool multiWireAllowed() const
true if multiple wires are allowed to connect to an input port, such as an input port of an add opera...
Definition: port.h:85
void operator=(const Port &)=delete
std::shared_ptr< Group > GroupPtr
Definition: port.h:32
InputPort(Item &item)
Definition: port.h:111
std::weak_ptr< VariableValue > variableValue
Definition: port.h:49
std::size_t numWires() const
Definition: port.h:72
Item & item()
owner of this port
Definition: port.h:66
virtual ~PortExclude()=default
void eraseWire(Wire *)
remove wire from wires. No ownership passed.
Definition: port.cc:60
const Item & item() const
owner of this port
Definition: port.h:67
bool multiWireAllowed() const override
true if multiple wires are allowed to connect to an input port, such as an input port of an add opera...
Definition: port.h:116
virtual double identity() const
input port value if no wire attached
Definition: port.h:91