From dd02235752e7c3afb9b0aca78b0933ec9139ae6f Mon Sep 17 00:00:00 2001 From: Takayama Fumihiko Date: Wed, 12 Jun 2024 21:01:34 +0900 Subject: [PATCH] Add configuration_json_helper::helper_values --- .../include/grabber/device_grabber.hpp | 3 +- .../configuration_json_helper.hpp | 54 ++++++++++++++++--- .../core_configuration/core_configuration.hpp | 44 +++++++-------- .../details/global_configuration.hpp | 46 ++++++++-------- .../details/machine_specific.hpp | 22 ++++---- .../core_configuration/details/profile.hpp | 20 +++---- src/share/memory_utility.hpp | 15 ++++++ 7 files changed, 123 insertions(+), 81 deletions(-) create mode 100644 src/share/memory_utility.hpp diff --git a/src/core/grabber/include/grabber/device_grabber.hpp b/src/core/grabber/include/grabber/device_grabber.hpp index 9acc66bc5..95de3fda7 100644 --- a/src/core/grabber/include/grabber/device_grabber.hpp +++ b/src/core/grabber/include/grabber/device_grabber.hpp @@ -17,6 +17,7 @@ #include "logger.hpp" #include "manipulator/manipulator_managers_connector.hpp" #include "manipulator/manipulators/post_event_to_virtual_devices/post_event_to_virtual_devices.hpp" +#include "memory_utility.hpp" #include "monitor/configuration_monitor.hpp" #include "monitor/event_tap_monitor.hpp" #include "notification_message_manager.hpp" @@ -221,7 +222,7 @@ class device_grabber final : public pqrs::dispatcher::extra::dispatcher_client { auto entry = std::make_shared(device_id, *device_ptr, - core_configuration_.get()); + memory_utility::make_weak(core_configuration_)); entries_[device_id] = entry; entry->hid_queue_values_arrived.connect([this](auto&& entry, auto&& event_queue) { diff --git a/src/share/core_configuration/configuration_json_helper.hpp b/src/share/core_configuration/configuration_json_helper.hpp index e58f64c5a..35ddb9116 100644 --- a/src/share/core_configuration/configuration_json_helper.hpp +++ b/src/share/core_configuration/configuration_json_helper.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include namespace krbn { @@ -57,7 +58,7 @@ template class object_t final : public base_t { public: object_t(const std::string& key, - std::unique_ptr& value) + gsl::not_null>& value) : key_(key), value_(value) { } @@ -68,7 +69,7 @@ class object_t final : public base_t { void update_value(const nlohmann::json& json) override { if (auto v = pqrs::json::find_object(json, key_)) { - value_ = std::make_unique(v->value()); + value_ = std::make_shared(v->value()); } } @@ -83,14 +84,14 @@ class object_t final : public base_t { private: std::string key_; - std::unique_ptr& value_; + gsl::not_null>& value_; }; template class array_t final : public base_t { public: array_t(const std::string& key, - std::vector>& value) + std::vector>>& value) : key_(key), value_(value) { } @@ -108,7 +109,7 @@ class array_t final : public base_t { for (const auto& j : json[key_]) { try { - value_.push_back(std::make_unique(j)); + value_.push_back(std::make_shared(j)); } catch (const pqrs::json::unmarshal_error& e) { throw pqrs::json::unmarshal_error(fmt::format("`{0}` entry error: {1}", key_, e.what())); } @@ -134,7 +135,48 @@ class array_t final : public base_t { private: std::string key_; - std::vector>& value_; + std::vector>>& value_; +}; + +class helper_values final { +public: + template + void push_back_value(const std::string& key, + T& value, + const T& default_value) { + values_.push_back(std::make_shared>(key, + value, + default_value)); + } + + template + void push_back_object(const std::string& key, + gsl::not_null>& value) { + values_.push_back(std::make_shared>(key, + value)); + } + + template + void push_back_array(const std::string& key, + std::vector>>& value) { + values_.push_back(std::make_shared>(key, + value)); + } + + void update_value(const nlohmann::json& json) { + for (const auto& v : values_) { + v->update_value(json); + } + } + + void update_json(nlohmann::json& json) const { + for (const auto& v : values_) { + v->update_json(json); + } + } + +private: + std::vector>> values_; }; } // namespace configuration_json_helper diff --git a/src/share/core_configuration/core_configuration.hpp b/src/share/core_configuration/core_configuration.hpp index 0df813ebb..1a2c3ce30 100644 --- a/src/share/core_configuration/core_configuration.hpp +++ b/src/share/core_configuration/core_configuration.hpp @@ -39,14 +39,14 @@ class core_configuration final { uid_t expected_file_owner) : json_(nlohmann::json::object()), loaded_(false), - global_configuration_(std::make_unique(nlohmann::json::object())), - machine_specific_(std::make_unique(nlohmann::json::object())) { - helper_values_.push_back(std::make_unique>("global", - global_configuration_)); - helper_values_.push_back(std::make_unique>("machine_specific", - machine_specific_)); - helper_values_.push_back(std::make_unique>("profiles", - profiles_)); + global_configuration_(std::make_shared(nlohmann::json::object())), + machine_specific_(std::make_shared(nlohmann::json::object())) { + helper_values_.push_back_object("global", + global_configuration_); + helper_values_.push_back_object("machine_specific", + machine_specific_); + helper_values_.push_back_array("profiles", + profiles_); bool valid_file_owner = false; @@ -69,9 +69,7 @@ class core_configuration final { try { json_ = json_utility::parse_jsonc(input); - for (const auto& v : helper_values_) { - v->update_value(json_); - } + helper_values_.update_value(json_); loaded_ = true; @@ -86,7 +84,7 @@ class core_configuration final { // Fallbacks if (profiles_.empty()) { - profiles_.push_back(std::make_unique(nlohmann::json({ + profiles_.push_back(std::make_shared(nlohmann::json({ {"name", "Default profile"}, {"selected", true}, }))); @@ -94,13 +92,11 @@ class core_configuration final { } nlohmann::json to_json(void) const { - auto json = json_; + auto j = json_; - for (const auto& v : helper_values_) { - v->update_json(json); - } + helper_values_.update_json(j); - return json; + return j; } bool is_loaded(void) const { return loaded_; } @@ -119,7 +115,7 @@ class core_configuration final { return *machine_specific_; } - const std::vector>& get_profiles(void) const { + const std::vector>>& get_profiles(void) const { return profiles_; } @@ -142,13 +138,13 @@ class core_configuration final { } void push_back_profile(void) { - profiles_.push_back(std::make_unique(nlohmann::json({ + profiles_.push_back(std::make_shared(nlohmann::json({ {"name", "New profile"}, }))); } void duplicate_profile(const details::profile& profile) { - profiles_.push_back(std::make_unique(nlohmann::json(profile))); + profiles_.push_back(std::make_shared(nlohmann::json(profile))); auto& p = *(profiles_.back()); p.set_name(p.get_name() + " (copy)"); @@ -253,10 +249,10 @@ class core_configuration final { nlohmann::json json_; bool loaded_; - std::unique_ptr global_configuration_; - std::unique_ptr machine_specific_; - std::vector> profiles_; - std::vector> helper_values_; + gsl::not_null> global_configuration_; + gsl::not_null> machine_specific_; + std::vector>> profiles_; + configuration_json_helper::helper_values helper_values_; }; } // namespace core_configuration } // namespace krbn diff --git a/src/share/core_configuration/details/global_configuration.hpp b/src/share/core_configuration/details/global_configuration.hpp index 6ae6f1d8c..3f91e47b3 100644 --- a/src/share/core_configuration/details/global_configuration.hpp +++ b/src/share/core_configuration/details/global_configuration.hpp @@ -25,38 +25,34 @@ class global_configuration final { enable_notification_window_(enable_notification_window_default_value), ask_for_confirmation_before_quitting_(ask_for_confirmation_before_quitting_default_value), unsafe_ui_(unsafe_ui_default_value) { - helper_values_.push_back(std::make_unique>("check_for_updates_on_startup", - check_for_updates_on_startup_, - check_for_updates_on_startup_default_value)); - helper_values_.push_back(std::make_unique>("show_in_menu_bar", - show_in_menu_bar_, - show_in_menu_bar_default_value)); - helper_values_.push_back(std::make_unique>("show_profile_name_in_menu_bar", - show_profile_name_in_menu_bar_, - show_profile_name_in_menu_bar_default_value)); - helper_values_.push_back(std::make_unique>("enable_notification_window", - enable_notification_window_, - enable_notification_window_default_value)); - helper_values_.push_back(std::make_unique>("ask_for_confirmation_before_quitting", - ask_for_confirmation_before_quitting_, - ask_for_confirmation_before_quitting_default_value)); - helper_values_.push_back(std::make_unique>("unsafe_ui", - unsafe_ui_, - unsafe_ui_default_value)); + helper_values_.push_back_value("check_for_updates_on_startup", + check_for_updates_on_startup_, + check_for_updates_on_startup_default_value); + helper_values_.push_back_value("show_in_menu_bar", + show_in_menu_bar_, + show_in_menu_bar_default_value); + helper_values_.push_back_value("show_profile_name_in_menu_bar", + show_profile_name_in_menu_bar_, + show_profile_name_in_menu_bar_default_value); + helper_values_.push_back_value("enable_notification_window", + enable_notification_window_, + enable_notification_window_default_value); + helper_values_.push_back_value("ask_for_confirmation_before_quitting", + ask_for_confirmation_before_quitting_, + ask_for_confirmation_before_quitting_default_value); + helper_values_.push_back_value("unsafe_ui", + unsafe_ui_, + unsafe_ui_default_value); pqrs::json::requires_object(json, "json"); - for (const auto& v : helper_values_) { - v->update_value(json); - } + helper_values_.update_value(json); } nlohmann::json to_json(void) const { auto j = json_; - for (const auto& v : helper_values_) { - v->update_json(j); - } + helper_values_.update_json(j); return j; } @@ -111,7 +107,7 @@ class global_configuration final { bool enable_notification_window_; bool ask_for_confirmation_before_quitting_; bool unsafe_ui_; - std::vector> helper_values_; + configuration_json_helper::helper_values helper_values_; }; inline void to_json(nlohmann::json& json, const global_configuration& global_configuration) { diff --git a/src/share/core_configuration/details/machine_specific.hpp b/src/share/core_configuration/details/machine_specific.hpp index 78ddcb675..ad19581dd 100644 --- a/src/share/core_configuration/details/machine_specific.hpp +++ b/src/share/core_configuration/details/machine_specific.hpp @@ -18,23 +18,19 @@ class machine_specific final { entry(const nlohmann::json& json) : json_(json), enable_multitouch_extension_(enable_multitouch_extension_default_value) { - helper_values_.push_back(std::make_unique>("enable_multitouch_extension", - enable_multitouch_extension_, - enable_multitouch_extension_default_value)); + helper_values_.push_back_value("enable_multitouch_extension", + enable_multitouch_extension_, + enable_multitouch_extension_default_value); pqrs::json::requires_object(json, "json"); - for (const auto& v : helper_values_) { - v->update_value(json); - } + helper_values_.update_value(json); } nlohmann::json to_json(void) const { auto j = json_; - for (const auto& v : helper_values_) { - v->update_json(j); - } + helper_values_.update_json(j); return j; } @@ -49,7 +45,7 @@ class machine_specific final { private: nlohmann::json json_; bool enable_multitouch_extension_; - std::vector> helper_values_; + configuration_json_helper::helper_values helper_values_; }; machine_specific(const machine_specific&) = delete; @@ -60,7 +56,7 @@ class machine_specific final { for (const auto& [key, value] : json.items()) { if (value.is_object()) { - entries_[karabiner_machine_identifier(key)] = std::make_unique(value); + entries_[karabiner_machine_identifier(key)] = std::make_shared(value); } } } @@ -82,7 +78,7 @@ class machine_specific final { entry& get_entry(const karabiner_machine_identifier& identifier = constants::get_karabiner_machine_identifier()) { if (!entries_.contains(identifier)) { - entries_[identifier] = std::make_unique(nlohmann::json::object()); + entries_[identifier] = std::make_shared(nlohmann::json::object()); } return *(entries_[identifier]); @@ -90,7 +86,7 @@ class machine_specific final { private: nlohmann::json json_; - std::unordered_map> entries_; + std::unordered_map> entries_; }; inline void to_json(nlohmann::json& json, const machine_specific& value) { diff --git a/src/share/core_configuration/details/profile.hpp b/src/share/core_configuration/details/profile.hpp index 61d752183..133269a18 100644 --- a/src/share/core_configuration/details/profile.hpp +++ b/src/share/core_configuration/details/profile.hpp @@ -18,8 +18,8 @@ class profile final { profile(const nlohmann::json& json) : json_(json), selected_(false) { - helper_values_.push_back(std::make_unique>("devices", - devices_)); + helper_values_.push_back_array("devices", + devices_); // ---------------------------------------- // Set default value @@ -31,9 +31,7 @@ class profile final { pqrs::json::requires_object(json, "json"); - for (const auto& v : helper_values_) { - v->update_value(json); - } + helper_values_.update_value(json); for (const auto& [key, value] : json.items()) { if (key == "name") { @@ -141,9 +139,7 @@ class profile final { nlohmann::json to_json(void) const { auto j = json_; - for (const auto& v : helper_values_) { - v->update_json(j); - } + helper_values_.update_json(j); j["name"] = name_; j["selected"] = selected_; @@ -258,7 +254,7 @@ class profile final { return virtual_hid_keyboard_; } - const std::vector>& get_devices(void) const { + const std::vector>>& get_devices(void) const { return devices_; } @@ -977,7 +973,7 @@ class profile final { } } - devices_.push_back(std::make_unique(nlohmann::json({ + devices_.push_back(std::make_shared(nlohmann::json({ {"identifiers", identifiers}, }))); } @@ -990,8 +986,8 @@ class profile final { details::simple_modifications fn_function_keys_; details::complex_modifications complex_modifications_; details::virtual_hid_keyboard virtual_hid_keyboard_; - std::vector> devices_; - std::vector> helper_values_; + std::vector>> devices_; + configuration_json_helper::helper_values helper_values_; }; inline void to_json(nlohmann::json& json, const profile& profile) { diff --git a/src/share/memory_utility.hpp b/src/share/memory_utility.hpp new file mode 100644 index 000000000..8a5f64c80 --- /dev/null +++ b/src/share/memory_utility.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include +#include + +namespace krbn { +namespace memory_utility { + +template +inline std::weak_ptr make_weak(gsl::not_null> p) { + return std::weak_ptr(p.get()); +} + +} // namespace memory_utility +} // namespace krbn