22 #ifndef INCLUDE_OLAD_PREFERENCES_H_
23 #define INCLUDE_OLAD_PREFERENCES_H_
27 #include <ola/thread/Thread.h>
28 #include <ola/io/SelectServer.h>
46 virtual bool IsValid(
const std::string &value)
const = 0;
57 m_empty_ok(empty_ok) {
59 bool IsValid(
const std::string &value)
const;
62 const bool m_empty_ok;
72 explicit SetValidator(
const std::set<T> &values) : m_values(values) {}
73 bool IsValid(
const std::string &value)
const;
86 bool IsValid(
const std::string &value)
const;
91 static const char ENABLED[];
92 static const char DISABLED[];
101 UIntValidator(
unsigned int greater_than,
unsigned int less_than)
102 : m_gt(greater_than),
104 bool IsValid(
const std::string &value)
const;
107 unsigned int m_gt, m_lt;
117 : m_gt(greater_than),
119 bool IsValid(
const std::string &value)
const;
132 m_empty_ok(empty_ok) {}
134 bool IsValid(
const std::string &value)
const;
147 explicit Preferences(
const std::string name): m_preference_name(name) {}
157 virtual bool Load() = 0;
162 virtual bool Save()
const = 0;
167 virtual void Clear() = 0;
173 virtual std::string
Source()
const = 0;
180 virtual void SetValue(
const std::string &key,
const std::string &value) = 0;
188 virtual void SetValue(
const std::string &key,
unsigned int value) = 0;
196 virtual void SetValue(
const std::string &key,
int value) = 0;
204 const std::string &value) = 0;
212 virtual void SetMultipleValue(
const std::string &key,
unsigned int value) = 0;
233 const std::string &value) = 0;
246 unsigned int value) = 0;
267 virtual std::string
GetValue(
const std::string &key)
const = 0;
275 const std::string &key)
const = 0;
282 virtual bool HasKey(
const std::string &key)
const = 0;
288 virtual void RemoveValue(
const std::string &key) = 0;
303 virtual void SetValueAsBool(
const std::string &key,
bool value) = 0;
306 std::string m_preference_name;
331 virtual Preferences *Create(
const std::string &name) = 0;
332 std::map<std::string, Preferences*> m_preferences_map;
343 virtual bool Load() {
return true; }
344 virtual bool Save()
const {
return true; }
345 virtual void Clear();
347 virtual std::string
Source()
const {
return "Not Saved"; }
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);
353 const std::string &value);
358 const std::string &value);
366 virtual std::string
GetValue(
const std::string &key)
const;
368 const std::string &key)
const;
369 virtual bool HasKey(
const std::string &key)
const;
378 return m_pref_map == other.m_pref_map;
382 typedef std::multimap<std::string, std::string> PreferencesMap;
383 PreferencesMap m_pref_map;
400 typedef std::multimap<std::string, std::string> PreferencesMap;
403 void SavePreferences(
const std::string &filename,
404 const PreferencesMap &preferences);
414 bool Join(
void *ptr = NULL);
429 void SaveToFile(
const std::string *filename,
430 const PreferencesMap *preferences);
446 const std::string &name,
449 m_directory(directory),
450 m_saver_thread(saver_thread) {}
453 virtual bool Save()
const;
461 std::string
Source()
const {
return FileName(); }
464 const std::string m_directory;
467 bool ChangeDir()
const;
472 const std::string FileName()
const;
473 static const char OLA_CONFIG_PREFIX[];
474 static const char OLA_CONFIG_SUFFIX[];
481 : m_directory(directory) {
482 m_saver_thread.Start();
486 m_saver_thread.
Join();
490 const std::string m_directory;
498 #endif // INCLUDE_OLAD_PREFERENCES_H_