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