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