settings_interface.h (10683B)
1 // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com> 2 // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) 3 4 #pragma once 5 6 #include "small_string.h" 7 #include "types.h" 8 9 #include <optional> 10 #include <string> 11 #include <vector> 12 13 class Error; 14 15 class SettingsInterface 16 { 17 public: 18 virtual ~SettingsInterface() = default; 19 20 virtual bool Save(Error* error = nullptr) = 0; 21 virtual void Clear() = 0; 22 virtual bool IsEmpty() = 0; 23 24 virtual bool GetIntValue(const char* section, const char* key, s32* value) const = 0; 25 virtual bool GetUIntValue(const char* section, const char* key, u32* value) const = 0; 26 virtual bool GetFloatValue(const char* section, const char* key, float* value) const = 0; 27 virtual bool GetDoubleValue(const char* section, const char* key, double* value) const = 0; 28 virtual bool GetBoolValue(const char* section, const char* key, bool* value) const = 0; 29 virtual bool GetStringValue(const char* section, const char* key, std::string* value) const = 0; 30 virtual bool GetStringValue(const char* section, const char* key, SmallStringBase* value) const = 0; 31 32 virtual void SetIntValue(const char* section, const char* key, s32 value) = 0; 33 virtual void SetUIntValue(const char* section, const char* key, u32 value) = 0; 34 virtual void SetFloatValue(const char* section, const char* key, float value) = 0; 35 virtual void SetDoubleValue(const char* section, const char* key, double value) = 0; 36 virtual void SetBoolValue(const char* section, const char* key, bool value) = 0; 37 virtual void SetStringValue(const char* section, const char* key, const char* value) = 0; 38 39 virtual std::vector<std::string> GetStringList(const char* section, const char* key) const = 0; 40 virtual void SetStringList(const char* section, const char* key, const std::vector<std::string>& items) = 0; 41 virtual bool RemoveFromStringList(const char* section, const char* key, const char* item) = 0; 42 virtual bool AddToStringList(const char* section, const char* key, const char* item) = 0; 43 44 virtual std::vector<std::pair<std::string, std::string>> GetKeyValueList(const char* section) const = 0; 45 virtual void SetKeyValueList(const char* section, const std::vector<std::pair<std::string, std::string>>& items) = 0; 46 47 virtual bool ContainsValue(const char* section, const char* key) const = 0; 48 virtual void DeleteValue(const char* section, const char* key) = 0; 49 virtual void ClearSection(const char* section) = 0; 50 virtual void RemoveSection(const char* section) = 0; 51 virtual void RemoveEmptySections() = 0; 52 53 ALWAYS_INLINE s32 GetIntValue(const char* section, const char* key, s32 default_value = 0) const 54 { 55 s32 value; 56 return GetIntValue(section, key, &value) ? value : default_value; 57 } 58 59 ALWAYS_INLINE u32 GetUIntValue(const char* section, const char* key, u32 default_value = 0) const 60 { 61 u32 value; 62 return GetUIntValue(section, key, &value) ? value : default_value; 63 } 64 65 ALWAYS_INLINE float GetFloatValue(const char* section, const char* key, float default_value = 0.0f) const 66 { 67 float value; 68 return GetFloatValue(section, key, &value) ? value : default_value; 69 } 70 71 ALWAYS_INLINE double GetDoubleValue(const char* section, const char* key, double default_value = 0.0) const 72 { 73 double value; 74 return GetDoubleValue(section, key, &value) ? value : default_value; 75 } 76 77 ALWAYS_INLINE bool GetBoolValue(const char* section, const char* key, bool default_value = false) const 78 { 79 bool value; 80 return GetBoolValue(section, key, &value) ? value : default_value; 81 } 82 83 ALWAYS_INLINE std::string GetStringValue(const char* section, const char* key, const char* default_value = "") const 84 { 85 std::string value; 86 if (!GetStringValue(section, key, &value)) 87 value.assign(default_value); 88 return value; 89 } 90 91 ALWAYS_INLINE SmallString GetSmallStringValue(const char* section, const char* key, 92 const char* default_value = "") const 93 { 94 SmallString value; 95 if (!GetStringValue(section, key, &value)) 96 value.assign(default_value); 97 return value; 98 } 99 100 ALWAYS_INLINE TinyString GetTinyStringValue(const char* section, const char* key, 101 const char* default_value = "") const 102 { 103 TinyString value; 104 if (!GetStringValue(section, key, &value)) 105 value.assign(default_value); 106 return value; 107 } 108 109 ALWAYS_INLINE std::optional<s32> GetOptionalIntValue(const char* section, const char* key, 110 std::optional<s32> default_value = std::nullopt) 111 { 112 s32 ret; 113 return GetIntValue(section, key, &ret) ? std::optional<s32>(ret) : default_value; 114 } 115 116 ALWAYS_INLINE std::optional<u32> GetOptionalUIntValue(const char* section, const char* key, 117 std::optional<u32> default_value = std::nullopt) 118 { 119 u32 ret; 120 return GetUIntValue(section, key, &ret) ? std::optional<u32>(ret) : default_value; 121 } 122 123 ALWAYS_INLINE std::optional<float> GetOptionalFloatValue(const char* section, const char* key, 124 std::optional<float> default_value = std::nullopt) 125 { 126 float ret; 127 return GetFloatValue(section, key, &ret) ? std::optional<float>(ret) : default_value; 128 } 129 130 ALWAYS_INLINE std::optional<double> GetOptionalDoubleValue(const char* section, const char* key, 131 std::optional<double> default_value = std::nullopt) 132 { 133 double ret; 134 return GetDoubleValue(section, key, &ret) ? std::optional<double>(ret) : default_value; 135 } 136 137 ALWAYS_INLINE std::optional<bool> GetOptionalBoolValue(const char* section, const char* key, 138 std::optional<bool> default_value = std::nullopt) 139 { 140 bool ret; 141 return GetBoolValue(section, key, &ret) ? std::optional<bool>(ret) : default_value; 142 } 143 144 ALWAYS_INLINE std::optional<std::string> 145 GetOptionalStringValue(const char* section, const char* key, 146 std::optional<const char*> default_value = std::nullopt) const 147 { 148 std::string ret; 149 return GetStringValue(section, key, &ret) ? 150 std::optional<std::string>(ret) : 151 (default_value.has_value() ? std::optional<std::string>(default_value.value()) : 152 std::optional<std::string>()); 153 } 154 155 ALWAYS_INLINE std::optional<SmallString> 156 GetOptionalSmallStringValue(const char* section, const char* key, 157 std::optional<const char*> default_value = std::nullopt) const 158 { 159 SmallString ret; 160 return GetStringValue(section, key, &ret) ? 161 std::optional<SmallString>(ret) : 162 (default_value.has_value() ? std::optional<SmallString>(default_value.value()) : 163 std::optional<SmallString>()); 164 } 165 166 ALWAYS_INLINE std::optional<TinyString> 167 GetOptionalTinyStringValue(const char* section, const char* key, 168 std::optional<const char*> default_value = std::nullopt) const 169 { 170 TinyString ret; 171 return GetStringValue(section, key, &ret) ? 172 std::optional<TinyString>(ret) : 173 (default_value.has_value() ? std::optional<TinyString>(default_value.value()) : 174 std::optional<TinyString>()); 175 } 176 177 ALWAYS_INLINE void SetOptionalIntValue(const char* section, const char* key, const std::optional<s32>& value) 178 { 179 value.has_value() ? SetIntValue(section, key, value.value()) : DeleteValue(section, key); 180 } 181 182 ALWAYS_INLINE void SetOptionalUIntValue(const char* section, const char* key, const std::optional<u32>& value) 183 { 184 value.has_value() ? SetUIntValue(section, key, value.value()) : DeleteValue(section, key); 185 } 186 187 ALWAYS_INLINE void SetOptionalFloatValue(const char* section, const char* key, const std::optional<float>& value) 188 { 189 value.has_value() ? SetFloatValue(section, key, value.value()) : DeleteValue(section, key); 190 } 191 192 ALWAYS_INLINE void SetOptionalDoubleValue(const char* section, const char* key, const std::optional<double>& value) 193 { 194 value.has_value() ? SetDoubleValue(section, key, value.value()) : DeleteValue(section, key); 195 } 196 197 ALWAYS_INLINE void SetOptionalBoolValue(const char* section, const char* key, const std::optional<bool>& value) 198 { 199 value.has_value() ? SetBoolValue(section, key, value.value()) : DeleteValue(section, key); 200 } 201 202 ALWAYS_INLINE void SetOptionalStringValue(const char* section, const char* key, 203 const std::optional<const char*>& value) 204 { 205 value.has_value() ? SetStringValue(section, key, value.value()) : DeleteValue(section, key); 206 } 207 208 ALWAYS_INLINE void CopyBoolValue(const SettingsInterface& si, const char* section, const char* key) 209 { 210 bool value; 211 if (si.GetBoolValue(section, key, &value)) 212 SetBoolValue(section, key, value); 213 else 214 DeleteValue(section, key); 215 } 216 217 ALWAYS_INLINE void CopyIntValue(const SettingsInterface& si, const char* section, const char* key) 218 { 219 s32 value; 220 if (si.GetIntValue(section, key, &value)) 221 SetIntValue(section, key, value); 222 else 223 DeleteValue(section, key); 224 } 225 226 ALWAYS_INLINE void CopyUIntValue(const SettingsInterface& si, const char* section, const char* key) 227 { 228 u32 value; 229 if (si.GetUIntValue(section, key, &value)) 230 SetUIntValue(section, key, value); 231 else 232 DeleteValue(section, key); 233 } 234 235 ALWAYS_INLINE void CopyFloatValue(const SettingsInterface& si, const char* section, const char* key) 236 { 237 float value; 238 if (si.GetFloatValue(section, key, &value)) 239 SetFloatValue(section, key, value); 240 else 241 DeleteValue(section, key); 242 } 243 244 ALWAYS_INLINE void CopyDoubleValue(const SettingsInterface& si, const char* section, const char* key) 245 { 246 double value; 247 if (si.GetDoubleValue(section, key, &value)) 248 SetDoubleValue(section, key, value); 249 else 250 DeleteValue(section, key); 251 } 252 253 ALWAYS_INLINE void CopyStringValue(const SettingsInterface& si, const char* section, const char* key) 254 { 255 std::string value; 256 if (si.GetStringValue(section, key, &value)) 257 SetStringValue(section, key, value.c_str()); 258 else 259 DeleteValue(section, key); 260 } 261 262 ALWAYS_INLINE void CopyStringListValue(const SettingsInterface& si, const char* section, const char* key) 263 { 264 std::vector<std::string> value(si.GetStringList(section, key)); 265 if (!value.empty()) 266 SetStringList(section, key, value); 267 else 268 DeleteValue(section, key); 269 } 270 };