comparison include/core/ini.h @ 221:53211cb1e7f5

library: add initial library stuff nice
author Paper <paper@paper.us.eu.org>
date Mon, 08 Jan 2024 13:21:08 -0500
parents 7d3ad9529c4c
children 862d0d8619f6
comparison
equal deleted inserted replaced
220:79a87a6dd39d 221:53211cb1e7f5
11 11
12 namespace INI { 12 namespace INI {
13 13
14 /* very simple tutorial on how to give anyone who reads 14 /* very simple tutorial on how to give anyone who reads
15 your code an aneurysm */ 15 your code an aneurysm */
16 template< class... >
17 using void_t = void;
18
19 template <typename T, typename = void> 16 template <typename T, typename = void>
20 struct is_toutf8string_available : std::false_type {}; 17 struct is_toutf8string_available : std::false_type {};
21 18
22 template<typename T> 19 template<typename T>
23 struct is_toutf8string_available<T, 20 struct is_toutf8string_available<T,
24 void_t<decltype(Strings::ToUtf8String(std::declval<T>()))>> : std::true_type {}; 21 std::void_t<decltype(Strings::ToUtf8String(std::declval<T>()))>> : std::true_type {};
25 22
26 template <typename T, typename = void> 23 template <typename T, typename = void>
27 struct is_translation_available : std::false_type {}; 24 struct is_translation_available : std::false_type {};
28 25
29 template<typename T> 26 template<typename T>
30 struct is_translation_available<T, 27 struct is_translation_available<T,
31 void_t<decltype(Translate::ToString(std::declval<T>()))>> : std::true_type {}; 28 std::void_t<decltype(Translate::ToString(std::declval<T>()))>> : std::true_type {};
32 29
33 template<typename T> 30 template<typename T>
34 T GetIniValue(const mINI::INIStructure& ini, const std::string& section, 31 T GetIniValue(const mINI::INIStructure& ini, const std::string& section,
35 const std::string& value, const T& def) { 32 const std::string& value, const T& def) {
36 if (!ini.has(section) || !ini.get(section).has(value)) 33 if (!ini.has(section) || !ini.get(section).has(value))
37 return def; 34 return def;
38 35
39 const std::string val = ini.get(section).get(value); 36 const std::string val = ini.get(section).get(value);
40 37
41 if constexpr (std::is_arithmetic<T>::value) { 38 if constexpr (std::is_integral<T>::value) {
42 /* Integer? */ 39 /* Integer? */
43 if constexpr (std::is_same<T, bool>::value) { 40 if constexpr (std::is_same<T, bool>::value) {
44 /* Boolean? */ 41 /* Boolean? */
45 return Strings::ToBool(val, def); 42 return Strings::ToBool(val, def);
46 } else { 43 } else {
57 void SetIniValue(mINI::INIStructure& ini, const std::string& section, 54 void SetIniValue(mINI::INIStructure& ini, const std::string& section,
58 const std::string& key, const T& value) { 55 const std::string& key, const T& value) {
59 auto& ini_key = ini[section][key]; 56 auto& ini_key = ini[section][key];
60 57
61 if constexpr (is_translation_available<T>::value) { 58 if constexpr (is_translation_available<T>::value) {
59 /* prioritize translation */
62 ini_key = Translate::ToString(value); 60 ini_key = Translate::ToString(value);
63 } else if constexpr (std::is_same<T, std::string>::value) { 61 } else if constexpr (std::is_same<T, std::string>::value) {
62 /* lmfao */
64 ini_key = value; 63 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) { 64 } else if constexpr (is_toutf8string_available<T>::value) {
68 ini_key = Strings::ToUtf8String(value); 65 ini_key = Strings::ToUtf8String(value);
69 } 66 }
70 } 67 }
71 68