[vala/wip/transform: 19/63] Deserialize hash tables



commit c992d0b1d0b1787186885978a18f28de5dcbbd45
Author: Luca Bruno <lucabru src gnome org>
Date:   Fri Dec 30 18:18:44 2011 +0100

    Deserialize hash tables

 codegen/valagvarianttransformer.vala   |   44 ++++++++++++++++++++++++++++++++
 vala/valaobjectcreationexpression.vala |   10 ++++---
 2 files changed, 50 insertions(+), 4 deletions(-)
---
diff --git a/codegen/valagvarianttransformer.vala b/codegen/valagvarianttransformer.vala
index 0961dd2..32377c1 100644
--- a/codegen/valagvarianttransformer.vala
+++ b/codegen/valagvarianttransformer.vala
@@ -344,6 +344,44 @@ public class Vala.GVariantTransformer : CodeTransformer {
 		return expression (result);
 	}
 
+	Expression deserialize_hash_table (DataType type, Expression expr) {
+		var variant = b.add_temp_declaration (data_type ("GLib.Variant", true), null);
+		var iterator = b.add_temp_declaration (data_type ("GLib.VariantIter", true), null);
+		b.add_assignment (expression (variant), expr);
+		b.add_assignment (expression (iterator), expression (@"$variant.iterator ()"));
+
+		var type_args = type.get_type_arguments ();
+		assert (type_args.size == 2);
+		var key_type = type_args.get (0);
+		var value_type = type_args.get (1);
+
+		var hash_table_new = new ObjectCreationExpression (null, b.source_reference);
+		hash_table_new.type_reference = type.copy ();
+		if (key_type.data_type == context.analyzer.string_type.data_type) {
+			hash_table_new.add_argument (expression ("GLib.str_hash"));
+			hash_table_new.add_argument (expression ("GLib.str_equal"));
+		} else {
+			hash_table_new.add_argument (expression ("GLib.direct_hash"));
+			hash_table_new.add_argument (expression ("GLib.direct_equal"));
+		}
+
+		var hash_table = b.add_temp_declaration (copy_type (type, true), hash_table_new);
+		var new_variant = b.add_temp_declaration (data_type ("GLib.Variant", true), null);
+
+		b.open_while (expression (@"($new_variant = $iterator.next_value ()) != null"));
+
+		Expression key = deserialize_expression (key_type, expression (@"$new_variant.get_child_value (0)"));
+		Expression value = deserialize_expression (value_type, expression (@"$new_variant.get_child_value (1)"));
+		var insert = (MethodCall) expression (@"$hash_table.insert ()");
+		insert.add_argument (key);
+		insert.add_argument (value);
+		b.add_expression (insert);
+
+		b.close ();
+
+		return expression (hash_table);
+	}
+
 	Expression? deserialize_expression (DataType type, Expression expr) {
 		BasicTypeInfo basic_type;
 		Expression result = null;
@@ -353,6 +391,12 @@ public class Vala.GVariantTransformer : CodeTransformer {
 			result = deserialize_array ((ArrayType) type, expr);
 		} else if (type.data_type is Struct) {
 			result = deserialize_struct ((Struct) type.data_type, expr);
+		} else if (type is ObjectType) {
+			if (type.data_type == context.analyzer.gvariant_type.data_type) {
+				result = new MethodCall (new MemberAccess (expr, "get_variant"), b.source_reference);
+			} else if (type.data_type.get_full_name () == "GLib.HashTable") {
+				result = deserialize_hash_table ((ObjectType) type, expr);
+			}
 		}
 		return result;
 	}
diff --git a/vala/valaobjectcreationexpression.vala b/vala/valaobjectcreationexpression.vala
index a9c8765..8de79e0 100644
--- a/vala/valaobjectcreationexpression.vala
+++ b/vala/valaobjectcreationexpression.vala
@@ -41,11 +41,13 @@ public class Vala.ObjectCreationExpression : Expression {
 	 * The construction method to use or the data type to be created
 	 * with the default construction method.
 	 */
-	public MemberAccess member_name {
+	public MemberAccess? member_name {
 		get { return _member_name; }
 		set {
 			_member_name = value;
-			_member_name.parent_node = this;
+			if (_member_name != null) {
+				_member_name.parent_node = this;
+			}
 		}
 	}
 
@@ -58,7 +60,7 @@ public class Vala.ObjectCreationExpression : Expression {
 	private List<MemberInitializer> object_initializer = new ArrayList<MemberInitializer> ();
 
 	private DataType _data_type;
-	private MemberAccess _member_name;
+	private MemberAccess? _member_name;
 
 	/**
 	 * Creates a new object creation expression.
@@ -67,7 +69,7 @@ public class Vala.ObjectCreationExpression : Expression {
 	 * @param source_reference reference to source code
 	 * @return                 newly created object creation expression
 	 */
-	public ObjectCreationExpression (MemberAccess member_name, SourceReference source_reference) {
+	public ObjectCreationExpression (MemberAccess? member_name, SourceReference? source_reference = null) {
 		this.source_reference = source_reference;
 		this.member_name = member_name;
 	}



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