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 "memory.hh"
7 #include <iostream>
8 
9 using std::string;
10 
11 namespace openmsx {
12 
14  CommandController& controller,
15  string_ref name,
16  string_ref description,
17  bool useCliComm_,
18  bool save)
19  : callbackSetting2(make_unique<StringSetting>(
20  controller, name, description, string_ref{},
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 =
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
TclObject getValue() const
Definition: TclCallback.cc:35
const string_ref getFullName() const
Definition: Setting.hh:33
void printWarning(string_ref message)
Definition: CliComm.cc:20
TclObject execute()
Definition: TclCallback.cc:40
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
void save(SDL_Surface *surface, const std::string &filename)
Definition: PNG.cc:369
const std::string & getMessage() const
Definition: MSXException.hh:13
const TclObject & getValue() const final override
Gets the current value of this setting as a TclObject.
Definition: Setting.hh:133
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
void addListElement(string_ref element)
Definition: TclObject.cc:69
virtual CliComm & getCliComm()=0
Interpreter & getInterpreter() const
Definition: Setting.cc:162
CommandController & getCommandController() const
Definition: Setting.hh:167
TclObject executeCommand(Interpreter &interp, bool compile=false)
Interpret this TclObject as a command and execute it.
Definition: TclObject.cc:208
TclCallback(CommandController &controller, string_ref name, string_ref description, bool useCliComm=true, bool save=true)
Definition: TclCallback.cc:13
std::unique_ptr< T > make_unique()
Definition: memory.hh:27