changeset 26863:c589db954a4e

new functions for getting a functicon from an octave_value object These functions will replace extract_function. We need to have something similar but that returns an octave_value object that contains an octave_fcn_handle object, not a bare pointer to an octave_function object. * interpreter-private.h, interpreter-private.cc (get_function_handle): New functions.
author John W. Eaton <jwe@octave.org>
date Fri, 08 Mar 2019 02:49:52 +0000
parents ea4a36fd48b6
children 041caa61ed34
files libinterp/corefcn/interpreter-private.cc libinterp/corefcn/interpreter-private.h
diffstat 2 files changed, 63 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/interpreter-private.cc	Fri Feb 15 13:25:11 2019 +0100
+++ b/libinterp/corefcn/interpreter-private.cc	Fri Mar 08 02:49:52 2019 +0000
@@ -24,6 +24,7 @@
 #  include "config.h"
 #endif
 
+#include <list>
 #include <string>
 
 #include "bp-table.h"
@@ -39,6 +40,8 @@
 #include "load-path.h"
 #include "load-save.h"
 #include "oct-hist.h"
+#include "ov.h"
+#include "ov-fcn-inline.h"
 #include "pager.h"
 #include "symtab.h"
 
@@ -178,4 +181,45 @@
 
     return interp.get_gtk_manager ();
   }
+
+  octave_value
+  get_function_handle (octave::interpreter& interp, const octave_value& arg,
+                       const std::string& parameter_name)
+  {
+    std::list<std::string> parameter_names;
+    parameter_names.push_back (parameter_name);
+    return get_function_handle (interp, arg, parameter_names);
+  }
+
+  octave_value
+  get_function_handle (octave::interpreter& interp, const octave_value& arg,
+                       const std::list<std::string>& parameter_names)
+  {
+    if (arg.is_function_handle () || arg.is_inline_function ())
+      return arg;
+    else if (arg.is_string ())
+      {
+        std::string fstr = arg.string_value ();
+
+        if (fstr.empty ())
+          return octave_value ();
+
+        octave::symbol_table& symtab = interp.get_symbol_table ();
+
+        octave_value fcn = symtab.find_function (fstr);
+
+        if (fcn.is_defined ())
+          return fcn;
+
+        fcn = octave_value (new octave_fcn_inline (fstr, parameter_names));
+
+        if (fcn.is_defined ())
+          warning_with_id ("Octave:function-from-text",
+                           "get_function_handle: passing function body as text is discouraged; use an anonymous function instead");
+
+        return fcn;
+      }
+
+    return octave_value ();
+  }
 }
--- a/libinterp/corefcn/interpreter-private.h	Fri Feb 15 13:25:11 2019 +0100
+++ b/libinterp/corefcn/interpreter-private.h	Fri Mar 08 02:49:52 2019 +0000
@@ -25,6 +25,7 @@
 
 #include "octave-config.h"
 
+#include <list>
 #include <string>
 
 #include "symtab.h"
@@ -82,6 +83,24 @@
   extern cdef_manager& __get_cdef_manager__ (const std::string& who);
 
   extern gtk_manager& __get_gtk_manager__ (const std::string& who);
+
+
+  // Functions that could be methods in the interpreter class but maybe
+  // shouldn't be exposed as part of the public interface.
+
+  // Convert octave_value object ARG to be a function handle object.  It
+  // may be a function handle, inline function, the name of a function,
+  // or the text of an inline function that has the given argument names
+  // PARAMETER_NAMES.  The latter form is deprecated.
+
+  octave_value
+  get_function_handle (octave::interpreter& interp, const octave_value& arg,
+                       const std::string& parameter_name);
+
+  octave_value
+  get_function_handle (octave::interpreter& interp, const octave_value& arg,
+                       const std::list<std::string>& parameter_names
+                         = std::list<std::string> ());
 }
 
 #endif