EmuTime.hh 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #ifndef EMUTIME_HH
  2. #define EMUTIME_HH
  3. #include "EmuDuration.hh"
  4. #include "serialize.hh"
  5. #include <iosfwd>
  6. #include <cassert>
  7. #include <limits>
  8. namespace openmsx {
  9. // EmuTime is only a very small class (one 64-bit member). On 64-bit CPUs
  10. // it's cheaper to pass this by value. On 32-bit CPUs pass-by-reference
  11. // is cheaper.
  12. template <typename T, bool C = sizeof(void*) < 8> struct EmuTime_param_impl;
  13. template <typename T> struct EmuTime_param_impl<T, true> { // pass by reference
  14. using param = const T&;
  15. static param dummy() { static constexpr auto e = T::zero(); return e; }
  16. };
  17. template <typename T> struct EmuTime_param_impl<T, false> { // pass by value
  18. using param = const T;
  19. static param dummy() { return T(); }
  20. };
  21. class EmuTime
  22. {
  23. public:
  24. using param = EmuTime_param_impl<EmuTime>::param;
  25. static param dummy() { return EmuTime_param_impl<EmuTime>::dummy(); }
  26. // Note: default copy constructor and assigment operator are ok.
  27. static constexpr EmuTime makeEmuTime(uint64_t u) { return EmuTime(u); }
  28. // comparison operators
  29. constexpr bool operator==(EmuTime::param e) const
  30. { return time == e.time; }
  31. constexpr bool operator!=(EmuTime::param e) const
  32. { return time != e.time; }
  33. constexpr bool operator< (EmuTime::param e) const
  34. { return time < e.time; }
  35. constexpr bool operator<=(EmuTime::param e) const
  36. { return time <= e.time; }
  37. constexpr bool operator> (EmuTime::param e) const
  38. { return time > e.time; }
  39. constexpr bool operator>=(EmuTime::param e) const
  40. { return time >= e.time; }
  41. // arithmetic operators
  42. constexpr EmuTime operator+(EmuDuration::param d) const
  43. { return EmuTime(time + d.time); }
  44. constexpr EmuTime operator-(EmuDuration::param d) const
  45. { assert(time >= d.time);
  46. return EmuTime(time - d.time); }
  47. constexpr EmuTime& operator+=(EmuDuration::param d)
  48. { time += d.time; return *this; }
  49. constexpr EmuTime& operator-=(EmuDuration::param d)
  50. { assert(time >= d.time);
  51. time -= d.time; return *this; }
  52. constexpr EmuDuration operator-(EmuTime::param e) const
  53. { assert(time >= e.time);
  54. return EmuDuration(time - e.time); }
  55. static constexpr EmuTime zero()
  56. {
  57. return EmuTime(uint64_t(0));
  58. }
  59. static constexpr EmuTime infinity()
  60. {
  61. return EmuTime(std::numeric_limits<uint64_t>::max());
  62. }
  63. template<typename Archive>
  64. void serialize(Archive& ar, unsigned /*version*/)
  65. {
  66. ar.serialize("time", time);
  67. }
  68. private:
  69. EmuTime() = default; // uninitialized
  70. constexpr explicit EmuTime(uint64_t n) : time(n) {}
  71. uint64_t time;
  72. // friends
  73. friend EmuTime_param_impl<EmuTime>;
  74. friend std::ostream& operator<<(std::ostream& os, EmuTime::param time);
  75. friend class DynamicClock;
  76. template<unsigned, unsigned> friend class Clock;
  77. };
  78. template<> struct SerializeAsMemcpy<EmuTime> : std::true_type {};
  79. } // namespace openmsx
  80. #endif