openMSX
TclCallback.cc
Go to the documentation of this file.
1 #include "TclCallback.hh"
2 #include "TclObject.hh"
4 #include "CliComm.hh"
5 #include "CommandException.hh"
6 #include "StringSetting.hh"
7 #include "memory.hh"
8 #include <iostream>
9 #include <cassert>
10 
11 using std::string;
12 
13 namespace openmsx {
14 
16  CommandController& controller,
17  string_ref name,
18  string_ref description,
19  bool useCliComm_,
20  bool save)
21  : callbackSetting2(make_unique<StringSetting>(
22  controller, name, description, "",
23  save ? Setting::SAVE : Setting::DONT_SAVE))
24  , callbackSetting(*callbackSetting2)
25  , useCliComm(useCliComm_)
26 {
27 }
28 
30  : callbackSetting(setting)
31  , useCliComm(true)
32 {
33 }
34 
36 {
37 }
38 
40 {
41  return callbackSetting;
42 }
43 
44 string TclCallback::getValue() const
45 {
46  return getSetting().getValue();
47 }
48 
50 {
51  const string callback = getValue();
52  if (callback.empty()) return;
53 
54  TclObject command(callbackSetting.getInterpreter());
55  command.addListElement(callback);
56  executeCommon(command);
57 }
58 
59 void TclCallback::execute(int arg1, int arg2)
60 {
61  const string callback = getValue();
62  if (callback.empty()) return;
63 
64  TclObject command(callbackSetting.getInterpreter());
65  command.addListElement(callback);
66  command.addListElement(arg1);
67  command.addListElement(arg2);
68  executeCommon(command);
69 }
70 
71 void TclCallback::execute(int arg1, string_ref arg2)
72 {
73  const string callback = getValue();
74  if (callback.empty()) return;
75 
76  TclObject command(callbackSetting.getInterpreter());
77  command.addListElement(callback);
78  command.addListElement(arg1);
79  command.addListElement(arg2);
80  executeCommon(command);
81 }
82 
84 {
85  const std::string callback = getValue();
86  if (callback.empty()) return;
87 
88  TclObject command(callbackSetting.getInterpreter());
89  command.addListElement(callback);
90  command.addListElement(arg1);
91  command.addListElement(arg2);
92  executeCommon(command);
93 }
94 
95 void TclCallback::executeCommon(TclObject& command)
96 {
97  try {
98  command.executeCommand();
99  } catch (CommandException& e) {
100  string message =
101  "Error executing callback function \"" +
102  getSetting().getName() + "\": " + e.getMessage();
103  if (useCliComm) {
105  message);
106  } else {
107  std::cerr << message << std::endl;
108  }
109  }
110 }
111 
112 } // namespace openmsx