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
openmsx::Setting::getInterpreter
Interpreter & getInterpreter() const
Definition: Setting.cc:162
openmsx::CommandController
Definition: CommandController.hh:18
openmsx::Setting::getValue
const TclObject & getValue() const final override
Gets the current value of this setting as a TclObject.
Definition: Setting.hh:135
openmsx::Setting::DONT_SAVE
@ DONT_SAVE
Definition: Setting.hh:127
openmsx::Setting::getCommandController
CommandController & getCommandController() const
Definition: Setting.hh:169
StringSetting.hh
openmsx::StringSetting
Definition: StringSetting.hh:9
CommandController.hh
openmsx::CliComm::printWarning
void printWarning(std::string_view message)
Definition: CliComm.cc:10
openmsx::CommandController::getCliComm
virtual CliComm & getCliComm()=0
openmsx::TclCallback::getValue
TclObject getValue() const
Definition: TclCallback.cc:35
openmsx::TclCallback::execute
TclObject execute()
Definition: TclCallback.cc:40
openmsx::TclCallback::getSetting
StringSetting & getSetting() const
Definition: TclCallback.hh:31
openmsx::TclObject::executeCommand
TclObject executeCommand(Interpreter &interp, bool compile=false)
Interpret this TclObject as a command and execute it.
Definition: TclObject.cc:172
TclCallback.hh
openmsx::TclCallback::TclCallback
TclCallback(CommandController &controller, std::string_view name, std::string_view description, bool useCliComm=true, bool save=true)
Definition: TclCallback.cc:13
openmsx::Setting::SAVE
@ SAVE
Definition: Setting.hh:126
openmsx::makeTclList
TclObject makeTclList(Args &&... args)
Definition: TclObject.hh:280
openmsx::TclObject
Definition: TclObject.hh:22
CliComm.hh
strCat
std::string strCat(Ts &&...ts)
Definition: strCat.hh:573
CommandException.hh
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
openmsx::TclCallback::~TclCallback
~TclCallback()