openMSX
Schedulable.hh
Go to the documentation of this file.
1 #ifndef SCHEDULABLE_HH
2 #define SCHEDULABLE_HH
3 
4 #include "EmuTime.hh"
5 #include "serialize.hh"
6 #include "serialize_meta.hh"
7 #include "serialize_stl.hh"
8 #include <cassert>
9 #include <vector>
10 
11 namespace openmsx {
12 
13 class Scheduler;
14 
15 // For backwards-compatible savestates
17 {
18  template <typename Archive>
19  void serialize(Archive& ar, unsigned /*version*/) {
20  assert(ar.isLoader());
21  ar.serialize("time", time,
22  "type", userData);
23  }
24 
25  EmuTime time = EmuTime::zero();
26  int userData = 0;
27 };
28 
34 {
35 public:
36  Schedulable(const Schedulable&) = delete;
37  Schedulable& operator=(const Schedulable&) = delete;
38 
43  virtual void executeUntil(EmuTime::param time) = 0;
44 
56  virtual void schedulerDeleted();
57 
58  Scheduler& getScheduler() const { return scheduler; }
59 
62  EmuTime::param getCurrentTime() const;
63 
64  template <typename Archive>
65  void serialize(Archive& ar, unsigned version);
66 
67  template <typename Archive>
68  static std::vector<SyncPointBW> serializeBW(Archive& ar) {
69  assert(ar.isLoader());
70  ar.beginTag("Schedulable");
71  std::vector<SyncPointBW> result;
72  ar.serialize("syncPoints", result);
73  ar.endTag("Schedulable");
74  return result;
75  }
76  template <typename Archive>
77  static void restoreOld(Archive& ar, std::vector<Schedulable*> schedulables) {
78  assert(ar.isLoader());
79  for (auto* s : schedulables) {
80  s->removeSyncPoints();
81  }
82  for (auto& old : serializeBW(ar)) {
83  unsigned i = old.userData;
84  if (i < schedulables.size()) {
85  schedulables[i]->setSyncPoint(old.time);
86  }
87  }
88  }
89 
90 protected:
91  explicit Schedulable(Scheduler& scheduler);
92  ~Schedulable();
93 
94  void setSyncPoint(EmuTime::param timestamp);
95  bool removeSyncPoint();
96  void removeSyncPoints();
97  bool pendingSyncPoint() const;
98  bool pendingSyncPoint(EmuTime& result) const;
99 
100 private:
101  Scheduler& scheduler;
102 };
103 REGISTER_BASE_CLASS(Schedulable, "Schedulable");
104 
105 } // namespace openmsx
106 
107 #endif
REGISTER_BASE_CLASS(Connector, "Connector")
static void restoreOld(Archive &ar, std::vector< Schedulable *> schedulables)
Definition: Schedulable.hh:77
Every class that wants to get scheduled at some point must inherit from this class.
Definition: Schedulable.hh:33
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
static std::vector< SyncPointBW > serializeBW(Archive &ar)
Definition: Schedulable.hh:68
Scheduler & getScheduler() const
Definition: Schedulable.hh:58
void serialize(Archive &ar, unsigned)
Definition: Schedulable.hh:19