From 3def677b32e7cf1916de2cdf6be01a7a0cc43d68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Tue, 18 Oct 2016 17:27:13 +0200 Subject: Converter: use builders and c++ facilities for schema conversions --- libqmenumodel/src/converter.cpp | 60 +++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 38 deletions(-) (limited to 'libqmenumodel') diff --git a/libqmenumodel/src/converter.cpp b/libqmenumodel/src/converter.cpp index 3131d4f..aef34d0 100644 --- a/libqmenumodel/src/converter.cpp +++ b/libqmenumodel/src/converter.cpp @@ -303,69 +303,53 @@ GVariant* Converter::toGVariantWithSchema(const QVariant &value, const char* sch } } else if (g_variant_type_is_array(schema_type)) { if (value.canConvert(QVariant::List)) { + const GVariantType* entryType = g_variant_type_element(schema_type); + const gchar* entryTypeString = g_variant_type_peek_string(entryType); - const GVariantType* entry_type; - GVariant* data; - entry_type = g_variant_type_element(schema_type); - const gchar* entryTypeString = g_variant_type_peek_string(entry_type); - - const QVariantList &lst = value.toList(); - GVariant **vars = g_new(GVariant*, lst.size()); - + GVariantBuilder *b = g_variant_builder_new(G_VARIANT_TYPE_ARRAY); bool ok = true; - for (int i=0, iMax=lst.size(); i < iMax; i++) { - data = Converter::toGVariantWithSchema(lst.at(i), entryTypeString); + + for (const QVariant &v : value.toList()) { + GVariant *data = toGVariantWithSchema(v, entryTypeString); if (data) { - vars[i] = data; - } - else { + g_variant_builder_add_value(b, data); + } else { ok = false; qWarning() << "Failed to convert list to array with schema:" << schema; break; } } if (ok) { - result = g_variant_new_array(entry_type, vars, lst.size()); + result = g_variant_builder_end(b); } - g_free(vars); + g_variant_builder_unref(b); } } else if (g_variant_type_is_tuple(schema_type)) { if (value.canConvert(QVariant::List)) { - GVariant* data; - - QVariantList lst = value.toList(); - GVariant **vars = g_new(GVariant*, lst.size()); - - const GVariantType* entry_type = g_variant_type_first(schema_type); + const GVariantType* entryType = g_variant_type_first(schema_type); + GVariantBuilder *b = g_variant_builder_new(G_VARIANT_TYPE_TUPLE); bool ok = true; - for (int i=0,iMax=lst.size(); i < iMax; i++) { - - gchar* entryTypeString = g_variant_type_dup_string(entry_type); - data = Converter::toGVariantWithSchema(lst.at(i), entryTypeString); + for (const QVariant &v : value.toList()) { + gchar* entryTypeString = g_variant_type_dup_string(entryType); + GVariant *data = toGVariantWithSchema(v, entryTypeString); + g_free(entryTypeString); if (data) { - vars[i] = data; - } - else { + g_variant_builder_add_value(b, data); + entryType = g_variant_type_next(entryType); + } else { ok = false; - qWarning() << "Failed to convert list to tuple with schema:" << schema; - g_free(entryTypeString); - break; - } - g_free(entryTypeString); - - entry_type = g_variant_type_next(entry_type); - if (!entry_type) { + qWarning() << "Failed to convert list to array with schema:" << schema; break; } } if (ok) { - result = g_variant_new_tuple(vars, lst.size()); + result = g_variant_builder_end(b); } - g_free(vars); + g_variant_builder_unref(b); } } -- cgit v1.2.3