diff libinterp/corefcn/graphics.in.h @ 24252:f8c263f961c1

use shared_ptr to manage graphics_object and graphics_event objects * graphics.in.h, graphics.cc (base_graphics_object::count): Delete data member and all uses. (graphics_object::rep): Manage with shared_ptr. (class graphics_object): Use default constructor and destructor functions where possible. (base_graphics_event::count): Delete data member and all uses. (graphics_event::rep): Manage with shared_ptr. (class graphics_event): Use default constructor and destructor functions where possible. (graphics_event::create_callback_event, graphics_Event::create_fucntion_event, graphics_event::create_set_event): Don't set rep directly.
author John W. Eaton <jwe@octave.org>
date Wed, 15 Nov 2017 14:21:52 -0500
parents fdd67d871a72
children ca03c9f3fa4b
line wrap: on
line diff
--- a/libinterp/corefcn/graphics.in.h	Tue Nov 14 09:41:07 2017 +0100
+++ b/libinterp/corefcn/graphics.in.h	Wed Nov 15 14:21:52 2017 -0500
@@ -31,6 +31,7 @@
 #include <algorithm>
 #include <list>
 #include <map>
+#include <memory>
 #include <set>
 #include <sstream>
 #include <string>
@@ -2405,7 +2406,7 @@
 public:
   friend class graphics_object;
 
-  base_graphics_object (void) : count (1), toolkit_flag (false) { }
+  base_graphics_object (void) : toolkit_flag (false) { }
 
   // No copying!
 
@@ -2642,8 +2643,6 @@
   }
 
 protected:
-  // A reference count.
-  octave::refcount<int> count;
 
   // A flag telling whether this object is a valid object
   // in the backend context.
@@ -2653,35 +2652,16 @@
 class OCTINTERP_API graphics_object
 {
 public:
+
   graphics_object (void) : rep (new base_graphics_object ()) { }
 
-  graphics_object (base_graphics_object *new_rep)
-    : rep (new_rep) { }
-
-  graphics_object (const graphics_object& obj) : rep (obj.rep)
-  {
-    rep->count++;
-  }
-
-  graphics_object& operator = (const graphics_object& obj)
-  {
-    if (rep != obj.rep)
-      {
-        if (--rep->count == 0)
-          delete rep;
-
-        rep = obj.rep;
-        rep->count++;
-      }
-
-    return *this;
-  }
-
-  ~graphics_object (void)
-  {
-    if (--rep->count == 0)
-      delete rep;
-  }
+  graphics_object (base_graphics_object *new_rep) : rep (new_rep) { }
+
+  graphics_object (const graphics_object& obj) = default;
+
+  graphics_object& operator = (const graphics_object& obj) = default;
+
+  ~graphics_object (void) = default;
 
   void mark_modified (void) { rep->mark_modified (); }
 
@@ -2879,7 +2859,8 @@
   { rep->reset_default_properties (); }
 
 private:
-  base_graphics_object *rep;
+
+  std::shared_ptr<base_graphics_object> rep;
 };
 
 // ---------------------------------------------------------------------
@@ -5966,57 +5947,40 @@
 base_graphics_event
 {
 public:
+
   friend class graphics_event;
 
-  base_graphics_event (void) : count (1) { }
+  base_graphics_event (void) = default;
 
   virtual ~base_graphics_event (void) = default;
 
   virtual void execute (void) = 0;
-
-private:
-  octave::refcount<int> count;
 };
 
 class
 graphics_event
 {
 public:
+
   typedef void (*event_fcn) (void*);
 
-  graphics_event (void) : rep (nullptr) { }
-
-  graphics_event (const graphics_event& e) : rep (e.rep)
-  {
-    rep->count++;
-  }
-
-  ~graphics_event (void)
-  {
-    if (rep && --rep->count == 0)
-      delete rep;
-  }
-
-  graphics_event& operator = (const graphics_event& e)
-  {
-    if (rep != e.rep)
-      {
-        if (rep && --rep->count == 0)
-          delete rep;
-
-        rep = e.rep;
-        if (rep)
-          rep->count++;
-      }
-
-    return *this;
-  }
+  graphics_event (void) = default;
+
+  graphics_event (base_graphics_event *new_rep) : rep (new_rep) { }
+
+  graphics_event (const graphics_event& e) = default;
+
+  ~graphics_event (void) = default;
+
+  graphics_event& operator = (const graphics_event& e) = default;
 
   void execute (void)
-  { if (rep) rep->execute (); }
-
-  bool ok (void) const
-  { return (rep != nullptr); }
+  {
+    if (ok ())
+      rep->execute ();
+  }
+
+  bool ok (void) const { return (rep != nullptr); }
 
   static graphics_event
   create_callback_event (const graphics_handle& h,
@@ -6036,7 +6000,8 @@
                     const octave_value& value,
                     bool notify_toolkit = true);
 private:
-  base_graphics_event *rep;
+
+  std::shared_ptr <base_graphics_event> rep;
 };
 
 class OCTINTERP_API gh_manager