openMSX
TclCallback.cc
Go to the documentation of this file.
1#include "TclCallback.hh"
3#include "CliComm.hh"
4#include "CommandException.hh"
5#include <iostream>
6#include <memory>
7
8namespace openmsx {
9
11 CommandController& controller,
12 std::string_view name,
13 static_string_view description,
14 bool useCliComm_,
15 bool save)
16 : callbackSetting2(std::in_place,
17 controller, name, description, std::string_view{},
18 save ? Setting::SAVE : Setting::DONT_SAVE)
19 , callbackSetting(*callbackSetting2)
20 , useCliComm(useCliComm_)
21{
22}
23
25 : callbackSetting(setting)
26 , useCliComm(true)
27{
28}
29
31{
32 return getSetting().getValue();
33}
34
36{
37 const auto& callback = getValue();
38 if (callback.empty()) return {};
39
40 auto command = makeTclList(callback);
41 return executeCommon(command);
42}
43
45{
46 const auto& callback = getValue();
47 if (callback.empty()) return {};
48
49 auto command = makeTclList(callback, arg1);
50 return executeCommon(command);
51}
52
54{
55 const auto& callback = getValue();
56 if (callback.empty()) return {};
57
58 auto command = makeTclList(callback, arg1, arg2);
59 return executeCommon(command);
60}
61
62TclObject TclCallback::execute(int arg1, std::string_view arg2)
63{
64 const auto& callback = getValue();
65 if (callback.empty()) return {};
66
67 auto command = makeTclList(callback, arg1, arg2);
68 return executeCommon(command);
69}
70
71TclObject TclCallback::execute(std::string_view arg1, std::string_view arg2)
72{
73 const auto& callback = getValue();
74 if (callback.empty()) return {};
75
76 auto command = makeTclList(callback, arg1, arg2);
77 return executeCommon(command);
78}
79
80TclObject TclCallback::executeCommon(TclObject& command)
81{
82 try {
83 return command.executeCommand(callbackSetting.getInterpreter());
84 } catch (CommandException& e) {
85 auto message = strCat(
86 "Error executing callback function \"",
87 getSetting().getFullName(), "\": ", e.getMessage());
88 if (useCliComm) {
90 message);
91 } else {
92 std::cerr << message << '\n';
93 }
94 return {};
95 }
96}
97
98} // namespace openmsx
BaseSetting * setting
Definition: Interpreter.cc:27
void printWarning(std::string_view message)
Definition: CliComm.cc:10
virtual CliComm & getCliComm()=0
CommandController & getCommandController() const
Definition: Setting.hh:177
Interpreter & getInterpreter() const
Definition: Setting.cc:148
const TclObject & getValue() const final
Gets the current value of this setting as a TclObject.
Definition: Setting.hh:142
TclObject execute()
Definition: TclCallback.cc:35
TclObject getValue() const
Definition: TclCallback.cc:30
TclCallback(CommandController &controller, std::string_view name, static_string_view description, bool useCliComm=true, bool save=true)
Definition: TclCallback.cc:10
StringSetting & getSetting() const
Definition: TclCallback.hh:31
TclObject executeCommand(Interpreter &interp, bool compile=false)
Interpret this TclObject as a command and execute it.
Definition: TclObject.cc:181
static_string_view
constexpr double e
Definition: Math.hh:18
This file implemented 3 utility functions:
Definition: Autofire.cc:9
TclObject makeTclList(Args &&... args)
Definition: TclObject.hh:281
STL namespace.
std::string strCat(Ts &&...ts)
Definition: strCat.hh:549