sandbox
Loading...
Searching...
No Matches
settings.hpp
1#ifndef LIBSBX_CORE_SETTINGS_HPP_
2#define LIBSBX_CORE_SETTINGS_HPP_
3
4#include <variant>
5#include <string>
6#include <unordered_map>
7
8#include <libsbx/utility/hashed_string.hpp>
9
10#include <libsbx/memory/observer_ptr.hpp>
11
12namespace sbx::core {
13
14template<typename Type>
15concept setting_type = std::is_same_v<Type, bool> || std::is_same_v<Type, std::uint32_t> || std::is_same_v<Type, std::int32_t> || std::is_same_v<Type, std::float_t> || std::is_same_v<Type, std::string>;
16
17class settings {
18
19public:
20
21 using entry_type = std::variant<std::monostate, bool, std::uint32_t, std::int32_t, std::float_t, std::string>;
22
23 struct value_type {
24 entry_type entry;
25 entry_type min;
26 entry_type max;
27 };
28
29 struct group_entry {
31 value_type& value;
32 }; // struct group_entry
33
34 struct group {
36 std::vector<group_entry> entries;
37 }; // struct group
38
39 settings() = default;
40
41 template<setting_type Type>
42 auto set(const utility::hashed_string& key, const Type& value) -> void {
43 _settings[key] = value_type{value, std::monostate{}, std::monostate{}};
44 }
45
46 template<setting_type Type>
47 auto set(const utility::hashed_string& key, const Type& value, const Type& min, const Type& max) -> void {
48 _settings[key] = value_type{value, min, max};
49 }
50
51 template<setting_type Type>
52 auto get(const utility::hashed_string& key) const -> memory::observer_ptr<const Type> {
53 if (auto entry = _settings.find(key); entry != _settings.end()) {
54 return std::get_if<Type>(&entry->second.entry);
55 }
56
57 return nullptr;
58 }
59
60 // template<setting_type Type>
61 // auto get_or_set(const utility::hashed_string& key, const Type& default_value) -> memory::observer_ptr<const Type> {
62 // if (auto entry = _settings.find(key); entry != _settings.end()) {
63 // return std::get_if<Type>(&entry->second);
64 // }
65
66 // set(key, default_value);
67
68 // return get<Type>(key);
69 // }
70
71 template<typename Callable>
72 requires (std::is_invocable_v<Callable, const utility::hashed_string&, group&>)
73 auto for_each(Callable&& callable) -> void {
74 auto grouped = std::unordered_map<utility::hashed_string, group>{};
75
76 for (auto& [key, value] : _settings) {
77 const auto position = key.rfind("::");
78
79 const auto has_namespace = (position != utility::hashed_string::npos);
80
81 const auto group_name = has_namespace ? key.substr(0, position) : key;
82 const auto entry_name = has_namespace ? key.substr(position + 2u) : key;
83
84 grouped[group_name].name = group_name;
85 grouped[group_name].entries.emplace_back(group_entry{entry_name, value});
86 }
87
88 for (auto& [group_name, group] : grouped) {
89 std::invoke(callable, group_name, group);
90 }
91 }
92
93private:
94
95 std::unordered_map<utility::hashed_string, value_type> _settings;
96
97}; // class settings
98
99} // namespace sbx::core
100
101#endif // LIBSBX_CORE_SETTINGS_HPP_
Definition: settings.hpp:17
Definition: hashed_string.hpp:15
Definition: settings.hpp:29
Definition: settings.hpp:34
Definition: settings.hpp:23