OXYGENHUB
◆ main openmsx / src / input / RecordedCommand.hh
2.1 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#ifndef RECORDEDCOMMAND_HH
2
#define RECORDEDCOMMAND_HH
3
4
#include "Command.hh"
5
#include "EmuTime.hh"
6
#include "StateChange.hh"
7
#include "StateChangeListener.hh"
8
#include "TclObject.hh"
9
10
#include "dynarray.hh"
11
12
namespace openmsx {
13
14
class CommandController;
15
class StateChangeDistributor;
16
class Scheduler;
17
18
/** Commands that directly influence the MSX state should send and events
19
  * so that they can be recorded by the event recorder. This class helps to
20
  * implement that.
21
  *
22
  * Note: when a recorded command is later replayed, it's important to check
23
  * whether it's actually a recorded command and not some arbitrary other
24
  * command that someone might have inserted in a replay file. IOW when you
25
  * load a replay file from an untrusted source, it should never be able to
26
  * cause any harm. Blindly executing any Tcl command would be bad.
27
  */
28
class RecordedCommand : public Command, private StateChangeListener
29
{
30
public:
31
	/** This is like the execute() method of the Command class, it only
32
	  * has an extra time parameter.
33
	  */
34
	virtual void execute(
35
		std::span<const TclObject> tokens, TclObject& result,
36
		EmuTime time) = 0;
37
38
	/** It's possible that in some cases the command doesn't need to be
39
	  * recorded after all (e.g. a query subcommand). In that case you can
40
	  * override this method. Return false iff the command doesn't need
41
	  * to be recorded.
42
	  */
43
	[[nodiscard]] virtual bool needRecord(std::span<const TclObject> tokens) const;
44
45
protected:
46
	RecordedCommand(CommandController& commandController,
47
	                StateChangeDistributor& stateChangeDistributor,
48
	                Scheduler& scheduler,
49
	                std::string_view name);
50
	~RecordedCommand();
51
52
private:
53
	// Command
54
	void execute(std::span<const TclObject> tokens, TclObject& result) override;
55
56
	// StateChangeListener
57
	void signalStateChange(const StateChange& event) override;
58
	void stopReplay(EmuTime time) noexcept override;
59
60
private:
61
	StateChangeDistributor& stateChangeDistributor;
62
	Scheduler& scheduler;
63
	TclObject dummyResultObject;
64
	TclObject* currentResultObject;
65
};
66
67
} // namespace openmsx
68
69
#endif