diff --git a/librecomp/src/config/ConfigOption.hpp b/librecomp/src/config/ConfigOption.hpp index 71a067b..fef446f 100644 --- a/librecomp/src/config/ConfigOption.hpp +++ b/librecomp/src/config/ConfigOption.hpp @@ -7,14 +7,15 @@ namespace recomp::config { enum class ConfigOptionType { - Label, + Label, // todo // Base types Checkbox, RadioTabs, Dropdown, Range, Color, - Trigger, + Button, // todo + TextField, // Group types CheckboxGroup, Group, @@ -28,7 +29,8 @@ NLOHMANN_JSON_SERIALIZE_ENUM(ConfigOptionType, { {ConfigOptionType::Dropdown, "Dropdown"}, {ConfigOptionType::Range, "Range"}, {ConfigOptionType::Color, "Color"}, - {ConfigOptionType::Trigger, "Trigger"}, + {ConfigOptionType::Button, "Button"}, + {ConfigOptionType::TextField, "TextField"}, {ConfigOptionType::CheckboxGroup, "CheckboxGroup"}, {ConfigOptionType::Group, "Group"} }); diff --git a/librecomp/src/config/ConfigRegistry.cpp b/librecomp/src/config/ConfigRegistry.cpp index eaddf89..0b1375a 100644 --- a/librecomp/src/config/ConfigRegistry.cpp +++ b/librecomp/src/config/ConfigRegistry.cpp @@ -73,6 +73,20 @@ void register_config_option( config_registry.key_ref_map[this_key] = { config_group, json_path }; switch (type) { + case ConfigOptionType::TextField: { + std::string default_val = ""; + if (j.find("default") != j.end()) { + default_val = get_string_in_json(j, "default"); + } + if (j.find("maxlength") != j.end()) { + if (!j["maxlength"].is_number()) { + TODO_PARSE_ERROR(this_key + "/maxlength" , "number"); + return; + } + } + set_config_store_value_and_default(this_key, default_val, default_val); + break; + } case ConfigOptionType::Checkbox: { bool default_val = false; if (j.find("default") != j.end()) { diff --git a/librecomp/src/config/ConfigStore.hpp b/librecomp/src/config/ConfigStore.hpp index 74c2952..20a2f75 100644 --- a/librecomp/src/config/ConfigStore.hpp +++ b/librecomp/src/config/ConfigStore.hpp @@ -21,9 +21,9 @@ struct string_hash { } }; -namespace recomp::config { - typedef std::variant config_store_value; - typedef std::unordered_map> config_store_map; +namespace recomp::config { + using config_store_value = std::variant; + using config_store_map = std::unordered_map>; struct ConfigStore { config_store_map map; @@ -34,6 +34,13 @@ namespace recomp::config { extern ConfigStore config_store; + // Index of variant type config_store_value + enum class ConfigStoreValueType { + Null, + String, + Int + }; + void set_config_store_value(std::string_view key, config_store_value value); void set_config_store_default_value(std::string_view key, config_store_value value); void set_config_store_value_and_default(std::string_view key, config_store_value value, config_store_value default_value); @@ -68,6 +75,22 @@ namespace recomp::config { return get_config_store_default_value(key); } }; + + // Get a value from the config store, if it doesn't exist then return the supplied value + template + T get_config_store_value_with_default(std::string_view key, T default_value) { + std::lock_guard lock{ config_store.store_mutex }; + auto it = config_store.map.find(key); + if (it != config_store.map.end()) { + if (std::holds_alternative(it->second)) { + return std::get(it->second); + } else { + throw std::runtime_error("Stored value is not of requested type"); + } + } else { + return default_value; + } + }; }; #endif