Open Lighting Architecture 0.10.9
Loading...
Searching...
No Matches
Preferences.h
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 *
16 * Preferences.h
17 * Interface for the Preferences class - this allows storing user preferences /
18 * settings.
19 * Copyright (C) 2005 Simon Newton
20 */
21
22#ifndef INCLUDE_OLAD_PREFERENCES_H_
23#define INCLUDE_OLAD_PREFERENCES_H_
24
25#include <ola/base/Macro.h>
26#include <ola/Logging.h>
27#include <ola/io/SelectServer.h>
28// On MinGW, Thread.h pulls in pthread.h which pulls in Windows.h, which needs
29// to be after WinSock2.h, hence this order
30#include <ola/thread/Thread.h>
31
32#include <map>
33#include <vector>
34#include <set>
35#include <string>
36
37
38namespace ola {
39
40/*
41 * Checks the value of a variable
42 */
43class Validator {
44 public:
45 Validator() {}
46 virtual ~Validator() {}
47
48 virtual bool IsValid(const std::string &value) const = 0;
49};
50
51
52/*
53 * Check a value is a non-empty string
54 */
56 public:
57 explicit StringValidator(bool empty_ok = false)
58 : Validator(),
59 m_empty_ok(empty_ok) {
60 }
61 bool IsValid(const std::string &value) const;
62
63 private:
64 const bool m_empty_ok;
65};
66
67
68/*
69 * Check that a value is one of a set of values
70 */
71template <class T>
72class SetValidator: public Validator {
73 public:
74 explicit SetValidator(const std::set<T> &values) : m_values(values) {}
75 bool IsValid(const std::string &value) const;
76
77 private:
78 std::set<T> m_values;
79};
80
81
82/*
83 * Check that a value is a valid bool
84 */
85class BoolValidator: public Validator {
86 public:
88 bool IsValid(const std::string &value) const;
89
90 // On win32 TRUE and FALSE are #define'd. We can #undef them here but that
91 // doesn't fix the case in the calling code. So we use ENABLED and DISABLED
92 // instead.
93 static const char ENABLED[];
94 static const char DISABLED[];
95};
96
97
98/*
99 * Check that a value falls within a range of unsigned ints.
100 */
102 public:
103 UIntValidator(unsigned int greater_than, unsigned int less_than)
104 : m_gt(greater_than),
105 m_lt(less_than) {}
106 bool IsValid(const std::string &value) const;
107
108 private:
109 unsigned int m_gt, m_lt;
110};
111
112
113/*
114 * Check that a value falls within a range of ints.
115 */
116class IntValidator: public Validator {
117 public:
118 IntValidator(int greater_than, int less_than)
119 : m_gt(greater_than),
120 m_lt(less_than) {}
121 bool IsValid(const std::string &value) const;
122
123 private:
124 int m_gt, m_lt;
125};
126
127
128/*
129 * Check an IPv4 address is valid
130 */
132 public:
133 explicit IPv4Validator(bool empty_ok = true):
134 m_empty_ok(empty_ok) {}
135
136 bool IsValid(const std::string &value) const;
137 private:
138 bool m_empty_ok;
139
140 IPv4Validator(const IPv4Validator &) = delete;
141 const IPv4Validator &operator=(const IPv4Validator &) = delete;
142};
143
144
145/*
146 * The abstract Preferences class
147 */
149 public:
150 explicit Preferences(const std::string name): m_preference_name(name) {}
151
155 virtual ~Preferences() {}
156
160 virtual bool Load() = 0;
161
165 virtual bool Save() const = 0;
166
170 virtual void Clear() = 0;
171
176 virtual std::string ConfigLocation() const = 0;
177
183 virtual void SetValue(const std::string &key, const std::string &value) = 0;
184
191 virtual void SetValue(const std::string &key, unsigned int value) = 0;
192
199 virtual void SetValue(const std::string &key, int value) = 0;
200
206 virtual void SetMultipleValue(const std::string &key,
207 const std::string &value) = 0;
208
215 virtual void SetMultipleValue(const std::string &key, unsigned int value) = 0;
216
223 virtual void SetMultipleValue(const std::string &key, int value) = 0;
224
234 virtual bool SetDefaultValue(const std::string &key,
235 const Validator &validator,
236 const std::string &value) = 0;
237
248 virtual bool SetDefaultValue(const std::string &key,
249 const Validator &validator,
250 const char value[]) = 0;
251
261 virtual bool SetDefaultValue(const std::string &key,
262 const Validator &validator,
263 unsigned int value) = 0;
264
274 virtual bool SetDefaultValue(const std::string &key,
275 const Validator &validator,
276 int value) = 0;
277
287 virtual bool SetDefaultValue(const std::string &key,
288 const Validator &validator,
289 bool value) = 0;
290
297 virtual std::string GetValue(const std::string &key) const = 0;
298
304 virtual std::vector<std::string> GetMultipleValue(
305 const std::string &key) const = 0;
306
312 virtual bool HasKey(const std::string &key) const = 0;
313
318 virtual void RemoveValue(const std::string &key) = 0;
319
320 // bool helper methods
326 virtual bool GetValueAsBool(const std::string &key) const = 0;
327
333 virtual void SetValueAsBool(const std::string &key, bool value) = 0;
334
335 protected:
336 std::string m_preference_name;
337
338 private:
339 Preferences(const Preferences &) = delete;
340 const Preferences &operator=(const Preferences &) = delete;
341};
342
343
348 public:
350
354 virtual ~PreferencesFactory();
355
359 virtual Preferences *NewPreference(const std::string &name);
360
365 virtual std::string ConfigLocation() const = 0;
366
367 private:
368 virtual Preferences *Create(const std::string &name) = 0;
369 std::map<std::string, Preferences*> m_preferences_map;
370};
371
372
373/*
374 * MemoryPreferences just stores the preferences in memory. Useful for testing.
375 */
377 public:
378 explicit MemoryPreferences(const std::string name): Preferences(name) {}
379 virtual ~MemoryPreferences();
380 virtual bool Load() { return true; }
381 virtual bool Save() const { return true; }
382 virtual void Clear();
383
384 virtual std::string ConfigLocation() const { return "Not Saved"; }
385
386 virtual void SetValue(const std::string &key, const std::string &value);
387 virtual void SetValue(const std::string &key, unsigned int value);
388 virtual void SetValue(const std::string &key, int value);
389 virtual void SetMultipleValue(const std::string &key,
390 const std::string &value);
391 virtual void SetMultipleValue(const std::string &key, unsigned int value);
392 virtual void SetMultipleValue(const std::string &key, int value);
393 virtual bool SetDefaultValue(const std::string &key,
394 const Validator &validator,
395 const std::string &value);
396 virtual bool SetDefaultValue(const std::string &key,
397 const Validator &validator,
398 const char value[]);
399 virtual bool SetDefaultValue(const std::string &key,
400 const Validator &validator,
401 unsigned int value);
402 virtual bool SetDefaultValue(const std::string &key,
403 const Validator &validator,
404 int value);
405 virtual bool SetDefaultValue(const std::string &key,
406 const Validator &validator,
407 bool value);
408
409 virtual std::string GetValue(const std::string &key) const;
410 virtual std::vector<std::string> GetMultipleValue(
411 const std::string &key) const;
412 virtual bool HasKey(const std::string &key) const;
413
414 virtual void RemoveValue(const std::string &key);
415
416 // bool helper methods
417 virtual bool GetValueAsBool(const std::string &key) const;
418 virtual void SetValueAsBool(const std::string &key, bool value);
419
420 bool operator==(const MemoryPreferences &other) {
421 return m_pref_map == other.m_pref_map;
422 }
423
424 protected:
425 typedef std::multimap<std::string, std::string> PreferencesMap;
426 PreferencesMap m_pref_map;
427};
428
429
431 public:
432 virtual std::string ConfigLocation() const { return "Not Saved"; }
433
434 private:
435 MemoryPreferences *Create(const std::string &name) {
436 return new MemoryPreferences(name);
437 }
438};
439
440
445 public:
446 typedef std::multimap<std::string, std::string> PreferencesMap;
448
449 void SavePreferences(const std::string &filename,
450 const PreferencesMap &preferences);
451
455 void *Run();
456
460 bool Join(void *ptr = NULL);
461
467 void Syncronize();
468
469 private:
471
475 void CompleteSyncronization(ola::thread::ConditionVariable *condition,
476 ola::thread::Mutex *mutex);
477};
478
479
480/*
481 * FilePreferences uses one file per namespace
482 */
484 public:
485 explicit FileBackedPreferences(const std::string &directory,
486 const std::string &name,
487 FilePreferenceSaverThread *saver_thread)
488 : MemoryPreferences(name),
489 m_directory(directory),
490 m_saver_thread(saver_thread) {}
491
492 virtual bool Load();
493 virtual bool Save() const;
494
499 bool LoadFromFile(const std::string &filename);
500
501 std::string ConfigLocation() const { return FileName(); }
502
503 private:
504 const std::string m_directory;
505 FilePreferenceSaverThread *m_saver_thread;
506
507 bool ChangeDir() const;
508
512 const std::string FileName() const;
513 static const char OLA_CONFIG_PREFIX[];
514 static const char OLA_CONFIG_SUFFIX[];
515};
516
517
519 public:
520 explicit FileBackedPreferencesFactory(const std::string &directory)
521 : m_directory(directory) {
522 m_saver_thread.Start();
523 }
524
526 m_saver_thread.Join();
527 }
528
529 virtual std::string ConfigLocation() const { return m_directory; }
530
531 private:
532 const std::string m_directory;
533 FilePreferenceSaverThread m_saver_thread;
534
535 FileBackedPreferences *Create(const std::string &name) {
536 return new FileBackedPreferences(m_directory, name, &m_saver_thread);
537 }
538};
539} // namespace ola
540#endif // INCLUDE_OLAD_PREFERENCES_H_
Header file for OLA Logging.
Helper macros.
Definition Preferences.h:85
Definition Preferences.h:518
virtual std::string ConfigLocation() const
The location where preferences will be stored.
Definition Preferences.h:529
Definition Preferences.h:483
virtual bool Save() const
Definition Preferences.cpp:420
bool LoadFromFile(const std::string &filename)
Load these preferences from a file.
Definition Preferences.cpp:432
virtual bool Load()
Definition Preferences.cpp:415
std::string ConfigLocation() const
The location of where these preferences are stored.
Definition Preferences.h:501
Definition Preferences.h:444
void * Run()
Definition Preferences.cpp:376
bool Join(void *ptr=NULL)
Definition Preferences.cpp:382
void Syncronize()
Definition Preferences.cpp:388
Definition Preferences.h:131
Definition Preferences.h:116
Definition Preferences.h:430
virtual std::string ConfigLocation() const
The location where preferences will be stored.
Definition Preferences.h:432
Definition Preferences.h:376
virtual bool Save() const
Definition Preferences.h:381
virtual std::string GetValue(const std::string &key) const
Get a preference value.
Definition Preferences.cpp:305
virtual std::vector< std::string > GetMultipleValue(const std::string &key) const
Returns all preference values corresponding to this key.
Definition Preferences.cpp:315
virtual void SetValue(const std::string &key, const std::string &value)
Set a preference value, overriding the existing value.
Definition Preferences.cpp:227
virtual std::string ConfigLocation() const
The location of where these preferences are stored.
Definition Preferences.h:384
virtual void SetMultipleValue(const std::string &key, const std::string &value)
Adds this preference value to the store.
Definition Preferences.cpp:244
virtual bool HasKey(const std::string &key) const
Check if a preference key exists.
Definition Preferences.cpp:327
virtual void RemoveValue(const std::string &key)
Remove a preference value.
Definition Preferences.cpp:332
virtual bool SetDefaultValue(const std::string &key, const Validator &validator, const std::string &value)
Set a preference value if it doesn't already exist, or if it exists and doesn't pass the validator.
Definition Preferences.cpp:261
virtual bool Load()
Definition Preferences.h:380
virtual bool GetValueAsBool(const std::string &key) const
Get a preference value as a bool.
Definition Preferences.cpp:337
virtual void SetValueAsBool(const std::string &key, bool value)
Set a value as a bool.
Definition Preferences.cpp:347
virtual void Clear()
Definition Preferences.cpp:222
Definition Preferences.h:347
virtual std::string ConfigLocation() const =0
The location where preferences will be stored.
virtual Preferences * NewPreference(const std::string &name)
Definition Preferences.cpp:202
virtual ~PreferencesFactory()
Definition Preferences.cpp:192
Definition Preferences.h:148
virtual bool HasKey(const std::string &key) const =0
Check if a preference key exists.
virtual bool SetDefaultValue(const std::string &key, const Validator &validator, unsigned int value)=0
Set a preference value if it doesn't already exist, or if it exists and doesn't pass the validator....
virtual void SetMultipleValue(const std::string &key, int value)=0
Adds this preference value to the store. This helper accepts an int.
virtual void SetValue(const std::string &key, const std::string &value)=0
Set a preference value, overriding the existing value.
virtual void SetValueAsBool(const std::string &key, bool value)=0
Set a value as a bool.
virtual bool GetValueAsBool(const std::string &key) const =0
Get a preference value as a bool.
virtual bool SetDefaultValue(const std::string &key, const Validator &validator, const char value[])=0
Set a preference value if it doesn't already exist, or if it exists and doesn't pass the validator....
virtual void SetMultipleValue(const std::string &key, unsigned int value)=0
Adds this preference value to the store. This helper accepts an unsigned int.
virtual bool SetDefaultValue(const std::string &key, const Validator &validator, int value)=0
Set a preference value if it doesn't already exist, or if it exists and doesn't pass the validator....
virtual bool Load()=0
virtual void SetValue(const std::string &key, int value)=0
Set a preference value, overriding the existing value. This helper accepts an int.
virtual bool SetDefaultValue(const std::string &key, const Validator &validator, bool value)=0
Set a preference value if it doesn't already exist, or if it exists and doesn't pass the validator....
virtual bool Save() const =0
virtual std::string ConfigLocation() const =0
The location of where these preferences are stored.
virtual std::vector< std::string > GetMultipleValue(const std::string &key) const =0
Returns all preference values corresponding to this key.
virtual std::string GetValue(const std::string &key) const =0
Get a preference value.
virtual void RemoveValue(const std::string &key)=0
Remove a preference value.
virtual void SetValue(const std::string &key, unsigned int value)=0
Set a preference value, overriding the existing value. This helper accepts an unsigned int.
virtual ~Preferences()
Definition Preferences.h:155
virtual bool SetDefaultValue(const std::string &key, const Validator &validator, const std::string &value)=0
Set a preference value if it doesn't already exist, or if it exists and doesn't pass the validator.
virtual void Clear()=0
virtual void SetMultipleValue(const std::string &key, const std::string &value)=0
Adds this preference value to the store.
Definition Preferences.h:72
Definition Preferences.h:55
Definition Preferences.h:101
Definition Preferences.h:43
A single threaded I/O event management system.
Definition SelectServer.h:63
Definition Mutex.h:83
Definition Mutex.h:41
Definition Thread.h:52
virtual bool Start()
Start the thread and wait for the thread to be running.
Definition Thread.cpp:90
The namespace containing all OLA symbols.
Definition Credentials.cpp:44