UniSet  2.6.0
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, bool state ) = 0;
64  virtual bool getOut() const = 0;
65 
66  inline ElementID getId() const
67  {
68  return myid;
69  }
70  virtual std::string getType() const
71  {
72  return "?type?";
73  }
74 
75  virtual std::shared_ptr<Element> find( const ElementID& id );
76 
77  virtual void addChildOut( std::shared_ptr<Element> el, size_t in_num );
78  virtual void delChildOut( std::shared_ptr<Element> el );
79  inline size_t outCount() const
80  {
81  return outs.size();
82  }
83 
84  virtual void addInput( size_t num, bool state = false );
85  virtual void delInput( size_t num );
86  inline size_t inCount() const
87  {
88  return ins.size();
89  }
90 
91  friend std::ostream& operator<<(std::ostream& os, Element& el )
92  {
93  return os << "[" << el.getType() << "]" << el.getId();
94  }
95 
96  friend std::ostream& operator<<(std::ostream& os, std::shared_ptr<Element> el )
97  {
98  if( el )
99  return os << (*(el.get()));
100 
101  return os;
102  }
103 
104  protected:
105  Element(): myid(DefaultElementID) {}; // нельзя создать элемент без id
106 
107  struct ChildInfo
108  {
109  ChildInfo(std::shared_ptr<Element> e, size_t n):
110  el(e), num(n) {}
111  ChildInfo(): el(0), num(0) {}
112 
113  std::shared_ptr<Element> el;
114  size_t num;
115  };
116 
117  typedef std::list<ChildInfo> OutputList;
118  OutputList outs;
119  virtual void setChildOut();
120 
121  struct InputInfo
122  {
123  InputInfo(): num(0), state(false), type(unknown) {}
124  InputInfo(size_t n, bool s): num(n), state(s), type(unknown) {}
125  size_t num;
126  bool state;
127  InputType type;
128  };
129 
130  typedef std::list<InputInfo> InputList;
131  InputList ins;
132 
133  ElementID myid;
134 
135  private:
136 
137 
138  };
139  // ---------------------------------------------------------------------------
140  class TOR:
141  public Element
142  {
143 
144  public:
145  TOR( ElementID id, size_t numbers = 0, bool st = false );
146  virtual ~TOR();
147 
148  virtual void setIn( size_t num, bool state ) override;
149  virtual bool getOut() const override
150  {
151  return myout;
152  }
153 
154  virtual std::string getType() const override
155  {
156  return "OR";
157  }
158 
159  protected:
160  TOR(): myout(false) {}
161  bool myout;
162 
163 
164  private:
165  };
166  // ---------------------------------------------------------------------------
167  class TAND:
168  public TOR
169  {
170 
171  public:
172  TAND(ElementID id, size_t numbers = 0, bool st = false );
173  virtual ~TAND();
174 
175  virtual void setIn( size_t num, bool state ) override;
176  virtual std::string getType() const override
177  {
178  return "AND";
179  }
180 
181  protected:
182  TAND() {}
183 
184  private:
185  };
186 
187  // ---------------------------------------------------------------------------
188  // элемент с одним входом и выходом
189  class TNOT:
190  public Element
191  {
192 
193  public:
194  TNOT( ElementID id, bool out_default );
195  virtual ~TNOT();
196 
197  virtual bool getOut() const override
198  {
199  return myout;
200  }
201 
202  /* num игнорируется, т.к. элемент с одним входом
203  */
204  virtual void setIn( size_t num, bool state ) override ;
205  virtual std::string getType() const override
206  {
207  return "NOT";
208  }
209  virtual void addInput( size_t num, bool state = false ) override {}
210  virtual void delInput( size_t num ) override {}
211 
212  protected:
213  TNOT(): myout(false) {}
214  bool myout;
215 
216  private:
217  };
218  // --------------------------------------------------------------------------
219 } // end of namespace uniset
220 // ---------------------------------------------------------------------------
221 #endif
Definition: Element.h:39
Definition: Element.h:140
virtual void tick()
Definition: Element.h:61
Definition: Element.h:121
Definition: CallbackTimer.h:29
Definition: Element.h:189
Definition: Exceptions.h:44
Definition: Element.h:107
Definition: Element.h:30
Definition: Element.h:167