[vala/wip/transform: 27/102] Deserialize hash tables



commit 2a7dce9eae150435529349ed69b8180fd406d1f7
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 |  2 +-
 2 files changed, 45 insertions(+), 1 deletion(-)
---
diff --git a/codegen/valagvarianttransformer.vala b/codegen/valagvarianttransformer.vala
index 1d6e6b344..c342fba04 100644
--- a/codegen/valagvarianttransformer.vala
+++ b/codegen/valagvarianttransformer.vala
@@ -346,6 +346,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;
@@ -355,6 +393,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 a89545782..968922c8d 100644
--- a/vala/valaobjectcreationexpression.vala
+++ b/vala/valaobjectcreationexpression.vala
@@ -69,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]