Open Lighting Architecture  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  *
16  * preferences.h
17  * Interface for the Preferences class - this allows storing user preferences /
18  * settings.
19  * Copyright (C) 2005-2006 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/thread/Thread.h>
28 #include <ola/io/SelectServer.h>
29 
30 #include <map>
31 #include <vector>
32 #include <set>
33 #include <string>
34 
35 
36 namespace ola {
37 
38 /*
39  * Checks the value of a variable
40  */
41 class Validator {
42  public:
43  Validator() {}
44  virtual ~Validator() {}
45 
46  virtual bool IsValid(const std::string &value) const = 0;
47 };
48 
49 
50 /*
51  * Check a value is a non-empty string
52  */
53 class StringValidator: public Validator {
54  public:
55  explicit StringValidator(bool empty_ok = false)
56  : Validator(),
57  m_empty_ok(empty_ok) {
58  }
59  bool IsValid(const std::string &value) const;
60 
61  private:
62  const bool m_empty_ok;
63 };
64 
65 
66 /*
67  * Check that a value is one of a set of values
68  */
69 template <class T>
70 class SetValidator: public Validator {
71  public:
72  explicit SetValidator(const std::set<T> &values) : m_values(values) {}
73  bool IsValid(const std::string &value) const;
74 
75  private:
76  std::set<T> m_values;
77 };
78 
79 
80 /*
81  * Check that a value is a valid bool
82  */
83 class BoolValidator: public Validator {
84  public:
85  BoolValidator(): Validator() {}
86  bool IsValid(const std::string &value) const;
87 
88  // On win32 TRUE and FALSE are #define'd. We can #undef them here but that
89  // doesn't fix the case in the calling code. So we use ENABLED and DISABLED
90  // instead.
91  static const char ENABLED[];
92  static const char DISABLED[];
93 };
94 
95 
96 /*
97  * Check that a value falls within a range of unsigned ints.
98  */
99 class UIntValidator: public Validator {
100  public:
101  UIntValidator(unsigned int greater_than, unsigned int less_than)
102  : m_gt(greater_than),
103  m_lt(less_than) {}
104  bool IsValid(const std::string &value) const;
105 
106  private:
107  unsigned int m_gt, m_lt;
108 };
109 
110 
111 /*
112  * Check that a value falls within a range of ints.
113  */
114 class IntValidator: public Validator {
115  public:
116  IntValidator(int greater_than, int less_than)
117  : m_gt(greater_than),
118  m_lt(less_than) {}
119  bool IsValid(const std::string &value) const;
120 
121  private:
122  int m_gt, m_lt;
123 };
124 
125 
126 /*
127  * Check an IPv4 address is valid
128  */
129 class IPv4Validator: public Validator {
130  public:
131  explicit IPv4Validator(bool empty_ok = true):
132  m_empty_ok(empty_ok) {}
133 
134  bool IsValid(const std::string &value) const;
135  private:
136  bool m_empty_ok;
137 
138  DISALLOW_COPY_AND_ASSIGN(IPv4Validator);
139 };
140 
141 
142 /*
143  * The abstract Preferences class
144  */
145 class Preferences {
146  public:
147  explicit Preferences(const std::string name): m_preference_name(name) {}
148 
152  virtual ~Preferences() {}
153 
157  virtual bool Load() = 0;
158 
162  virtual bool Save() const = 0;
163 
167  virtual void Clear() = 0;
168 
173  virtual std::string Source() const = 0;
174 
180  virtual void SetValue(const std::string &key, const std::string &value) = 0;
181 
188  virtual void SetValue(const std::string &key, unsigned int value) = 0;
189 
196  virtual void SetValue(const std::string &key, int value) = 0;
197 
203  virtual void SetMultipleValue(const std::string &key,
204  const std::string &value) = 0;
205 
212  virtual void SetMultipleValue(const std::string &key, unsigned int value) = 0;
213 
220  virtual void SetMultipleValue(const std::string &key, int value) = 0;
221 
231  virtual bool SetDefaultValue(const std::string &key,
232  const Validator &validator,
233  const std::string &value) = 0;
234 
244  virtual bool SetDefaultValue(const std::string &key,
245  const Validator &validator,
246  unsigned int value) = 0;
247 
257  virtual bool SetDefaultValue(const std::string &key,
258  const Validator &validator,
259  int value) = 0;
260 
267  virtual std::string GetValue(const std::string &key) const = 0;
268 
274  virtual std::vector<std::string> GetMultipleValue(
275  const std::string &key) const = 0;
276 
282  virtual bool HasKey(const std::string &key) const = 0;
283 
288  virtual void RemoveValue(const std::string &key) = 0;
289 
290  // bool helper methods
296  virtual bool GetValueAsBool(const std::string &key) const = 0;
297 
303  virtual void SetValueAsBool(const std::string &key, bool value) = 0;
304 
305  protected:
306  std::string m_preference_name;
307 
308  private:
309  DISALLOW_COPY_AND_ASSIGN(Preferences);
310 };
311 
312 
317  public:
318  PreferencesFactory() {}
319 
323  virtual ~PreferencesFactory();
324 
328  virtual Preferences *NewPreference(const std::string &name);
329 
330  private:
331  virtual Preferences *Create(const std::string &name) = 0;
332  std::map<std::string, Preferences*> m_preferences_map;
333 };
334 
335 
336 /*
337  * MemoryPreferences just stores the preferences in memory. Useful for testing.
338  */
340  public:
341  explicit MemoryPreferences(const std::string name): Preferences(name) {}
342  virtual ~MemoryPreferences();
343  virtual bool Load() { return true; }
344  virtual bool Save() const { return true; }
345  virtual void Clear();
346 
347  virtual std::string Source() const { return "Not Saved"; }
348 
349  virtual void SetValue(const std::string &key, const std::string &value);
350  virtual void SetValue(const std::string &key, unsigned int value);
351  virtual void SetValue(const std::string &key, int value);
352  virtual void SetMultipleValue(const std::string &key,
353  const std::string &value);
354  virtual void SetMultipleValue(const std::string &key, unsigned int value);
355  virtual void SetMultipleValue(const std::string &key, int value);
356  virtual bool SetDefaultValue(const std::string &key,
357  const Validator &validator,
358  const std::string &value);
359  virtual bool SetDefaultValue(const std::string &key,
360  const Validator &validator,
361  unsigned int value);
362  virtual bool SetDefaultValue(const std::string &key,
363  const Validator &validator,
364  int value);
365 
366  virtual std::string GetValue(const std::string &key) const;
367  virtual std::vector<std::string> GetMultipleValue(
368  const std::string &key) const;
369  virtual bool HasKey(const std::string &key) const;
370 
371  virtual void RemoveValue(const std::string &key);
372 
373  // bool helper methods
374  virtual bool GetValueAsBool(const std::string &key) const;
375  virtual void SetValueAsBool(const std::string &key, bool value);
376 
377  bool operator==(const MemoryPreferences &other) {
378  return m_pref_map == other.m_pref_map;
379  }
380 
381  protected:
382  typedef std::multimap<std::string, std::string> PreferencesMap;
383  PreferencesMap m_pref_map;
384 };
385 
386 
388  private:
389  MemoryPreferences *Create(const std::string &name) {
390  return new MemoryPreferences(name);
391  }
392 };
393 
394 
399  public:
400  typedef std::multimap<std::string, std::string> PreferencesMap;
402 
403  void SavePreferences(const std::string &filename,
404  const PreferencesMap &preferences);
405 
409  void *Run();
410 
414  bool Join(void *ptr = NULL);
415 
421  void Syncronize();
422 
423  private:
425 
429  void SaveToFile(const std::string *filename,
430  const PreferencesMap *preferences);
431 
435  void CompleteSyncronization(ola::thread::ConditionVariable *condition,
436  ola::thread::Mutex *mutex);
437 };
438 
439 
440 /*
441  * FilePreferences uses one file per namespace
442  */
444  public:
445  explicit FileBackedPreferences(const std::string &directory,
446  const std::string &name,
447  FilePreferenceSaverThread *saver_thread)
448  : MemoryPreferences(name),
449  m_directory(directory),
450  m_saver_thread(saver_thread) {}
451 
452  virtual bool Load();
453  virtual bool Save() const;
454 
459  bool LoadFromFile(const std::string &filename);
460 
461  std::string Source() const { return FileName(); }
462 
463  private:
464  const std::string m_directory;
465  FilePreferenceSaverThread *m_saver_thread;
466 
467  bool ChangeDir() const;
468 
472  const std::string FileName() const;
473  static const char OLA_CONFIG_PREFIX[];
474  static const char OLA_CONFIG_SUFFIX[];
475 };
476 
477 
479  public:
480  explicit FileBackedPreferencesFactory(const std::string &directory)
481  : m_directory(directory) {
482  m_saver_thread.Start();
483  }
484 
486  m_saver_thread.Join();
487  }
488 
489  private:
490  const std::string m_directory;
491  FilePreferenceSaverThread m_saver_thread;
492 
493  FileBackedPreferences *Create(const std::string &name) {
494  return new FileBackedPreferences(m_directory, name, &m_saver_thread);
495  }
496 };
497 } // namespace ola
498 #endif // INCLUDE_OLAD_PREFERENCES_H_