]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Add support for g_boxed_free / g_boxed_copy
authorJürg Billeter <j@bitron.ch>
Thu, 8 Jul 2010 19:15:53 +0000 (21:15 +0200)
committerJürg Billeter <j@bitron.ch>
Thu, 8 Jul 2010 20:34:01 +0000 (22:34 +0200)
codegen/valaccodebasemodule.vala
vala/valaclass.vala

index b06df434a90852952e8dadcf6316c2f740c583f7..3fb6553edbeb3fa089e9e30d4203705b56d030cb 100644 (file)
@@ -2331,6 +2331,12 @@ public class Vala.CCodeBaseModule : CCodeModule {
                                if (dup_function == null) {
                                        dup_function = "";
                                }
+                       } else if (cl != null && cl.is_gboxed) {
+                               // allow duplicates of gboxed instances
+                               dup_function = type.data_type.get_dup_function ();
+                               if (dup_function == null) {
+                                       dup_function = "";
+                               }
                        } else if (type is ValueType) {
                                dup_function = type.data_type.get_dup_function ();
                                if (dup_function == null && type.nullable) {
@@ -2642,6 +2648,43 @@ public class Vala.CCodeBaseModule : CCodeModule {
                return dup_func;
        }
 
+       protected string generate_dup_func_wrapper (DataType type) {
+               string destroy_func = "_vala_%s_copy".printf (type.data_type.get_cname ());
+
+               if (!add_wrapper (destroy_func)) {
+                       // wrapper already defined
+                       return destroy_func;
+               }
+
+               // declaration
+
+               var function = new CCodeFunction (destroy_func, type.get_cname ());
+               function.modifiers = CCodeModifiers.STATIC;
+               function.add_parameter (new CCodeFormalParameter ("self", type.get_cname ()));
+
+               // definition
+
+               var block = new CCodeBlock ();
+
+               var cl = type.data_type as Class;
+               assert (cl != null && cl.is_gboxed);
+
+               var free_call = new CCodeFunctionCall (new CCodeIdentifier ("g_boxed_copy"));
+               free_call.add_argument (new CCodeIdentifier (cl.get_type_id ()));
+               free_call.add_argument (new CCodeIdentifier ("self"));
+
+               block.add_statement (new CCodeReturnStatement (free_call));
+
+               // append to file
+
+               source_declarations.add_type_member_declaration (function.copy ());
+
+               function.block = block;
+               source_type_member_definition.append (function);
+
+               return destroy_func;
+       }
+
        protected string generate_destroy_func_wrapper (DataType type) {
                string destroy_func = "_vala_%s_free".printf (type.data_type.get_cname ());
 
@@ -2660,10 +2703,19 @@ public class Vala.CCodeBaseModule : CCodeModule {
 
                var block = new CCodeBlock ();
 
-               var free_call = new CCodeFunctionCall (new CCodeIdentifier (type.data_type.get_free_function ()));
-               free_call.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("self")));
+               var cl = type.data_type as Class;
+               if (cl != null && cl.is_gboxed) {
+                       var free_call = new CCodeFunctionCall (new CCodeIdentifier ("g_boxed_free"));
+                       free_call.add_argument (new CCodeIdentifier (cl.get_type_id ()));
+                       free_call.add_argument (new CCodeIdentifier ("self"));
+
+                       block.add_statement (new CCodeExpressionStatement (free_call));
+               } else {
+                       var free_call = new CCodeFunctionCall (new CCodeIdentifier (type.data_type.get_free_function ()));
+                       free_call.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("self")));
 
-               block.add_statement (new CCodeExpressionStatement (free_call));
+                       block.add_statement (new CCodeExpressionStatement (free_call));
+               }
 
                // append to file
 
@@ -2707,7 +2759,7 @@ public class Vala.CCodeBaseModule : CCodeModule {
                                        }
                                } else {
                                        var cl = type.data_type as Class;
-                                       if (cl != null && cl.free_function_address_of) {
+                                       if (cl != null && (cl.free_function_address_of || cl.is_gboxed)) {
                                                unref_function = generate_destroy_func_wrapper (type);
                                        } else {
                                                unref_function = type.data_type.get_free_function ();
index 91fa016c766fdeb9374f02d50433c69155808ebd..1b675dad6253d772bef83db767665f1644f21a2c 100644 (file)
@@ -108,6 +108,8 @@ public class Vala.Class : ObjectTypeSymbol {
         */
        public bool free_function_address_of { get; private set; }
 
+       public bool is_gboxed { get { return (free_function == "g_boxed_free"); } }
+
        private string cname;
        public string const_cname { get; set; }
        private string lower_case_cprefix;