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