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 */
6
7
8 #include "Setting.h"
9
10 #include <new>
11
12
13 // #pragma mark - Setting
14
15
~Setting()16 Setting::~Setting()
17 {
18 }
19
20
21 // #pragma mark - BoolSetting
22
23
24 setting_type
Type() const25 BoolSetting::Type() const
26 {
27 return SETTING_TYPE_BOOL;
28 }
29
30
31 BVariant
DefaultValue() const32 BoolSetting::DefaultValue() const
33 {
34 return DefaultBoolValue();
35 }
36
37
38 // #pragma mark - FloatSetting
39
40
41 setting_type
Type() const42 FloatSetting::Type() const
43 {
44 return SETTING_TYPE_FLOAT;
45 }
46
47
48 BVariant
DefaultValue() const49 FloatSetting::DefaultValue() const
50 {
51 return DefaultFloatValue();
52 }
53
54
55 // #pragma mark - SettingsOption
56
57
~SettingsOption()58 SettingsOption::~SettingsOption()
59 {
60 }
61
62
63 // #pragma mark - OptionsSetting
64
65
66 setting_type
Type() const67 OptionsSetting::Type() const
68 {
69 return SETTING_TYPE_OPTIONS;
70 }
71
72
73 BVariant
DefaultValue() const74 OptionsSetting::DefaultValue() const
75 {
76 SettingsOption* option = DefaultOption();
77 return option != NULL
78 ? BVariant(option->ID(), B_VARIANT_DONT_COPY_DATA) : BVariant();
79 }
80
81
82 // #pragma mark - BoundedSetting
83
84
85 setting_type
Type() const86 BoundedSetting::Type() const
87 {
88 return SETTING_TYPE_BOUNDED;
89 }
90
91
92 // #pragma mark - RangeSetting
93
94
95 setting_type
Type() const96 RangeSetting::Type() const
97 {
98 return SETTING_TYPE_RANGE;
99 }
100
101
102 // #pragma mark - RectSetting
103
104 setting_type
Type() const105 RectSetting::Type() const
106 {
107 return SETTING_TYPE_RECT;
108 }
109
110
111 BVariant
DefaultValue() const112 RectSetting::DefaultValue() const
113 {
114 return DefaultRectValue();
115 }
116
117
118 // #pragma mark - StringSetting
119
120
121 setting_type
Type() const122 StringSetting::Type() const
123 {
124 return SETTING_TYPE_STRING;
125 }
126
127
128 BVariant
DefaultValue() const129 StringSetting::DefaultValue() const
130 {
131 return DefaultStringValue().String();
132 }
133
134
135 // #pragma mark - AbstractSetting
136
137
AbstractSetting(const BString & id,const BString & name)138 AbstractSetting::AbstractSetting(const BString& id, const BString& name)
139 :
140 fID(id),
141 fName(name)
142 {
143 }
144
145
146 const char*
ID() const147 AbstractSetting::ID() const
148 {
149 return fID;
150 }
151
152
153 const char*
Name() const154 AbstractSetting::Name() const
155 {
156 return fName;
157 }
158
159
160 // #pragma mark - BoolSettingImpl
161
162
BoolSettingImpl(const BString & id,const BString & name,bool defaultValue)163 BoolSettingImpl::BoolSettingImpl(const BString& id, const BString& name,
164 bool defaultValue)
165 :
166 AbstractSetting(id, name),
167 fDefaultValue(defaultValue)
168 {
169 }
170
171
172 bool
DefaultBoolValue() const173 BoolSettingImpl::DefaultBoolValue() const
174 {
175 return fDefaultValue;
176 }
177
178
179 // #pragma mark - FloatSettingImpl
180
181
FloatSettingImpl(const BString & id,const BString & name,float defaultValue)182 FloatSettingImpl::FloatSettingImpl(const BString& id, const BString& name,
183 float defaultValue)
184 :
185 AbstractSetting(id, name),
186 fDefaultValue(defaultValue)
187 {
188 }
189
190
191 float
DefaultFloatValue() const192 FloatSettingImpl::DefaultFloatValue() const
193 {
194 return fDefaultValue;
195 }
196
197
198 // #pragma mark - OptionsSettingImpl
199
200
201 class OptionsSettingImpl::Option : public SettingsOption {
202 public:
Option(const BString & id,const BString & name)203 Option(const BString& id, const BString& name)
204 {
205 }
206
ID() const207 virtual const char* ID() const
208 {
209 return fID;
210 }
211
Name() const212 virtual const char* Name() const
213 {
214 return fName;
215 }
216
217 private:
218 BString fID;
219 BString fName;
220 };
221
222
OptionsSettingImpl(const BString & id,const BString & name)223 OptionsSettingImpl::OptionsSettingImpl(const BString& id, const BString& name)
224 :
225 AbstractSetting(id, name),
226 fDefaultOption(NULL)
227 {
228 }
229
230
~OptionsSettingImpl()231 OptionsSettingImpl::~OptionsSettingImpl()
232 {
233 SetDefaultOption(NULL);
234
235 for (int32 i = 0; SettingsOption* option = fOptions.ItemAt(i); i++)
236 option->ReleaseReference();
237 }
238
239
240 SettingsOption*
DefaultOption() const241 OptionsSettingImpl::DefaultOption() const
242 {
243 return fDefaultOption != NULL ? fDefaultOption : fOptions.ItemAt(0);
244 }
245
246
247 int32
CountOptions() const248 OptionsSettingImpl::CountOptions() const
249 {
250 return fOptions.CountItems();
251 }
252
253
254 SettingsOption*
OptionAt(int32 index) const255 OptionsSettingImpl::OptionAt(int32 index) const
256 {
257 return fOptions.ItemAt(index);
258 }
259
260
261 SettingsOption*
OptionByID(const char * id) const262 OptionsSettingImpl::OptionByID(const char* id) const
263 {
264 for (int32 i = 0; SettingsOption* option = fOptions.ItemAt(i); i++) {
265 if (strcmp(option->ID(), id) == 0)
266 return option;
267 }
268
269 return NULL;
270 }
271
272
273 bool
AddOption(SettingsOption * option)274 OptionsSettingImpl::AddOption(SettingsOption* option)
275 {
276 if (!fOptions.AddItem(option))
277 return false;
278
279 option->AcquireReference();
280 return true;
281 }
282
283
284 bool
AddOption(const BString & id,const BString & name)285 OptionsSettingImpl::AddOption(const BString& id, const BString& name)
286 {
287 Option* option = new(std::nothrow) Option(id, name);
288 if (option == NULL)
289 return false;
290 BReference<Option> optionReference(option, true);
291
292 return AddOption(option);
293 }
294
295
296 void
SetDefaultOption(SettingsOption * option)297 OptionsSettingImpl::SetDefaultOption(SettingsOption* option)
298 {
299 if (option == fDefaultOption)
300 return;
301
302 if (fDefaultOption != NULL)
303 fDefaultOption->ReleaseReference();
304
305 fDefaultOption = option;
306
307 if (fDefaultOption != NULL)
308 fDefaultOption->AcquireReference();
309 }
310
311
312 // #pragma mark - RangeSettingImpl
313
314
BoundedSettingImpl(const BString & id,const BString & name,const BVariant & lowerBound,const BVariant & upperBound,const BVariant & defaultValue)315 BoundedSettingImpl::BoundedSettingImpl(const BString& id, const BString& name,
316 const BVariant& lowerBound, const BVariant& upperBound,
317 const BVariant& defaultValue)
318 :
319 AbstractSetting(id, name),
320 fLowerBound(lowerBound),
321 fUpperBound(upperBound),
322 fDefaultValue(defaultValue)
323 {
324 }
325
326
327 BVariant
DefaultValue() const328 BoundedSettingImpl::DefaultValue() const
329 {
330 return fDefaultValue;
331 }
332
333
334 BVariant
LowerBound() const335 BoundedSettingImpl::LowerBound() const
336 {
337 return fLowerBound;
338 }
339
340
341 BVariant
UpperBound() const342 BoundedSettingImpl::UpperBound() const
343 {
344 return fUpperBound;
345 }
346
347
348 // #pragma mark - RangeSettingImpl
349
350
RangeSettingImpl(const BString & id,const BString & name,const BVariant & lowerBound,const BVariant & upperBound,const BVariant & lowerValue,const BVariant & upperValue)351 RangeSettingImpl::RangeSettingImpl(const BString& id, const BString& name,
352 const BVariant& lowerBound, const BVariant& upperBound,
353 const BVariant& lowerValue, const BVariant& upperValue)
354 :
355 AbstractSetting(id, name),
356 fLowerBound(lowerBound),
357 fUpperBound(upperBound),
358 fLowerValue(lowerValue),
359 fUpperValue(upperValue)
360 {
361 }
362
363
364 BVariant
DefaultValue() const365 RangeSettingImpl::DefaultValue() const
366 {
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();
371 }
372
373
374 BVariant
LowerBound() const375 RangeSettingImpl::LowerBound() const
376 {
377 return fLowerBound;
378 }
379
380
381 BVariant
UpperBound() const382 RangeSettingImpl::UpperBound() const
383 {
384 return fUpperBound;
385 }
386
387
388 BVariant
LowerValue() const389 RangeSettingImpl::LowerValue() const
390 {
391 return fLowerValue;
392 }
393
394
395 BVariant
UpperValue() const396 RangeSettingImpl::UpperValue() const
397 {
398 return fUpperValue;
399 }
400
401
402 // #pragma mark - RectSettingImpl
403
404
RectSettingImpl(const BString & id,const BString & name,const BRect & defaultValue)405 RectSettingImpl::RectSettingImpl(const BString& id, const BString& name,
406 const BRect& defaultValue)
407 :
408 AbstractSetting(id, name),
409 fDefaultValue(defaultValue)
410 {
411 }
412
413
414 BRect
DefaultRectValue() const415 RectSettingImpl::DefaultRectValue() const
416 {
417 return fDefaultValue;
418 }
419
420
421 // #pragma mark - StringSettingImpl
422
423
StringSettingImpl(const BString & id,const BString & name,const BString & defaultValue)424 StringSettingImpl::StringSettingImpl(const BString& id, const BString& name,
425 const BString& defaultValue)
426 :
427 AbstractSetting(id, name),
428 fDefaultValue(defaultValue)
429 {
430 }
431
432
433 const BString&
DefaultStringValue() const434 StringSettingImpl::DefaultStringValue() const
435 {
436 return fDefaultValue;
437 }
438