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  typedef const EmuTime param;
18  static param dummy() { return EmuTime(); }
19 #else
20  typedef const EmuTime& param;
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