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  string_view name,
16  string_view description,
17  bool useCliComm_,
18  bool save)
19  : callbackSetting2(std::make_unique<StringSetting>(
20  controller, name, description, 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;
46  command.addListElement(callback);
47  return executeCommon(command);
48 }
49 
51 {
52  const auto& callback = getValue();
53  if (callback.empty()) return TclObject();
54 
55  TclObject command;
56  command.addListElement(callback);
57  command.addListElement(arg1);
58  return executeCommon(command);
59 }
60 
61 TclObject TclCallback::execute(int arg1, int arg2)
62 {
63  const auto& callback = getValue();
64  if (callback.empty()) return TclObject();
65 
66  TclObject command;
67  command.addListElement(callback);
68  command.addListElement(arg1);
69  command.addListElement(arg2);
70  return executeCommon(command);
71 }
72 
74 {
75  const auto& callback = getValue();
76  if (callback.empty()) return TclObject();
77 
78  TclObject command;
79  command.addListElement(callback);
80  command.addListElement(arg1);
81  command.addListElement(arg2);
82  return executeCommon(command);
83 }
84 
86 {
87  const auto& callback = getValue();
88  if (callback.empty()) return TclObject();
89 
90  TclObject command;
91  command.addListElement(callback);
92  command.addListElement(arg1);
93  command.addListElement(arg2);
94  return executeCommon(command);
95 }
96 
97 TclObject TclCallback::executeCommon(TclObject& command)
98 {
99  try {
100  return command.executeCommand(callbackSetting.getInterpreter());
101  } catch (CommandException& e) {
102  string message = strCat(
103  "Error executing callback function \"",
104  getSetting().getFullName(), "\": ", e.getMessage());
105  if (useCliComm) {
107  message);
108  } else {
109  std::cerr << message << std::endl;
110  }
111  return TclObject();
112  }
113 }
114 
115 } // namespace openmsx
StringSetting & getSetting() const
Definition: TclCallback.hh:31
TclCallback(CommandController &controller, string_view name, string_view description, bool useCliComm=true, bool save=true)
Definition: TclCallback.cc:13
const std::string & getMessage() const &
Definition: MSXException.hh:23
TclObject getValue() const
Definition: TclCallback.cc:35
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:134
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:15
void save(SDL_Surface *image, const std::string &filename)
Definition: PNG.cc:366
virtual CliComm & getCliComm()=0
void addListElement(string_view element)
Definition: TclObject.cc:69
Interpreter & getInterpreter() const
Definition: Setting.cc:162
std::string strCat(Ts &&...ts)
Definition: strCat.hh:577
CommandController & getCommandController() const
Definition: Setting.hh:168
TclObject executeCommand(Interpreter &interp, bool compile=false)
Interpret this TclObject as a command and execute it.
Definition: TclObject.cc:208
void printWarning(string_view message)
Definition: CliComm.cc:20