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 +++++++++++++++-------------------------- tests/client/convertertest.cpp | 26 +++++++++--------- 2 files changed, 35 insertions(+), 51 deletions(-) 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); } } diff --git a/tests/client/convertertest.cpp b/tests/client/convertertest.cpp index 32d6091..5a9367f 100644 --- a/tests/client/convertertest.cpp +++ b/tests/client/convertertest.cpp @@ -295,38 +295,38 @@ private Q_SLOTS: void testSchemaConvert() { // convert to integer - compareWithSchema(QVariant::fromValue(1), "i"); - compareWithSchema(QVariant::fromValue(1.1), "i"); + QVERIFY(compareWithSchema(QVariant::fromValue(1), "i")); + QVERIFY(compareWithSchema(QVariant::fromValue(1.1), "i")); // convert to integer - compareWithSchema(QVariant::fromValue(true), "b"); - compareWithSchema(QVariant::fromValue(1), "b"); + QVERIFY(compareWithSchema(QVariant::fromValue(true), "b")); + QVERIFY(compareWithSchema(QVariant::fromValue(1), "b")); // convert to double - compareWithSchema(QVariant::fromValue(1.0), "d"); - compareWithSchema(QVariant::fromValue(1), "d"); + QVERIFY(compareWithSchema(QVariant::fromValue(1.0), "d")); + QVERIFY(compareWithSchema(QVariant::fromValue(1), "d")); // convert to string - compareWithSchema(QVariant::fromValue(1), "s"); - compareWithSchema(QVariant::fromValue(1.1), "s"); + QVERIFY(compareWithSchema(QVariant::fromValue(1), "s")); + QVERIFY(compareWithSchema(QVariant::fromValue(1.1), "s")); // convert to tuple - compareWithSchema(QVariantList() << QVariant::fromValue(true) << QVariant::fromValue(1) << QVariant::fromValue(1) << QVariant::fromValue("test1"), "(bdis)"); + QVERIFY(compareWithSchema(QVariantList() << QVariant::fromValue(true) << QVariant::fromValue(1) << QVariant::fromValue(1) << QVariant::fromValue("test1"), "(bdis)")); // convert to array - compareWithSchema(QVariantList() << QVariant::fromValue(1) << QVariant::fromValue(1), "ad"); - compareWithSchema(QVariantList() << QVariant::fromValue("test1") << QVariant::fromValue("test2"), "as"); + QVERIFY(compareWithSchema(QVariantList() << QVariant::fromValue(1) << QVariant::fromValue(1), "ad")); + QVERIFY(compareWithSchema(QVariantList() << QVariant::fromValue("test1") << QVariant::fromValue("test2"), "as")); // convert to array of tuple QVariantList si1(QVariantList() << QVariant::fromValue("test1") << QVariant::fromValue(1)); QVariantList si2(QVariantList() << QVariant::fromValue("test1") << QVariant::fromValue(1)); - compareWithSchema(QVariantList() << QVariant::fromValue(si1) << QVariant::fromValue(si2), "a(sd)"); + QVERIFY(compareWithSchema(QVariantList() << QVariant::fromValue(si1) << QVariant::fromValue(si2), "a(sd)")); // convert to vardict QVariantMap map; map["test1"] = QVariant::fromValue(1); map["test2"] = QVariant::fromValue(1); - compareWithSchema(map, "a{sv}"); + QVERIFY(compareWithSchema(map, "a{sv}")); } }; -- cgit v1.2.3