[vala] GValue: Support conversion from and to string[]



commit bcff68f08f4e49fdd489c4f23f688bbb51068349
Author: Jürg Billeter <j bitron ch>
Date:   Sun Sep 6 18:46:22 2009 +0200

    GValue: Support conversion from and to string[]

 codegen/valaccodebasemodule.vala |   24 +++++++++++++++++++++++-
 vala/valaarraytype.vala          |   13 +++++++++++++
 2 files changed, 36 insertions(+), 1 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index f701903..0df998c 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 3c19f29..710a17b 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;
+		}
+	}
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]