UniSet
1.4.0
|
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 "TriggerOR.h" 00025 //--------------------------------------------------------------------------- 00026 template<class Caller, typename InputType> 00027 TriggerOR<Caller,InputType>::TriggerOR(Caller* c, Action a): 00028 cal(c), 00029 act(a) 00030 { 00031 } 00032 00033 template<class Caller, typename InputType> 00034 TriggerOR<Caller,InputType>::~TriggerOR() 00035 { 00036 } 00037 00038 //--------------------------------------------------------------------------- 00039 template<class Caller, typename InputType> 00040 bool TriggerOR<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 TriggerOR<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 TriggerOR<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 TriggerOR<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 TriggerOR<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 // если хоть один вход "1" на выходе "1" 00092 // и прекращаем дальнейший поиск 00093 out = true; 00094 if( old != out ) 00095 { 00096 // try 00097 // { 00098 (cal->*act)(out); 00099 // } 00100 // catch(...){} 00101 } 00102 return; 00103 } 00104 } 00105 00106 out = false; 00107 00108 if( old != out ) 00109 { 00110 // try 00111 // { 00112 (cal->*act)(out); 00113 // } 00114 // catch(...){} 00115 } 00116 } 00117 //--------------------------------------------------------------------------- 00118 template<class Caller, typename InputType> 00119 void TriggerOR<Caller,InputType>::update() 00120 { 00121 (cal->*act)(out); 00122 } 00123 //--------------------------------------------------------------------------- 00124 template<class Caller, typename InputType> 00125 void TriggerOR<Caller,InputType>::reset() 00126 { 00127 out = false; 00128 (cal->*act)(out); 00129 } 00130 //---------------------------------------------------------------------------