[vala/staging: 2/4] Add explicit copy method for arrays



commit d274caac1745465c50cfe14908452e0d024011f1
Author: Luca Bruno <lucabru src gnome org>
Date:   Sat Jul 30 16:41:34 2011 +0200

    Add explicit copy method for arrays
    
    https://bugzilla.gnome.org/show_bug.cgi?id=650663

 codegen/valaccodememberaccessmodule.vala |    2 +-
 codegen/valaccodemethodcallmodule.vala   |    3 ++
 tests/basic-types/arrays.vala            |    8 ++++++
 vala/Makefile.am                         |    1 +
 vala/valaarraycopymethod.vala            |   38 ++++++++++++++++++++++++++++++
 vala/valaarraytype.vala                  |   16 ++++++++++++
 6 files changed, 67 insertions(+), 1 deletions(-)
---
diff --git a/codegen/valaccodememberaccessmodule.vala b/codegen/valaccodememberaccessmodule.vala
index af6fc65..cfbd9d3 100644
--- a/codegen/valaccodememberaccessmodule.vala
+++ b/codegen/valaccodememberaccessmodule.vala
@@ -36,7 +36,7 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
                if (expr.symbol_reference is Method) {
                        var m = (Method) expr.symbol_reference;
 
-                       if (!(m is DynamicMethod || m is ArrayMoveMethod || m is ArrayResizeMethod)) {
+                       if (!(m is DynamicMethod || m is ArrayMoveMethod || m is ArrayResizeMethod || m is 
ArrayCopyMethod)) {
                                generate_method_declaration (m, cfile);
 
                                if (!m.external && m.external_package) {
diff --git a/codegen/valaccodemethodcallmodule.vala b/codegen/valaccodemethodcallmodule.vala
index e323bed..2cd12b6 100644
--- a/codegen/valaccodemethodcallmodule.vala
+++ b/codegen/valaccodemethodcallmodule.vala
@@ -188,6 +188,9 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
                        in_arg_map.set (get_param_pos (0), new CCodeIdentifier (get_ccode_name 
(array_type.element_type)));
                } else if (m is ArrayMoveMethod) {
                        requires_array_move = true;
+               } else if (m is ArrayCopyMethod) {
+                       expr.target_value = copy_value (ma.inner.target_value, expr);
+                       return;
                }
 
                CCodeExpression instance = null;
diff --git a/tests/basic-types/arrays.vala b/tests/basic-types/arrays.vala
index fece749..7685304 100644
--- a/tests/basic-types/arrays.vala
+++ b/tests/basic-types/arrays.vala
@@ -191,6 +191,13 @@ void test_void_array () {
        assert ((void*) null in a);
 }
 
+void test_explicit_copying () {
+       int[] a0 = { 1, 2, 3};
+       var a1 = a0.copy ();
+       assert (a1.length == 3);
+       assert (a0[1] == a1[1]);
+}
+
 void main () {
        test_integer_array ();
        test_string_array ();
@@ -202,4 +209,5 @@ void main () {
        test_delegate_array ();
        test_generics_array ();
        test_void_array ();
+       test_explicit_copying ();
 }
diff --git a/vala/Makefile.am b/vala/Makefile.am
index 134d732..397594c 100644
--- a/vala/Makefile.am
+++ b/vala/Makefile.am
@@ -18,6 +18,7 @@ noinst_LTLIBRARIES = \
 
 libvalacore_la_VALASOURCES = \
        valaaddressofexpression.vala \
+       valaarraycopymethod.vala \
        valaarraycreationexpression.vala \
        valaarraylengthfield.vala \
        valaarraymovemethod.vala \
diff --git a/vala/valaarraycopymethod.vala b/vala/valaarraycopymethod.vala
new file mode 100644
index 0000000..97086cd
--- /dev/null
+++ b/vala/valaarraycopymethod.vala
@@ -0,0 +1,38 @@
+/* valaarraycopymethod.vala
+ *
+ * Copyright (C) 2011  Luca Bruno
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Author:
+ *     Luca Bruno <lucabru src gnome org>
+ */
+
+using GLib;
+
+/**
+ * Represents the Array.copy method.
+ */
+public class Vala.ArrayCopyMethod : Method {
+       /**
+        * Creates a new array copy method.
+        *
+        * @return newly created method
+        */
+       public ArrayCopyMethod (SourceReference source_reference) {
+               base ("copy", new InvalidType (), source_reference);
+               external = true;
+       }
+}
diff --git a/vala/valaarraytype.vala b/vala/valaarraytype.vala
index f6d0132..0acd0df 100644
--- a/vala/valaarraytype.vala
+++ b/vala/valaarraytype.vala
@@ -67,6 +67,7 @@ public class Vala.ArrayType : ReferenceType {
        private ArrayLengthField length_field;
        private ArrayResizeMethod resize_method;
        private ArrayMoveMethod move_method;
+       private ArrayCopyMethod copy_method;
 
        public ArrayType (DataType element_type, int rank, SourceReference? source_reference) {
                this.element_type = element_type;
@@ -84,6 +85,8 @@ public class Vala.ArrayType : ReferenceType {
                                return null;
                        }
                        return get_resize_method ();
+               } else if (member_name == "copy") {
+                       return get_copy_method ();
                }
                return null;
        }
@@ -145,6 +148,19 @@ public class Vala.ArrayType : ReferenceType {
                return move_method;
        }
 
+       private ArrayCopyMethod get_copy_method () {
+               if (copy_method == null) {
+                       copy_method = new ArrayCopyMethod (source_reference);
+
+                       copy_method.return_type = this.copy ();
+                       copy_method.return_type.value_owned = true;
+                       copy_method.access = SymbolAccessibility.PUBLIC;
+
+                       copy_method.set_attribute_string ("CCode", "cname", "_vala_array_copy");
+               }
+               return copy_method;
+       }
+
        public override DataType copy () {
                var result = new ArrayType (element_type.copy (), rank, source_reference);
                result.value_owned = value_owned;


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