RADLib
RADical C++ application framework
RADSettings.h
Go to the documentation of this file.
1 #ifndef RADSETTINGS_H
2 #define RADSETTINGS_H
3 
9 #pragma once
10 
11 #include <RADCore/RADCore.h>
12 
13 #include <map>
14 #include <string>
15 
16 namespace RADSettings {
17 
19  enum class Format {
21  Json,
23  Ini
24  };
25 
27  struct AppIdentity {
29  std::string organization;
31  std::string application;
32  };
33 
35  class Settings {
36  public:
41 
43  const std::string& fileName() const;
45  Format format() const;
47  bool load(std::string* error = nullptr);
49  bool save(std::string* error = nullptr) const;
51  bool contains(const std::string& key) const;
53  void remove(const std::string& key);
55  void clear();
56 
58  RADCore::RADJsonValue value(const std::string& key, RADCore::RADJsonValue defaultValue = {}) const;
60  void setValue(const std::string& key, RADCore::RADJsonValue value);
62  bool boolValue(const std::string& key, bool defaultValue = false) const;
64  int64_t intValue(const std::string& key, int64_t defaultValue = 0) const;
66  double doubleValue(const std::string& key, double defaultValue = 0.0) const;
68  std::string stringValue(const std::string& key, const std::string& defaultValue = {}) const;
70  void setBool(const std::string& key, bool value);
72  void setInt(const std::string& key, int64_t value);
74  void setDouble(const std::string& key, double value);
76  void setString(const std::string& key, std::string value);
77 
79  static std::string defaultFileName(const AppIdentity& identity, Format format = Format::Json);
80 
81  private:
82  bool loadJson(std::string* error);
83  bool saveJson(std::string* error) const;
84  bool loadIni(std::string* error);
85  bool saveIni(std::string* error) const;
86 
87  std::string fileName_;
88  Format format_ = Format::Json;
89  std::map<std::string, RADCore::RADJsonValue> values_;
90  };
91 
92 } // namespace RADSettings
93 
94 #endif
JSON value supporting null, bool, number, string, array, and object.
Definition: RADCore.h:509
Typed application settings store.
Definition: RADSettings.h:35
Settings(AppIdentity identity, Format format=Format::Json)
Creates settings under the platform config path from identity.
bool contains(const std::string &key) const
Returns true when key exists.
const std::string & fileName() const
Returns backing file path.
void setBool(const std::string &key, bool value)
Sets a bool.
bool boolValue(const std::string &key, bool defaultValue=false) const
Returns a bool.
void setString(const std::string &key, std::string value)
Sets a string.
int64_t intValue(const std::string &key, int64_t defaultValue=0) const
Returns an integer.
Format format() const
Returns file format.
void clear()
Clears all keys.
void setDouble(const std::string &key, double value)
Sets a double.
RADCore::RADJsonValue value(const std::string &key, RADCore::RADJsonValue defaultValue={}) const
Returns a JSON value.
void remove(const std::string &key)
Removes a key.
bool save(std::string *error=nullptr) const
Saves settings, creating parent directories when needed.
double doubleValue(const std::string &key, double defaultValue=0.0) const
Returns a double.
void setValue(const std::string &key, RADCore::RADJsonValue value)
Sets a JSON value.
Settings(std::string fileName, Format format=Format::Json)
Creates settings at fileName using format.
std::string stringValue(const std::string &key, const std::string &defaultValue={}) const
Returns a string.
bool load(std::string *error=nullptr)
Loads settings; missing files are treated as empty settings.
void setInt(const std::string &key, int64_t value)
Sets an integer.
static std::string defaultFileName(const AppIdentity &identity, Format format=Format::Json)
Returns a config file path for identity.
Definition: RADSettings.h:16
Format
Settings file format.
Definition: RADSettings.h:19
@ Ini
INI-style section/key settings file.
@ Json
JSON object settings file.
Application identity used for standard settings paths.
Definition: RADSettings.h:27
std::string application
Application name.
Definition: RADSettings.h:31
std::string organization
Organization or vendor name.
Definition: RADSettings.h:29