changeset 12560:d6ad4ed57dda

Add onCleanup function to documentation.
author Rik <octave@nomad.inbox5.com>
date Thu, 31 Mar 2011 20:24:06 -0700
parents a12d7f53c2ab
children 0ade5992e374
files doc/ChangeLog doc/interpreter/errors.txi doc/interpreter/octave.texi src/ChangeLog src/DLD-FUNCTIONS/onCleanup.cc
diffstat 5 files changed, 45 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/doc/ChangeLog	Thu Mar 31 10:41:32 2011 -0700
+++ b/doc/ChangeLog	Thu Mar 31 20:24:06 2011 -0700
@@ -1,3 +1,8 @@
+2011-03-31  Rik  <octave@nomad.inbox5.com>
+
+	* interpreter/errors.txi, interpreter/octave.texi: Add onCleanup
+	function to documentation.
+
 2011-03-31  Rik  <octave@nomad.inbox5.com>
 
 	* interpreter/contrib.txi: Add recommended format for commit messages
--- a/doc/interpreter/errors.txi	Thu Mar 31 10:41:32 2011 -0700
+++ b/doc/interpreter/errors.txi	Thu Mar 31 20:24:06 2011 -0700
@@ -49,6 +49,7 @@
 @menu
 * Raising Errors::
 * Catching Errors::
+* Recovering From Errors::
 @end menu
 
 @node Raising Errors
@@ -212,6 +213,38 @@
 
 @DOCSTRING(errno_list)
 
+@node Recovering From Errors
+@subsection Recovering From Errors
+
+Octave provides several ways of recovering from errors.  There are
+@code{try}/@code{catch} blocks, 
+@code{unwind_protect}/@code{unwind_protect_cleanup} blocks, 
+and finally the @code{onCleanup} command.
+
+The @code{onCleanup} command associates an ordinary Octave variable (the
+trigger) with an arbitrary function (the action).  Whenever the Octave variable
+ceases to exist---whether due to a function return, an error, or simply because
+the variable has been removed with @code{clear}---then the assigned function
+is executed.
+
+The function can do anything necessary for cleanup such as closing open file
+handles, printing an error message, or restoring global variables to their
+initial values.  The last example is a very convenient idiom for Octave code.
+For example:
+
+@example
+@group
+function rand42
+  old_state = rand ('state');
+  restore_state = onCleanup (@@() rand ('state', old_state);
+  rand ('state', 42);
+  @dots{}
+endfunction  # rand generator state restored by onCleanup
+@end group
+@end example
+
+@DOCSTRING(onCleanup)
+
 @node Handling Warnings
 @section Handling Warnings
 
--- a/doc/interpreter/octave.texi	Thu Mar 31 10:41:32 2011 -0700
+++ b/doc/interpreter/octave.texi	Thu Mar 31 20:24:06 2011 -0700
@@ -443,6 +443,7 @@
 
 * Raising Errors::
 * Catching Errors::
+* Recovering From Errors::
 
 Handling Warnings
 
--- a/src/ChangeLog	Thu Mar 31 10:41:32 2011 -0700
+++ b/src/ChangeLog	Thu Mar 31 20:24:06 2011 -0700
@@ -1,3 +1,8 @@
+2011-03-31  Rik  <octave@nomad.inbox5.com>
+
+	* DLD-FUNCTIONS/onCleanup.cc: Remove non-existent @seealso link in
+	docstring.
+
 2011-03-31  Rik  <octave@nomad.inbox5.com>
 
 	* DLD-FUNCTIONS/quadcc.cc: Add reference to original paper in docstring.
--- a/src/DLD-FUNCTIONS/onCleanup.cc	Thu Mar 31 10:41:32 2011 -0700
+++ b/src/DLD-FUNCTIONS/onCleanup.cc	Thu Mar 31 20:24:06 2011 -0700
@@ -257,7 +257,7 @@
 elements) or returned from a function, @var{action} will be executed after\n\
 clearing the last copy of the object.  Note that if multiple local onCleanup\n\
 variables are created, the order in which they are called is unspecified.\n\
-@seealso{unwind_protect}\n\
+For similar functionality @xref{The @code{unwind_protect} Statement}.\n\
 @end deftypefn")
 {
   octave_value retval;