UniSet  2.6.0
HourGlass.h
1 /*
2  * Copyright (c) 2015 Pavel Vainerman.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, version 2.1.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Lesser Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 // --------------------------------------------------------------------------
17 // idea: lav@etersoft.ru
18 // implementation: pv@etersoft.ru, lav@etersoft.ru
19 // --------------------------------------------------------------------------
20 #ifndef HourGlass_H_
21 #define HourGlass_H_
22 // --------------------------------------------------------------------------
23 #include "PassiveTimer.h"
24 // --------------------------------------------------------------------------
25 namespace uniset
26 {
61  class HourGlass
62  {
63  public:
64  HourGlass() noexcept {}
65  ~HourGlass() noexcept {}
66 
67  // запустить часы (заново)
68  inline void run( timeout_t msec ) noexcept
69  {
70  t.setTiming(msec);
71  _state = true;
72  _sand = msec;
73  _size = msec;
74  }
75 
76  inline void reset() noexcept
77  {
78  run(_size);
79  }
80 
81  // "ёмкость" песочных часов..
82  inline timeout_t duration() const noexcept
83  {
84  return _size;
85  }
86  // перевернуть часы
87  // true - засечь время
88  // false - перевернуть часы (обратный ход)
89  // возвращает аргумент (т.е. идёт ли отсчёт времени)
90  inline bool rotate( bool st ) noexcept
91  {
92  if( st == _state )
93  return st;
94 
95  _state = st;
96 
97  // TODO 24.10.2015 Lav: follow code is very simular to remain function
98  if( !_state )
99  {
100  timeout_t cur = t.getCurrent();
101 
102  if( cur > _size )
103  cur = _size;
104 
105  _sand = ( _sand > cur ) ? (_sand - cur) : 0;
106 
107  t.setTiming(cur);
108  }
109  else
110  {
111  timeout_t cur = t.getCurrent();
112 
113  if( cur > _size )
114  cur = _size;
115 
116  _sand += cur;
117 
118  if( _sand > _size )
119  _sand = _size;
120 
121  t.setTiming(_sand);
122  }
123 
124  return st;
125  }
126 
127  // получить прошедшее время
128  inline timeout_t current() const noexcept
129  {
130  return t.getCurrent();
131  }
132 
133  // получить заданное время
134  inline timeout_t interval() const noexcept
135  {
136  return t.getInterval();
137  }
138 
139  // проверить наступление
140  inline bool check() const noexcept
141  {
142  // пока часы не "стоят"
143  // всегда false
144  if( !_state )
145  return false;
146 
147  return t.checkTime();
148  }
149 
150  inline bool enabled() const noexcept
151  {
152  return _state;
153  }
154 
155  // текущее "насыпавшееся" количество "песка" (прошедшее время)
156  inline timeout_t amount() const noexcept
157  {
158  return ( _size - remain() );
159  }
160 
161  // остаток песка (времени) (оставшееся время)
162  inline timeout_t remain() const noexcept
163  {
164  timeout_t c = t.getCurrent();
165 
166  if( c > _size )
167  c = _size;
168 
169  // _state=false - означает, что песок пересыпается обратно..
170  if( !_state )
171  {
172  timeout_t ret = ( _sand + c );
173 
174  if( ret > _size )
175  return _size;
176 
177  return ret;
178  }
179 
180  // _state=true - означает, что песок пересыпается..
181  int ret = ( _sand - c );
182 
183  if( ret < 0 )
184  return 0;
185 
186  return ret;
187  }
188 
189  protected:
191  bool _state = { false };
192  timeout_t _sand = { 0 };
193  timeout_t _size = { 0 };
194  };
195  // -------------------------------------------------------------------------
196 } // end of uniset namespace
197 // --------------------------------------------------------------------------
198 #endif
199 // --------------------------------------------------------------------------
Пассивный таймер
Definition: PassiveTimer.h:90
Definition: CallbackTimer.h:29
PassiveTimer t
Definition: HourGlass.h:190
virtual timeout_t getCurrent() const noexceptoverride
Definition: PassiveTimer.cc:79
bool _state
Definition: HourGlass.h:191
timeout_t _size
Definition: HourGlass.h:193
virtual bool checkTime() const noexceptoverride
Definition: PassiveTimer.cc:46
virtual timeout_t setTiming(timeout_t msec) noexceptoverride
Definition: PassiveTimer.cc:59
Definition: HourGlass.h:61
virtual timeout_t getInterval() const noexceptoverride
Definition: PassiveTimer.cc:84
timeout_t _sand
Definition: HourGlass.h:192