diff options
author | Marco Trevisan (Treviño) <mail@3v1n0.net> | 2016-10-18 17:27:13 +0200 |
---|---|---|
committer | Marco Trevisan (Treviño) <mail@3v1n0.net> | 2016-10-18 17:27:13 +0200 |
commit | 3def677b32e7cf1916de2cdf6be01a7a0cc43d68 (patch) | |
tree | ecbc895a635fb05c9a31f85c70e9c217226e65f6 /libqmenumodel/src/converter.cpp | |
parent | 3bd198ee6bdaec2fefff032098b2442b0160c961 (diff) | |
download | qmenumodel-3def677b32e7cf1916de2cdf6be01a7a0cc43d68.tar.gz qmenumodel-3def677b32e7cf1916de2cdf6be01a7a0cc43d68.tar.bz2 qmenumodel-3def677b32e7cf1916de2cdf6be01a7a0cc43d68.zip |
Converter: use builders and c++ facilities for schema conversions
Diffstat (limited to 'libqmenumodel/src/converter.cpp')
-rw-r--r-- | libqmenumodel/src/converter.cpp | 60 |
1 files changed, 22 insertions, 38 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); } } |