headers/bsd: Add sys/queue.h.
[haiku.git] / src / kits / debugger / settings / generic / Setting.cpp
blob440bae3c602b3e2f7d4ae7a25b474f08fae17734
1 /*
2 * Copyright 2013-2016, Rene Gollent, rene@gollent.com.
3 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
4 * Distributed under the terms of the MIT License.
5 */
8 #include "Setting.h"
10 #include <new>
13 // #pragma mark - Setting
16 Setting::~Setting()
21 // #pragma mark - BoolSetting
24 setting_type
25 BoolSetting::Type() const
27 return SETTING_TYPE_BOOL;
31 BVariant
32 BoolSetting::DefaultValue() const
34 return DefaultBoolValue();
38 // #pragma mark - FloatSetting
41 setting_type
42 FloatSetting::Type() const
44 return SETTING_TYPE_FLOAT;
48 BVariant
49 FloatSetting::DefaultValue() const
51 return DefaultFloatValue();
55 // #pragma mark - SettingsOption
58 SettingsOption::~SettingsOption()
63 // #pragma mark - OptionsSetting
66 setting_type
67 OptionsSetting::Type() const
69 return SETTING_TYPE_OPTIONS;
73 BVariant
74 OptionsSetting::DefaultValue() const
76 SettingsOption* option = DefaultOption();
77 return option != NULL
78 ? BVariant(option->ID(), B_VARIANT_DONT_COPY_DATA) : BVariant();
82 // #pragma mark - BoundedSetting
85 setting_type
86 BoundedSetting::Type() const
88 return SETTING_TYPE_BOUNDED;
92 // #pragma mark - RangeSetting
95 setting_type
96 RangeSetting::Type() const
98 return SETTING_TYPE_RANGE;
102 // #pragma mark - RectSetting
104 setting_type
105 RectSetting::Type() const
107 return SETTING_TYPE_RECT;
111 BVariant
112 RectSetting::DefaultValue() const
114 return DefaultRectValue();
118 // #pragma mark - StringSetting
121 setting_type
122 StringSetting::Type() const
124 return SETTING_TYPE_STRING;
128 BVariant
129 StringSetting::DefaultValue() const
131 return DefaultStringValue().String();
135 // #pragma mark - AbstractSetting
138 AbstractSetting::AbstractSetting(const BString& id, const BString& name)
140 fID(id),
141 fName(name)
146 const char*
147 AbstractSetting::ID() const
149 return fID;
153 const char*
154 AbstractSetting::Name() const
156 return fName;
160 // #pragma mark - BoolSettingImpl
163 BoolSettingImpl::BoolSettingImpl(const BString& id, const BString& name,
164 bool defaultValue)
166 AbstractSetting(id, name),
167 fDefaultValue(defaultValue)
172 bool
173 BoolSettingImpl::DefaultBoolValue() const
175 return fDefaultValue;
179 // #pragma mark - FloatSettingImpl
182 FloatSettingImpl::FloatSettingImpl(const BString& id, const BString& name,
183 float defaultValue)
185 AbstractSetting(id, name),
186 fDefaultValue(defaultValue)
191 float
192 FloatSettingImpl::DefaultFloatValue() const
194 return fDefaultValue;
198 // #pragma mark - OptionsSettingImpl
201 class OptionsSettingImpl::Option : public SettingsOption {
202 public:
203 Option(const BString& id, const BString& name)
207 virtual const char* ID() const
209 return fID;
212 virtual const char* Name() const
214 return fName;
217 private:
218 BString fID;
219 BString fName;
223 OptionsSettingImpl::OptionsSettingImpl(const BString& id, const BString& name)
225 AbstractSetting(id, name),
226 fDefaultOption(NULL)
231 OptionsSettingImpl::~OptionsSettingImpl()
233 SetDefaultOption(NULL);
235 for (int32 i = 0; SettingsOption* option = fOptions.ItemAt(i); i++)
236 option->ReleaseReference();
240 SettingsOption*
241 OptionsSettingImpl::DefaultOption() const
243 return fDefaultOption != NULL ? fDefaultOption : fOptions.ItemAt(0);
247 int32
248 OptionsSettingImpl::CountOptions() const
250 return fOptions.CountItems();
254 SettingsOption*
255 OptionsSettingImpl::OptionAt(int32 index) const
257 return fOptions.ItemAt(index);
261 SettingsOption*
262 OptionsSettingImpl::OptionByID(const char* id) const
264 for (int32 i = 0; SettingsOption* option = fOptions.ItemAt(i); i++) {
265 if (strcmp(option->ID(), id) == 0)
266 return option;
269 return NULL;
273 bool
274 OptionsSettingImpl::AddOption(SettingsOption* option)
276 if (!fOptions.AddItem(option))
277 return false;
279 option->AcquireReference();
280 return true;
284 bool
285 OptionsSettingImpl::AddOption(const BString& id, const BString& name)
287 Option* option = new(std::nothrow) Option(id, name);
288 if (option == NULL)
289 return false;
290 BReference<Option> optionReference(option, true);
292 return AddOption(option);
296 void
297 OptionsSettingImpl::SetDefaultOption(SettingsOption* option)
299 if (option == fDefaultOption)
300 return;
302 if (fDefaultOption != NULL)
303 fDefaultOption->ReleaseReference();
305 fDefaultOption = option;
307 if (fDefaultOption != NULL)
308 fDefaultOption->AcquireReference();
312 // #pragma mark - RangeSettingImpl
315 BoundedSettingImpl::BoundedSettingImpl(const BString& id, const BString& name,
316 const BVariant& lowerBound, const BVariant& upperBound,
317 const BVariant& defaultValue)
319 AbstractSetting(id, name),
320 fLowerBound(lowerBound),
321 fUpperBound(upperBound),
322 fDefaultValue(defaultValue)
327 BVariant
328 BoundedSettingImpl::DefaultValue() const
330 return fDefaultValue;
334 BVariant
335 BoundedSettingImpl::LowerBound() const
337 return fLowerBound;
341 BVariant
342 BoundedSettingImpl::UpperBound() const
344 return fUpperBound;
348 // #pragma mark - RangeSettingImpl
351 RangeSettingImpl::RangeSettingImpl(const BString& id, const BString& name,
352 const BVariant& lowerBound, const BVariant& upperBound,
353 const BVariant& lowerValue, const BVariant& upperValue)
355 AbstractSetting(id, name),
356 fLowerBound(lowerBound),
357 fUpperBound(upperBound),
358 fLowerValue(lowerValue),
359 fUpperValue(upperValue)
364 BVariant
365 RangeSettingImpl::DefaultValue() const
367 // this one doesn't really make sense for RangeSetting since it
368 // describes a pair of values, which BVariant can't readily
369 // represent.
370 return BVariant();
374 BVariant
375 RangeSettingImpl::LowerBound() const
377 return fLowerBound;
381 BVariant
382 RangeSettingImpl::UpperBound() const
384 return fUpperBound;
388 BVariant
389 RangeSettingImpl::LowerValue() const
391 return fLowerValue;
395 BVariant
396 RangeSettingImpl::UpperValue() const
398 return fUpperValue;
402 // #pragma mark - RectSettingImpl
405 RectSettingImpl::RectSettingImpl(const BString& id, const BString& name,
406 const BRect& defaultValue)
408 AbstractSetting(id, name),
409 fDefaultValue(defaultValue)
414 BRect
415 RectSettingImpl::DefaultRectValue() const
417 return fDefaultValue;
421 // #pragma mark - StringSettingImpl
424 StringSettingImpl::StringSettingImpl(const BString& id, const BString& name,
425 const BString& defaultValue)
427 AbstractSetting(id, name),
428 fDefaultValue(defaultValue)
433 const BString&
434 StringSettingImpl::DefaultStringValue() const
436 return fDefaultValue;