| 1 |
#ifndef STATECHANGEDISTRIBUTOR_HH |
| 2 |
#define STATECHANGEDISTRIBUTOR_HH |
| 3 |
|
| 4 |
#include "EmuTime.hh" |
| 5 |
#include "ReverseManager.hh" |
| 6 |
#include "StateChangeListener.hh" |
| 7 |
|
| 8 |
#include "ScopedAssign.hh" |
| 9 |
|
| 10 |
#include <utility> |
| 11 |
#include <vector> |
| 12 |
|
| 13 |
namespace openmsx { |
| 14 |
|
| 15 |
class StateChangeDistributor |
| 16 |
{ |
| 17 |
public: |
| 18 |
StateChangeDistributor() = default; |
| 19 |
StateChangeDistributor(const StateChangeDistributor&) = delete; |
| 20 |
StateChangeDistributor(StateChangeDistributor&&) = delete; |
| 21 |
StateChangeDistributor& operator=(const StateChangeDistributor&) = delete; |
| 22 |
StateChangeDistributor& operator=(StateChangeDistributor&&) = delete; |
| 23 |
~StateChangeDistributor(); |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
void registerListener (StateChangeListener& listener); |
| 29 |
void unregisterListener(StateChangeListener& listener); |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
void registerRecorder (ReverseManager& recorder); |
| 39 |
void unregisterRecorder(ReverseManager& recorder); |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
template<typename T, typename... Args> |
| 55 |
void distributeNew(EmuTime time, Args&& ...args) { |
| 56 |
if (recorder) { |
| 57 |
if (isReplaying()) { |
| 58 |
if (viewOnlyMode || blockNewEventsDuringReplay) return; |
| 59 |
stopReplay(time); |
| 60 |
} |
| 61 |
assert(!isReplaying()); |
| 62 |
const auto& event = recorder->record<T>(time, std::forward<Args>(args)...); |
| 63 |
distribute(event); |
| 64 |
} else { |
| 65 |
StateChange event(std::in_place_type_t<T>{}, |
| 66 |
time, std::forward<Args>(args)...); |
| 67 |
distribute(event); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
void distributeReplay(const StateChange& event) const { |
| 72 |
assert(isReplaying()); |
| 73 |
distribute(event); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
void stopReplay(EmuTime time); |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
void setViewOnlyMode(bool value) { viewOnlyMode = value; } |
| 90 |
[[nodiscard]] bool isViewOnlyMode() const { return viewOnlyMode; } |
| 91 |
|
| 92 |
[[nodiscard]] auto tempBlockNewEventsDuringReplay() { |
| 93 |
return ScopedAssign{blockNewEventsDuringReplay, true}; |
| 94 |
} |
| 95 |
|
| 96 |
[[nodiscard]] bool isReplaying() const; |
| 97 |
|
| 98 |
private: |
| 99 |
[[nodiscard]] bool isRegistered(StateChangeListener* listener) const; |
| 100 |
void distribute(const StateChange& event) const; |
| 101 |
|
| 102 |
private: |
| 103 |
std::vector<StateChangeListener*> listeners; |
| 104 |
ReverseManager* recorder = nullptr; |
| 105 |
bool viewOnlyMode = false; |
| 106 |
bool blockNewEventsDuringReplay = false; |
| 107 |
}; |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
#endif |