Mercurial > minori
comparison include/core/ini.h @ 120:275da698697d
config: template-ify INI
now it's... slightly less ugly :')
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Wed, 08 Nov 2023 18:13:37 -0500 |
| parents | 254b1d2b7096 |
| children | bc218c9d2ea6 |
comparison
equal
deleted
inserted
replaced
| 119:4eae379cb1ff | 120:275da698697d |
|---|---|
| 1 #ifndef __core__ini_h | 1 #ifndef __core__ini_h |
| 2 #define __core__ini_h | 2 #define __core__ini_h |
| 3 | 3 |
| 4 #define MINI_CASE_SENSITIVE | 4 #define MINI_CASE_SENSITIVE |
| 5 #include "mini/ini.h" | 5 #include "mini/ini.h" |
| 6 #include "core/strings.h" | |
| 7 #include "gui/translate/anime.h" | |
| 8 #include "gui/translate/config.h" | |
| 9 #include <type_traits> | |
| 10 #include <string> | |
| 6 | 11 |
| 7 namespace INI { | 12 namespace INI { |
| 8 | 13 |
| 9 std::string GetIniString(const mINI::INIStructure& ini, const std::string& section, | 14 /* very simple tutorial on how to give anyone who reads |
| 10 const std::string& value, const std::string& def = ""); | 15 your code an aneurysm */ |
| 16 template< class... > | |
| 17 using void_t = void; | |
| 18 | |
| 19 template <typename T, typename = void> | |
| 20 struct is_toutf8string_available : std::false_type {}; | |
| 21 | |
| 22 template<typename T> | |
| 23 struct is_toutf8string_available<T, | |
| 24 void_t<decltype(Strings::ToUtf8String(std::declval<T>()))>> : std::true_type {}; | |
| 25 | |
| 26 template <typename T, typename = void> | |
| 27 struct is_translation_available : std::false_type {}; | |
| 28 | |
| 29 template<typename T> | |
| 30 struct is_translation_available<T, | |
| 31 void_t<decltype(Translate::ToString(std::declval<T>()))>> : std::true_type {}; | |
| 32 | |
| 33 template<typename T> | |
| 34 T GetIniValue(const mINI::INIStructure& ini, const std::string& section, | |
| 35 const std::string& value, const T& def) { | |
| 36 if (!ini.has(section) || !ini.get(section).has(value)) | |
| 37 return def; | |
| 38 | |
| 39 const std::string val = ini.get(section).get(value); | |
| 40 | |
| 41 if constexpr (std::is_arithmetic<T>::value) { | |
| 42 /* Integer? */ | |
| 43 if constexpr (std::is_same<T, bool>::value) { | |
| 44 /* Boolean? */ | |
| 45 return Strings::ToBool(val); | |
| 46 } else if constexpr (std::is_unsigned<T>::value) { | |
| 47 /* Unsigned? */ | |
| 48 return Strings::ToUnsignedInt(val); | |
| 49 } else { | |
| 50 /* Always fall back to long long */ | |
| 51 return Strings::ToInt(val); | |
| 52 } | |
| 53 } else { | |
| 54 return val; | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 /* this should be able to handle most of our custom types */ | |
| 59 template<typename T> | |
| 60 void SetIniValue(mINI::INIStructure& ini, const std::string& section, | |
| 61 const std::string& key, const T& value) { | |
| 62 auto& ini_key = ini[section][key]; | |
| 63 | |
| 64 if constexpr (is_translation_available<T>::value) { | |
| 65 ini_key = Translate::ToString(value); | |
| 66 } else if constexpr (std::is_same<T, std::string>::value) { | |
| 67 ini_key = value; | |
| 68 } else if constexpr (std::is_arithmetic<T>::value && !std::is_same<T, bool>::value) { | |
| 69 ini_key = std::to_string(value); | |
| 70 } else if constexpr (is_toutf8string_available<T>::value) { | |
| 71 ini_key = Strings::ToUtf8String(value); | |
| 72 } | |
| 73 } | |
| 11 | 74 |
| 12 } | 75 } |
| 13 | 76 |
| 14 #endif | 77 #endif |
