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:
19
25
33 void registerRecorder (ReverseManager& recorder);
34 void unregisterRecorder(ReverseManager& recorder);
35
49 template<typename T, typename... Args>
50 void distributeNew(EmuTime::param time, Args&& ...args) {
51 if (recorder) {
52 if (isReplaying()) {
53 if (viewOnlyMode || blockNewEventsDuringReplay) return;
54 stopReplay(time);
55 }
56 assert(!isReplaying());
57 const auto& event = recorder->record<T>(time, std::forward<Args>(args)...);
58 distribute(event); // might throw, ok
59 } else {
60 T event(time, std::forward<Args>(args)...);
61 distribute(event); // might throw, ok
62 }
63 }
64
65 void distributeReplay(const StateChange& event) {
66 assert(isReplaying());
67 distribute(event);
68 }
69
76 void stopReplay(EmuTime::param time);
77
83 void setViewOnlyMode(bool value) { viewOnlyMode = value; }
84 [[nodiscard]] bool isViewOnlyMode() const { return viewOnlyMode; }
85
86 [[nodiscard]] auto tempBlockNewEventsDuringReplay() {
87 return ScopedAssign{blockNewEventsDuringReplay, true};
88 }
89
90 [[nodiscard]] bool isReplaying() const;
91
92private:
93 [[nodiscard]] bool isRegistered(StateChangeListener* listener) const;
94 void distribute(const StateChange& event);
95
96private:
97 std::vector<StateChangeListener*> listeners; // unordered
98 ReverseManager* recorder = nullptr;
99 bool viewOnlyMode = false;
100 bool blockNewEventsDuringReplay = false; // used when executing callbacks during replay
101};
102
103} // namespace openmsx
104
105#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)
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.
This file implemented 3 utility functions:
Definition Autofire.cc:9