VirtualC64 v5.0 beta
Commodore 64 Emulator
Loading...
Searching...
No Matches
Chrono.h
1// -----------------------------------------------------------------------------
2// This file is part of VirtualC64
3//
4// Copyright (C) Dirk W. Hoffmann. www.dirkwhoffmann.de
5// This FILE is dual-licensed. You are free to choose between:
6//
7// - The GNU General Public License v3 (or any later version)
8// - The Mozilla Public License v2
9//
10// SPDX-License-Identifier: GPL-3.0-or-later OR MPL-2.0
11// -----------------------------------------------------------------------------
12
13#pragma once
14
15#include "Types.h"
16#include <ctime>
17
18namespace vc64::util {
19
20class Time {
21
22public:
23
24 i64 ticks = 0;
25
26public:
27
28 static Time now();
29 static Time nanoseconds(i64 value) { return Time(value); }
30 static Time microseconds(i64 value) { return Time(value * 1000); }
31 static Time milliseconds(i64 value) { return Time(value * 1000000); }
32 static Time seconds(i64 value) { return Time(value * 1000000000); }
33 static Time seconds(float value) { return Time(i64(value * 1000000000.f)); }
34 static std::tm local(const std::time_t &time);
35
36 Time() { };
37 Time(i64 value) : ticks(value) { };
38
39 i64 asNanoseconds() const { return ticks; }
40 i64 asMicroseconds() const { return ticks / 1000; }
41 i64 asMilliseconds() const { return ticks / 1000000; }
42 float asSeconds() const { return ticks / 1000000000.f; }
43
44 bool operator==(const Time &rhs) const;
45 bool operator!=(const Time &rhs) const;
46 bool operator<=(const Time &rhs) const;
47 bool operator>=(const Time &rhs) const;
48 bool operator<(const Time &rhs) const;
49 bool operator>(const Time &rhs) const;
50 Time operator+(const Time &rhs) const;
51 Time operator-(const Time &rhs) const;
52 Time operator*(const long i) const;
53 Time operator/(const long i) const;
54 Time& operator+=(const Time &rhs);
55 Time& operator-=(const Time &rhs);
56 Time& operator*=(const long i);
57 Time& operator/=(const long i);
58 Time abs() const;
59 Time diff() const;
60
61 void sleep();
62 void sleepUntil();
63};
64
65class Clock {
66
67 Time start;
68 Time elapsed;
69
70 bool paused = false;
71
72 void updateElapsed();
73
74public:
75
76 Clock();
77
78 Time getElapsedTime();
79
80 Time stop();
81 Time go();
82 Time restart();
83};
84
85class StopWatch {
86
87 string description;
88 Clock clock;
89
90public:
91
92 StopWatch(const string &description = "");
93 ~StopWatch();
94};
95
96}