[vala/wip/transform: 19/99] Deserialize structs



commit c12ab88aa4ef5a740bc390615e5846e68575b7a9
Author: Luca Bruno <lucabru src gnome org>
Date:   Fri Dec 30 17:56:43 2011 +0100

    Deserialize structs

 codegen/valagvarianttransformer.vala |   30 ++++++++++++++++++++++++++++++
 1 files changed, 30 insertions(+), 0 deletions(-)
---
diff --git a/codegen/valagvarianttransformer.vala b/codegen/valagvarianttransformer.vala
index 8e18c5d..0961dd2 100644
--- a/codegen/valagvarianttransformer.vala
+++ b/codegen/valagvarianttransformer.vala
@@ -316,6 +316,34 @@ public class Vala.GVariantTransformer : CodeTransformer {
                b.close ();
        }
 
+       Expression? deserialize_struct (Struct st, 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 = context.analyzer.get_data_type_for_symbol (st);
+               type.value_owned = true;
+               var result = b.add_temp_declaration (type, expression ("{}"));
+
+               bool field_found = false;
+
+               foreach (var f in st.get_fields ()) {
+                       if (f.binding != MemberBinding.INSTANCE) {
+                               continue;
+                       }
+
+                       field_found = true;
+
+                       b.add_assignment (expression (@"$result.$(f.name)"), deserialize_expression 
(f.variable_type, expression (@"$iterator.next_value ()")));
+               }
+
+               if (!field_found) {
+                       return null;
+               }
+
+               return expression (result);
+       }
+
        Expression? deserialize_expression (DataType type, Expression expr) {
                BasicTypeInfo basic_type;
                Expression result = null;
@@ -323,6 +351,8 @@ public class Vala.GVariantTransformer : CodeTransformer {
                        result = deserialize_basic (basic_type, expr);
                } else if (type is ArrayType) {
                        result = deserialize_array ((ArrayType) type, expr);
+               } else if (type.data_type is Struct) {
+                       result = deserialize_struct ((Struct) type.data_type, expr);
                }
                return result;
        }


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