From 578b0bf3c81ef8f94e5e9b4f427720846bc7a5c5 Mon Sep 17 00:00:00 2001 From: Ilia Bozhinov Date: Mon, 14 Nov 2022 15:22:58 +0100 Subject: [PATCH] compound-option: allow setting those options without giving explicit tuple names --- include/wayfire/config/compound-option.hpp | 50 ++++++++++++++++++++++ test/option_test.cpp | 37 ++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/include/wayfire/config/compound-option.hpp b/include/wayfire/config/compound-option.hpp index c500c55..3b411a7 100644 --- a/include/wayfire/config/compound-option.hpp +++ b/include/wayfire/config/compound-option.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -15,6 +16,25 @@ template using compound_list_t = std::vector>; +template +using simple_list_t = + std::vector>; + +template +std::tuple pop_first(const std::tuple& tuple) +{ + return std::apply([] (auto&&, const auto&... args) + { + return std::tie(args...); + }, tuple); +} + +template +std::tuple push_first(const T1& t, const std::tuple& tuple) +{ + return std::tuple_cat(std::tie(t), tuple); +} + /** * A base class containing information about an entry in a tuple. */ @@ -132,6 +152,19 @@ class compound_option_t : public option_base_t return result; } + template + simple_list_t get_value_simple() const + { + auto list = get_value(); + simple_list_t result; + for (const auto& val : list) + { + result.push_back(pop_first(val)); + } + + return result; + } + /** * Set the value of the option. * @@ -146,6 +179,23 @@ class compound_option_t : public option_base_t notify_updated(); } + /** + * Set the value of the option. + * + * Throws an exception in case of wrong template types. + */ + template + void set_value_simple(const simple_list_t& value) + { + compound_list_t list; + for (int i = 0; i < (int)value.size(); i++) + { + list.push_back(push_first(std::to_string(i), value[i])); + } + + this->set_value(list); + } + using stored_type_t = std::vector>; /** * Get the string data stored in the compound option. diff --git a/test/option_test.cpp b/test/option_test.cpp index 8eb6d6f..18a821a 100644 --- a/test/option_test.cpp +++ b/test/option_test.cpp @@ -254,3 +254,40 @@ TEST_CASE("compound options") }; CHECK(!opt.set_value_untyped(v4)); } + +TEST_CASE("Plain list compound options") +{ + using namespace wf::config; + + compound_option_t::entries_t entries; + entries.push_back(std::make_unique>("hey_")); + entries.push_back(std::make_unique>("bey_")); + compound_option_t opt{"Test", std::move(entries)}; + + simple_list_t simple_list = { + {0, 0.0}, + {1, -1.5} + }; + + opt.set_value_simple(simple_list); + auto with_names = opt.get_value(); + + compound_list_t compound_list = { + {"0", 0, 0.0}, + {"1", 1, -1.5} + }; + CHECK(compound_list == opt.get_value()); + + compound_list = { + {"test", 10, 0.0}, + {"blah", 20, 15.6} + }; + opt.set_value(compound_list); + + simple_list = { + {10, 0.0}, + {20, 15.6} + }; + + CHECK(simple_list == opt.get_value_simple()); +}