Skip to content

Commit

Permalink
compound-option: allow setting those options without giving explicit …
Browse files Browse the repository at this point in the history
…tuple names
  • Loading branch information
ammen99 committed Nov 14, 2022
1 parent 7d0c16f commit 578b0bf
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
50 changes: 50 additions & 0 deletions include/wayfire/config/compound-option.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <string>
#include <wayfire/config/types.hpp>
#include <wayfire/config/option.hpp>
#include <wayfire/util/log.hpp>
Expand All @@ -15,6 +16,25 @@ template<class... Args>
using compound_list_t =
std::vector<std::tuple<std::string, Args...>>;

template<class... Args>
using simple_list_t =
std::vector<std::tuple<Args...>>;

template<typename T1, typename... Ts>
std::tuple<Ts...> pop_first(const std::tuple<T1, Ts...>& tuple)
{
return std::apply([] (auto&&, const auto&... args)
{
return std::tie(args...);
}, tuple);
}

template<typename T1, typename... Ts>
std::tuple<T1, Ts...> push_first(const T1& t, const std::tuple<Ts...>& tuple)
{
return std::tuple_cat(std::tie(t), tuple);
}

/**
* A base class containing information about an entry in a tuple.
*/
Expand Down Expand Up @@ -132,6 +152,19 @@ class compound_option_t : public option_base_t
return result;
}

template<class... Args>
simple_list_t<Args...> get_value_simple() const
{
auto list = get_value<Args...>();
simple_list_t<Args...> result;
for (const auto& val : list)
{
result.push_back(pop_first(val));
}

return result;
}

/**
* Set the value of the option.
*
Expand All @@ -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<class... Args>
void set_value_simple(const simple_list_t<Args...>& value)
{
compound_list_t<Args...> 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<std::vector<std::string>>;
/**
* Get the string data stored in the compound option.
Expand Down
37 changes: 37 additions & 0 deletions test/option_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<compound_option_entry_t<int>>("hey_"));
entries.push_back(std::make_unique<compound_option_entry_t<double>>("bey_"));
compound_option_t opt{"Test", std::move(entries)};

simple_list_t<int, double> simple_list = {
{0, 0.0},
{1, -1.5}
};

opt.set_value_simple(simple_list);
auto with_names = opt.get_value<int, double>();

compound_list_t<int, double> compound_list = {
{"0", 0, 0.0},
{"1", 1, -1.5}
};
CHECK(compound_list == opt.get_value<int, double>());

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<int, double>());
}

0 comments on commit 578b0bf

Please sign in to comment.