Skip to content

Commit

Permalink
Add configuration_json_helper::helper_values
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Jun 12, 2024
1 parent f1ca9b2 commit dd02235
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 81 deletions.
3 changes: 2 additions & 1 deletion src/core/grabber/include/grabber/device_grabber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -221,7 +222,7 @@ class device_grabber final : public pqrs::dispatcher::extra::dispatcher_client {

auto entry = std::make_shared<device_grabber_details::entry>(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) {
Expand Down
54 changes: 48 additions & 6 deletions src/share/core_configuration/configuration_json_helper.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <gsl/gsl>
#include <pqrs/json.hpp>

namespace krbn {
Expand Down Expand Up @@ -57,7 +58,7 @@ template <typename T>
class object_t final : public base_t {
public:
object_t(const std::string& key,
std::unique_ptr<T>& value)
gsl::not_null<std::shared_ptr<T>>& value)
: key_(key),
value_(value) {
}
Expand All @@ -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<T>(v->value());
value_ = std::make_shared<T>(v->value());
}
}

Expand All @@ -83,14 +84,14 @@ class object_t final : public base_t {

private:
std::string key_;
std::unique_ptr<T>& value_;
gsl::not_null<std::shared_ptr<T>>& value_;
};

template <typename T>
class array_t final : public base_t {
public:
array_t(const std::string& key,
std::vector<std::unique_ptr<T>>& value)
std::vector<gsl::not_null<std::shared_ptr<T>>>& value)
: key_(key),
value_(value) {
}
Expand All @@ -108,7 +109,7 @@ class array_t final : public base_t {

for (const auto& j : json[key_]) {
try {
value_.push_back(std::make_unique<T>(j));
value_.push_back(std::make_shared<T>(j));
} catch (const pqrs::json::unmarshal_error& e) {
throw pqrs::json::unmarshal_error(fmt::format("`{0}` entry error: {1}", key_, e.what()));
}
Expand All @@ -134,7 +135,48 @@ class array_t final : public base_t {

private:
std::string key_;
std::vector<std::unique_ptr<T>>& value_;
std::vector<gsl::not_null<std::shared_ptr<T>>>& value_;
};

class helper_values final {
public:
template <typename T>
void push_back_value(const std::string& key,
T& value,
const T& default_value) {
values_.push_back(std::make_shared<value_t<T>>(key,
value,
default_value));
}

template <typename T>
void push_back_object(const std::string& key,
gsl::not_null<std::shared_ptr<T>>& value) {
values_.push_back(std::make_shared<object_t<T>>(key,
value));
}

template <typename T>
void push_back_array(const std::string& key,
std::vector<gsl::not_null<std::shared_ptr<T>>>& value) {
values_.push_back(std::make_shared<array_t<T>>(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<gsl::not_null<std::shared_ptr<configuration_json_helper::base_t>>> values_;
};

} // namespace configuration_json_helper
Expand Down
44 changes: 20 additions & 24 deletions src/share/core_configuration/core_configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ class core_configuration final {
uid_t expected_file_owner)
: json_(nlohmann::json::object()),
loaded_(false),
global_configuration_(std::make_unique<details::global_configuration>(nlohmann::json::object())),
machine_specific_(std::make_unique<details::machine_specific>(nlohmann::json::object())) {
helper_values_.push_back(std::make_unique<configuration_json_helper::object_t<details::global_configuration>>("global",
global_configuration_));
helper_values_.push_back(std::make_unique<configuration_json_helper::object_t<details::machine_specific>>("machine_specific",
machine_specific_));
helper_values_.push_back(std::make_unique<configuration_json_helper::array_t<details::profile>>("profiles",
profiles_));
global_configuration_(std::make_shared<details::global_configuration>(nlohmann::json::object())),
machine_specific_(std::make_shared<details::machine_specific>(nlohmann::json::object())) {
helper_values_.push_back_object<details::global_configuration>("global",
global_configuration_);
helper_values_.push_back_object<details::machine_specific>("machine_specific",
machine_specific_);
helper_values_.push_back_array<details::profile>("profiles",
profiles_);

bool valid_file_owner = false;

Expand All @@ -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;

Expand All @@ -86,21 +84,19 @@ class core_configuration final {

// Fallbacks
if (profiles_.empty()) {
profiles_.push_back(std::make_unique<details::profile>(nlohmann::json({
profiles_.push_back(std::make_shared<details::profile>(nlohmann::json({
{"name", "Default profile"},
{"selected", true},
})));
}
}

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_; }
Expand All @@ -119,7 +115,7 @@ class core_configuration final {
return *machine_specific_;
}

const std::vector<std::unique_ptr<details::profile>>& get_profiles(void) const {
const std::vector<gsl::not_null<std::shared_ptr<details::profile>>>& get_profiles(void) const {
return profiles_;
}

Expand All @@ -142,13 +138,13 @@ class core_configuration final {
}

void push_back_profile(void) {
profiles_.push_back(std::make_unique<details::profile>(nlohmann::json({
profiles_.push_back(std::make_shared<details::profile>(nlohmann::json({
{"name", "New profile"},
})));
}

void duplicate_profile(const details::profile& profile) {
profiles_.push_back(std::make_unique<details::profile>(nlohmann::json(profile)));
profiles_.push_back(std::make_shared<details::profile>(nlohmann::json(profile)));

auto& p = *(profiles_.back());
p.set_name(p.get_name() + " (copy)");
Expand Down Expand Up @@ -253,10 +249,10 @@ class core_configuration final {
nlohmann::json json_;
bool loaded_;

std::unique_ptr<details::global_configuration> global_configuration_;
std::unique_ptr<details::machine_specific> machine_specific_;
std::vector<std::unique_ptr<details::profile>> profiles_;
std::vector<std::unique_ptr<configuration_json_helper::base_t>> helper_values_;
gsl::not_null<std::shared_ptr<details::global_configuration>> global_configuration_;
gsl::not_null<std::shared_ptr<details::machine_specific>> machine_specific_;
std::vector<gsl::not_null<std::shared_ptr<details::profile>>> profiles_;
configuration_json_helper::helper_values helper_values_;
};
} // namespace core_configuration
} // namespace krbn
46 changes: 21 additions & 25 deletions src/share/core_configuration/details/global_configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<configuration_json_helper::value_t<bool>>("check_for_updates_on_startup",
check_for_updates_on_startup_,
check_for_updates_on_startup_default_value));
helper_values_.push_back(std::make_unique<configuration_json_helper::value_t<bool>>("show_in_menu_bar",
show_in_menu_bar_,
show_in_menu_bar_default_value));
helper_values_.push_back(std::make_unique<configuration_json_helper::value_t<bool>>("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<configuration_json_helper::value_t<bool>>("enable_notification_window",
enable_notification_window_,
enable_notification_window_default_value));
helper_values_.push_back(std::make_unique<configuration_json_helper::value_t<bool>>("ask_for_confirmation_before_quitting",
ask_for_confirmation_before_quitting_,
ask_for_confirmation_before_quitting_default_value));
helper_values_.push_back(std::make_unique<configuration_json_helper::value_t<bool>>("unsafe_ui",
unsafe_ui_,
unsafe_ui_default_value));
helper_values_.push_back_value<bool>("check_for_updates_on_startup",
check_for_updates_on_startup_,
check_for_updates_on_startup_default_value);
helper_values_.push_back_value<bool>("show_in_menu_bar",
show_in_menu_bar_,
show_in_menu_bar_default_value);
helper_values_.push_back_value<bool>("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<bool>("enable_notification_window",
enable_notification_window_,
enable_notification_window_default_value);
helper_values_.push_back_value<bool>("ask_for_confirmation_before_quitting",
ask_for_confirmation_before_quitting_,
ask_for_confirmation_before_quitting_default_value);
helper_values_.push_back_value<bool>("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;
}
Expand Down Expand Up @@ -111,7 +107,7 @@ class global_configuration final {
bool enable_notification_window_;
bool ask_for_confirmation_before_quitting_;
bool unsafe_ui_;
std::vector<std::unique_ptr<configuration_json_helper::base_t>> helper_values_;
configuration_json_helper::helper_values helper_values_;
};

inline void to_json(nlohmann::json& json, const global_configuration& global_configuration) {
Expand Down
22 changes: 9 additions & 13 deletions src/share/core_configuration/details/machine_specific.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<configuration_json_helper::value_t<bool>>("enable_multitouch_extension",
enable_multitouch_extension_,
enable_multitouch_extension_default_value));
helper_values_.push_back_value<bool>("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;
}
Expand All @@ -49,7 +45,7 @@ class machine_specific final {
private:
nlohmann::json json_;
bool enable_multitouch_extension_;
std::vector<std::unique_ptr<configuration_json_helper::base_t>> helper_values_;
configuration_json_helper::helper_values helper_values_;
};

machine_specific(const machine_specific&) = delete;
Expand All @@ -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<entry>(value);
entries_[karabiner_machine_identifier(key)] = std::make_shared<entry>(value);
}
}
}
Expand All @@ -82,15 +78,15 @@ 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<entry>(nlohmann::json::object());
entries_[identifier] = std::make_shared<entry>(nlohmann::json::object());
}

return *(entries_[identifier]);
}

private:
nlohmann::json json_;
std::unordered_map<karabiner_machine_identifier, std::unique_ptr<entry>> entries_;
std::unordered_map<karabiner_machine_identifier, std::shared_ptr<entry>> entries_;
};

inline void to_json(nlohmann::json& json, const machine_specific& value) {
Expand Down
Loading

0 comments on commit dd02235

Please sign in to comment.