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