|
UniSet
2.2.1
|
00001 #ifndef Element_H_ 00002 #define Element_H_ 00003 // -------------------------------------------------------------------------- 00004 #include <memory> 00005 #include <string> 00006 #include <list> 00007 #include <ostream> 00008 #include "Exceptions.h" 00009 // -------------------------------------------------------------------------- 00010 00011 class LogicException: 00012 public UniSetTypes::Exception 00013 { 00014 public: 00015 LogicException(): UniSetTypes::Exception("LogicException") {} 00016 explicit LogicException( const std::string& err): UniSetTypes::Exception(err) {} 00017 }; 00018 00019 00020 class Element 00021 { 00022 public: 00023 00024 typedef std::string ElementID; 00025 static const ElementID DefaultElementID; 00026 00027 enum InputType 00028 { 00029 unknown, 00030 external, 00031 internal 00032 }; 00033 00034 explicit Element( const ElementID& id ): myid(id) {}; 00035 virtual ~Element() {}; 00036 00037 00042 virtual void tick() {} 00043 00044 virtual void setIn( int num, bool state ) = 0; 00045 virtual bool getOut() = 0; 00046 00047 inline ElementID getId() 00048 { 00049 return myid; 00050 } 00051 virtual std::string getType() 00052 { 00053 return "?type?"; 00054 } 00055 00056 virtual std::shared_ptr<Element> find( const ElementID& id ); 00057 00058 virtual void addChildOut( std::shared_ptr<Element> el, int in_num ); 00059 virtual void delChildOut( std::shared_ptr<Element> el ); 00060 inline int outCount() 00061 { 00062 return outs.size(); 00063 } 00064 00065 virtual void addInput( int num, bool state = false ); 00066 virtual void delInput( int num ); 00067 inline int inCount() 00068 { 00069 return ins.size(); 00070 } 00071 00072 friend std::ostream& operator<<(std::ostream& os, Element& el ) 00073 { 00074 return os << "[" << el.getType() << "]" << el.getId(); 00075 } 00076 00077 friend std::ostream& operator<<(std::ostream& os, std::shared_ptr<Element> el ) 00078 { 00079 if( el ) 00080 return os << (*(el.get())); 00081 00082 return os; 00083 } 00084 00085 protected: 00086 Element(): myid(DefaultElementID) {}; // нельзя создать элемент без id 00087 00088 struct ChildInfo 00089 { 00090 ChildInfo(std::shared_ptr<Element> e, int n): 00091 el(e), num(n) {} 00092 ChildInfo(): el(0), num(0) {} 00093 00094 std::shared_ptr<Element> el; 00095 int num; 00096 }; 00097 00098 typedef std::list<ChildInfo> OutputList; 00099 OutputList outs; 00100 virtual void setChildOut(); 00101 00102 00103 struct InputInfo 00104 { 00105 InputInfo(): num(0), state(false), type(unknown) {} 00106 InputInfo(int n, bool s): num(n), state(s), type(unknown) {} 00107 int num; 00108 bool state; 00109 InputType type; 00110 }; 00111 00112 typedef std::list<InputInfo> InputList; 00113 InputList ins; 00114 00115 ElementID myid; 00116 00117 private: 00118 00119 00120 }; 00121 // --------------------------------------------------------------------------- 00122 class TOR: 00123 public Element 00124 { 00125 00126 public: 00127 TOR( ElementID id, unsigned int numbers = 0, bool st = false ); 00128 virtual ~TOR(); 00129 00130 virtual void setIn( int num, bool state ) override; 00131 virtual bool getOut() override 00132 { 00133 return myout; 00134 } 00135 00136 virtual std::string getType() override 00137 { 00138 return "OR"; 00139 } 00140 00141 protected: 00142 TOR(): myout(false) {} 00143 bool myout; 00144 00145 00146 private: 00147 }; 00148 // --------------------------------------------------------------------------- 00149 class TAND: 00150 public TOR 00151 { 00152 00153 public: 00154 TAND( ElementID id, int numbers = 0, bool st = false ); 00155 virtual ~TAND(); 00156 00157 virtual void setIn( int num, bool state ) override; 00158 virtual std::string getType() override 00159 { 00160 return "AND"; 00161 } 00162 00163 protected: 00164 TAND() {} 00165 00166 private: 00167 }; 00168 00169 // --------------------------------------------------------------------------- 00170 // элемент с одним входом и выходом 00171 class TNOT: 00172 public Element 00173 { 00174 00175 public: 00176 TNOT( ElementID id, bool out_default ); 00177 virtual ~TNOT(); 00178 00179 virtual bool getOut() override 00180 { 00181 return myout; 00182 } 00183 00184 /* num игнорируется, т.к. элемент с одним входом 00185 */ 00186 virtual void setIn( int num, bool state ) override ; 00187 virtual std::string getType() override 00188 { 00189 return "NOT"; 00190 } 00191 virtual void addInput( int num, bool state = false ) override {} 00192 virtual void delInput( int num ) override {} 00193 00194 protected: 00195 TNOT(): myout(false) {} 00196 bool myout; 00197 00198 private: 00199 }; 00200 00201 // --------------------------------------------------------------------------- 00202 #endif
1.7.6.1