summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMateusz Majewski <m.majewski2@samsung.com>2022-06-24 12:06:23 +0200
committerHyotaek Shim <hyotaek.shim@samsung.com>2022-06-27 10:45:59 +0000
commit4ad1f5abacf41d30005d39c17bf06be13131c86f (patch)
tree58d7d04b029698a9b9a60c9e01cb8a9f79bbb450
parent53a18a95170dd4cafb267763e9d8feca392b006b (diff)
downloadsessiond-4ad1f5abacf41d30005d39c17bf06be13131c86f.tar.gz
sessiond-4ad1f5abacf41d30005d39c17bf06be13131c86f.tar.bz2
sessiond-4ad1f5abacf41d30005d39c17bf06be13131c86f.zip
Refactor tuple_g_variant_helpers
... to be a bit more extensible. Change-Id: I2c37833fd8c5296d2d81413316173a35910e7a8f
-rw-r--r--sessiond/src/tuple_g_variant_helpers.hpp51
1 files changed, 41 insertions, 10 deletions
diff --git a/sessiond/src/tuple_g_variant_helpers.hpp b/sessiond/src/tuple_g_variant_helpers.hpp
index f720676..a772e33 100644
--- a/sessiond/src/tuple_g_variant_helpers.hpp
+++ b/sessiond/src/tuple_g_variant_helpers.hpp
@@ -65,23 +65,30 @@ constexpr std::string type_of_g_variant()
return std::string("(") + concat_gvariant_types<Ts ...>() + ")";
}
-template <size_t I, typename T>
-std::tuple<T> tuple_from_g_variant_inner(GVariant *variant)
+template <typename T>
+T value_from_g_variant_one_inner(GVariant *variant)
{
char output_type[2] = { type_of_g_variant_one<T>() };
T output_slot;
- g_variant_get_child(variant, I, output_type, &output_slot);
- return std::make_tuple(output_slot);
+ g_variant_get(variant, output_type, &output_slot);
+ return output_slot;
+}
+
+template <size_t I, typename T>
+std::tuple<T> tuple_from_g_variant_inner(GVariant *variant)
+{
+ GVariant *i = g_variant_get_child_value(variant, I);
+ T output = value_from_g_variant_one_inner<T>(i);
+ return std::make_tuple(output);
}
template <size_t I, typename T, typename T1, typename... Ts>
std::tuple<T, T1, Ts...> tuple_from_g_variant_inner(GVariant *variant)
{
- char output_type[2] = { type_of_g_variant_one<T>() };
- T output_slot;
- g_variant_get_child(variant, I, output_type, &output_slot);
+ GVariant *i = g_variant_get_child_value(variant, I);
+ T output = value_from_g_variant_one_inner<T>(i);
auto rest = tuple_from_g_variant_inner<I + 1, T1, Ts...>(variant);
- return std::tuple_cat(std::make_tuple(output_slot), std::move(rest));
+ return std::tuple_cat(std::make_tuple(output), std::move(rest));
}
template <typename... Ts>
@@ -93,10 +100,34 @@ std::tuple<Ts...> tuple_from_g_variant(GVariant *variant)
return tuple_from_g_variant_inner<0, Ts...>(variant);
}
+template <typename T>
+GVariant *value_to_g_variant_one_inner(const T &value)
+{
+ char input_type[2] = { type_of_g_variant_one<T>() };
+ return g_variant_new(input_type, value);
+}
+
+template <size_t I, typename T>
+void fill_g_variant_one_table_inner(GVariant **table, const T &value)
+{
+ table[I] = value_to_g_variant_one_inner<T>(value);
+}
+
+template <size_t I, typename T, typename T1, typename... Ts>
+void fill_g_variant_one_table_inner(GVariant **table, const T &value, const T1 &next_value, const Ts &...vals)
+{
+ table[I] = value_to_g_variant_one_inner<T>(value);
+ fill_g_variant_one_table_inner<I + 1, T1, Ts...>(table, next_value, vals...);
+}
+
template <typename... Ts>
-GVariant *vals_to_g_variant(Ts... vals)
+GVariant *vals_to_g_variant(const Ts &...vals)
{
- return g_variant_new(type_of_g_variant<Ts...>().c_str(), vals...);
+ GVariant *table[sizeof...(vals)];
+ fill_g_variant_one_table_inner<0, Ts...>(table, vals...);
+ return g_variant_new_tuple(table, sizeof...(vals));
+ // Note that there is no need to free the GVariants in table,
+ // as they were floating and consumed by g_variant_new_tuple.
}
template <typename... Ts>