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 };
104 
105 } // namespace openmsx
106 
107 #endif
openmsx::Schedulable::restoreOld
static void restoreOld(Archive &ar, std::vector< Schedulable * > schedulables)
Definition: Schedulable.hh:77
openmsx::SyncPointBW::time
EmuTime time
Definition: Schedulable.hh:25
openmsx::Schedulable::serializeBW
static std::vector< SyncPointBW > serializeBW(Archive &ar)
Definition: Schedulable.hh:68
openmsx::Scheduler
Definition: Scheduler.hh:34
serialize.hh
openmsx::Schedulable::operator=
Schedulable & operator=(const Schedulable &)=delete
serialize_meta.hh
openmsx::SyncPointBW::userData
int userData
Definition: Schedulable.hh:26
openmsx::Schedulable
Every class that wants to get scheduled at some point must inherit from this class.
Definition: Schedulable.hh:34
openmsx::Schedulable::Schedulable
Schedulable(const Schedulable &)=delete
openmsx::Schedulable::executeUntil
virtual void executeUntil(EmuTime::param time)=0
When the previously registered syncPoint is reached, this method gets called.
openmsx::REGISTER_BASE_CLASS
REGISTER_BASE_CLASS(Connector, "Connector")
openmsx::Schedulable::~Schedulable
~Schedulable()
Definition: Schedulable.cc:12
openmsx::Schedulable::setSyncPoint
void setSyncPoint(EmuTime::param timestamp)
Definition: Schedulable.cc:23
openmsx::Schedulable::removeSyncPoints
void removeSyncPoints()
Definition: Schedulable.cc:33
openmsx::Schedulable::pendingSyncPoint
bool pendingSyncPoint() const
Definition: Schedulable.cc:38
serialize_stl.hh
openmsx::Schedulable::getScheduler
Scheduler & getScheduler() const
Definition: Schedulable.hh:58
EmuTime.hh
openmsx::Schedulable::getCurrentTime
EmuTime::param getCurrentTime() const
Convenience method: This is the same as getScheduler().getCurrentTime().
Definition: Schedulable.cc:49
openmsx::Schedulable::schedulerDeleted
virtual void schedulerDeleted()
Just before the the Scheduler is deleted, it calls this method of all the Schedulables that are still...
Definition: Schedulable.cc:17
openmsx::Schedulable::serialize
void serialize(Archive &ar, unsigned version)
Definition: Schedulable.cc:55
openmsx::SyncPointBW::serialize
void serialize(Archive &ar, unsigned)
Definition: Schedulable.hh:19
openmsx::Scheduler
Scheduler
Definition: Scheduler.cc:132
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
openmsx::Schedulable::removeSyncPoint
bool removeSyncPoint()
Definition: Schedulable.cc:28
openmsx::SyncPointBW
Definition: Schedulable.hh:17