openMSX
StateChangeDistributor.hh
Go to the documentation of this file.
1#ifndef STATECHANGEDISTRIBUTOR_HH
2#define STATECHANGEDISTRIBUTOR_HH
3
4#include "EmuTime.hh"
5#include "ReverseManager.hh"
7
8#include "ScopedAssign.hh"
9
10#include <vector>
11
12namespace openmsx {
13
15{
16public:
23
29
37 void registerRecorder (ReverseManager& recorder);
38 void unregisterRecorder(ReverseManager& recorder);
39
53 template<typename T, typename... Args>
54 void distributeNew(EmuTime::param time, Args&& ...args) {
55 if (recorder) {
56 if (isReplaying()) {
57 if (viewOnlyMode || blockNewEventsDuringReplay) return;
58 stopReplay(time);
59 }
60 assert(!isReplaying());
61 const auto& event = recorder->record<T>(time, std::forward<Args>(args)...);
62 distribute(event); // might throw, ok
63 } else {
64 T event(time, std::forward<Args>(args)...);
65 distribute(event); // might throw, ok
66 }
67 }
68
69 void distributeReplay(const StateChange& event) const {
70 assert(isReplaying());
71 distribute(event);
72 }
73
80 void stopReplay(EmuTime::param time);
81
87 void setViewOnlyMode(bool value) { viewOnlyMode = value; }
88 [[nodiscard]] bool isViewOnlyMode() const { return viewOnlyMode; }
89
90 [[nodiscard]] auto tempBlockNewEventsDuringReplay() {
91 return ScopedAssign{blockNewEventsDuringReplay, true};
92 }
93
94 [[nodiscard]] bool isReplaying() const;
95
96private:
97 [[nodiscard]] bool isRegistered(StateChangeListener* listener) const;
98 void distribute(const StateChange& event) const;
99
100private:
101 std::vector<StateChangeListener*> listeners; // unordered
102 ReverseManager* recorder = nullptr;
103 bool viewOnlyMode = false;
104 bool blockNewEventsDuringReplay = false; // used when executing callbacks during replay
105};
106
107} // namespace openmsx
108
109#endif
Assign new value to some variable and restore the original value when this object goes out of scope.
StateChange & record(EmuTime::param time, Args &&...args)
void unregisterRecorder(ReverseManager &recorder)
StateChangeDistributor(const StateChangeDistributor &)=delete
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...
StateChangeDistributor & operator=(StateChangeDistributor &&)=delete
StateChangeDistributor(StateChangeDistributor &&)=delete
void registerRecorder(ReverseManager &recorder)
(Un)registers the given object to receive state change events.
void distributeReplay(const StateChange &event) const
void unregisterListener(StateChangeListener &listener)
void setViewOnlyMode(bool value)
Set viewOnlyMode.
void stopReplay(EmuTime::param time)
Explicitly stop replay.
StateChangeDistributor & operator=(const StateChangeDistributor &)=delete
Base class for all external MSX state changing events.
This file implemented 3 utility functions:
Definition Autofire.cc:11