annotate include/core/ini.h @ 187:9613d72b097e

*: multiple performance improvements like marking `static const` when it makes sense... date: change old stupid heap-based method to a structure which should make copying the thing actually make a copy. also many performance-based changes, like removing the std::tie dependency and forward-declaring nlohmann json *: replace every instance of QString::fromUtf8 to Strings::ToQString. the main difference is that our function will always convert exactly what is in the string, while some other times it would only convert up to the nearest NUL byte
author Paper <mrpapersonic@gmail.com>
date Wed, 06 Dec 2023 13:43:54 -0500
parents 7d3ad9529c4c
children 53211cb1e7f5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
1 #ifndef __core__ini_h
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
2 #define __core__ini_h
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
3
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4 #define MINI_CASE_SENSITIVE
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
5 #include "mini/ini.h"
120
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
6 #include "core/strings.h"
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
7 #include "gui/translate/anime.h"
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
8 #include "gui/translate/config.h"
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
9 #include <type_traits>
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
10 #include <string>
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
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
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
14 /* very simple tutorial on how to give anyone who reads
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
15 your code an aneurysm */
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
16 template< class... >
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
17 using void_t = void;
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
18
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
19 template <typename T, typename = void>
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
20 struct is_toutf8string_available : std::false_type {};
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
21
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
22 template<typename T>
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
23 struct is_toutf8string_available<T,
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
24 void_t<decltype(Strings::ToUtf8String(std::declval<T>()))>> : std::true_type {};
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
25
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
26 template <typename T, typename = void>
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
27 struct is_translation_available : std::false_type {};
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
28
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
29 template<typename T>
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
30 struct is_translation_available<T,
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
31 void_t<decltype(Translate::ToString(std::declval<T>()))>> : std::true_type {};
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
32
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
33 template<typename T>
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
34 T GetIniValue(const mINI::INIStructure& ini, const std::string& section,
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
35 const std::string& value, const T& def) {
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
36 if (!ini.has(section) || !ini.get(section).has(value))
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
37 return def;
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
38
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
39 const std::string val = ini.get(section).get(value);
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
40
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
41 if constexpr (std::is_arithmetic<T>::value) {
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
42 /* Integer? */
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
43 if constexpr (std::is_same<T, bool>::value) {
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
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
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
46 } else {
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
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
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
49 }
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
50 } else {
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
51 return val;
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
52 }
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
53 }
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
54
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
55 /* this should be able to handle most of our custom types */
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
56 template<typename T>
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
57 void SetIniValue(mINI::INIStructure& ini, const std::string& section,
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
58 const std::string& key, const T& value) {
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
59 auto& ini_key = ini[section][key];
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
60
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
61 if constexpr (is_translation_available<T>::value) {
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
62 ini_key = Translate::ToString(value);
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
63 } else if constexpr (std::is_same<T, std::string>::value) {
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
64 ini_key = value;
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
65 } else if constexpr (std::is_arithmetic<T>::value && !std::is_same<T, bool>::value) {
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
66 ini_key = std::to_string(value);
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
67 } else if constexpr (is_toutf8string_available<T>::value) {
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
68 ini_key = Strings::ToUtf8String(value);
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
69 }
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
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
275da698697d config: template-ify INI
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
74 #endif