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 <iostream>
6 #include <memory>
7 
8 namespace openmsx {
9 
11  CommandController& controller,
12  std::string_view name,
13  static_string_view description,
14  bool useCliComm_,
15  bool save)
16  : callbackSetting2(std::in_place,
17  controller, name, description, std::string_view{},
18  save ? Setting::SAVE : Setting::DONT_SAVE)
19  , callbackSetting(*callbackSetting2)
20  , useCliComm(useCliComm_)
21 {
22 }
23 
25  : callbackSetting(setting)
26  , useCliComm(true)
27 {
28 }
29 
31 {
32  return getSetting().getValue();
33 }
34 
36 {
37  const auto& callback = getValue();
38  if (callback.empty()) return TclObject();
39 
40  TclObject command = makeTclList(callback);
41  return executeCommon(command);
42 }
43 
45 {
46  const auto& callback = getValue();
47  if (callback.empty()) return TclObject();
48 
49  TclObject command = makeTclList(callback, arg1);
50  return executeCommon(command);
51 }
52 
53 TclObject TclCallback::execute(int arg1, int arg2)
54 {
55  const auto& callback = getValue();
56  if (callback.empty()) return TclObject();
57 
58  TclObject command = makeTclList(callback, arg1, arg2);
59  return executeCommon(command);
60 }
61 
62 TclObject TclCallback::execute(int arg1, std::string_view arg2)
63 {
64  const auto& callback = getValue();
65  if (callback.empty()) return TclObject();
66 
67  TclObject command = makeTclList(callback, arg1, arg2);
68  return executeCommon(command);
69 }
70 
71 TclObject TclCallback::execute(std::string_view arg1, std::string_view arg2)
72 {
73  const auto& callback = getValue();
74  if (callback.empty()) return TclObject();
75 
76  TclObject command = makeTclList(callback, arg1, arg2);
77  return executeCommon(command);
78 }
79 
80 TclObject TclCallback::executeCommon(TclObject& command)
81 {
82  try {
83  return command.executeCommand(callbackSetting.getInterpreter());
84  } catch (CommandException& e) {
85  auto message = strCat(
86  "Error executing callback function \"",
87  getSetting().getFullName(), "\": ", e.getMessage());
88  if (useCliComm) {
90  message);
91  } else {
92  std::cerr << message << '\n';
93  }
94  return TclObject();
95  }
96 }
97 
98 } // namespace openmsx
BaseSetting * setting
Definition: Interpreter.cc:27
void printWarning(std::string_view message)
Definition: CliComm.cc:10
virtual CliComm & getCliComm()=0
const TclObject & getValue() const final
Gets the current value of this setting as a TclObject.
Definition: Setting.hh:142
Interpreter & getInterpreter() const
Definition: Setting.cc:148
CommandController & getCommandController() const
Definition: Setting.hh:177
TclObject execute()
Definition: TclCallback.cc:35
TclObject getValue() const
Definition: TclCallback.cc:30
StringSetting & getSetting() const
Definition: TclCallback.hh:31
TclCallback(CommandController &controller, std::string_view name, static_string_view description, bool useCliComm=true, bool save=true)
Definition: TclCallback.cc:10
TclObject executeCommand(Interpreter &interp, bool compile=false)
Interpret this TclObject as a command and execute it.
Definition: TclObject.cc:181
static_string_view
This file implemented 3 utility functions:
Definition: Autofire.cc:9
TclObject makeTclList(Args &&... args)
Definition: TclObject.hh:290
std::string strCat(Ts &&...ts)
Definition: strCat.hh:591