OpenASIP  2.0
SettingCommand.icc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * Templated simulator setting.
26  *
27  * Allows setting the varying parts of setting types using
28  * policy classes: SettingValueType and SettingAction. The previous
29  * validates and parses the user defined value, the latter performs the
30  * activity that should be done when the value is changed. This class
31  * is used in implementation to provide easy generation of SimulatorSetting
32  * classes without duplicating code.
33  */
34 template <class SettingValueType, class SettingAction>
35 class TemplatedSimulatorSetting : public SettingCommand::SimulatorSetting {
36 public:
37 
38  /**
39  * Constructor.
40  *
41  * @param description Description of the setting, to be displayed in
42  * the setting listing.
43  */
44  TemplatedSimulatorSetting(const std::string& description) :
45  description_(description) {
46  value_ = SettingAction::defaultValue();
47  }
48 
49  /**
50  * Destructor.
51  */
52  virtual ~TemplatedSimulatorSetting() {
53  }
54 
55  /**
56  * Sets a new value to the setting.
57  *
58  * SettingValueType class is used to validate and parse the value,
59  * SettingAction is used to perform any action connected to changing
60  * the value.
61  *
62  * @param interpreter Interpreter to set possible error messages to.
63  * @param simFront SimulatorFrontend to use in actions.
64  * @param newValue New value to set.
65  * @return Returns true if setting the value was successful.
66  */
67  virtual bool setValue(
68  SimulatorInterpreter& interpreter,
69  SimulatorFrontend& simFront,
70  const DataObject& newValue) {
71 
72  if (!SettingValueType::valid(newValue)) {
73  interpreter.setError(
74  (SimulatorToolbox::textGenerator().text(
75  Texts::TXT_INTERP_SETTING_PARSE_ERROR) %
76  SettingValueType::validStringsDescription()).str());
77  return false;
78  }
79  bool success =
80  SettingAction::execute(
81  interpreter, simFront, SettingValueType::parseValue(newValue));
82 
83  if (success) {
84  if ((SettingAction::warnOnExistingProgramAndMachine()) &&
85  (simFront.isProgramLoaded() || simFront.isMachineLoaded()) &&
86  (newValue != value_)) {
87  interpreter.setError(SimulatorToolbox::textGenerator().text(
88  Texts::TXT_STARTUP_SETTINGS_CHANGED_WARNING).str());
89 
90  value_ = newValue;
91  return false;
92  }
93 
94  value_ = newValue;
95  return true;
96  } else {
97  return false;
98  }
99  }
100 
101  /**
102  * Returns the current value of the setting as a string.
103  *
104  * Delegates formatting of the string to SettingValueType class.
105  *
106  * @param Current value of the setting as a string.
107  */
108  virtual std::string valueAsString() {
109  return SettingValueType::valueAsString(value_);
110  }
111 
112  /**
113  * Returns the description of the setting.
114  */
115  virtual std::string description() {
116  return description_;
117  }
118 
119 private:
120  /// the current value of the setting
121  DataObject value_;
122  /// the description of the setting
123  std::string description_;
124 };
125 
126 /**
127  * Boolean setting type.
128  */
129 class BooleanSetting {
130 public:
131 
132  /**
133  * Validates the given value.
134  *
135  * @param value The value to be validated.
136  * @return True if the value can be interpreted as a boolean.
137  */
138  static bool valid(const DataObject& value) {
139  std::string lowerCaseString =
140  StringTools::stringToLower(value.stringValue());
141  return (lowerCaseString == "0" || lowerCaseString == "1" ||
142  lowerCaseString == "true" || lowerCaseString == "false" ||
143  lowerCaseString == "enabled" ||
144  lowerCaseString == "disabled" || lowerCaseString == "on" ||
145  lowerCaseString == "off");
146  }
147 
148  /**
149  * Returns the string that describes the valid value strings for this
150  * setting type.
151  *
152  * @return Description of the valid strings for this setting.
153  */
154  static std::string validStringsDescription() {
155  return "one of {0, 1, true, false, enabled, disabled, on, off}";
156  }
157 
158  /**
159  * Returns the value formatted as a boolean expression.
160  *
161  * @return Value as string.
162  */
163  static std::string valueAsString(const DataObject& value) {
164  if (value.stringValue() == "0") {
165  return "0";
166  } else {
167  return "1";
168  }
169  }
170 
171  /**
172  * Parses the value.
173  *
174  * @param value The value to parse.
175  * @return The parsed value.
176  */
177  static bool parseValue(const DataObject& value) {
178  std::string lowerCaseString =
179  StringTools::stringToLower(value.stringValue());
180  return (lowerCaseString == "1" || lowerCaseString == "true" ||
181  lowerCaseString == "enabled" || lowerCaseString == "on");
182  }
183 
184  /// type of this setting
185  typedef bool ValueType;
186 };
187 
188 /**
189  * String setting type.
190  */
191 class StringSetting {
192 public:
193 
194  /**
195  * Validates the given value.
196  *
197  * @param value The value to be validated.
198  * @return True if the value can be interpreted as a boolean.
199  */
200  static bool valid(const DataObject&) {
201  return true;
202  }
203 
204  /**
205  * Returns the string that describes the valid value strings for this
206  * setting type.
207  *
208  * @return Description of the valid strings for this setting.
209  */
210  static std::string validStringsDescription() {
211  return "string";
212  }
213 
214  /**
215  * Returns the value.
216  *
217  * @return Value as string.
218  */
219  static std::string valueAsString(const DataObject& value) {
220  return value.stringValue();
221  }
222 
223  /**
224  * Parses the value.
225  *
226  * @param value The value to parse.
227  * @return The parsed value.
228  */
229  static std::string parseValue(const DataObject& value) {
230  return valueAsString(value);
231  }
232 
233  /// type of this setting
234  typedef std::string ValueType;
235 };
236 
237 /**
238  * Unsigned integer setting type.
239  */
240 class PositiveIntegerSetting {
241 public:
242 
243  /**
244  * Validates the given value.
245  *
246  * @param value The value to be validated.
247  * @return True if the value can be interpreted as a boolean.
248  */
249  static bool valid(const DataObject& value) {
250  try {
251  /// @todo this is broken, what if the given value is uint and its
252  /// value is larger than 31 bits?
253  int val = value.integerValue();
254  return val >= 0;
255  } catch (const NumberFormatException&) {
256  return false;
257  }
258  }
259 
260  /**
261  * Returns the string that describes the valid value strings for this
262  * setting type.
263  *
264  * @return Description of the valid strings for this setting.
265  */
266  static std::string validStringsDescription() {
267  return "positive integer";
268  }
269 
270  /**
271  * Returns the value.
272  *
273  * @return Value as string.
274  */
275  static std::string valueAsString(const DataObject& value) {
276  return value.stringValue();
277  }
278 
279  /**
280  * Parses the value.
281  *
282  * @param value The value to parse.
283  * @return The parsed value.
284  */
285  static unsigned int parseValue(const DataObject& value) {
286  return static_cast<unsigned int>(value.integerValue());
287  }
288 
289  /// type of this setting
290  typedef unsigned int ValueType;
291 };
292