openMSX
Setting.cc
Go to the documentation of this file.
1 #include "Setting.hh"
2 #include "Observer.hh"
3 #include "CommandController.hh"
6 #include "SettingsConfig.hh"
7 #include "TclObject.hh"
8 #include "CliComm.hh"
9 #include "XMLElement.hh"
10 #include "checked_cast.hh"
11 #include <algorithm>
12 #include <cassert>
13 
14 using std::string;
15 
16 namespace openmsx {
17 
18 Setting::Setting(CommandController& commandController_, string_ref name_,
19  string_ref desc_, SaveSetting save_)
20  : commandController(commandController_)
21  , name (name_.str())
22  , description(desc_.str())
23  , save(save_)
24 {
25 }
26 
28 {
29 }
30 
31 const string& Setting::getName() const
32 {
33  return name;
34 }
35 
37 {
38  return description;
39 }
40 
41 void Setting::changeValueString(const std::string& valueString)
42 {
43  getCommandController().changeSetting(*this, valueString);
44 }
45 
46 void Setting::notify() const
47 {
49  commandController.getCliComm().update(
51 
52  // Always keep SettingsConfig in sync.
53  // TODO At the moment (partly because of this change) the whole Settings
54  // structure is more complicated than it could be. Though we're close
55  // to a release, so now is not the time to do big refactorings.
56  sync(getGlobalCommandController().getSettingsConfig().getXMLElement());
57 }
58 
60 {
61  TclObject result;
62  info(result);
63  commandController.getCliComm().update(
65 }
66 
68 {
69  return save == SAVE;
70 }
72 {
73  return save != DONT_TRANSFER;
74 }
75 
76 void Setting::setDontSaveValue(const std::string& dontSaveValue_)
77 {
78  dontSaveValue = dontSaveValue_;
79 }
80 
81 void Setting::sync(XMLElement& config) const
82 {
83  auto& settings = config.getCreateChild("settings");
84  if (!needLoadSave() || hasDefaultValue()) {
85  // remove setting
86  if (auto* elem = settings.findChildWithAttribute(
87  "setting", "id", getName())) {
88  settings.removeChild(*elem);
89  }
90  } else {
91  // add (or overwrite) setting
92  auto& elem = settings.getCreateChildWithAttribute(
93  "setting", "id", getName());
94  // check for non-saveable value
95  // (mechanism can be generalize later when needed)
96  string tmp = getValueString();
97  if (tmp == dontSaveValue) tmp = getRestoreValueString();
98  elem.setData(tmp);
99  }
100 }
101 
102 void Setting::info(TclObject& result) const
103 {
104  result.addListElement(getTypeString());
106  additionalInfo(result);
107 }
108 
110 {
111  return commandController;
112 }
113 
115 {
116  if (auto globalCommandController =
117  dynamic_cast<GlobalCommandController*>(&commandController)) {
118  return *globalCommandController;
119  } else {
120  return checked_cast<MSXCommandController*>(&commandController)
122  }
123 }
124 
126 {
128 }
129 
130 } // namespace openmsx