From: Jürg Billeter Date: Sun, 6 Sep 2009 16:46:22 +0000 (+0200) Subject: GValue: Support conversion from and to string[] X-Git-Tag: 0.7.6~127 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bcff68f08f4e49fdd489c4f23f688bbb51068349;p=thirdparty%2Fvala.git GValue: Support conversion from and to string[] --- diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala index f70190363..0df998cf7 100644 --- a/codegen/valaccodebasemodule.vala +++ b/codegen/valaccodebasemodule.vala @@ -419,13 +419,29 @@ internal class Vala.CCodeBaseModule : CCodeModule { } public override CCodeIdentifier get_value_setter_function (DataType type_reference) { + var array_type = type_reference as ArrayType; if (type_reference.data_type != null) { return new CCodeIdentifier (type_reference.data_type.get_set_value_function ()); + } else if (array_type != null && array_type.element_type.data_type == string_type.data_type) { + // G_TYPE_STRV + return new CCodeIdentifier ("g_value_set_boxed"); } else { return new CCodeIdentifier ("g_value_set_pointer"); } } + CCodeIdentifier get_value_getter_function (DataType type_reference) { + var array_type = type_reference as ArrayType; + if (type_reference.data_type != null) { + return new CCodeIdentifier (type_reference.data_type.get_get_value_function ()); + } else if (array_type != null && array_type.element_type.data_type == string_type.data_type) { + // G_TYPE_STRV + return new CCodeIdentifier ("g_value_get_boxed"); + } else { + return new CCodeIdentifier ("g_value_get_pointer"); + } + } + public virtual void append_vala_array_free () { } @@ -3451,9 +3467,15 @@ internal class Vala.CCodeBaseModule : CCodeModule { if (expr.inner.value_type != null && gvalue_type != null && expr.inner.value_type.data_type == gvalue_type && expr.type_reference.get_type_id () != null) { // explicit conversion from GValue - var ccall = new CCodeFunctionCall (new CCodeIdentifier (expr.type_reference.data_type.get_get_value_function ())); + var ccall = new CCodeFunctionCall (get_value_getter_function (expr.type_reference)); ccall.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, (CCodeExpression) expr.inner.ccodenode)); expr.ccodenode = ccall; + if (expr.type_reference is ArrayType) { + // null-terminated string array + var len_call = new CCodeFunctionCall (new CCodeIdentifier ("g_strv_length")); + len_call.add_argument (ccall); + expr.append_array_size (len_call); + } return; } diff --git a/vala/valaarraytype.vala b/vala/valaarraytype.vala index 3c19f29fd..710a17b6b 100644 --- a/vala/valaarraytype.vala +++ b/vala/valaarraytype.vala @@ -175,6 +175,11 @@ public class Vala.ArrayType : ReferenceType { } public override bool compatible (DataType target_type) { + if (target_type.get_type_id () == "G_TYPE_VALUE" && element_type.data_type == CodeContext.get ().root.scope.lookup ("string")) { + // allow implicit conversion from string[] to GValue + return true; + } + if (target_type is PointerType || (target_type.data_type != null && target_type.data_type.get_attribute ("PointerType") != null)) { /* any array type can be cast to a generic pointer */ return true; @@ -234,4 +239,12 @@ public class Vala.ArrayType : ReferenceType { } return element_type.check (analyzer); } + + public override string? get_type_id () { + if (element_type.data_type == CodeContext.get ().root.scope.lookup ("string")) { + return "G_TYPE_STRV"; + } else { + return null; + } + } }