changeset 29251:30a5e691dd9f

allow unwind_action functions to be empty and set after object creation * unwind-prot.h (unwind_action::unwind_action): New overload that allows unwind_action to be created without a valid function. (unwind_action::set): New functions. Allow action to be changed or set to an invalid function. (unwind_action::run): New function. Skip action if it is invalid. (unwind_action::~unwind_action): Use run to execute action.
author John W. Eaton <jwe@octave.org>
date Sun, 03 Jan 2021 14:37:13 -0500
parents 3ad713d82d18
children e274759bcb12
files liboctave/util/action-container.h liboctave/util/unwind-prot.h
diffstat 2 files changed, 30 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/util/action-container.h	Sun Jan 03 20:19:04 2021 +0100
+++ b/liboctave/util/action-container.h	Sun Jan 03 14:37:13 2021 -0500
@@ -70,6 +70,9 @@
     {
     public:
 
+      // FIXME: Do we need to apply std::forward to the arguments to
+      // std::bind here?
+
       template <typename F, typename... Args>
       fcn_elem (F&& fcn, Args&&... args)
         : m_fcn (std::bind (fcn, args...))
--- a/liboctave/util/unwind-prot.h	Sun Jan 03 20:19:04 2021 +0100
+++ b/liboctave/util/unwind-prot.h	Sun Jan 03 14:37:13 2021 -0500
@@ -162,10 +162,15 @@
   // called immediately after the object is constructed instead of at
   // the end of the current scope.
 
-  class unwind_action
+  class OCTAVE_API unwind_action
   {
   public:
 
+    unwind_action (void) : m_fcn () { }
+
+    // FIXME: Do we need to apply std::forward to the arguments to
+    // std::bind here?
+
     template <typename F, typename... Args>
     unwind_action (F&& fcn, Args&&... args)
       : m_fcn (std::bind (fcn, args...))
@@ -177,7 +182,27 @@
 
     unwind_action& operator = (const unwind_action&) = delete;
 
-    ~unwind_action (void) { m_fcn (); }
+    ~unwind_action (void) { run (); }
+
+    // FIXME: Do we need to apply std::forward to the arguments to
+    // std::bind here?
+
+    template <typename F, typename... Args>
+    void set (F&& fcn, Args&&... args)
+    {
+      m_fcn = std::bind (fcn, args...);
+    }
+
+    void set (void) { m_fcn = nullptr; }
+
+    void run (void)
+    {
+      if (m_fcn)
+        m_fcn ();
+
+      // Invalidate so action won't run again when object is deleted.
+      set ();
+    }
 
   private: