UniSet  2.24.2
Element.h
1 /*
2  * Copyright (c) 2015 Pavel Vainerman.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, version 2.1.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Lesser Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 // --------------------------------------------------------------------------
17 #ifndef Element_H_
18 #define Element_H_
19 // --------------------------------------------------------------------------
20 #include <memory>
21 #include <string>
22 #include <list>
23 #include <ostream>
24 #include "Exceptions.h"
25 //--------------------------------------------------------------------------
26 namespace uniset
27 {
28  // --------------------------------------------------------------------------
29 
31  public uniset::Exception
32  {
33  public:
34  LogicException(): uniset::Exception("LogicException") {}
35  explicit LogicException( const std::string& err): uniset::Exception(err) {}
36  };
37 
38 
39  class Element
40  {
41  public:
42 
43  typedef std::string ElementID;
44  static const ElementID DefaultElementID;
45 
46  enum InputType
47  {
48  unknown,
49  external,
50  internal
51  };
52 
53  explicit Element( const ElementID& id ): myid(id) {};
54  virtual ~Element() {};
55 
56 
61  virtual void tick() {}
62 
63  virtual void setIn( size_t num, long value ) = 0;
64  virtual long getOut() const = 0;
65 
66  ElementID getId() const;
67 
68  virtual std::string getType() const
69  {
70  return "?type?";
71  }
72 
73  virtual std::shared_ptr<Element> find( const ElementID& id );
74 
75  virtual void addChildOut( std::shared_ptr<Element>& el, size_t in_num );
76  virtual void delChildOut( std::shared_ptr<Element>& el );
77  size_t outCount() const;
78 
79  virtual void addInput( size_t num, long value = 0 );
80  virtual void delInput( size_t num );
81  size_t inCount() const;
82 
83  friend std::ostream& operator<<(std::ostream& os, const Element& el );
84  friend std::ostream& operator<<(std::ostream& os, const std::shared_ptr<Element>& el );
85 
86  protected:
87  Element(): myid(DefaultElementID) {}; // нельзя создать элемент без id
88 
89  struct ChildInfo
90  {
91  ChildInfo(std::shared_ptr<Element> e, size_t n):
92  el(e), num(n) {}
93  ChildInfo(): el(0), num(0) {}
94 
95  std::shared_ptr<Element> el;
96  size_t num;
97  };
98 
99  typedef std::list<ChildInfo> OutputList;
100  OutputList outs;
101  virtual void setChildOut();
102 
103  struct InputInfo
104  {
105  InputInfo(): num(0), value(0), type(unknown) {}
106  InputInfo(size_t n, long v): num(n), value(v), type(unknown) {}
107  size_t num;
108  long value;
109  InputType type;
110  };
111 
112  typedef std::list<InputInfo> InputList;
113  InputList ins;
114 
115  ElementID myid;
116 
117  private:
118 
119 
120  };
121  // ---------------------------------------------------------------------------
122  class TOR:
123  public Element
124  {
125 
126  public:
127  TOR( ElementID id, size_t numbers = 0, bool outstate = false );
128  virtual ~TOR();
129 
130  virtual void setIn( size_t num, long value ) override;
131  virtual long getOut() const override;
132 
133  virtual std::string getType() const override
134  {
135  return "OR";
136  }
137 
138  protected:
139  TOR(): myout(false) {}
140  bool myout;
141 
142 
143  private:
144  };
145  // ---------------------------------------------------------------------------
146  class TAND:
147  public TOR
148  {
149 
150  public:
151  TAND(ElementID id, size_t numbers = 0, bool st = false );
152  virtual ~TAND();
153 
154  virtual void setIn( size_t num, long value ) override;
155  virtual std::string getType() const override
156  {
157  return "AND";
158  }
159 
160  protected:
161  TAND() {}
162 
163  private:
164  };
165 
166  // ---------------------------------------------------------------------------
167  // элемент с одним входом и выходом
168  class TNOT:
169  public Element
170  {
171 
172  public:
173  TNOT( ElementID id, bool out_default );
174  virtual ~TNOT();
175 
176  virtual long getOut() const override
177  {
178  return ( myout ? 1 : 0 );
179  }
180 
182  virtual void setIn( size_t num, long value ) override ;
183  virtual std::string getType() const override
184  {
185  return "NOT";
186  }
187  virtual void addInput( size_t num, long value = 0 ) override {}
188  virtual void delInput( size_t num ) override {}
189 
190  protected:
191  TNOT(): myout(false) {}
192  bool myout;
193 
194  private:
195  };
196  // --------------------------------------------------------------------------
197 } // end of namespace uniset
198 // ---------------------------------------------------------------------------
199 #endif
Definition: Element.h:40
virtual void tick()
Definition: Element.h:61
Definition: Exceptions.h:46
Definition: Element.h:32
Definition: Element.h:148
Definition: Element.h:170
virtual void setIn(size_t num, long value) override
Definition: TNOT.cc:39
Definition: Element.h:124
Definition: CommonEventLoop.h:15
Definition: Element.h:90
Definition: Element.h:104