[vala] GValue: Support implicit conversion to nullable GValue
- From: Jürg Billeter <juergbi src gnome org>
- To: svn-commits-list gnome org
- Subject: [vala] GValue: Support implicit conversion to nullable GValue
- Date: Tue, 28 Jul 2009 13:30:45 +0000 (UTC)
commit fc48375697f83fb1b9182ecda57f708652911346
Author: Frederik Sdun <frederik sdun googlemail com>
Date: Tue Jul 28 15:28:40 2009 +0200
GValue: Support implicit conversion to nullable GValue
Fixes bug 589155.
codegen/valaccodebasemodule.vala | 22 +++++++++++++-
tests/Makefile.am | 1 +
tests/structs/gvalue.test | 59 ++++++++++++++++++++++++++++++++++++++
3 files changed, 80 insertions(+), 2 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 572fbb8..2ac63ba 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -3683,13 +3683,31 @@ internal class Vala.CCodeBaseModule : CCodeModule {
var ccomma = new CCodeCommaExpression ();
+ if (target_type.nullable) {
+ var newcall = new CCodeFunctionCall (new CCodeIdentifier ("g_new0"));
+ newcall.add_argument (new CCodeConstant ("GValue"));
+ newcall.add_argument (new CCodeConstant ("1"));
+ var newassignment = new CCodeAssignment (get_variable_cexpression (decl.name), newcall);
+ ccomma.append_expression (newassignment);
+ }
+
var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_value_init"));
- ccall.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, get_variable_cexpression (decl.name)));
+ if (target_type.nullable) {
+ ccall.add_argument (get_variable_cexpression (decl.name));
+ }
+ else {
+ ccall.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, get_variable_cexpression (decl.name)));
+ }
ccall.add_argument (new CCodeIdentifier (expression_type.get_type_id ()));
ccomma.append_expression (ccall);
ccall = new CCodeFunctionCall (get_value_setter_function (expression_type));
- ccall.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, get_variable_cexpression (decl.name)));
+ if (target_type.nullable) {
+ ccall.add_argument (get_variable_cexpression (decl.name));
+ }
+ else {
+ ccall.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, get_variable_cexpression (decl.name)));
+ }
ccall.add_argument (cexpr);
ccomma.append_expression (ccall);
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 2d69f96..c5599ab 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -29,6 +29,7 @@ TESTS = \
control-flow/sideeffects.test \
enums/enums.test \
structs/structs.test \
+ structs/gvalue.test \
delegates/delegates.test \
objects/classes.test \
objects/fields.test \
diff --git a/tests/structs/gvalue.test b/tests/structs/gvalue.test
new file mode 100644
index 0000000..8bd7c42
--- /dev/null
+++ b/tests/structs/gvalue.test
@@ -0,0 +1,59 @@
+
+Program: test
+
+void test_value () {
+ Value v1 = 1;
+ assert (v1.get_int() == 1);
+
+ string s = "hello";
+ Value v2 = s;
+ assert (v2.get_string () == s);
+}
+
+void test_value_array () {
+ int[] iarray = {1,2,3,4,5};
+ Value[] viarray = {1,2,3,4,5};
+ assert (viarray.length == iarray.length);
+ for (int i = 0; i < viarray.length; i++) {
+ assert (viarray[i].get_int () == iarray[i]);
+ }
+
+ string[] sarray = { "hello", "vala", "world" };
+ Value[] vsarray = { "hello", "vala", "world" };
+ assert (vsarray.length == sarray.length);
+ for (int i = 0; i < vsarray.length; i++) {
+ assert (vsarray[i].get_string () == sarray[i]);
+ }
+}
+
+void test_nullable_value () {
+ Value? v1 = 1;
+ assert (v1.get_int() == 1);
+
+ string s = "hello";
+ Value? v2 = s;
+ assert (v2.get_string () == s);
+}
+
+void test_nullable_value_array () {
+ int[] iarray = {1,2,3,4,5};
+ Value?[] viarray = {1,2,3,4,5};
+ assert (viarray.length == iarray.length);
+ for (int i = 0; i < viarray.length; i++) {
+ assert (viarray[i].get_int () == iarray[i]);
+ }
+
+ string[] sarray = { "hello", "vala", "world" };
+ Value?[] vsarray = { "hello", "vala", "world" };
+ assert (vsarray.length == sarray.length);
+ for (int i = 0; i < vsarray.length; i++) {
+ assert (vsarray[i].get_string () == sarray[i]);
+ }
+}
+
+void main () {
+ test_value ();
+ test_value_array ();
+ test_nullable_value ();
+ test_nullable_value_array ();
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]