UniSet 2.32.1
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// --------------------------------------------------------------------------
25namespace uniset
26{
27 // header only
28
64 {
65 public:
66 HourGlass() noexcept {}
67 ~HourGlass() noexcept {}
68
69 // запустить часы (заново)
70 inline void run( timeout_t msec ) noexcept
71 {
72 t.setTiming(msec);
73 _state = true;
74 _sand = msec;
75 _size = msec;
76 }
77
78 inline void reset() noexcept
79 {
80 run(_size);
81 }
82
83 // "ёмкость" песочных часов..
84 inline timeout_t duration() const noexcept
85 {
86 return _size;
87 }
88 // перевернуть часы
89 // true - засечь время
90 // false - перевернуть часы (обратный ход)
91 // возвращает аргумент (т.е. идёт ли отсчёт времени)
92 inline bool rotate( bool st ) noexcept
93 {
94 if( st == _state )
95 return st;
96
97 _state = st;
98
99 // TODO 24.10.2015 Lav: follow code is very simular to remain function
100 if( !_state )
101 {
102 timeout_t cur = t.getCurrent();
103
104 if( cur > _size )
105 cur = _size;
106
107 _sand = ( _sand > cur ) ? (_sand - cur) : 0;
108
109 t.setTiming(cur);
110 }
111 else
112 {
113 timeout_t cur = t.getCurrent();
114
115 if( cur > _size )
116 cur = _size;
117
118 _sand += cur;
119
120 if( _sand > _size )
121 _sand = _size;
122
124 }
125
126 return st;
127 }
128
129 // получить прошедшее время
130 inline timeout_t current() const noexcept
131 {
132 return t.getCurrent();
133 }
134
135 // получить заданное время
136 inline timeout_t interval() const noexcept
137 {
138 return t.getInterval();
139 }
140
141 // проверить наступление
142 inline bool check() const noexcept
143 {
144 // пока часы не "стоят"
145 // всегда false
146 if( !_state )
147 return false;
148
149 return t.checkTime();
150 }
151
152 inline bool enabled() const noexcept
153 {
154 return _state;
155 }
156
157 // текущее "насыпавшееся" количество "песка" (прошедшее время)
158 inline timeout_t amount() const noexcept
159 {
160 return ( _size - remain() );
161 }
162
163 // остаток песка (времени) (оставшееся время)
164 inline timeout_t remain() const noexcept
165 {
166 timeout_t c = t.getCurrent();
167
168 if( c > _size )
169 c = _size;
170
171 // _state=false - означает, что песок пересыпается обратно..
172 if( !_state )
173 {
174 timeout_t ret = ( _sand + c );
175
176 if( ret > _size )
177 return _size;
178
179 return ret;
180 }
181
182 // _state=true - означает, что песок пересыпается..
183 int ret = ( _sand - c );
184
185 if( ret < 0 )
186 return 0;
187
188 return ret;
189 }
190
191 protected:
193 bool _state = { false };
194 timeout_t _sand = { 0 };
195 timeout_t _size = { 0 };
196 };
197 // -------------------------------------------------------------------------
198} // end of uniset namespace
199// --------------------------------------------------------------------------
200#endif
201// --------------------------------------------------------------------------
Definition HourGlass.h:64
timeout_t _sand
Definition HourGlass.h:194
bool _state
Definition HourGlass.h:193
timeout_t _size
Definition HourGlass.h:195
PassiveTimer t
Definition HourGlass.h:192
Пассивный таймер
Definition PassiveTimer.h:94
virtual timeout_t setTiming(timeout_t msec) noexcept override
Definition PassiveTimer.cc:59
virtual bool checkTime() const noexcept override
Definition PassiveTimer.cc:46
virtual timeout_t getInterval() const noexcept override
Definition PassiveTimer.cc:84
virtual timeout_t getCurrent() const noexcept override
Definition PassiveTimer.cc:79
Definition Calibration.h:27