[vala/wip/transform: 292/373] Deserialize structs



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

    Deserialize structs

 codegen/valagvarianttransformer.vala | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
---
diff --git a/codegen/valagvarianttransformer.vala b/codegen/valagvarianttransformer.vala
index c041b4d6a..1d6e6b344 100644
--- a/codegen/valagvarianttransformer.vala
+++ b/codegen/valagvarianttransformer.vala
@@ -318,6 +318,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;
@@ -325,6 +353,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]