openMSX
SettingImpl.hh
Go to the documentation of this file.
1 #ifndef SETTINGIMPL_HH
2 #define SETTINGIMPL_HH
3 
4 #include "Setting.hh"
5 
6 namespace openmsx {
7 
8 template<typename T> class SettingChecker;
9 
10 // non-templatized base class for SettingImpl
11 class SettingImplBase : public Setting
12 {
13 protected:
14  SettingImplBase(CommandController& commandController,
15  string_ref name, string_ref description,
16  SaveSetting save);
17  void init();
18  void destroy();
19  void syncProxy();
20 
21  virtual void setValueString2(const std::string& valueString, bool check) = 0;
22 };
23 
24 template <typename POLICY>
25 class SettingImpl : public SettingImplBase, public POLICY
26 {
27 public:
28  typedef POLICY Policy;
29  typedef typename POLICY::Type Type;
30 
31  SettingImpl(CommandController& commandController,
32  string_ref name, string_ref description,
33  const Type& initialValue, SaveSetting save);
34 
35  template <typename T1>
36  SettingImpl(CommandController& commandController,
37  string_ref name, string_ref description,
38  const Type& initialValue, SaveSetting save,
39  T1 extra1);
40 
41  template <typename T1, typename T2>
42  SettingImpl(CommandController& commandController,
43  string_ref name, string_ref description,
44  const Type& initialValue, SaveSetting save,
45  T1 extra1, T2 extra2);
46 
47  virtual ~SettingImpl();
48 
51  Type getValue() const;
52 
60  void changeValue(Type newValue);
61 
64  Type getDefaultValue() const;
65 
68  void setRestoreValue(const Type& value);
69 
72  void setChecker(SettingChecker<POLICY>* checker, bool checkNow = true);
73 
74  // virtual methods from Setting class
75  virtual string_ref getTypeString() const;
76  virtual std::string getValueString() const;
77  virtual std::string getDefaultValueString() const;
78  virtual std::string getRestoreValueString() const;
79  virtual void setValueStringDirect(const std::string& valueString);
80  virtual void restoreDefault();
81  virtual bool hasDefaultValue() const;
82  virtual void tabCompletion(std::vector<std::string>& tokens) const;
83  virtual void additionalInfo(TclObject& result) const;
84 
85 private:
86  void setValue2(Type newValue, bool check);
87  virtual void setValueString2(const std::string& valueString, bool check);
88 
89  SettingChecker<POLICY>* checker;
90  Type value;
91  const Type defaultValue;
92  Type restoreValue;
93 };
94 
95 template<typename POLICY> class SettingChecker
96 {
97 public:
98  virtual void check(SettingImpl<POLICY>& setting,
99  typename SettingImpl<POLICY>::Type& value) = 0;
100 protected:
101  virtual ~SettingChecker() {}
102 };
103 
104 
105 template<typename POLICY>
107  CommandController& commandController,
108  string_ref name, string_ref description,
109  const Type& initialValue, SaveSetting save)
110  : SettingImplBase(commandController, name, description, save)
111  , POLICY()
112  , checker(nullptr)
113  , value(initialValue), defaultValue(initialValue)
114  , restoreValue(initialValue)
115 {
116  init();
117 }
118 
119 template<typename POLICY>
120 template<typename T1>
122  CommandController& commandController,
123  string_ref name, string_ref description,
124  const Type& initialValue, SaveSetting save, T1 extra1)
125  : SettingImplBase(commandController, name, description, save)
126  , POLICY(extra1)
127  , checker(nullptr)
128  , value(initialValue), defaultValue(initialValue)
129  , restoreValue(initialValue)
130 {
131  init();
132 }
133 
134 template<typename POLICY>
135 template<typename T1, typename T2>
137  CommandController& commandController,
138  string_ref name, string_ref description,
139  const Type& initialValue, SaveSetting save, T1 extra1, T2 extra2)
140  : SettingImplBase(commandController, name, description, save)
141  , POLICY(extra1, extra2)
142  , checker(nullptr)
143  , value(initialValue), defaultValue(initialValue)
144  , restoreValue(initialValue)
145 {
146  init();
147 }
148 
149 template<typename POLICY>
151 {
152  destroy();
153 }
154 
155 template<typename POLICY>
157 {
158  return POLICY::checkGetValue(value);
159 }
160 
161 template<typename POLICY>
163 {
164  changeValueString(POLICY::toString(newValue));
165 }
166 
167 template<typename POLICY>
168 void SettingImpl<POLICY>::setValue2(Type newValue, bool check)
169 {
170  if (check) {
171  POLICY::checkSetValue(newValue);
172  }
173  if (checker) {
174  checker->check(*this, newValue);
175  }
176  if (newValue != value) {
177  value = newValue;
178  notify();
179  }
180  syncProxy();
181 }
182 
183 template<typename POLICY>
185 {
186  return POLICY::checkGetValue(defaultValue);
187 }
188 
189 template<typename POLICY>
191 {
192  restoreValue = value;
193 }
194 
195 template<typename POLICY>
197 {
198  checker = checker_;
199  if (checker && checkNow) {
200  setValue2(getValue(), true);
201  }
202 }
203 
204 template<typename POLICY>
206 {
207  return POLICY::getTypeString();
208 }
209 
210 template<typename POLICY>
212 {
213  return POLICY::toString(getValue());
214 }
215 
216 template<typename POLICY>
218 {
219  return POLICY::toString(getDefaultValue());
220 }
221 
222 template<typename POLICY>
224 {
225  return POLICY::toString(POLICY::checkGetValue(restoreValue));
226 }
227 
228 template<typename POLICY>
229 void SettingImpl<POLICY>::setValueStringDirect(const std::string& valueString)
230 {
231  setValueString2(valueString, true);
232 }
233 
234 template<typename POLICY>
235 void SettingImpl<POLICY>::setValueString2(const std::string& valueString, bool check)
236 {
237  setValue2(POLICY::fromString(valueString), check);
238 }
239 
240 template<typename POLICY>
242 {
243  changeValue(restoreValue);
244 }
245 
246 template<typename POLICY>
248 {
249  return getValue() == getDefaultValue();
250 }
251 
252 template<typename POLICY>
253 void SettingImpl<POLICY>::tabCompletion(std::vector<std::string>& tokens) const
254 {
255  POLICY::tabCompletion(tokens);
256 }
257 
258 template<typename POLICY>
260 {
261  POLICY::additionalInfo(result);
262 }
263 
264 } // namespace openmsx
265 
266 #endif