UniSet  2.6.0
Element.h
00001 /*
00002  * Copyright (c) 2015 Pavel Vainerman.
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU Lesser General Public License as
00006  * published by the Free Software Foundation, version 2.1.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00011  * Lesser General Lesser Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00015  */
00016 // --------------------------------------------------------------------------
00017 #ifndef Element_H_
00018 #define Element_H_
00019 // --------------------------------------------------------------------------
00020 #include <memory>
00021 #include <string>
00022 #include <list>
00023 #include <ostream>
00024 #include "Exceptions.h"
00025 //--------------------------------------------------------------------------
00026 namespace uniset
00027 {
00028 // --------------------------------------------------------------------------
00029 
00030 class LogicException:
00031     public uniset::Exception
00032 {
00033     public:
00034         LogicException(): uniset::Exception("LogicException") {}
00035         explicit LogicException( const std::string& err): uniset::Exception(err) {}
00036 };
00037 
00038 
00039 class Element
00040 {
00041     public:
00042 
00043         typedef std::string ElementID;
00044         static const ElementID DefaultElementID;
00045 
00046         enum InputType
00047         {
00048             unknown,
00049             external,
00050             internal
00051         };
00052 
00053         explicit Element( const ElementID& id ): myid(id) {};
00054         virtual ~Element() {};
00055 
00056 
00061         virtual void tick() {}
00062 
00063         virtual void setIn( size_t num, bool state ) = 0;
00064         virtual bool getOut() const = 0;
00065 
00066         inline ElementID getId() const
00067         {
00068             return myid;
00069         }
00070         virtual std::string getType() const
00071         {
00072             return "?type?";
00073         }
00074 
00075         virtual std::shared_ptr<Element> find( const ElementID& id );
00076 
00077         virtual void addChildOut( std::shared_ptr<Element> el, size_t in_num );
00078         virtual void delChildOut( std::shared_ptr<Element> el );
00079         inline size_t outCount() const
00080         {
00081             return outs.size();
00082         }
00083 
00084         virtual void addInput( size_t num, bool state = false );
00085         virtual void delInput( size_t num );
00086         inline size_t inCount() const
00087         {
00088             return ins.size();
00089         }
00090 
00091         friend std::ostream& operator<<(std::ostream& os, Element& el )
00092         {
00093             return os << "[" << el.getType() << "]" << el.getId();
00094         }
00095 
00096         friend std::ostream& operator<<(std::ostream& os, std::shared_ptr<Element> el )
00097         {
00098             if( el )
00099                 return os << (*(el.get()));
00100 
00101             return os;
00102         }
00103 
00104     protected:
00105         Element(): myid(DefaultElementID) {}; // нельзя создать элемент без id
00106 
00107         struct ChildInfo
00108         {
00109             ChildInfo(std::shared_ptr<Element> e, size_t n):
00110                 el(e), num(n) {}
00111             ChildInfo(): el(0), num(0) {}
00112 
00113             std::shared_ptr<Element> el;
00114             size_t num;
00115         };
00116 
00117         typedef std::list<ChildInfo> OutputList;
00118         OutputList outs;
00119         virtual void setChildOut();
00120 
00121         struct InputInfo
00122         {
00123             InputInfo(): num(0), state(false), type(unknown) {}
00124             InputInfo(size_t n, bool s): num(n), state(s), type(unknown) {}
00125             size_t num;
00126             bool state;
00127             InputType type;
00128         };
00129 
00130         typedef std::list<InputInfo> InputList;
00131         InputList ins;
00132 
00133         ElementID myid;
00134 
00135     private:
00136 
00137 
00138 };
00139 // ---------------------------------------------------------------------------
00140 class TOR:
00141     public Element
00142 {
00143 
00144     public:
00145         TOR( ElementID id, size_t numbers = 0, bool st = false );
00146         virtual ~TOR();
00147 
00148         virtual void setIn( size_t num, bool state ) override;
00149         virtual bool getOut() const override
00150         {
00151             return myout;
00152         }
00153 
00154         virtual std::string getType() const override
00155         {
00156             return "OR";
00157         }
00158 
00159     protected:
00160         TOR(): myout(false) {}
00161         bool myout;
00162 
00163 
00164     private:
00165 };
00166 // ---------------------------------------------------------------------------
00167 class TAND:
00168     public TOR
00169 {
00170 
00171     public:
00172         TAND(ElementID id, size_t numbers = 0, bool st = false );
00173         virtual ~TAND();
00174 
00175         virtual void setIn( size_t num, bool state ) override;
00176         virtual std::string getType() const override
00177         {
00178             return "AND";
00179         }
00180 
00181     protected:
00182         TAND() {}
00183 
00184     private:
00185 };
00186 
00187 // ---------------------------------------------------------------------------
00188 // элемент с одним входом и выходом
00189 class TNOT:
00190     public Element
00191 {
00192 
00193     public:
00194         TNOT( ElementID id, bool out_default );
00195         virtual ~TNOT();
00196 
00197         virtual bool getOut() const override
00198         {
00199             return myout;
00200         }
00201 
00202         /* num игнорируется, т.к. элемент с одним входом
00203          */
00204         virtual void setIn( size_t num, bool state ) override ;
00205         virtual std::string getType() const override
00206         {
00207             return "NOT";
00208         }
00209         virtual void addInput( size_t num, bool state = false ) override {}
00210         virtual void delInput( size_t num ) override {}
00211 
00212     protected:
00213         TNOT(): myout(false) {}
00214         bool myout;
00215 
00216     private:
00217 };
00218 // --------------------------------------------------------------------------
00219 } // end of namespace uniset
00220 // ---------------------------------------------------------------------------
00221 #endif