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