1#ifndef LIBSBX_CORE_SETTINGS_HPP_
2#define LIBSBX_CORE_SETTINGS_HPP_
6#include <unordered_map>
8#include <libsbx/utility/hashed_string.hpp>
10#include <libsbx/memory/observer_ptr.hpp>
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>;
21 using entry_type = std::variant<std::monostate, bool, std::uint32_t, std::int32_t, std::float_t, std::string>;
36 std::vector<group_entry> entries;
41 template<setting_type Type>
43 _settings[key] =
value_type{value, std::monostate{}, std::monostate{}};
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};
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);
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>{};
76 for (
auto& [key, value] : _settings) {
77 const auto position = key.rfind(
"::");
79 const auto has_namespace = (position != utility::hashed_string::npos);
81 const auto group_name = has_namespace ? key.substr(0, position) : key;
82 const auto entry_name = has_namespace ? key.substr(position + 2u) : key;
84 grouped[group_name].name = group_name;
85 grouped[group_name].entries.emplace_back(group_entry{entry_name, value});
88 for (
auto& [group_name, group] : grouped) {
89 std::invoke(callable, group_name, group);
95 std::unordered_map<utility::hashed_string, value_type> _settings;
Definition: settings.hpp:17
Definition: hashed_string.hpp:15
Definition: settings.hpp:29
Definition: settings.hpp:34
Definition: settings.hpp:23