openMSX
TclCallback.cc
Go to the documentation of this file.
1 #include "TclCallback.hh"
2 #include "CommandController.hh"
3 #include "CliComm.hh"
4 #include "CommandException.hh"
5 #include "StringSetting.hh"
6 #include <iostream>
7 #include <memory>
8 
9 using std::string;
10 
11 namespace openmsx {
12 
14  CommandController& controller,
15  std::string_view name,
16  std::string_view description,
17  bool useCliComm_,
18  bool save)
19  : callbackSetting2(std::make_unique<StringSetting>(
20  controller, name, description, std::string_view{},
22  , callbackSetting(*callbackSetting2)
23  , useCliComm(useCliComm_)
24 {
25 }
26 
28  : callbackSetting(setting)
29  , useCliComm(true)
30 {
31 }
32 
33 TclCallback::~TclCallback() = default;
34 
36 {
37  return getSetting().getValue();
38 }
39 
41 {
42  const auto& callback = getValue();
43  if (callback.empty()) return TclObject();
44 
45  TclObject command = makeTclList(callback);
46  return executeCommon(command);
47 }
48 
50 {
51  const auto& callback = getValue();
52  if (callback.empty()) return TclObject();
53 
54  TclObject command = makeTclList(callback, arg1);
55  return executeCommon(command);
56 }
57 
58 TclObject TclCallback::execute(int arg1, int arg2)
59 {
60  const auto& callback = getValue();
61  if (callback.empty()) return TclObject();
62 
63  TclObject command = makeTclList(callback, arg1, arg2);
64  return executeCommon(command);
65 }
66 
67 TclObject TclCallback::execute(int arg1, std::string_view arg2)
68 {
69  const auto& callback = getValue();
70  if (callback.empty()) return TclObject();
71 
72  TclObject command = makeTclList(callback, arg1, arg2);
73  return executeCommon(command);
74 }
75 
76 TclObject TclCallback::execute(std::string_view arg1, std::string_view arg2)
77 {
78  const auto& callback = getValue();
79  if (callback.empty()) return TclObject();
80 
81  TclObject command = makeTclList(callback, arg1, arg2);
82  return executeCommon(command);
83 }
84 
85 TclObject TclCallback::executeCommon(TclObject& command)
86 {
87  try {
88  return command.executeCommand(callbackSetting.getInterpreter());
89  } catch (CommandException& e) {
90  string message = strCat(
91  "Error executing callback function \"",
92  getSetting().getFullName(), "\": ", e.getMessage());
93  if (useCliComm) {
95  message);
96  } else {
97  std::cerr << message << '\n';
98  }
99  return TclObject();
100  }
101 }
102 
103 } // namespace openmsx
StringSetting & getSetting() const
Definition: TclCallback.hh:31
const std::string & getMessage() const &
Definition: MSXException.hh:23
TclObject getValue() const
Definition: TclCallback.cc:35
TclCallback(CommandController &controller, std::string_view name, std::string_view description, bool useCliComm=true, bool save=true)
Definition: TclCallback.cc:13
TclObject execute()
Definition: TclCallback.cc:40
STL namespace.
const TclObject & getValue() const final override
Gets the current value of this setting as a TclObject.
Definition: Setting.hh:135
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
void printWarning(std::string_view message)
Definition: CliComm.cc:10
virtual CliComm & getCliComm()=0
Interpreter & getInterpreter() const
Definition: Setting.cc:162
std::string strCat(Ts &&...ts)
Definition: strCat.hh:573
CommandController & getCommandController() const
Definition: Setting.hh:169
TclObject makeTclList(Args &&... args)
Definition: TclObject.hh:280
TclObject executeCommand(Interpreter &interp, bool compile=false)
Interpret this TclObject as a command and execute it.
Definition: TclObject.cc:172