openMSX
TclCallback.cc
Go to the documentation of this file.
1#include "TclCallback.hh"
3#include "CliComm.hh"
4#include "CommandException.hh"
6#include "Reactor.hh"
7#include "checked_cast.hh"
8#include <iostream>
9#include <memory>
10
11namespace openmsx {
12
14 CommandController& controller,
15 std::string_view name,
16 static_string_view description,
17 std::string_view defaultValue,
18 Setting::SaveSetting saveSetting,
19 bool isMessageCallback_)
20 : callbackSetting2(std::in_place,
21 controller, name, description, defaultValue,
22 saveSetting)
23 , callbackSetting(*callbackSetting2)
24 , isMessageCallback(isMessageCallback_)
25{
26}
27
29 : callbackSetting(setting)
30 , isMessageCallback(false)
31{
32}
33
35{
36 return getSetting().getValue();
37}
38
40{
41 const auto& callback = getValue();
42 if (callback.empty()) return {};
43
44 auto command = makeTclList(callback);
45 return executeCommon(command);
46}
47
49{
50 const auto& callback = getValue();
51 if (callback.empty()) return {};
52
53 auto command = makeTclList(callback, arg1);
54 return executeCommon(command);
55}
56
57TclObject TclCallback::execute(int arg1, int arg2) const
58{
59 const auto& callback = getValue();
60 if (callback.empty()) return {};
61
62 auto command = makeTclList(callback, arg1, arg2);
63 return executeCommon(command);
64}
65
66TclObject TclCallback::execute(int arg1, std::string_view arg2) const
67{
68 const auto& callback = getValue();
69 if (callback.empty()) return {};
70
71 auto command = makeTclList(callback, arg1, arg2);
72 return executeCommon(command);
73}
74
75TclObject TclCallback::execute(std::string_view arg1, std::string_view arg2) const
76{
77 const auto& callback = getValue();
78 if (callback.empty()) return {};
79
80 auto command = makeTclList(callback, arg1, arg2);
81 return executeCommon(command);
82}
83
84TclObject TclCallback::executeCommon(TclObject& command) const
85{
86 try {
87 return command.executeCommand(callbackSetting.getInterpreter());
88 } catch (CommandException& e) {
89 auto message = strCat(
90 "Error executing callback function \"",
91 getSetting().getFullName(), "\": ", e.getMessage());
92 auto& commandController = getSetting().getCommandController();
93 if (!isMessageCallback) {
94 commandController.getCliComm().printWarning(message);
95 } else {
96 if (checked_cast<GlobalCommandController&>(commandController).getReactor().isFullyStarted()) {
97 std::cerr << message << '\n';
98 } else {
99 // This is a message callback that cannot be
100 // executed yet.
101 // Let the caller deal with this.
102 throw command;
103 }
104 }
105 return {};
106 }
107}
108
109} // namespace openmsx
BaseSetting * setting
void printWarning(std::string_view message)
Definition CliComm.cc:10
virtual CliComm & getCliComm()=0
CommandController & getCommandController() const
Definition Setting.hh:160
Interpreter & getInterpreter() const
Definition Setting.cc:139
const TclObject & getValue() const final
Gets the current value of this setting as a TclObject.
Definition Setting.hh:133
TclCallback(CommandController &controller, std::string_view name, static_string_view description, std::string_view defaultValue, Setting::SaveSetting saveSetting, bool isMessageCallback=false)
TclObject getValue() const
TclObject execute() const
StringSetting & getSetting() const
TclObject executeCommand(Interpreter &interp, bool compile=false)
Interpret this TclObject as a command and execute it.
Definition TclObject.cc:249
static_string_view
This file implemented 3 utility functions:
Definition Autofire.cc:9
TclObject makeTclList(Args &&... args)
Definition TclObject.hh:289
STL namespace.
std::string strCat()
Definition strCat.hh:703