UniSet  1.4.0
TriggerAND_template.h
См. документацию.
00001 /* This file is part of the UniSet project
00002  * Copyright (c) 2002 Free Software Foundation, Inc.
00003  * Copyright (c) 2002 Pavel Vainerman
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018  */
00019 // --------------------------------------------------------------------------
00023 // --------------------------------------------------------------------------
00024 #include "TriggerAND.h"
00025 //---------------------------------------------------------------------------
00026 template<class Caller, typename InputType>
00027 TriggerAND<Caller,InputType>::TriggerAND(Caller* c, Action a):
00028 cal(c),
00029 act(a)
00030 {
00031 }
00032 
00033 template<class Caller, typename InputType>
00034 TriggerAND<Caller,InputType>::~TriggerAND()
00035 {
00036 }
00037 
00038 //---------------------------------------------------------------------------
00039 template<class Caller, typename InputType>
00040 bool TriggerAND<Caller,InputType>::commit(InputType num, bool state)
00041 {
00042     typename InputMap::iterator it=inputs.find(num);
00043     if( it!=inputs.end() )
00044     {
00045         inputs[num] = state;
00046         check();    
00047         return true;
00048     }
00049     
00050     return false;
00051 }
00052 
00053 //---------------------------------------------------------------------------
00054 template<class Caller, typename InputType>
00055 void TriggerAND<Caller,InputType>::add(InputType num, bool state)
00056 {
00057     inputs[num] = state;
00058     check();
00059 }
00060 
00061 //---------------------------------------------------------------------------
00062 template<class Caller, typename InputType>
00063 void TriggerAND<Caller,InputType>::remove(InputType num)
00064 {
00065     typename InputMap::iterator it=inputs.find(num);
00066     if( it!=inputs.end() )
00067         inputs.erase(it);
00068 
00069     check();        
00070 }
00071 
00072 //---------------------------------------------------------------------------
00073 template<class Caller, typename InputType>
00074 bool TriggerAND<Caller,InputType>::getState(InputType num)
00075 {
00076     typename InputMap::iterator it=inputs.find(num);
00077     if( it!=inputs.end() )
00078         return it->second;
00079 
00080     return false; // throw NotFound
00081 }
00082 //---------------------------------------------------------------------------
00083 template<class Caller, typename InputType>
00084 void TriggerAND<Caller,InputType>::check()
00085 {
00086     bool old = out;
00087     for( typename InputMap::iterator it=inputs.begin(); it!=inputs.end(); ++it )
00088     {
00089         if( !it->second )
00090         {
00091             // если хоть один вход "0" на выходе "0"
00092             // и прекращаем дальнейший поиск
00093             out = false;
00094             if( old != out )
00095             {
00096 //              try
00097 //              {
00098                     (cal->*act)(out);
00099 //              }
00100 //              catch(...){}
00101             }
00102             return;
00103         }
00104     }
00105     
00106     out = true;
00107     if( old != out )
00108     {
00109 //      try
00110 //      {
00111             (cal->*act)(out);
00112 //      }
00113 //      catch(...){}
00114     }
00115 }
00116 //---------------------------------------------------------------------------
00117 template<class Caller, typename InputType>
00118 void TriggerAND<Caller,InputType>::update()
00119 {
00120     (cal->*act)(out);
00121 }
00122 //---------------------------------------------------------------------------
00123 template<class Caller, typename InputType>
00124 void TriggerAND<Caller,InputType>::reset()
00125 {
00126     out = false;
00127     (cal->*act)(out);
00128 }
00129 //---------------------------------------------------------------------------