UniSet  1.4.0
SandClock.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 SandClock_H_
00024 #define SandClock_H_
00025 // --------------------------------------------------------------------------
00026 #include "PassiveTimer.h"
00027 // --------------------------------------------------------------------------
00028 class SandClock
00029 {
00030     public:
00031         SandClock(): _state(false),_sand(0),_size(0){}
00032         ~SandClock(){}
00033     
00034         // запустить часы (заново)
00035         inline void run( int msec )
00036         {
00037             t.setTiming(msec);
00038             _state  = true;
00039             _sand   = msec;
00040             _size   = msec;
00041         }
00042         
00043         inline void reset ()
00044         {
00045             run(_size);
00046         }
00047 
00048         inline int duration()
00049         {
00050             return _size;
00051         }
00052         // перевернуть часы
00053         // true - засечь время
00054         // false - перевернуть часы (обратный ход)
00055         // возвращает аргумент (т.е. идёт ли отсчёт времени)
00056         inline bool rotate( bool st )
00057         {
00058             if( st == _state )
00059                 return st;
00060                 
00061             _state = st;
00062             if( !_state )
00063             {
00064                 int cur = t.getCurrent();
00065                 _sand -= cur;
00066 
00067                 if( _sand < 0 )
00068                     _sand = 0;
00069 
00070 //              std::cout << "перевернули: прошло " << cur
00071 //                          << " осталось " << sand 
00072 //                          << " засекаем " << cur << endl;
00073 
00074                 t.setTiming(cur);
00075             }
00076             else
00077             {
00078                 _sand += t.getCurrent();
00079                 if( _sand > _size )
00080                     _sand = _size;
00081 
00082 //              std::cout << "вернули: прошло " << t.getCurrent()
00083 //                          << " осталось " << sand 
00084 //                          << " засекам " << sand << endl;
00085     
00086                 t.setTiming(_sand);
00087             }
00088             return st;
00089         }
00090 
00091         // получить прошедшее время
00092         // для положения st
00093         inline int current( bool st )
00094         {
00095             return t.getCurrent();
00096         }
00097 
00098         // получить заданное время
00099         // для положения st
00100         inline int interval( bool st )
00101         {
00102             return t.getInterval();
00103         }
00104         
00105         // проверить наступление
00106         inline bool check()
00107         {
00108             // пока часы не "стоят"
00109             // всегда false
00110             if( !_state )
00111                 return false;
00112 
00113             return t.checkTime();
00114         }
00115 
00116         inline bool state(){ return _state; }
00117         
00118     protected:
00119         PassiveTimer t;
00120         bool _state;
00121         int _sand;
00122         int _size;
00123 };
00124 // --------------------------------------------------------------------------
00125 #endif
00126 // --------------------------------------------------------------------------