changeset 27735:8600f5ea1ec1

use std::shared_ptr to manage stream rep * oct-stream.h, oct-stream.cc (base_stream::m_count): Delete member variable and all uses. (class graphics_toolkit): Use default copy constructor, assignment operator, and destructor. (base_stream::m_rep): Declare as std::shared_ptr<base_stream> instead of using bare pointer.
author John W. Eaton <jwe@octave.org>
date Thu, 21 Nov 2019 19:23:20 -0600
parents a9780be9ecbc
children bd80e14f268a
files libinterp/corefcn/oct-stream.cc libinterp/corefcn/oct-stream.h
diffstat 2 files changed, 10 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/oct-stream.cc	Thu Nov 21 18:41:08 2019 -0500
+++ b/libinterp/corefcn/oct-stream.cc	Thu Nov 21 19:23:20 2019 -0600
@@ -6048,40 +6048,6 @@
     error (who, std::string ("stream not open for ") + rw);
   }
 
-  stream::stream (base_stream *bs)
-    : m_rep (bs)
-  { }
-
-  stream::~stream (void)
-  {
-    if (m_rep && --m_rep->m_count == 0)
-      delete m_rep;
-  }
-
-  stream::stream (const stream& s)
-    : m_rep (s.m_rep)
-  {
-    if (m_rep)
-      m_rep->m_count++;
-  }
-
-  stream&
-  stream::operator = (const stream& s)
-  {
-    if (m_rep != s.m_rep)
-      {
-        if (m_rep && --m_rep->m_count == 0)
-          delete m_rep;
-
-        m_rep = s.m_rep;
-
-        if (m_rep)
-          m_rep->m_count++;
-      }
-
-    return *this;
-  }
-
   int
   stream::flush (void)
   {
--- a/libinterp/corefcn/oct-stream.h	Thu Nov 21 18:41:08 2019 -0500
+++ b/libinterp/corefcn/oct-stream.h	Thu Nov 21 19:23:20 2019 -0600
@@ -29,6 +29,7 @@
 #include <iosfwd>
 #include <list>
 #include <map>
+#include <memory>
 #include <string>
 
 // These only appear as reference arguments or return values.
@@ -41,7 +42,6 @@
 
 #include "data-conv.h"
 #include "mach-info.h"
-#include "oct-refcount.h"
 
 namespace octave
 {
@@ -69,7 +69,7 @@
     base_stream (std::ios::openmode arg_md = std::ios::in | std::ios::out,
                  mach_info::float_format ff = mach_info::native_float_format (),
                  const std::string& encoding = "utf-8")
-      : m_count (1), m_mode (arg_md), m_flt_fmt (ff), m_encoding (encoding),
+      : m_mode (arg_md), m_flt_fmt (ff), m_encoding (encoding),
         m_fail (false), m_open_state (true), m_errmsg ()
     { }
 
@@ -170,9 +170,6 @@
 
   private:
 
-    // A reference count.
-    refcount<octave_idx_type> m_count;
-
     // The permission bits for the file.  Should be some combination of
     // std::ios::open_mode bits.
     int m_mode;
@@ -252,13 +249,14 @@
   {
   public:
 
-    stream (base_stream *bs = nullptr);
-
-    ~stream (void);
+    // BS must be allocated with new or nullptr.
+    stream (base_stream *bs = nullptr) : m_rep (bs) { }
 
-    stream (const stream&);
+    stream (const stream&) = default;
 
-    stream& operator = (const stream&);
+    stream& operator = (const stream&) = default;
+
+    ~stream (void) = default;
 
     int flush (void);
 
@@ -355,7 +353,7 @@
 
     int file_number (void) { return m_rep ? m_rep->file_number () : -1; }
 
-    bool is_valid (void) const { return (m_rep != nullptr); }
+    bool is_valid (void) const { return bool (m_rep); }
 
     bool ok (void) const { return m_rep && m_rep->ok (); }
 
@@ -389,7 +387,7 @@
   private:
 
     // The actual representation of this stream.
-    base_stream *m_rep;
+    std::shared_ptr<base_stream> m_rep;
 
     bool stream_ok (bool clear = true) const
     {