2 Copyright (c) 2002-2009 Tampere University.
4 This file is part of TTA-Based Codesign Environment (TCE).
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:
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
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.
25 * Templated simulator setting.
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.
34 template <class SettingValueType, class SettingAction>
35 class TemplatedSimulatorSetting : public SettingCommand::SimulatorSetting {
41 * @param description Description of the setting, to be displayed in
42 * the setting listing.
44 TemplatedSimulatorSetting(const std::string& description) :
45 description_(description) {
46 value_ = SettingAction::defaultValue();
52 virtual ~TemplatedSimulatorSetting() {
56 * Sets a new value to the setting.
58 * SettingValueType class is used to validate and parse the value,
59 * SettingAction is used to perform any action connected to changing
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.
67 virtual bool setValue(
68 SimulatorInterpreter& interpreter,
69 SimulatorFrontend& simFront,
70 const DataObject& newValue) {
72 if (!SettingValueType::valid(newValue)) {
74 (SimulatorToolbox::textGenerator().text(
75 Texts::TXT_INTERP_SETTING_PARSE_ERROR) %
76 SettingValueType::validStringsDescription()).str());
80 SettingAction::execute(
81 interpreter, simFront, SettingValueType::parseValue(newValue));
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());
102 * Returns the current value of the setting as a string.
104 * Delegates formatting of the string to SettingValueType class.
106 * @param Current value of the setting as a string.
108 virtual std::string valueAsString() {
109 return SettingValueType::valueAsString(value_);
113 * Returns the description of the setting.
115 virtual std::string description() {
120 /// the current value of the setting
122 /// the description of the setting
123 std::string description_;
127 * Boolean setting type.
129 class BooleanSetting {
133 * Validates the given value.
135 * @param value The value to be validated.
136 * @return True if the value can be interpreted as a boolean.
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");
149 * Returns the string that describes the valid value strings for this
152 * @return Description of the valid strings for this setting.
154 static std::string validStringsDescription() {
155 return "one of {0, 1, true, false, enabled, disabled, on, off}";
159 * Returns the value formatted as a boolean expression.
161 * @return Value as string.
163 static std::string valueAsString(const DataObject& value) {
164 if (value.stringValue() == "0") {
174 * @param value The value to parse.
175 * @return The parsed value.
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");
184 /// type of this setting
185 typedef bool ValueType;
189 * String setting type.
191 class StringSetting {
195 * Validates the given value.
197 * @param value The value to be validated.
198 * @return True if the value can be interpreted as a boolean.
200 static bool valid(const DataObject&) {
205 * Returns the string that describes the valid value strings for this
208 * @return Description of the valid strings for this setting.
210 static std::string validStringsDescription() {
217 * @return Value as string.
219 static std::string valueAsString(const DataObject& value) {
220 return value.stringValue();
226 * @param value The value to parse.
227 * @return The parsed value.
229 static std::string parseValue(const DataObject& value) {
230 return valueAsString(value);
233 /// type of this setting
234 typedef std::string ValueType;
238 * Unsigned integer setting type.
240 class PositiveIntegerSetting {
244 * Validates the given value.
246 * @param value The value to be validated.
247 * @return True if the value can be interpreted as a boolean.
249 static bool valid(const DataObject& value) {
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();
255 } catch (const NumberFormatException&) {
261 * Returns the string that describes the valid value strings for this
264 * @return Description of the valid strings for this setting.
266 static std::string validStringsDescription() {
267 return "positive integer";
273 * @return Value as string.
275 static std::string valueAsString(const DataObject& value) {
276 return value.stringValue();
282 * @param value The value to parse.
283 * @return The parsed value.
285 static unsigned int parseValue(const DataObject& value) {
286 return static_cast<unsigned int>(value.integerValue());
289 /// type of this setting
290 typedef unsigned int ValueType;