openMSX
MSXCommandController.cc
Go to the documentation of this file.
3 #include "Reactor.hh"
4 #include "MSXEventDistributor.hh"
5 #include "MSXMotherBoard.hh"
6 #include "SettingsConfig.hh"
7 #include "SettingsManager.hh"
8 #include "InfoCommand.hh"
9 #include "Interpreter.hh"
10 #include "Setting.hh"
11 #include "Event.hh"
12 #include "MSXException.hh"
13 #include "KeyRange.hh"
14 #include "memory.hh"
15 #include "stl.hh"
16 #include <iostream>
17 
18 using std::string;
19 
20 namespace openmsx {
21 
23  GlobalCommandController& globalCommandController_,
24  Reactor& reactor_,
25  MSXMotherBoard& motherboard_,
26  MSXEventDistributor& msxEventDistributor_,
27  const std::string& machineID_)
28  : globalCommandController(globalCommandController_)
29  , reactor(reactor_)
30  , motherboard(motherboard_)
31  , msxEventDistributor(msxEventDistributor_)
32  , machineID("::" + machineID_ + "::")
33 {
34  globalCommandController.getInterpreter().createNamespace(machineID);
35 
36  machineInfoCommand = make_unique<InfoCommand>(*this, "machine_info");
37  machineInfoCommand->setAllowedInEmptyMachine(true);
38 
39  msxEventDistributor.registerEventListener(*this);
40 }
41 
43 {
44  msxEventDistributor.unregisterEventListener(*this);
45 
46  machineInfoCommand.reset();
47 
48  #ifndef NDEBUG
49  for (auto* c : commandMap) {
50  std::cout << "Command not unregistered: " << c->getName() << std::endl;
51  }
52  for (auto* s : settings) {
53  std::cout << "Setting not unregistered: " << s->getFullName() << std::endl;
54  }
55  assert(commandMap.empty());
56  assert(settings.empty());
57  #endif
58 
59  globalCommandController.getInterpreter().deleteNamespace(machineID);
60 }
61 
62 string MSXCommandController::getFullName(string_ref name)
63 {
64  return machineID + name;
65 }
66 
67 void MSXCommandController::registerCommand(Command& command, const string& str)
68 {
69  assert(!hasCommand(str));
70  assert(command.getName() == str);
71  commandMap.insert_noDuplicateCheck(&command);
72 
73  string fullname = getFullName(str);
74  globalCommandController.registerCommand(command, fullname);
75  globalCommandController.registerProxyCommand(str);
76 
77  command.setAllowedInEmptyMachine(false);
78 }
79 
81 {
82  assert(hasCommand(str));
83  assert(command.getName() == str);
84  commandMap.erase(str);
85 
86  globalCommandController.unregisterProxyCommand(str);
87  string fullname = getFullName(str);
88  globalCommandController.unregisterCommand(command, fullname);
89 }
90 
92  string_ref str)
93 {
94  string fullname = getFullName(str);
95  globalCommandController.registerCompleter(completer, fullname);
96 }
97 
99  string_ref str)
100 {
101  string fullname = getFullName(str);
102  globalCommandController.unregisterCompleter(completer, fullname);
103 }
104 
106 {
107  setting.setPrefix(machineID);
108 
109  settings.push_back(&setting);
110 
111  globalCommandController.registerProxySetting(setting);
112  globalCommandController.getSettingsManager().registerSetting(setting);
113  globalCommandController.getInterpreter().registerSetting(setting);
114 }
115 
117 {
118  move_pop_back(settings, rfind_unguarded(settings, &setting));
119 
120  globalCommandController.unregisterProxySetting(setting);
121  globalCommandController.getInterpreter().unregisterSetting(setting);
122  globalCommandController.getSettingsManager().unregisterSetting(setting);
123 }
124 
126 {
127  auto it = commandMap.find(name);
128  return (it != end(commandMap)) ? *it : nullptr;
129 }
130 
132 {
133  return findCommand(command) != nullptr;
134 }
135 
137  CliConnection* connection)
138 {
139  return globalCommandController.executeCommand(command, connection);
140 }
141 
143 {
144  return motherboard.getMSXCliComm();
145 }
146 
148 {
149  return globalCommandController.getInterpreter();
150 }
151 
152 void MSXCommandController::signalEvent(
153  const std::shared_ptr<const Event>& event, EmuTime::param /*time*/)
154 {
155  if (event->getType() != OPENMSX_MACHINE_ACTIVATED) return;
156 
157  // simple way to synchronize proxy settings
158  for (auto* s : settings) {
159  try {
161  s->getFullNameObj(), s->getValue());
162  } catch (MSXException&) {
163  // ignore
164  }
165  }
166 }
167 
169 {
170  return reactor.getMotherBoard() == &motherboard;
171 }
172 
174 {
175  const auto& fromPrefix = from.getPrefix();
176  auto& manager = globalCommandController.getSettingsManager();
177  for (auto* s : settings) {
178  if (auto* fromSetting = manager.findSetting(fromPrefix, s->getBaseName())) {
179  if (!fromSetting->needTransfer()) continue;
180  try {
182  s->getFullNameObj(), fromSetting->getValue());
183  } catch (MSXException&) {
184  // ignore
185  }
186  }
187  }
188 }
189 
190 } // namespace openmsx
void transferSettings(const MSXCommandController &from)
Transfer setting values from one machine to another, used for during &#39;reverse&#39;.
Contains the main loop of openMSX.
Definition: Reactor.hh:61
TclObject executeCommand(const std::string &command, CliConnection *connection=nullptr) override
Execute the given command.
MSXCommandController(const MSXCommandController &)=delete
string_ref::const_iterator end(const string_ref &x)
Definition: string_ref.hh:167
void unregisterCommand(Command &command, string_ref str) override
void registerProxyCommand(const std::string &name)
bool isActive() const
Returns true iff the machine this controller belongs to is currently active.
void unregisterSetting(BaseSetting &setting)
void deleteNamespace(const std::string &name)
Delete the global namespace with given name.
Definition: Interpreter.cc:428
void unregisterSetting(Setting &setting) override
Send when a machine is (de)activated.
Definition: Event.hh:55
void registerEventListener(MSXEventListener &listener)
Registers a given object to receive certain events.
Command * findCommand(string_ref name) const
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
void move_pop_back(VECTOR &v, typename VECTOR::iterator it)
Erase the pointed to element from the given vector.
Definition: stl.hh:192
const std::string & getName() const
Definition: Completer.hh:16
MSXMotherBoard * getMotherBoard() const
Definition: Reactor.cc:328
void unregisterCommand(Command &command, string_ref str) override
void registerCommand(Command &command, const std::string &str) override
(Un)register a command
Interpreter & getInterpreter() override
const std::string & getPrefix() const
auto rfind_unguarded(RANGE &range, const VAL &val) -> decltype(std::begin(range))
Similar to the find(_if)_unguarded functions above, but searches from the back to front...
Definition: stl.hh:164
void registerSetting(BaseSetting &setting)
void unregisterCompleter(CommandCompleter &completer, string_ref str) override
void setAllowedInEmptyMachine(bool value)
Definition: Command.hh:60
void unregisterEventListener(MSXEventListener &listener)
Unregisters a previously registered event listener.
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
void registerCompleter(CommandCompleter &completer, string_ref str) override
(Un)register a command completer, used to complete build-in Tcl cmds
void registerCompleter(CommandCompleter &completer, string_ref str) override
(Un)register a command completer, used to complete build-in Tcl cmds
void createNamespace(const std::string &name)
Create the global namespace with given name.
Definition: Interpreter.cc:423
void setVariable(const TclObject &name, const TclObject &value)
Definition: Interpreter.cc:231
void registerCommand(Command &command, const std::string &str) override
(Un)register a command
void setPrefix(string_ref prefix)
Set a machine specific prefix.
Definition: Setting.hh:38
void registerSetting(Setting &setting) override
TODO.
bool hasCommand(string_ref command) const override
Does a command with this name already exist?
void registerSetting(BaseSetting &variable)
Definition: Interpreter.cc:253
TclObject executeCommand(const std::string &command, CliConnection *connection=nullptr) override
Execute the given command.
void unregisterCompleter(CommandCompleter &completer, string_ref str) override
void unregisterSetting(BaseSetting &variable)
Definition: Interpreter.cc:305