[vala/wip/transform: 289/373] Serialize hash table



commit 26a0184fe14af64b972bb4960d0ee764606690e4
Author: Luca Bruno <lucabru src gnome org>
Date:   Fri Dec 30 11:27:19 2011 +0100

    Serialize hash table

 codegen/valagvarianttransformer.vala | 24 ++++++++++++++++++++----
 vala/valacodebuilder.vala            |  2 ++
 2 files changed, 22 insertions(+), 4 deletions(-)
---
diff --git a/codegen/valagvarianttransformer.vala b/codegen/valagvarianttransformer.vala
index 211117b81..c2f09eb49 100644
--- a/codegen/valagvarianttransformer.vala
+++ b/codegen/valagvarianttransformer.vala
@@ -231,11 +231,19 @@ public class Vala.GVariantTransformer : CodeTransformer {
                return expression (@"$builder.end ()");
        }
 
-       Expression? serialize_expression (DataType type, Expression expr) {
-               if (type.data_type == context.analyzer.gvariant_type.data_type) {
-                       return expr;
-               }
+       Expression? serialize_hash_table (ObjectType type, Expression expr) {
+               string temp = b.add_temp_declaration (expr.value_type, expr);
+
+               var builderinit = expression (@"new GLib.VariantBuilder (new GLib.VariantType 
(\"$(get_type_signature (type))\"))");
+               var builder = b.add_temp_declaration (null, builderinit);
+
+               var for_each = expression (@"$temp.for_each ((k,v) => $builder.add (\"{?*}\", k, v))");
+               b.add_expression (for_each);
 
+               return expression (@"$builder.end ()");
+       }
+
+       Expression? serialize_expression (DataType type, Expression expr) {
                BasicTypeInfo basic_type;
                Expression result = null;
                if (get_basic_type_info (get_type_signature (type), out basic_type)) {
@@ -244,6 +252,14 @@ public class Vala.GVariantTransformer : CodeTransformer {
                        result = serialize_array ((ArrayType) type, expr);
                } else if (type.data_type is Struct) {
                        result = serialize_struct ((Struct) type.data_type, expr);
+               } else if (type is ObjectType) {
+                       if (type.data_type == context.analyzer.gvariant_type.data_type) {
+                               var variant_new = (ObjectCreationExpression) expression ("new 
GLib.Variant.variant ()");
+                               variant_new.add_argument (expr);
+                               result = variant_new;
+                       } else if (type.data_type.get_full_name () == "GLib.HashTable") {
+                               result = serialize_hash_table ((ObjectType) type, expr);
+                       }
                }
                return result;
        }
diff --git a/vala/valacodebuilder.vala b/vala/valacodebuilder.vala
index f08fc92bb..c15fc05e8 100644
--- a/vala/valacodebuilder.vala
+++ b/vala/valacodebuilder.vala
@@ -38,6 +38,8 @@ public class Vala.CodeBuilder {
 
                var statement_block = context.analyzer.get_current_block (insert_statement);
                statement_block.insert_before (insert_statement, current_block);
+               this.insert_statement = current_block;
+
                check_nodes.add (current_block);
        }
 


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