Mercurial > minori
annotate include/core/ini.h @ 211:7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Sun, 07 Jan 2024 09:54:17 -0500 |
| parents | 7d3ad9529c4c |
| children | 53211cb1e7f5 |
| rev | line source |
|---|---|
| 102 | 1 #ifndef __core__ini_h |
| 2 #define __core__ini_h | |
| 3 | |
| 4 #define MINI_CASE_SENSITIVE | |
| 5 #include "mini/ini.h" | |
| 120 | 6 #include "core/strings.h" |
| 7 #include "gui/translate/anime.h" | |
| 8 #include "gui/translate/config.h" | |
| 9 #include <type_traits> | |
| 10 #include <string> | |
| 102 | 11 |
|
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
12 namespace INI { |
|
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
13 |
| 120 | 14 /* very simple tutorial on how to give anyone who reads |
| 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? */ | |
|
136
7d3ad9529c4c
ini: fix bool getters to provide default vals for bools and ints
Paper <mrpapersonic@gmail.com>
parents:
122
diff
changeset
|
45 return Strings::ToBool(val, def); |
| 120 | 46 } else { |
| 47 /* Always fall back to long long */ | |
|
136
7d3ad9529c4c
ini: fix bool getters to provide default vals for bools and ints
Paper <mrpapersonic@gmail.com>
parents:
122
diff
changeset
|
48 return Strings::ToInt<T>(val, def); |
| 120 | 49 } |
| 50 } else { | |
| 51 return val; | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 /* this should be able to handle most of our custom types */ | |
| 56 template<typename T> | |
| 57 void SetIniValue(mINI::INIStructure& ini, const std::string& section, | |
| 58 const std::string& key, const T& value) { | |
| 59 auto& ini_key = ini[section][key]; | |
| 60 | |
| 61 if constexpr (is_translation_available<T>::value) { | |
| 62 ini_key = Translate::ToString(value); | |
| 63 } else if constexpr (std::is_same<T, std::string>::value) { | |
| 64 ini_key = value; | |
| 65 } else if constexpr (std::is_arithmetic<T>::value && !std::is_same<T, bool>::value) { | |
| 66 ini_key = std::to_string(value); | |
| 67 } else if constexpr (is_toutf8string_available<T>::value) { | |
| 68 ini_key = Strings::ToUtf8String(value); | |
| 69 } | |
| 70 } | |
|
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
71 |
|
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
72 } |
|
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
73 |
| 120 | 74 #endif |
