openMSX
StateChangeDistributor.hh
Go to the documentation of this file.
1 #ifndef STATECHANGEDISTRIBUTOR_HH
2 #define STATECHANGEDISTRIBUTOR_HH
3 
4 #include "ReverseManager.hh"
5 #include "StateChangeListener.hh"
6 #include "EmuTime.hh"
7 #include <memory>
8 #include <vector>
9 
10 namespace openmsx {
11 
13 {
14 public:
17 
21  void registerListener (StateChangeListener& listener);
23 
31  void registerRecorder (ReverseManager& recorder);
32  void unregisterRecorder(ReverseManager& recorder);
33 
45  template<typename T, typename... Args>
46  void distributeNew(EmuTime::param time, Args&& ...args) {
47  if (recorder) {
48  if (isReplaying()) {
49  if (viewOnlyMode) return;
50  stopReplay(time);
51  }
52  assert(!isReplaying());
53  const auto& event = recorder->record<T>(time, std::forward<Args>(args)...);
54  distribute(event); // might throw, ok
55  } else {
56  T event(time, std::forward<Args>(args)...);
57  distribute(event); // might throw, ok
58  }
59  }
60 
61  void distributeReplay(const StateChange& event) {
62  assert(isReplaying());
63  distribute(event);
64  }
65 
72  void stopReplay(EmuTime::param time);
73 
79  void setViewOnlyMode(bool value) { viewOnlyMode = value; }
80  [[nodiscard]] bool isViewOnlyMode() const { return viewOnlyMode; }
81 
82  [[nodiscard]] bool isReplaying() const;
83 
84 private:
85  [[nodiscard]] bool isRegistered(StateChangeListener* listener) const;
86  void distribute(const StateChange& event);
87 
88 private:
89  std::vector<StateChangeListener*> listeners; // unordered
90  ReverseManager* recorder = nullptr;
91  bool viewOnlyMode = false;
92 };
93 
94 } // namespace openmsx
95 
96 #endif
StateChange & record(EmuTime::param time, Args &&...args)
void unregisterRecorder(ReverseManager &recorder)
void registerListener(StateChangeListener &listener)
(Un)registers the given object to receive state change events.
void distributeNew(EmuTime::param time, Args &&...args)
Deliver the event to all registered listeners MSX input devices should call the distributeNew() versi...
void registerRecorder(ReverseManager &recorder)
(Un)registers the given object to receive state change events.
void unregisterListener(StateChangeListener &listener)
void setViewOnlyMode(bool value)
Set viewOnlyMode.
void stopReplay(EmuTime::param time)
Explicitly stop replay.
void distributeReplay(const StateChange &event)
Base class for all external MSX state changing events.
Definition: StateChange.hh:20
This file implemented 3 utility functions:
Definition: Autofire.cc:9