UniSet 2.32.1
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 <vector>
23#include <ostream>
24#include "Exceptions.h"
25//--------------------------------------------------------------------------
26namespace uniset
27{
28 // --------------------------------------------------------------------------
29
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, bool _init = false ): myid(id),init_out(_init) {};
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::vector<ChildInfo> OutputList;
100 OutputList outs;
101 virtual void setChildOut();
102
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::vector<InputInfo> InputList;
113 InputList ins;
114
115 ElementID myid;
116 bool init_out;
117
118 private:
119 };
120 // ---------------------------------------------------------------------------
121 class TOR:
122 public Element
123 {
124
125 public:
126 TOR( ElementID id, size_t numbers = 0, bool st = false );
127 virtual ~TOR();
128
129 virtual void setIn( size_t num, long value ) override;
130 virtual long getOut() const override;
131
132 virtual std::string getType() const override
133 {
134 return "OR";
135 }
136
137 protected:
138 TOR(): myout(false) {}
139 bool myout;
140
141
142 private:
143 };
144 // ---------------------------------------------------------------------------
145 class TAND:
146 public TOR
147 {
148
149 public:
150 TAND(ElementID id, size_t numbers = 0, bool st = false );
151 virtual ~TAND();
152
153 virtual void setIn( size_t num, long value ) override;
154 virtual std::string getType() const override
155 {
156 return "AND";
157 }
158
159 protected:
160 TAND() {}
161
162 private:
163 };
164
165 // ---------------------------------------------------------------------------
166 // элемент с одним входом и выходом
167 class TNOT:
168 public Element
169 {
170
171 public:
172 TNOT( ElementID id, bool st = false );
173 virtual ~TNOT();
174
175 virtual long getOut() const override
176 {
177 return ( myout ? 1 : 0 );
178 }
179
181 virtual void setIn( size_t num, long value ) override ;
182 virtual std::string getType() const override
183 {
184 return "NOT";
185 }
186 virtual void addInput( size_t num, long value = 0 ) override {}
187 virtual void delInput( size_t num ) override {}
188
189 protected:
190 TNOT(): myout(false) {}
191 bool myout;
192
193 private:
194 };
195 // --------------------------------------------------------------------------
196 /* элемент с одним управляющим входом, двумя входными параметрами и одним выходом.
197 * выход выбирается между двумя входными параметрами по состоянию управляющего входа:
198 * Входные параметры(ВП 1 и 2) - целочисленные переменные, управляющий вход(УВ) - булева переменная,
199 * выход(ВЫХ) - целочисленная переменная.
200 * Значения входные параметров задаются статически при создании элемента через
201 * параметры sel_true и sel_false, а также можно привязать внешние входы.
202 *
203 * Пример таблицы:
204 *
205 * ВП1(true_inp) | ВП2(false_inp) | УВ | ВЫХ
206 * 50 | 100 | 0 | 100
207 * 50 | 100 | 1 | 50
208
209 *
210 */
211 class TSEL_R:
212 public Element
213 {
214
215 public:
216 TSEL_R( ElementID id, bool st = false, long _sel_false = 0, long _sel_true = 1 );
217 virtual ~TSEL_R();
218
219 virtual long getOut() const override
220 {
221 return myout;
222 }
223
225 virtual void setIn( size_t num, long value ) override ;
226 virtual std::string getType() const override
227 {
228 return "SEL_R";
229 }
230 virtual void addInput( size_t num, long value = 0 ) override {}
231 virtual void delInput( size_t num ) override {}
232
233 protected:
234 TSEL_R(): myout(0), control_inp(false), false_inp(0), true_inp(1) {}
235 long myout; /*<! Selected value */
236 bool control_inp; /*<! Input selection */
237 long true_inp; /*<! Input value 1 when control_inp=true */
238 long false_inp; /*<! Input value 2 when control_inp=false */
239
240 private:
241 };
242 // --------------------------------------------------------------------------
243 /* Элемент с двумя входами и одним выходом.
244 * первый вход для выставления выхода в true.
245 * второй вход для сброса выхода в false.
246 * На вход подается только логическая единица
247 * ,а ноль игнорируется.
248 * Таблица истинности:
249 *
250 * вход 1(set) | вход 2(reset) | выход(out)
251 * 0 | 0 | значение по-умолчанию или предыдущее значение выхода
252 * 0 | 1 | 0
253 * 1 | 0 | 1
254 * 1 | 1 | доминантный вход
255 *
256 */
257 class TRS:
258 public Element
259 {
260
261 public:
262 TRS( ElementID id, bool st = false, bool _dominantReset = false );
263 virtual ~TRS();
264
265 virtual long getOut() const override
266 {
267 return ( myout ? 1 : 0 );
268 }
269
270 virtual void setIn( size_t num, long value ) override ;
271 virtual std::string getType() const override
272 {
273 return "RS";
274 }
275 virtual void addInput( size_t num, long value = 0 ) override {}
276 virtual void delInput( size_t num ) override {}
277
278 protected:
279 TRS(): myout(false), dominantReset(false), set_inp(false), reset_inp(false) {}
280 bool myout; /*<! Output */
281 bool dominantReset; /*<! Dominant reset input (by default: set input is dominant) */
282 bool set_inp; /*<! Set input */
283 bool reset_inp; /*<! Reset input */
284
285 private:
286 };
287 // --------------------------------------------------------------------------
288} // end of namespace uniset
289// ---------------------------------------------------------------------------
290#endif
Definition Element.h:40
virtual void tick()
Definition Element.h:61
Definition Exceptions.h:46
Definition Element.h:32
Definition Element.h:147
Definition Element.h:169
virtual void setIn(size_t num, long value) override
Definition TNOT.cc:38
Definition Element.h:123
Definition Element.h:259
Definition Element.h:213
virtual void setIn(size_t num, long value) override
Definition TSEL_R.cc:42
Definition Calibration.h:27
Definition Element.h:90
Definition Element.h:104