openMSX
EmuTime.hh
Go to the documentation of this file.
1#ifndef EMUTIME_HH
2#define EMUTIME_HH
3
4#include "EmuDuration.hh"
5#include "serialize.hh"
6#include <iosfwd>
7#include <cassert>
8#include <limits>
9
10namespace openmsx {
11
12// EmuTime is only a very small class (one 64-bit member). On 64-bit CPUs
13// it's cheaper to pass this by value. On 32-bit CPUs pass-by-reference
14// is cheaper.
15template<typename T, bool C = sizeof(void*) < 8> struct EmuTime_param_impl;
16template<typename T> struct EmuTime_param_impl<T, true> { // pass by reference
17 using param = const T&;
18 static param dummy() { static constexpr auto e = T::zero(); return e; }
19};
20template<typename T> struct EmuTime_param_impl<T, false> { // pass by value
21 using param = T;
22 static param dummy() { return T(); }
23};
24
25class EmuTime
26{
27public:
28 using param = EmuTime_param_impl<EmuTime>::param;
29 static param dummy() { return EmuTime_param_impl<EmuTime>::dummy(); }
30
31 // Note: default copy constructor and assignment operator are ok.
32
33 static constexpr EmuTime makeEmuTime(uint64_t u) { return EmuTime(u); }
34
35 // comparison operators
36 [[nodiscard]] constexpr auto operator<=>(const EmuTime&) const = default;
37
38 // arithmetic operators
39 // TODO these 2 should be 'friend', workaround for pre-gcc-13 bug
40 [[nodiscard]] constexpr EmuTime operator+(const EmuDuration& r) const
41 { return EmuTime(time + r.time); }
42 [[nodiscard]] constexpr EmuTime operator-(const EmuDuration& r) const
43 { assert(time >= r.time);
44 return EmuTime(time - r.time); }
45 constexpr EmuTime& operator+=(EmuDuration::param d)
46 { time += d.time; return *this; }
47 constexpr EmuTime& operator-=(EmuDuration::param d)
48 { assert(time >= d.time);
49 time -= d.time; return *this; }
50 [[nodiscard]] constexpr friend EmuDuration operator-(const EmuTime& l, const EmuTime& r)
51 { assert(l.time >= r.time);
52 return EmuDuration(l.time - r.time); }
53
54 [[nodiscard]] static constexpr EmuTime zero()
55 {
56 return EmuTime(uint64_t(0));
57 }
58 [[nodiscard]] static constexpr EmuTime infinity()
59 {
60 return EmuTime(std::numeric_limits<uint64_t>::max());
61 }
62
63 template<typename Archive>
64 void serialize(Archive& ar, unsigned /*version*/)
65 {
66 ar.serialize("time", time);
67 }
68
69private:
70 EmuTime() = default; // uninitialized
71 constexpr explicit EmuTime(uint64_t n) : time(n) {}
72
73private:
74 uint64_t time;
75
76 // friends
77 friend EmuTime_param_impl<EmuTime>;
78 friend std::ostream& operator<<(std::ostream& os, EmuTime::param time);
79 friend class DynamicClock;
80 template<unsigned, unsigned> friend class Clock;
81};
82
83template<> struct SerializeAsMemcpy<EmuTime> : std::true_type {};
84
85} // namespace openmsx
86
87#endif
const EmuDuration & param
constexpr double e
Definition Math.hh:21
constexpr matMxN< M, N, T > operator-(const matMxN< M, N, T > &A)
Definition gl_mat.hh:181
This file implemented 3 utility functions:
Definition Autofire.cc:11
std::ostream & operator<<(std::ostream &os, EnumTypeName< CacheLineCounters >)
void serialize(Archive &ar, T &t, unsigned version)