changeset 26655:cca0548f7b86

refactor calling object destructor * ov.h (octave_value::call_object_destructor): New function. * ov-base.h (octave_base_value::call_object_destructor): New virtual function. * ov-oncleanup.h, ov-oncleanup.cc (octave_oncleanup::call_object_destructor): New function. (octave_oncleanup::~octave_oncleanup): Use it.
author John W. Eaton <jwe@octave.org>
date Sat, 26 Jan 2019 15:03:57 +0000
parents c54953342372
children ab3babe4ea25
files libinterp/octave-value/ov-base.h libinterp/octave-value/ov-oncleanup.cc libinterp/octave-value/ov-oncleanup.h libinterp/octave-value/ov.h
diffstat 4 files changed, 60 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/octave-value/ov-base.h	Sat Jan 26 14:56:14 2019 +0000
+++ b/libinterp/octave-value/ov-base.h	Sat Jan 26 15:03:57 2019 +0000
@@ -719,6 +719,8 @@
 
   virtual bool islocked (void) const { return false; }
 
+  virtual void call_object_destructor (void) { }
+
   virtual octave_value dump (void) const;
 
   // Standard mappers.  Register new ones here.
--- a/libinterp/octave-value/ov-oncleanup.cc	Sat Jan 26 14:56:14 2019 +0000
+++ b/libinterp/octave-value/ov-oncleanup.cc	Sat Jan 26 15:03:57 2019 +0000
@@ -64,49 +64,7 @@
 
 octave_oncleanup::~octave_oncleanup (void)
 {
-  if (fcn.is_undefined ())
-    return;
-
-  octave::unwind_protect frame;
-
-  // Clear interrupts.
-  frame.protect_var (octave_interrupt_state);
-  octave_interrupt_state = 0;
-
-  // Disallow quit().
-  frame.protect_var (quit_allowed);
-  quit_allowed = false;
-
-  interpreter_try (frame);
-
-  try
-    {
-      // Run the actual code.
-      octave::feval (fcn);
-    }
-  catch (const octave::interrupt_exception&)
-    {
-      octave::interpreter::recover_from_exception ();
-
-      warning ("onCleanup: interrupt occurred in cleanup action");
-    }
-  catch (const octave::execution_exception&)
-    {
-      std::string msg = last_error_message ();
-      warning ("onCleanup: error caught while executing cleanup function:\n%s\n",
-               msg.c_str ());
-
-    }
-  catch (const octave::exit_exception&)
-    {
-      // This shouldn't happen since we disabled quit above.
-      warning ("onCleanup: exit disabled while executing cleanup function");
-    }
-  catch (...) // Yes, the black hole.  We're in a d-tor.
-    {
-      // This shouldn't happen, in theory.
-      warning ("onCleanup: internal error: unhandled exception in cleanup action");
-    }
+  call_object_destructor ();
 }
 
 octave_scalar_map
@@ -183,6 +141,57 @@
   os << ')';
 }
 
+void
+octave_oncleanup::call_object_destructor (void)
+{
+  if (fcn.is_undefined ())
+    return;
+
+  octave_value the_fcn = fcn;
+  fcn = octave_value ();
+
+  octave::unwind_protect frame;
+
+  // Clear interrupts.
+  frame.protect_var (octave_interrupt_state);
+  octave_interrupt_state = 0;
+
+  // Disallow quit().
+  frame.protect_var (quit_allowed);
+  quit_allowed = false;
+
+  interpreter_try (frame);
+
+  try
+    {
+      // Run the actual code.
+      octave::feval (the_fcn);
+    }
+  catch (const octave::interrupt_exception&)
+    {
+      octave::interpreter::recover_from_exception ();
+
+      warning ("onCleanup: interrupt occurred in cleanup action");
+    }
+  catch (const octave::execution_exception&)
+    {
+      std::string msg = last_error_message ();
+      warning ("onCleanup: error caught while executing cleanup function:\n%s\n",
+               msg.c_str ());
+
+    }
+  catch (const octave::exit_exception&)
+    {
+      // This shouldn't happen since we disabled quit above.
+      warning ("onCleanup: exit disabled while executing cleanup function");
+    }
+  catch (...) // Yes, the black hole.  We're in a d-tor.
+    {
+      // This shouldn't happen, in theory.
+      warning ("onCleanup: internal error: unhandled exception in cleanup action");
+    }
+}
+
 DEFUN (onCleanup, args, ,
        doc: /* -*- texinfo -*-
 @deftypefn {} {@var{obj} =} onCleanup (@var{function})
--- a/libinterp/octave-value/ov-oncleanup.h	Sat Jan 26 14:56:14 2019 +0000
+++ b/libinterp/octave-value/ov-oncleanup.h	Sat Jan 26 15:03:57 2019 +0000
@@ -35,7 +35,8 @@
 class octave_oncleanup : public octave_base_value
 {
 public:
-  octave_oncleanup (void) : fcn () { }
+
+  octave_oncleanup (void) = default;
 
   octave_oncleanup (const octave_value& fcn);
 
@@ -85,6 +86,8 @@
 
   void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
 
+  void call_object_destructor (void);
+
 private:
 
   DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
--- a/libinterp/octave-value/ov.h	Sat Jan 26 14:56:14 2019 +0000
+++ b/libinterp/octave-value/ov.h	Sat Jan 26 15:03:57 2019 +0000
@@ -1326,6 +1326,8 @@
 
   bool islocked (void) const { return rep->islocked (); }
 
+  void call_object_destructor (void) { return rep->call_object_destructor (); }
+
   octave_value dump (void) const { return rep->dump (); }
 
 #define MAPPER_FORWARD(F) \