File: CountdownTimer.h

package info (click to toggle)
tmatrix 1.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 244 kB
  • sloc: cpp: 1,214; ansic: 338; csh: 52; sh: 25; makefile: 8
file content (42 lines) | stat: -rw-r--r-- 810 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
 * Copyright (C) 2018-2021 Miloš Stojanović
 *
 * SPDX-License-Identifier: GPL-2.0-only
 */

#ifndef COUNTDOWN_TIMER_H
#define COUNTDOWN_TIMER_H

#include <stdexcept>
#include "Active.h"

class CountdownTimer final : public Active {
	int StartingTime;
	int CurrentTime;
public:
	CountdownTimer(int ST) : StartingTime{ST}, CurrentTime{ST} {}
	CountdownTimer(int ST, int CT) : StartingTime{ST}, CurrentTime{CT} {
		if (CT > ST) {
			throw std::invalid_argument("Current time is greater than starting time.");
		}
	}

	bool HasExpired() const { return CurrentTime <= 0; }
	bool IsZeroTimer() const { return StartingTime == 0; }

	void Update() final
	{
		CurrentTime--;
	}
	void Reset()
	{
		CurrentTime = StartingTime;
	}
	void ResetWithStartingTime(int ST)
	{
		StartingTime = ST;
		Reset();
	}
};

#endif