Open Lighting Architecture  Latest Git
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 
38 namespace ola {
39 
40 /*
41  * Checks the value of a variable
42  */
43 class 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  */
55 class StringValidator: public Validator {
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  */
71 template <class T>
72 class 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  */
85 class BoolValidator: public Validator {
86  public:
87  BoolValidator(): Validator() {}
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  */
101 class UIntValidator: public Validator {
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  */
116 class 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  */
131 class IPv4Validator: public Validator {
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 
141 };
142 
143 
144 /*
145  * The abstract Preferences class
146  */
147 class Preferences {
148  public:
149  explicit Preferences(const std::string name): m_preference_name(name) {}
150 
154  virtual ~Preferences() {}
155 
159  virtual bool Load() = 0;
160 
164  virtual bool Save() const = 0;
165 
169  virtual void Clear() = 0;
170 
175  virtual std::string ConfigLocation() const = 0;
176 
182  virtual void SetValue(const std::string &key, const std::string &value) = 0;
183 
190  virtual void SetValue(const std::string &key, unsigned int value) = 0;
191 
198  virtual void SetValue(const std::string &key, int value) = 0;
199 
205  virtual void SetMultipleValue(const std::string &key,
206  const std::string &value) = 0;
207 
214  virtual void SetMultipleValue(const std::string &key, unsigned int value) = 0;
215 
222  virtual void SetMultipleValue(const std::string &key, int value) = 0;
223 
233  virtual bool SetDefaultValue(const std::string &key,
234  const Validator &validator,
235  const std::string &value) = 0;
236 
247  virtual bool SetDefaultValue(const std::string &key,
248  const Validator &validator,
249  const char value[]) = 0;
250 
260  virtual bool SetDefaultValue(const std::string &key,
261  const Validator &validator,
262  unsigned int value) = 0;
263 
273  virtual bool SetDefaultValue(const std::string &key,
274  const Validator &validator,
275  int value) = 0;
276 
286  virtual bool SetDefaultValue(const std::string &key,
287  const Validator &validator,
288  bool value) = 0;
289 
296  virtual std::string GetValue(const std::string &key) const = 0;
297 
303  virtual std::vector<std::string> GetMultipleValue(
304  const std::string &key) const = 0;
305 
311  virtual bool HasKey(const std::string &key) const = 0;
312 
317  virtual void RemoveValue(const std::string &key) = 0;
318 
319  // bool helper methods
325  virtual bool GetValueAsBool(const std::string &key) const = 0;
326 
332  virtual void SetValueAsBool(const std::string &key, bool value) = 0;
333 
334  protected:
335  std::string m_preference_name;
336 
337  private:
339 };
340 
341 
346  public:
347  PreferencesFactory() {}
348 
352  virtual ~PreferencesFactory();
353 
357  virtual Preferences *NewPreference(const std::string &name);
358 
363  virtual std::string ConfigLocation() const = 0;
364 
365  private:
366  virtual Preferences *Create(const std::string &name) = 0;
367  std::map<std::string, Preferences*> m_preferences_map;
368 };
369 
370 
371 /*
372  * MemoryPreferences just stores the preferences in memory. Useful for testing.
373  */
375  public:
376  explicit MemoryPreferences(const std::string name): Preferences(name) {}
377  virtual ~MemoryPreferences();
378  virtual bool Load() { return true; }
379  virtual bool Save() const { return true; }
380  virtual void Clear();
381 
382  virtual std::string ConfigLocation() const { return "Not Saved"; }
383 
384  virtual void SetValue(const std::string &key, const std::string &value);
385  virtual void SetValue(const std::string &key, unsigned int value);
386  virtual void SetValue(const std::string &key, int value);
387  virtual void SetMultipleValue(const std::string &key,
388  const std::string &value);
389  virtual void SetMultipleValue(const std::string &key, unsigned int value);
390  virtual void SetMultipleValue(const std::string &key, int value);
391  virtual bool SetDefaultValue(const std::string &key,
392  const Validator &validator,
393  const std::string &value);
394  virtual bool SetDefaultValue(const std::string &key,
395  const Validator &validator,
396  const char value[]);
397  virtual bool SetDefaultValue(const std::string &key,
398  const Validator &validator,
399  unsigned int value);
400  virtual bool SetDefaultValue(const std::string &key,
401  const Validator &validator,
402  int value);
403  virtual bool SetDefaultValue(const std::string &key,
404  const Validator &validator,
405  bool value);
406 
407  virtual std::string GetValue(const std::string &key) const;
408  virtual std::vector<std::string> GetMultipleValue(
409  const std::string &key) const;
410  virtual bool HasKey(const std::string &key) const;
411 
412  virtual void RemoveValue(const std::string &key);
413 
414  // bool helper methods
415  virtual bool GetValueAsBool(const std::string &key) const;
416  virtual void SetValueAsBool(const std::string &key, bool value);
417 
418  bool operator==(const MemoryPreferences &other) {
419  return m_pref_map == other.m_pref_map;
420  }
421 
422  protected:
423  typedef std::multimap<std::string, std::string> PreferencesMap;
424  PreferencesMap m_pref_map;
425 };
426 
427 
429  public:
430  virtual std::string ConfigLocation() const { return "Not Saved"; }
431 
432  private:
433  MemoryPreferences *Create(const std::string &name) {
434  return new MemoryPreferences(name);
435  }
436 };
437 
438 
443  public:
444  typedef std::multimap<std::string, std::string> PreferencesMap;
446 
447  void SavePreferences(const std::string &filename,
448  const PreferencesMap &preferences);
449 
453  void *Run();
454 
458  bool Join(void *ptr = NULL);
459 
465  void Synchronize();
466 
467  private:
469 
473  void CompleteSynchronization(ola::thread::ConditionVariable *condition,
474  ola::thread::Mutex *mutex);
475 };
476 
477 
478 /*
479  * FilePreferences uses one file per namespace
480  */
482  public:
483  explicit FileBackedPreferences(const std::string &directory,
484  const std::string &name,
485  FilePreferenceSaverThread *saver_thread)
486  : MemoryPreferences(name),
487  m_directory(directory),
488  m_saver_thread(saver_thread) {}
489 
490  virtual bool Load();
491  virtual bool Save() const;
492 
497  bool LoadFromFile(const std::string &filename);
498 
499  std::string ConfigLocation() const { return FileName(); }
500 
501  private:
502  const std::string m_directory;
503  FilePreferenceSaverThread *m_saver_thread;
504 
505  bool ChangeDir() const;
506 
510  const std::string FileName() const;
511  static const char OLA_CONFIG_PREFIX[];
512  static const char OLA_CONFIG_SUFFIX[];
513 };
514 
515 
517  public:
518  explicit FileBackedPreferencesFactory(const std::string &directory)
519  : m_directory(directory) {
520  m_saver_thread.Start();
521  }
522 
524  m_saver_thread.Join();
525  }
526 
527  virtual std::string ConfigLocation() const { return m_directory; }
528 
529  private:
530  const std::string m_directory;
531  FilePreferenceSaverThread m_saver_thread;
532 
533  FileBackedPreferences *Create(const std::string &name) {
534  return new FileBackedPreferences(m_directory, name, &m_saver_thread);
535  }
536 };
537 } // namespace ola
538 #endif // INCLUDE_OLAD_PREFERENCES_H_
Definition: Preferences.h:131
Definition: Preferences.h:516
Definition: Preferences.h:345
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Creates dummy copy constructor and assignment operator declarations.
Definition: Macro.h:44
Definition: Preferences.h:481
virtual bool Save() const
Definition: Preferences.h:379
virtual std::string ConfigLocation() const
The location where preferences will be stored.
Definition: Preferences.h:527
Definition: Preferences.h:43
Definition: Preferences.h:72
std::string ConfigLocation() const
The location of where these preferences are stored.
Definition: Preferences.h:499
Definition: Thread.h:52
Definition: Preferences.h:101
Definition: Preferences.h:374
A single threaded I/O event management system.
Definition: SelectServer.h:63
Definition: Preferences.h:55
Definition: Preferences.h:147
Helper macros.
Definition: Preferences.h:428
virtual bool Load()
Definition: Preferences.h:378
virtual std::string ConfigLocation() const
The location where preferences will be stored.
Definition: Preferences.h:430
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
Definition: Preferences.h:442
Header file for OLA Logging.
virtual std::string ConfigLocation() const
The location of where these preferences are stored.
Definition: Preferences.h:382
virtual ~Preferences()
Definition: Preferences.h:154
Definition: Preferences.h:85
Definition: Mutex.h:41
Definition: Mutex.h:81
Definition: Preferences.h:116