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 <iostream>
16 
17 using std::string;
18 using std::vector;
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& p : commandMap) {
50  std::cout << "Command not unregistered: " << p.first() << std::endl;
51  }
52  for (auto& p : settingMap) {
53  std::cout << "Setting not unregistered: " << p.first() << std::endl;
54  }
55  assert(commandMap.empty());
56  assert(settingMap.empty());
57  #endif
58 
59  globalCommandController.getInterpreter().deleteNamespace(machineID);
60 }
61 
63 {
64  return globalCommandController;
65 }
66 
68 {
69  return *machineInfoCommand;
70 }
71 
73 {
74  return motherboard;
75 }
76 
77 string MSXCommandController::getFullName(string_ref name)
78 {
79  return "::" + machineID + "::" + name;
80 }
81 
82 void MSXCommandController::registerCommand(Command& command, const string& str)
83 {
84  assert(!hasCommand(str));
85  commandMap[str] = &command;
86 
87  string fullname = getFullName(str);
88  globalCommandController.registerCommand(command, fullname);
89  globalCommandController.registerProxyCommand(str);
90 
91  command.setAllowedInEmptyMachine(false);
92 }
93 
95 {
96  assert(hasCommand(str));
97  commandMap.erase(str);
98 
99  globalCommandController.unregisterProxyCommand(str);
100  string fullname = getFullName(str);
101  globalCommandController.unregisterCommand(command, fullname);
102 }
103 
105  string_ref str)
106 {
107  string fullname = getFullName(str);
108  globalCommandController.registerCompleter(completer, fullname);
109 }
110 
112  string_ref str)
113 {
114  string fullname = getFullName(str);
115  globalCommandController.unregisterCompleter(completer, fullname);
116 }
117 
119 {
120  const string& name = setting.getName();
121  assert(!findSetting(name));
122  settingMap[name] = &setting;
123 
124  globalCommandController.registerProxySetting(setting);
125  string fullname = getFullName(name);
126  globalCommandController.getSettingsConfig().getSettingsManager()
127  .registerSetting(setting, fullname);
128  globalCommandController.getInterpreter().registerSetting(setting, fullname);
129 }
130 
132 {
133  const string& name = setting.getName();
134  assert(findSetting(name));
135  settingMap.erase(name);
136 
137  globalCommandController.unregisterProxySetting(setting);
138  string fullname = getFullName(name);
139  globalCommandController.getInterpreter().unregisterSetting(setting, fullname);
140  globalCommandController.getSettingsConfig().getSettingsManager()
141  .unregisterSetting(setting, fullname);
142 }
143 
144 void MSXCommandController::changeSetting(Setting& setting, const string& value)
145 {
146  string fullname = getFullName(setting.getName());
147  globalCommandController.changeSetting(fullname, value);
148 }
149 
151 {
152  auto it = commandMap.find(name);
153  return (it != end(commandMap)) ? it->second : nullptr;
154 }
155 
157 {
158  auto it = settingMap.find(name);
159  return (it != end(settingMap)) ? it->second : nullptr;
160 }
161 
163 {
164  return const_cast<MSXCommandController*>(this)->findSetting(setting);
165 }
166 
168 {
169  return findCommand(command) != nullptr;
170 }
171 
172 string MSXCommandController::executeCommand(const string& command,
173  CliConnection* connection)
174 {
175  return globalCommandController.executeCommand(command, connection);
176 }
177 
178 vector<string> MSXCommandController::splitList(const string& list)
179 {
180  return globalCommandController.splitList(list);
181 }
182 
184 {
185  return motherboard.getMSXCliComm();
186 }
187 
189 {
190  return globalCommandController.getInterpreter();
191 }
192 
193 void MSXCommandController::signalEvent(
194  const std::shared_ptr<const Event>& event, EmuTime::param /*time*/)
195 {
196  if (event->getType() != OPENMSX_MACHINE_ACTIVATED) return;
197 
198  // simple way to synchronize proxy settings
199  for (auto* s : values(settingMap)) {
200  try {
201  changeSetting(*s, s->getString());
202  } catch (MSXException&) {
203  // ignore
204  }
205  }
206 }
207 
209 {
210  return reactor.getMotherBoard() == &motherboard;
211 }
212 
214 {
215  for (auto& p : settingMap) {
216  if (auto* fromSetting = from.findSetting(p.first())) {
217  if (!fromSetting->needTransfer()) continue;
218  try {
219  changeSetting(*p.second, fromSetting->getString());
220  } catch (MSXException&) {
221  // ignore
222  }
223  }
224  }
225 }
226 
227 } // namespace openmsx
detail::KeyRange< MAP, 1 > values(const MAP &map)
Definition: KeyRange.hh:101
void transferSettings(const MSXCommandController &from)
Transfer setting values from one machine to another, used for during 'reverse'.
Contains the main loop of openMSX.
Definition: Reactor.hh:62
virtual BaseSetting * findSetting(string_ref name)
string_ref::const_iterator end(const string_ref &x)
Definition: string_ref.hh:135
void unregisterSetting(BaseSetting &variable, const std::string &name)
Definition: Interpreter.cc:321
bool isActive() const
Returns true iff the machine this controller belongs to is currently active.
void registerProxyCommand(const std::string &name)
virtual void unregisterCompleter(CommandCompleter &completer, string_ref str)
void deleteNamespace(const std::string &name)
Delete the global namespace with given name.
Definition: Interpreter.cc:433
virtual bool hasCommand(string_ref command) const
Does a command with this name already exist?
virtual std::vector< std::string > splitList(const std::string &list)
Send when a machine is (de)activated.
Definition: Event.hh:71
virtual void registerCompleter(CommandCompleter &completer, string_ref str)
(Un)register a command completer, used to complete build-in Tcl cmds
void registerEventListener(MSXEventListener &listener)
Registers a given object to receive certain events.
SettingsManager & getSettingsManager()
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 unregisterSetting(BaseSetting &setting, string_ref name)
void registerSetting(BaseSetting &setting, string_ref name)
virtual std::vector< std::string > splitList(const std::string &list)
virtual void registerCompleter(CommandCompleter &completer, string_ref str)
(Un)register a command completer, used to complete build-in Tcl cmds
const std::string & getName() const
Get the name of this setting.
Definition: Setting.cc:23
MSXMotherBoard * getMotherBoard() const
Definition: Reactor.cc:402
void setAllowedInEmptyMachine(bool value)
Definition: Command.hh:57
void unregisterEventListener(MSXEventListener &listener)
Unregisters a previously registered event listener.
MSXCommandController(GlobalCommandController &globalCommandController, Reactor &reactor, MSXMotherBoard &motherboard, MSXEventDistributor &msxEventDistributor, const std::string &machineID)
virtual void registerCommand(Command &command, const std::string &str)
(Un)register a command
virtual SettingsConfig & getSettingsConfig()
GlobalCommandController & getGlobalCommandController()
virtual void unregisterCompleter(CommandCompleter &completer, string_ref str)
virtual Interpreter & getInterpreter()
virtual void unregisterCommand(Command &command, string_ref str)
void createNamespace(const std::string &name)
Create the global namespace with given name.
Definition: Interpreter.cc:428
virtual void unregisterSetting(Setting &setting)
virtual std::string executeCommand(const std::string &command, CliConnection *connection=nullptr)
Execute the given command.
void registerSetting(BaseSetting &variable, const std::string &name)
Definition: Interpreter.cc:270
virtual std::string executeCommand(const std::string &command, CliConnection *connection=nullptr)
Execute the given command.
MSXMotherBoard & getMSXMotherBoard() const
virtual void unregisterCommand(Command &command, string_ref str)
virtual void registerSetting(Setting &setting)
TODO.
virtual void changeSetting(Setting &setting, const std::string &value)
void changeSetting(const std::string &name, const std::string &value)
virtual void registerCommand(Command &command, const std::string &str)
(Un)register a command