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 <iosfwd>
6 #include <cassert>
7 
8 namespace openmsx {
9 
10 // EmuTime is only a very small class (one 64-bit member). On 64-bit CPUs
11 // it's cheaper to pass this by value. On 32-bit CPUs pass-by-reference
12 // is cheaper.
13 template <typename T, bool C = sizeof(void*) < 8> struct EmuTime_param_impl;
14 template <typename T> struct EmuTime_param_impl<T, true> { // pass by reference
15  using param = const T&;
16  static param dummy() { return T::zero; }
17 };
18 template <typename T> struct EmuTime_param_impl<T, false> { // pass by value
19  using param = const T;
20  static param dummy() { return T(); }
21 };
22 
23 class EmuTime
24 {
25 public:
26  using param = EmuTime_param_impl<EmuTime>::param;
27  static param dummy() { return EmuTime_param_impl<EmuTime>::dummy(); }
28 
29  // Note: default copy constructor and assigment operator are ok.
30 
31  static EmuTime makeEmuTime(uint64_t u) { return EmuTime(u); }
32 
33  // comparison operators
34  bool operator==(EmuTime::param e) const
35  { return time == e.time; }
36  bool operator!=(EmuTime::param e) const
37  { return time != e.time; }
38  bool operator< (EmuTime::param e) const
39  { return time < e.time; }
40  bool operator<=(EmuTime::param e) const
41  { return time <= e.time; }
42  bool operator> (EmuTime::param e) const
43  { return time > e.time; }
44  bool operator>=(EmuTime::param e) const
45  { return time >= e.time; }
46 
47  // arithmetic operators
48  const EmuTime operator+(EmuDuration::param d) const
49  { return EmuTime(time + d.time); }
50  const EmuTime operator-(EmuDuration::param d) const
51  { assert(time >= d.time);
52  return EmuTime(time - d.time); }
53  EmuTime& operator+=(EmuDuration::param d)
54  { time += d.time; return *this; }
55  EmuTime& operator-=(EmuDuration::param d)
56  { assert(time >= d.time);
57  time -= d.time; return *this; }
58  const EmuDuration operator-(EmuTime::param e) const
59  { assert(time >= e.time);
60  return EmuDuration(time - e.time); }
61 
62  static const EmuTime zero;
63  static const EmuTime infinity;
64 
65  template<typename Archive>
66  void serialize(Archive& ar, unsigned version);
67 
68 private:
69  EmuTime() {} // uninitialized
70  explicit EmuTime(uint64_t n) : time(n) {}
71 
72  uint64_t time;
73 
74  // friends
75  friend EmuTime_param_impl<EmuTime>;
76  friend std::ostream& operator<<(std::ostream& os, EmuTime::param time);
77  template<unsigned, unsigned> friend class Clock;
78  friend class DynamicClock;
79 };
80 
81 std::ostream& operator <<(std::ostream& os, EmuTime::param e);
82 
83 } // namespace openmsx
84 
85 #endif
bool operator>=(string_view x, string_view y)
Definition: string_view.hh:124
bool operator>(string_view x, string_view y)
Definition: string_view.hh:122
bool operator==(string_view x, string_view y)
Definition: string_view.hh:115
uint128 operator-(const uint128 &a, const uint128 &b)
Definition: uint128.hh:160
const EmuDuration & param
Definition: EmuDuration.hh:24
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
bool operator<=(string_view x, string_view y)
Definition: string_view.hh:123
uint128 operator+(const uint128 &a, const uint128 &b)
Definition: uint128.hh:156
std::ostream & operator<<(std::ostream &os, EmuTime::param time)
Definition: EmuTime.cc:11
void serialize(Archive &ar, T &t, unsigned version)
bool operator!=(string_view x, string_view y)
Definition: string_view.hh:121