diff libinterp/octave-value/ov.h @ 27352:7335ebd4c798

define some move constructors and assignment operators * Array.h, dim-vector.h, str-vec.h, ov.h: Define move constructors and assignment operators for the Array, dim_vector, string_vector, and octave_value classes. * ovl.h: Use default move constructor and assignment operator.
author John W. Eaton <jwe@octave.org>
date Wed, 21 Aug 2019 16:00:37 -0400
parents 823b4bcf79fc
children b442ec6dda5c
line wrap: on
line diff
--- a/libinterp/octave-value/ov.h	Wed Aug 21 05:49:38 2019 -0400
+++ b/libinterp/octave-value/ov.h	Wed Aug 21 16:00:37 2019 -0400
@@ -302,11 +302,17 @@
   // Copy constructor.
 
   octave_value (const octave_value& a)
+    : rep (a.rep)
   {
-    rep = a.rep;
     rep->count++;
   }
 
+  octave_value (octave_value&& a)
+    : rep (a.rep)
+  {
+    a.rep = nullptr;
+  }
+
   // This should only be called for derived types.
 
   octave_base_value * clone (void) const;
@@ -318,7 +324,11 @@
 
   ~octave_value (void)
   {
-    if (--rep->count == 0)
+    // Because we define a move constructor and a move assignment
+    // operator, rep may be a nullptr here.  We should only need to
+    // protect the move assignment operator in a similar way.
+
+    if (rep && --rep->count == 0)
       delete rep;
   }
 
@@ -367,6 +377,24 @@
     return *this;
   }
 
+  octave_value& operator = (octave_value&& a)
+  {
+    // Because we define a move constructor and a move assignment
+    // operator, rep may be a nullptr here.  We should only need to
+    // protect the destructor in a similar way.
+
+    if (this != &a)
+      {
+        if (rep && --rep->count == 0)
+          delete rep;
+
+        rep = a.rep;
+        a.rep = nullptr;
+      }
+
+    return *this;
+  }
+
   octave_idx_type get_count (void) const { return rep->count; }
 
   octave_base_value::type_conv_info numeric_conversion_function (void) const