UniSet  2.0.0
HourGlass.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 // --------------------------------------------------------------------------
00020 // idea: lav@etersoft.ru
00021 // realisation: pv@etersoft.ru, lav@etersoft.ru
00022 // --------------------------------------------------------------------------
00023 #ifndef HourGlass_H_
00024 #define HourGlass_H_
00025 // --------------------------------------------------------------------------
00026 #include "PassiveTimer.h"
00027 // --------------------------------------------------------------------------
00061 class HourGlass
00062 {
00063     public:
00064         HourGlass(): _state(false),_sand(0),_size(0){}
00065         ~HourGlass(){}
00066 
00067         // запустить часы (заново)
00068         inline void run( timeout_t msec )
00069         {
00070             t.setTiming(msec);
00071             _state   = true;
00072             _sand    = msec;
00073             _size    = msec;
00074         }
00075 
00076         inline void reset()
00077         {
00078             run(_size);
00079         }
00080 
00081         // "ёмкость" песочных часов..
00082         inline int duration()
00083         {
00084             return _size;
00085         }
00086         // перевернуть часы
00087         // true - засечь время
00088         // false - перевернуть часы (обратный ход)
00089         // возвращает аргумент (т.е. идёт ли отсчёт времени)
00090         inline bool rotate( bool st )
00091         {
00092             if( st == _state )
00093                 return st;
00094 
00095             _state = st;
00096             if( !_state )
00097             {
00098                 timeout_t cur = t.getCurrent();
00099                 if( cur > _size )
00100                     cur = _size;
00101 
00102                 _sand -= cur;
00103                 if( _sand < 0 )
00104                     _sand = 0;
00105 
00106                 t.setTiming(cur);
00107             }
00108             else
00109             {
00110                 timeout_t cur = t.getCurrent();
00111                 if( cur > _size )
00112                     cur = _size;
00113      
00114                 _sand += cur;
00115                 if( _sand > _size )
00116                     _sand = _size;
00117 
00118                 t.setTiming(_sand);
00119             }
00120             return st;
00121         }
00122 
00123         // получить прошедшее время
00124         inline timeout_t current()
00125         {
00126             return t.getCurrent();
00127         }
00128 
00129         // получить заданное время
00130         inline timeout_t interval()
00131         {
00132             return t.getInterval();
00133         }
00134 
00135         // проверить наступление
00136         inline bool check()
00137         {
00138             // пока часы не "стоят"
00139             // всегда false
00140             if( !_state )
00141                 return false;
00142 
00143             return t.checkTime();
00144         }
00145 
00146         inline bool state(){ return _state; }
00147 
00148         // текущее "насыпавшееся" количество "песка"
00149         inline timeout_t amount()
00150         {
00151             return ( _size - remain() );
00152         }
00153 
00154         // остаток песка (времени)
00155         inline timeout_t remain()
00156         {
00157             timeout_t c = t.getCurrent();
00158             if( c > _size )
00159                 c = _size;
00160             
00161             // _state=false - означает, что песок пересыпается обратно..
00162             if( !_state )
00163             {
00164                 int ret = ( _sand + c );
00165                 if( ret > _size )
00166                     return _size;
00167                 
00168                 return ret;
00169             }
00170 
00171             // _state=true  - означает, что песок пересыпается..
00172             int ret = ( _sand - c );
00173             if( ret < 0 )
00174                 return 0;
00175 
00176             return ret;
00177         }
00178 
00179     protected:
00180         PassiveTimer t;   
00181         bool _state;      
00182         int _sand;        
00183         timeout_t _size;  
00184 };
00185 // --------------------------------------------------------------------------
00186 #endif
00187 // --------------------------------------------------------------------------