Utilities
Utilities header only library
Config.h
1 /*
2  Copyright (C) Skeletex Research, s.r.o. - All Rights Reserved
3  Unauthorized copying of this file, via any medium is strictly prohibited
4  Proprietary and confidential
5 */
6 #ifndef UTILS_CONFIG_H
7 #define UTILS_CONFIG_H
8 #include <Utils/IniParser.h>
9 #include <Utils/Ulog.h>
10 
11 
12 
14 class Config
15 {
16 public:
17 
19  static std::string PopErrorLog()
20  {
21  return GetParser().PopErrorLog();
22  }
23 
25  static const std::unordered_set<std::string> &GetUnreadableRecords()
26  {
27  return GetParser().GetUnreadableRecords();
28  }
29 
31  static bool LoadFile(const std::string &file_name)
32  {
33  return GetParser().LoadFile(file_name);
34  }
35 
37  static void Clear()
38  {
39  GetParser().Clear();
40  }
41 
43  static bool Exists(const std::string &key)
44  {
45  return GetParser().Exists(key);
46  }
47 
49  static std::string GetStr(const std::string &key, const std::string &default_val = "")
50  {
51  auto str = GetParser().GetStr(key, default_val);
52  if (!std_ext::IsASCII(str))
53  {
54  ulog::Warn("Unsupported non-international character in config file: '" + key + "'. Using default value '" + default_val + "'.", "Config::GetStr");
55  return default_val;
56  }
57 
58 #if defined(REPO_DIR) && !defined(_DEPLOY)
59  str = std_ext::Replace(str, "$REPO_DIR$", REPO_DIR);
60 #endif
61  return str;
62  }
63 
65  static bool GetBool(const std::string &key, const bool default_val = false)
66  {
67  return GetParser().GetBool(key, default_val);
68  }
69 
71  static size_t GetSizeT(const std::string &key, const size_t default_val = 0)
72  {
73  return GetParser().GetSizeT(key, default_val);
74  }
75 
77  template<typename T>
78  static T GetUint(const std::string &key, const T default_val = 0)
79  {
80  return GetParser().GetUint<T>(key, default_val);
81  }
82 
84  static int GetInt(const std::string &key, const int default_val = 0)
85  {
86  return GetParser().GetInt(key, default_val);
87  }
88 
90  static float GetFloat(const std::string &key, const float default_val = 0.0f)
91  {
92  return GetParser().GetFloat(key, default_val);
93  }
94 
96  static double GetDouble(const std::string &key, const double default_val = 0.0)
97  {
98  return GetParser().GetDouble(key, default_val);
99  }
100 
102  template <glm::length_t N, typename Type>
103  static glm::vec<N, Type, glm::highp> GetVec(const std::string &key, const glm::vec<N, Type, glm::highp> default_val = glm::vec<N, Type, glm::highp>(0))
104  {
105  return GetParser().GetVec(key, default_val);
106  }
107 
109  static std::vector<std::string> GetArray(const std::string &key, const std::vector<std::string> &default_val = {})
110  {
111  return GetParser().GetArray(key, default_val);
112  };
113 
115  static std::vector<float> GetFloatArray(const std::string &key, const std::vector<float> &default_val = {})
116  {
117  return GetParser().GetFloatArray(key, default_val);
118  };
119 
121  static std::vector<int> GetIntArray(const std::string &key, const std::vector<int> &default_val = {})
122  {
123  return GetParser().GetIntArray(key, default_val);
124  };
125 
127  static std::vector<bool> GetBoolArray(const std::string &key, const std::vector<bool> &default_val = {})
128  {
129  return GetParser().GetBoolArray(key, default_val);
130  };
131 
132 private:
133 
134  utils::IniParser parser_;
135 
136  Config() = default;
137 
138  static Config &Self()
139  {
140  static Config instance; // Guaranteed to be destroyed.
141  // Instantiated on first use.
142  return instance;
143  }
144 
145  static utils::IniParser &GetParser()
146  {
147  return Self().parser_;
148  }
149 };
150 #endif /* !UTILS_CONFIG_H */
static void Clear()
Clears config data.
Definition: Config.h:37
static bool GetBool(const std::string &key, const bool default_val=false)
Returns a bool value for the key if the key does exist.
Definition: Config.h:65
static T GetUint(const std::string &key, const T default_val=0)
Returns an unsigned integer value for the key if the key does exist.
Definition: Config.h:78
static bool Exists(const std::string &key)
If the key does exist in the singletons data, returns true.
Definition: Config.h:43
static std::string PopErrorLog()
Returns the error log as a string.
Definition: Config.h:19
static double GetDouble(const std::string &key, const double default_val=0.0)
Returns a double value for the key if the key does exist.
Definition: Config.h:96
bool IsASCII(const std::string &s)
Check std::string for non-ASCII characters.
Definition: ExtSTD.h:1011
Singleton for holding and requesting configuration file parameters.
Definition: Config.h:14
static glm::vec< N, Type, glm::highp > GetVec(const std::string &key, const glm::vec< N, Type, glm::highp > default_val=glm::vec< N, Type, glm::highp >(0))
Returns a glm::vec value for the key if the key does exist.
Definition: Config.h:103
static bool LoadFile(const std::string &file_name)
Loads config file into the config singleton.
Definition: Config.h:31
static int GetInt(const std::string &key, const int default_val=0)
Returns an int value for the key if the key does exist.
Definition: Config.h:84
static std::vector< int > GetIntArray(const std::string &key, const std::vector< int > &default_val={})
Returns a std::vector<int> value for the key if the key does exist.
Definition: Config.h:121
static size_t GetSizeT(const std::string &key, const size_t default_val=0)
Returns a size_t value for the key if the key does exist.
Definition: Config.h:71
static std::string GetStr(const std::string &key, const std::string &default_val="")
Returns a string value for the key, if the exists.
Definition: Config.h:49
static std::vector< float > GetFloatArray(const std::string &key, const std::vector< float > &default_val={})
Returns a std::vector<float> value for the key if the key does exist.
Definition: Config.h:115
std::string Replace(const std::string &text, const std::string &key_to_search, const std::string &replace_text)
Replaces all occurrences of key_to_search with replace_text in specified text.
Definition: ExtSTD.h:117
static const std::unordered_set< std::string > & GetUnreadableRecords()
Get skipped unreadable (most likely non-ASCII) records.
Definition: Config.h:25
static float GetFloat(const std::string &key, const float default_val=0.0f)
Returns an float value for the key if the key does exist.
Definition: Config.h:90
static std::vector< std::string > GetArray(const std::string &key, const std::vector< std::string > &default_val={})
Returns a std::vector<std::string> value for the key if the key does exist.
Definition: Config.h:109
static std::vector< bool > GetBoolArray(const std::string &key, const std::vector< bool > &default_val={})
Returns a std::vector<bool> value for the key if the key does exist.
Definition: Config.h:127