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 [[nodiscard]] constexpr EmuTime operator+(EmuDuration::param d) const
40 { return EmuTime(time + d.time); }
41 [[nodiscard]] constexpr EmuTime operator-(EmuDuration::param d) const
42 { assert(time >= d.time);
43 return EmuTime(time - d.time); }
44 constexpr EmuTime& operator+=(EmuDuration::param d)
45 { time += d.time; return *this; }
46 constexpr EmuTime& operator-=(EmuDuration::param d)
47 { assert(time >= d.time);
48 time -= d.time; return *this; }
49 [[nodiscard]] constexpr EmuDuration operator-(EmuTime::param e) const
50 { assert(time >= e.time);
51 return EmuDuration(time - e.time); }
52
53 [[nodiscard]] static constexpr EmuTime zero()
54 {
55 return EmuTime(uint64_t(0));
56 }
57 [[nodiscard]] static constexpr EmuTime infinity()
58 {
59 return EmuTime(std::numeric_limits<uint64_t>::max());
60 }
61
62 template<typename Archive>
63 void serialize(Archive& ar, unsigned /*version*/)
64 {
65 ar.serialize("time", time);
66 }
67
68private:
69 EmuTime() = default; // uninitialized
70 constexpr explicit EmuTime(uint64_t n) : time(n) {}
71
72private:
73 uint64_t time;
74
75 // friends
76 friend EmuTime_param_impl<EmuTime>;
77 friend std::ostream& operator<<(std::ostream& os, EmuTime::param time);
78 friend class DynamicClock;
79 template<unsigned, unsigned> friend class Clock;
80};
81
82template<> struct SerializeAsMemcpy<EmuTime> : std::true_type {};
83
84} // namespace openmsx
85
86#endif
const EmuDuration & param
constexpr double e
Definition Math.hh:21
This file implemented 3 utility functions:
Definition Autofire.cc:9
std::ostream & operator<<(std::ostream &os, EnumTypeName< CacheLineCounters >)
void serialize(Archive &ar, T &t, unsigned version)
constexpr uint128 operator-(const uint128 &a, const uint128 &b)
Definition uint128.hh:187
constexpr uint128 operator+(const uint128 &a, const uint128 &b)
Definition uint128.hh:183
constexpr auto operator<=>(const uint128 &a, const uint128 &b)
Definition uint128.hh:226