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 std::string_view defaultValue,
15 Setting::SaveSetting saveSetting,
16 bool useCliComm_)
17 : callbackSetting2(std::in_place,
18 controller, name, description, defaultValue,
19 saveSetting)
20 , callbackSetting(*callbackSetting2)
21 , useCliComm(useCliComm_)
22{
23}
24
26 : callbackSetting(setting)
27 , useCliComm(true)
28{
29}
30
32{
33 return getSetting().getValue();
34}
35
37{
38 const auto& callback = getValue();
39 if (callback.empty()) return {};
40
41 auto command = makeTclList(callback);
42 return executeCommon(command);
43}
44
46{
47 const auto& callback = getValue();
48 if (callback.empty()) return {};
49
50 auto command = makeTclList(callback, arg1);
51 return executeCommon(command);
52}
53
54TclObject TclCallback::execute(int arg1, int arg2) const
55{
56 const auto& callback = getValue();
57 if (callback.empty()) return {};
58
59 auto command = makeTclList(callback, arg1, arg2);
60 return executeCommon(command);
61}
62
63TclObject TclCallback::execute(int arg1, std::string_view arg2) const
64{
65 const auto& callback = getValue();
66 if (callback.empty()) return {};
67
68 auto command = makeTclList(callback, arg1, arg2);
69 return executeCommon(command);
70}
71
72TclObject TclCallback::execute(std::string_view arg1, std::string_view arg2) const
73{
74 const auto& callback = getValue();
75 if (callback.empty()) return {};
76
77 auto command = makeTclList(callback, arg1, arg2);
78 return executeCommon(command);
79}
80
81TclObject TclCallback::executeCommon(TclObject& command) const
82{
83 try {
84 return command.executeCommand(callbackSetting.getInterpreter());
85 } catch (CommandException& e) {
86 auto message = strCat(
87 "Error executing callback function \"",
88 getSetting().getFullName(), "\": ", e.getMessage());
89 if (useCliComm) {
91 message);
92 } else {
93 std::cerr << message << '\n';
94 }
95 return {};
96 }
97}
98
99} // namespace openmsx
BaseSetting * setting
Definition: Interpreter.cc:28
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 getValue() const
Definition: TclCallback.cc:31
TclObject execute() const
Definition: TclCallback.cc:36
TclCallback(CommandController &controller, std::string_view name, static_string_view description, std::string_view defaultValue, Setting::SaveSetting saveSetting, bool useCliComm=true)
Definition: TclCallback.cc:10
StringSetting & getSetting() const
Definition: TclCallback.hh:32
TclObject executeCommand(Interpreter &interp, bool compile=false)
Interpret this TclObject as a command and execute it.
Definition: TclObject.cc:190
static_string_view
constexpr double e
Definition: Math.hh:21
This file implemented 3 utility functions:
Definition: Autofire.cc:9
TclObject makeTclList(Args &&... args)
Definition: TclObject.hh:283
STL namespace.
std::string strCat(Ts &&...ts)
Definition: strCat.hh:542