changeset 27595:18fea1c5b21a stable

doc: improve example of using global variables from oct-files. * examples/code/globaldemo.cc: Adapt file to the changes from cset b29904962d2d. * doc/interpreter/external.txi: Overhaul text to match the current implementation. With cset b29904962d2d a new way of accessing global variables via the interpreter's symbol table was introduced. Because of this, a recent question on the help mailing-list could not be sufficiently answered by pointing at the Octave documentation. https://lists.gnu.org/archive/html/help-octave/2019-10/msg00260.html
author Kai T. Ohlhus <k.ohlhus@gmail.com>
date Thu, 31 Oct 2019 14:48:22 +0900
parents 757a7119e319
children 92e829d0a63b f998e243fa78
files doc/interpreter/external.txi examples/code/globaldemo.cc
diffstat 2 files changed, 11 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/doc/interpreter/external.txi	Tue Oct 29 16:36:41 2019 -0700
+++ b/doc/interpreter/external.txi	Thu Oct 31 14:48:22 2019 +0900
@@ -876,11 +876,11 @@
 
 Global variables allow variables in the global scope to be accessed.  Global
 variables can be accessed within oct-files by using the support functions
-@w{@code{get_global_value}} and @w{@code{set_global_value}}.
-@w{@code{get_global_value}} takes two arguments, the first is a string
-representing the variable name to obtain.  The second argument is a boolean
-argument specifying what to do if no global variable of the desired name is
-found.  An example of the use of these two functions is
+@w{@code{global_varval}} and @w{@code{global_assign}} from the current
+interpreter's symbol table.  Both functions take as first argument a string
+representing the variable name to be obtained or assigned.  The second
+argument of @w{@code{global_assign}} is the value to be assigned.  An
+example of the use of these two functions is
 
 @example
 @EXAMPLEFILE(globaldemo.cc)
--- a/examples/code/globaldemo.cc	Tue Oct 29 16:36:41 2019 -0700
+++ b/examples/code/globaldemo.cc	Thu Oct 31 14:48:22 2019 +0900
@@ -1,6 +1,7 @@
 #include <octave/oct.h>
+#include <octave/interpreter.h>
 
-DEFUN_DLD (globaldemo, args, , "Global Demo")
+DEFMETHOD_DLD (globaldemo, interp, args, , "Global Demo")
 {
   if (args.length () != 1)
     print_usage ();
@@ -9,14 +10,16 @@
 
   std::string s = args(0).string_value ();
 
-  octave_value tmp = get_global_value (s, true);
+  octave::symbol_table& symtab = interp.get_symbol_table ();
+
+  octave_value tmp = symtab.global_varval (s);
 
   if (tmp.is_defined ())
     retval = tmp;
   else
     retval = "Global variable not found";
 
-  set_global_value ("a", 42.0);
+  symtab.global_assign ("a", 42.0);
 
   return retval;
 }