Open Lighting Architecture  0.9.6
 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., 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/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 ConfigLocation() 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 
245  virtual bool SetDefaultValue(const std::string &key,
246  const Validator &validator,
247  const char value[]) = 0;
248 
258  virtual bool SetDefaultValue(const std::string &key,
259  const Validator &validator,
260  unsigned int value) = 0;
261 
271  virtual bool SetDefaultValue(const std::string &key,
272  const Validator &validator,
273  int value) = 0;
274 
284  virtual bool SetDefaultValue(const std::string &key,
285  const Validator &validator,
286  bool value) = 0;
287 
294  virtual std::string GetValue(const std::string &key) const = 0;
295 
301  virtual std::vector<std::string> GetMultipleValue(
302  const std::string &key) const = 0;
303 
309  virtual bool HasKey(const std::string &key) const = 0;
310 
315  virtual void RemoveValue(const std::string &key) = 0;
316 
317  // bool helper methods
323  virtual bool GetValueAsBool(const std::string &key) const = 0;
324 
330  virtual void SetValueAsBool(const std::string &key, bool value) = 0;
331 
332  protected:
333  std::string m_preference_name;
334 
335  private:
336  DISALLOW_COPY_AND_ASSIGN(Preferences);
337 };
338 
339 
344  public:
345  PreferencesFactory() {}
346 
350  virtual ~PreferencesFactory();
351 
355  virtual Preferences *NewPreference(const std::string &name);
356 
361  virtual std::string ConfigLocation() const = 0;
362 
363  private:
364  virtual Preferences *Create(const std::string &name) = 0;
365  std::map<std::string, Preferences*> m_preferences_map;
366 };
367 
368 
369 /*
370  * MemoryPreferences just stores the preferences in memory. Useful for testing.
371  */
373  public:
374  explicit MemoryPreferences(const std::string name): Preferences(name) {}
375  virtual ~MemoryPreferences();
376  virtual bool Load() { return true; }
377  virtual bool Save() const { return true; }
378  virtual void Clear();
379 
380  virtual std::string ConfigLocation() const { return "Not Saved"; }
381 
382  virtual void SetValue(const std::string &key, const std::string &value);
383  virtual void SetValue(const std::string &key, unsigned int value);
384  virtual void SetValue(const std::string &key, int value);
385  virtual void SetMultipleValue(const std::string &key,
386  const std::string &value);
387  virtual void SetMultipleValue(const std::string &key, unsigned int value);
388  virtual void SetMultipleValue(const std::string &key, int value);
389  virtual bool SetDefaultValue(const std::string &key,
390  const Validator &validator,
391  const std::string &value);
392  virtual bool SetDefaultValue(const std::string &key,
393  const Validator &validator,
394  const char value[]);
395  virtual bool SetDefaultValue(const std::string &key,
396  const Validator &validator,
397  unsigned int value);
398  virtual bool SetDefaultValue(const std::string &key,
399  const Validator &validator,
400  int value);
401  virtual bool SetDefaultValue(const std::string &key,
402  const Validator &validator,
403  bool value);
404 
405  virtual std::string GetValue(const std::string &key) const;
406  virtual std::vector<std::string> GetMultipleValue(
407  const std::string &key) const;
408  virtual bool HasKey(const std::string &key) const;
409 
410  virtual void RemoveValue(const std::string &key);
411 
412  // bool helper methods
413  virtual bool GetValueAsBool(const std::string &key) const;
414  virtual void SetValueAsBool(const std::string &key, bool value);
415 
416  bool operator==(const MemoryPreferences &other) {
417  return m_pref_map == other.m_pref_map;
418  }
419 
420  protected:
421  typedef std::multimap<std::string, std::string> PreferencesMap;
422  PreferencesMap m_pref_map;
423 };
424 
425 
427  public:
428  virtual std::string ConfigLocation() const { return "Not Saved"; }
429 
430  private:
431  MemoryPreferences *Create(const std::string &name) {
432  return new MemoryPreferences(name);
433  }
434 };
435 
436 
441  public:
442  typedef std::multimap<std::string, std::string> PreferencesMap;
444 
445  void SavePreferences(const std::string &filename,
446  const PreferencesMap &preferences);
447 
451  void *Run();
452 
456  bool Join(void *ptr = NULL);
457 
463  void Syncronize();
464 
465  private:
467 
471  void CompleteSyncronization(ola::thread::ConditionVariable *condition,
472  ola::thread::Mutex *mutex);
473 };
474 
475 
476 /*
477  * FilePreferences uses one file per namespace
478  */
480  public:
481  explicit FileBackedPreferences(const std::string &directory,
482  const std::string &name,
483  FilePreferenceSaverThread *saver_thread)
484  : MemoryPreferences(name),
485  m_directory(directory),
486  m_saver_thread(saver_thread) {}
487 
488  virtual bool Load();
489  virtual bool Save() const;
490 
495  bool LoadFromFile(const std::string &filename);
496 
497  std::string ConfigLocation() const { return FileName(); }
498 
499  private:
500  const std::string m_directory;
501  FilePreferenceSaverThread *m_saver_thread;
502 
503  bool ChangeDir() const;
504 
508  const std::string FileName() const;
509  static const char OLA_CONFIG_PREFIX[];
510  static const char OLA_CONFIG_SUFFIX[];
511 };
512 
513 
515  public:
516  explicit FileBackedPreferencesFactory(const std::string &directory)
517  : m_directory(directory) {
518  m_saver_thread.Start();
519  }
520 
522  m_saver_thread.Join();
523  }
524 
525  virtual std::string ConfigLocation() const { return m_directory; }
526 
527  private:
528  const std::string m_directory;
529  FilePreferenceSaverThread m_saver_thread;
530 
531  FileBackedPreferences *Create(const std::string &name) {
532  return new FileBackedPreferences(m_directory, name, &m_saver_thread);
533  }
534 };
535 } // namespace ola
536 #endif // INCLUDE_OLAD_PREFERENCES_H_