openMSX
MSXCommandController.cc
Go to the documentation of this file.
3#include "Reactor.hh"
5#include "MSXMotherBoard.hh"
6#include "SettingsManager.hh"
7#include "Interpreter.hh"
8#include "Setting.hh"
9#include "Event.hh"
10#include "MSXException.hh"
11#include "TemporaryString.hh"
12#include "stl.hh"
13#include <iostream>
14#include <memory>
15
16namespace openmsx {
17
19 GlobalCommandController& globalCommandController_,
20 Reactor& reactor_,
21 MSXMotherBoard& motherboard_,
22 MSXEventDistributor& msxEventDistributor_,
23 const std::string& machineID_)
24 : globalCommandController(globalCommandController_)
25 , reactor(reactor_)
26 , motherboard(motherboard_)
27 , msxEventDistributor(msxEventDistributor_)
28 , machineID(strCat("::", machineID_, "::"))
29{
30 globalCommandController.getInterpreter().createNamespace(machineID);
31
32 machineInfoCommand.emplace(*this, "machine_info");
33 machineInfoCommand->setAllowedInEmptyMachine(true);
34
35 msxEventDistributor.registerEventListener(*this);
36}
37
39{
40 msxEventDistributor.unregisterEventListener(*this);
41
42 machineInfoCommand.reset();
43
44 #ifndef NDEBUG
45 for (auto* c : commandMap) {
46 std::cout << "Command not unregistered: " << c->getName() << '\n';
47 }
48 for (auto* s : settings) {
49 std::cout << "Setting not unregistered: " << s->getFullName() << '\n';
50 }
51 assert(commandMap.empty());
52 assert(settings.empty());
53 #endif
54
55 globalCommandController.getInterpreter().deleteNamespace(machineID);
56}
57
58TemporaryString MSXCommandController::getFullName(std::string_view name)
59{
60 return tmpStrCat(machineID, name);
61}
62
64{
65 assert(!hasCommand(str));
66 assert(command.getName() == str);
67 commandMap.insert_noDuplicateCheck(&command);
68
69 auto fullname = getFullName(str);
70 globalCommandController.registerCommand(command, fullname);
71 globalCommandController.registerProxyCommand(str);
72
73 command.setAllowedInEmptyMachine(false);
74}
75
76void MSXCommandController::unregisterCommand(Command& command, std::string_view str)
77{
78 assert(hasCommand(str));
79 assert(command.getName() == str);
80 commandMap.erase(str);
81
82 globalCommandController.unregisterProxyCommand(str);
83 auto fullname = getFullName(str);
84 globalCommandController.unregisterCommand(command, fullname);
85}
86
88 std::string_view str)
89{
90 auto fullname = getFullName(str);
91 globalCommandController.registerCompleter(completer, fullname);
92}
93
95 std::string_view str)
96{
97 auto fullname = getFullName(str);
98 globalCommandController.unregisterCompleter(completer, fullname);
99}
100
102{
103 setting.setPrefix(machineID);
104
105 settings.push_back(&setting);
106
107 globalCommandController.registerProxySetting(setting);
108 globalCommandController.getSettingsManager().registerSetting(setting);
109 globalCommandController.getInterpreter().registerSetting(setting);
110}
111
113{
114 move_pop_back(settings, rfind_unguarded(settings, &setting));
115
116 globalCommandController.unregisterProxySetting(setting);
117 globalCommandController.getInterpreter().unregisterSetting(setting);
118 globalCommandController.getSettingsManager().unregisterSetting(setting);
119}
120
121Command* MSXCommandController::findCommand(std::string_view name) const
122{
123 auto it = commandMap.find(name);
124 return (it != end(commandMap)) ? *it : nullptr;
125}
126
127bool MSXCommandController::hasCommand(std::string_view command) const
128{
129 return findCommand(command) != nullptr;
130}
131
133 CliConnection* connection)
134{
135 return globalCommandController.executeCommand(command, connection);
136}
137
139{
140 return motherboard.getMSXCliComm();
141}
142
144{
145 return globalCommandController.getInterpreter();
146}
147
148void MSXCommandController::signalMSXEvent(
149 const Event& event, EmuTime::param /*time*/) noexcept
150{
151 if (getType(event) != EventType::MACHINE_ACTIVATED) return;
152
153 // simple way to synchronize proxy settings
154 for (auto* s : settings) {
155 try {
156 getInterpreter().setVariable(
157 s->getFullNameObj(), s->getValue());
158 } catch (MSXException&) {
159 // ignore
160 }
161 }
162}
163
165{
166 return reactor.getMotherBoard() == &motherboard;
167}
168
170{
171 const auto& fromPrefix = from.getPrefix();
172 auto& manager = globalCommandController.getSettingsManager();
173 for (auto* s : settings) {
174 if (auto* fromSetting = manager.findSetting(fromPrefix, s->getBaseName())) {
175 if (!fromSetting->needTransfer()) continue;
176 try {
178 s->getFullNameObj(), fromSetting->getValue());
179 } catch (MSXException&) {
180 // ignore
181 }
182 }
183 }
184}
185
186} // namespace openmsx
BaseSetting * setting
Definition: Interpreter.cc:28
TemporaryString.
void setAllowedInEmptyMachine(bool value)
Definition: Command.hh:65
const std::string & getName() const
Definition: Completer.hh:23
void registerProxyCommand(std::string_view name)
void unregisterProxyCommand(std::string_view name)
void registerCommand(Command &command, zstring_view str) override
(Un)register a command
void registerCompleter(CommandCompleter &completer, std::string_view str) override
(Un)register a command completer, used to complete build-in Tcl cmds
TclObject executeCommand(zstring_view command, CliConnection *connection=nullptr) override
Execute the given command.
void unregisterCompleter(CommandCompleter &completer, std::string_view str) override
void unregisterCommand(Command &command, std::string_view str) override
void deleteNamespace(const std::string &name)
Delete the global namespace with given name.
Definition: Interpreter.cc:445
void registerSetting(BaseSetting &variable)
Definition: Interpreter.cc:271
void createNamespace(const std::string &name)
Create the global namespace with given name.
Definition: Interpreter.cc:440
void setVariable(const TclObject &name, const TclObject &value)
Definition: Interpreter.cc:250
void unregisterSetting(BaseSetting &variable)
Definition: Interpreter.cc:323
bool hasCommand(std::string_view command) const
Interpreter & getInterpreter() override
void registerCompleter(CommandCompleter &completer, std::string_view str) override
(Un)register a command completer, used to complete build-in Tcl cmds
TclObject executeCommand(zstring_view command, CliConnection *connection=nullptr) override
Execute the given command.
void unregisterCompleter(CommandCompleter &completer, std::string_view str) override
void registerCommand(Command &command, zstring_view str) override
(Un)register a command
void transferSettings(const MSXCommandController &from)
Transfer setting values from one machine to another, used for during 'reverse'.
void unregisterSetting(Setting &setting) override
const std::string & getPrefix() const
bool isActive() const
Returns true iff the machine this controller belongs to is currently active.
MSXCommandController(const MSXCommandController &)=delete
void unregisterCommand(Command &command, std::string_view str) override
void registerSetting(Setting &setting) override
TODO.
Command * findCommand(std::string_view name) const
void registerEventListener(MSXEventListener &listener)
Registers a given object to receive certain events.
void unregisterEventListener(MSXEventListener &listener)
Unregisters a previously registered event listener.
Contains the main loop of openMSX.
Definition: Reactor.hh:68
MSXMotherBoard * getMotherBoard() const
Definition: Reactor.cc:375
void unregisterSetting(BaseSetting &setting)
void registerSetting(BaseSetting &setting)
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
Definition: zstring_view.hh:22
This file implemented 3 utility functions:
Definition: Autofire.cc:9
EventType getType(const Event &event)
Definition: Event.hh:647
void move_pop_back(VECTOR &v, typename VECTOR::iterator it)
Erase the pointed to element from the given vector.
Definition: stl.hh:125
auto rfind_unguarded(RANGE &range, const VAL &val, Proj proj={})
Similar to the find(_if)_unguarded functions above, but searches from the back to front.
Definition: stl.hh:100
TemporaryString tmpStrCat(Ts &&... ts)
Definition: strCat.hh:610
std::string strCat(Ts &&...ts)
Definition: strCat.hh:542
constexpr auto end(const zstring_view &x)