UniSet  1.7.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 // --------------------------------------------------------------------------
00060 class HourGlass
00061 {
00062     public:
00063         HourGlass(): _state(false),_sand(0),_size(0){}
00064         ~HourGlass(){}
00065     
00066         // запустить часы (заново)
00067         inline void run( timeout_t msec )
00068         {
00069             t.setTiming(msec);
00070             _state  = true;
00071             _sand   = msec;
00072             _size   = msec;
00073         }
00074         
00075         inline void reset()
00076         {
00077             run(_size);
00078         }
00079 
00080         inline int duration()
00081         {
00082             return _size;
00083         }
00084         // перевернуть часы
00085         // true - засечь время
00086         // false - перевернуть часы (обратный ход)
00087         // возвращает аргумент (т.е. идёт ли отсчёт времени)
00088         inline bool rotate( bool st )
00089         {
00090             if( st == _state )
00091                 return st;
00092                 
00093             _state = st;
00094             if( !_state )
00095             {
00096                 int cur = t.getCurrent();
00097                 _sand -= cur;
00098 
00099                 if( _sand < 0 )
00100                     _sand = 0;
00101 
00102 //              std::cout << "перевернули: прошло " << cur
00103 //                          << " осталось " << sand 
00104 //                          << " засекаем " << cur << endl;
00105 
00106                 t.setTiming(cur);
00107             }
00108             else
00109             {
00110                 _sand += t.getCurrent();
00111                 if( _sand > _size )
00112                     _sand = _size;
00113 
00114 //              std::cout << "вернули: прошло " << t.getCurrent()
00115 //                          << " осталось " << sand 
00116 //                          << " засекам " << sand << endl;
00117     
00118                 t.setTiming(_sand);
00119             }
00120             return st;
00121         }
00122 
00123         // получить прошедшее время
00124         // для положения st
00125         inline timeout_t current( bool st )
00126         {
00127             return t.getCurrent();
00128         }
00129 
00130         // получить заданное время
00131         // для положения st
00132         inline timeout_t interval( bool st )
00133         {
00134             return t.getInterval();
00135         }
00136         
00137         // проверить наступление
00138         inline bool check()
00139         {
00140             // пока часы не "стоят"
00141             // всегда false
00142             if( !_state )
00143                 return false;
00144 
00145             return t.checkTime();
00146         }
00147 
00148         inline bool state(){ return _state; }
00149         
00150     protected:
00151         PassiveTimer t;
00152         bool _state;
00153         int _sand;
00154         timeout_t _size;
00155 };
00156 // --------------------------------------------------------------------------
00157 #endif
00158 // --------------------------------------------------------------------------