# HG changeset patch
# User John W. Eaton
# Date 1566417637 14400
# Wed Aug 21 16:00:37 2019 -0400
# Node ID a24338d5a788e1e23a42714c6a8208b646e2530b
# Parent 7593c4ac149c5a6408b54d2da2a19dc96ec5c481
move constructors and assignment operators
diff --git a/libinterp/octave-value/ovl.h b/libinterp/octave-value/ovl.h
--- a/libinterp/octave-value/ovl.h
+++ b/libinterp/octave-value/ovl.h
@@ -75,6 +75,9 @@ public:
octave_value_list (const octave_value_list& obj)
: m_data (obj.m_data), m_names (obj.m_names) { }
+ octave_value_list (octave_value_list&& obj)
+ : m_data (std::move (obj.m_data)), m_names (std::move (obj.m_names)) { }
+
// Concatenation constructor.
octave_value_list (const std::list&);
@@ -91,6 +94,17 @@ public:
return *this;
}
+ octave_value_list& operator = (octave_value_list&& obj)
+ {
+ if (this != &obj)
+ {
+ m_data = std::move (obj.m_data);
+ m_names = std::move (obj.m_names);
+ }
+
+ return *this;
+ }
+
Array array_value (void) const
{
Array retval;
diff --git a/liboctave/array/Array.h b/liboctave/array/Array.h
--- a/liboctave/array/Array.h
+++ b/liboctave/array/Array.h
@@ -300,11 +300,25 @@ public:
rep->count++;
}
+ Array (Array&& a)
+ : dimensions (std::move (a.dimensions)), rep (a.rep),
+ slice_data (a.slice_data), slice_len (a.slice_len)
+ {
+ a.rep = nullptr;
+ a.slice_data = nullptr;
+ a.slice_len = 0;
+ }
+
public:
virtual ~Array (void)
{
- if (--rep->count == 0)
+ // Because we define a move constructor and a move assignment
+ // operator, rep may be a nullptr here. We should not need to
+ // protect other instances. Deletion is the only valid operation on
+ // an object once it is moved.
+
+ if (rep && --rep->count == 0)
delete rep;
}
@@ -326,6 +340,23 @@ public:
return *this;
}
+ Array& operator = (Array&& a)
+ {
+ if (this != &a)
+ {
+ dimensions = std::move (a.dimensions);
+ rep = a.rep;
+ slice_data = a.slice_data;
+ slice_len = a.slice_len;
+
+ a.rep = nullptr;
+ a.slice_data = nullptr;
+ a.slice_len = 0;
+ }
+
+ return *this;
+ }
+
void fill (const T& val);
void clear (void);
diff --git a/liboctave/array/dim-vector.h b/liboctave/array/dim-vector.h
--- a/liboctave/array/dim-vector.h
+++ b/liboctave/array/dim-vector.h
@@ -258,7 +258,9 @@ public:
dim_vector (const dim_vector& dv) : rep (dv.rep)
{ OCTAVE_ATOMIC_INCREMENT (&(count ())); }
- // FIXME: Should be private, but required by array constructor for jit
+ dim_vector (dim_vector&& dv) : rep (dv.rep) { dv.rep = nullptr; }
+
+// FIXME: Should be private, but required by array constructor for jit
explicit dim_vector (octave_idx_type *r) : rep (r) { }
static dim_vector alloc (int n)
@@ -280,9 +282,25 @@ public:
return *this;
}
+ dim_vector& operator = (dim_vector&& dv)
+ {
+ if (&dv != this)
+ {
+ rep = dv.rep;
+ dv.rep = nullptr;
+ }
+
+ return *this;
+ }
+
~dim_vector (void)
{
- if (OCTAVE_ATOMIC_DECREMENT (&(count ())) == 0)
+ // Because we define a move constructor and a move assignment
+ // operator, rep may be a nullptr here. We should not need to
+ // protect other instances. Deletion is the only valid operation on
+ // an object once it is moved.
+
+ if (rep && OCTAVE_ATOMIC_DECREMENT (&(count ())) == 0)
freerep ();
}
diff --git a/liboctave/util/str-vec.h b/liboctave/util/str-vec.h
--- a/liboctave/util/str-vec.h
+++ b/liboctave/util/str-vec.h
@@ -47,6 +47,8 @@ public:
string_vector (const string_vector& s) : m_data (s.m_data) { }
+ string_vector (string_vector&& s) : m_data (std::move (s.m_data)) { }
+
//! Constructor for STL containers of std::string.
//!
//! Templated constructor for any template class with std::string as the
@@ -71,6 +73,14 @@ public:
return *this;
}
+ string_vector& operator = (string_vector&& s)
+ {
+ if (this != &s)
+ m_data = std::move (s.m_data);
+
+ return *this;
+ }
+
~string_vector (void) = default;
bool empty (void) const { return numel () == 0; }