GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
defun.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <string>
31 
32 #include "defun.h"
33 #include "dynamic-ld.h"
34 #include "error.h"
35 #include "help.h"
36 #include "ov.h"
37 #include "ov-builtin.h"
38 #include "ov-dld-fcn.h"
39 #include "ov-fcn.h"
40 #include "ov-mex-fcn.h"
41 #include "ov-usr-fcn.h"
42 #include "ovl.h"
43 #include "oct-lvalue.h"
44 #include "pager.h"
45 #include "interpreter-private.h"
46 #include "interpreter.h"
47 #include "symtab.h"
48 #include "variables.h"
49 #include "parse.h"
50 
51 // Print the usage part of the doc string of FCN (user-defined or DEFUN).
52 void
54 {
56 
57  const octave_function *cur = tw.current_function ();
58 
59  if (cur)
60  print_usage (cur->name ());
61  else
62  error ("print_usage: invalid function");
63 }
64 
65 void
66 print_usage (const std::string& name)
67 {
68  octave::feval ("print_usage", octave_value (name), 0);
69 }
70 
71 void
72 check_version (const std::string& version, const std::string& fcn)
73 {
74  if (version != OCTAVE_API_VERSION)
75  {
76  error ("API version %s found in .oct file function '%s'\n"
77  " does not match the running Octave (API version %s)\n"
78  " this can lead to incorrect results or other failures\n"
79  " you can fix this problem by recompiling this .oct file",
80  version.c_str (), fcn.c_str (), OCTAVE_API_VERSION);
81  }
82 }
83 
84 // Install variables and functions in the symbol tables.
85 
86 void
88  const octave::dynamic_library& shl, const std::string& doc,
89  bool relative)
90 {
91  octave_dld_function *fcn = new octave_dld_function (f, shl, name, doc);
92 
93  if (relative)
94  fcn->mark_relative ();
95 
96  octave_value fval (fcn);
97 
98  octave::symbol_table& symtab
99  = octave::__get_symbol_table__ ("install_dld_function");
100 
101  symtab.install_built_in_function (name, fval);
102 }
103 
104 void
106  const octave::dynamic_library& shl, const std::string& doc,
107  bool relative)
108 {
109  octave_dld_function *fcn = new octave_dld_function (m, shl, name, doc);
110 
111  if (relative)
112  fcn->mark_relative ();
113 
114  octave_value fval (fcn);
115 
116  octave::symbol_table& symtab
117  = octave::__get_symbol_table__ ("install_dld_function");
118 
119  symtab.install_built_in_function (name, fval);
120 }
121 
122 void
123 install_mex_function (void *fptr, bool fmex, const std::string& name,
124  const octave::dynamic_library& shl, bool relative)
125 {
126  octave_mex_function *fcn = new octave_mex_function (fptr, fmex, shl, name);
127 
128  if (relative)
129  fcn->mark_relative ();
130 
131  octave_value fval (fcn);
132 
133  octave::symbol_table& symtab
134  = octave::__get_symbol_table__ ("install_mex_function");
135 
136  symtab.install_built_in_function (name, fval);
137 }
138 
141 {
143 
144  octave::tree_evaluator& tw = octave::__get_evaluator__ ("get_current_shlib");
145 
146  octave_function *curr_fcn = tw.current_function ();
147 
148  if (curr_fcn)
149  {
150  if (curr_fcn->is_dld_function ())
151  {
153  = dynamic_cast<octave_dld_function *> (curr_fcn);
154  retval = dld->get_shlib ();
155  }
156  else if (curr_fcn->is_mex_function ())
157  {
159  = dynamic_cast<octave_mex_function *> (curr_fcn);
160  retval = mex->get_shlib ();
161  }
162  }
163 
164  return retval;
165 }
Definition: mex.cc:2130
void install_built_in_function(const std::string &name, const octave_value &fcn)
Definition: symtab.cc:401
octave_function * current_function(bool skip_first=false) const
Definition: pt-eval.cc:1907
virtual bool is_mex_function(void) const
Definition: ov-base.h:479
virtual bool is_dld_function(void) const
Definition: ov-base.h:477
octave_value_list(* fcn)(const octave_value_list &, int)
Definition: ov-builtin.h:63
octave_value_list(* meth)(octave::interpreter &, const octave_value_list &, int)
Definition: ov-builtin.h:60
octave::dynamic_library get_shlib(void) const
Definition: ov-dld-fcn.h:96
void mark_relative(void)
Definition: ov-fcn.h:210
std::string name(void) const
Definition: ov-fcn.h:214
void install_dld_function(octave_dld_function::fcn f, const std::string &name, const octave::dynamic_library &shl, const std::string &doc, bool relative)
Definition: defun.cc:87
void install_mex_function(void *fptr, bool fmex, const std::string &name, const octave::dynamic_library &shl, bool relative)
Definition: defun.cc:123
void print_usage(void)
Definition: defun.cc:53
void check_version(const std::string &version, const std::string &fcn)
Definition: defun.cc:72
octave::dynamic_library get_current_shlib(void)
Definition: defun.cc:140
void error(const char *fmt,...)
Definition: error.cc:968
QString name
F77_RET_T const F77_DBLE const F77_DBLE * f
T octave_idx_type m
Definition: mx-inlines.cc:773
FloatComplex(* fptr)(const FloatComplex &, float, int, octave_idx_type &)
Definition: lo-specfun.cc:1128
tree_evaluator & __get_evaluator__(const std::string &who)
octave_value_list feval(const char *name, const octave_value_list &args, int nargout)
Evaluate an Octave function (built-in or interpreted) and return the list of result values.
Definition: oct-parse.cc:9580
symbol_table & __get_symbol_table__(const std::string &who)
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811
#define OCTAVE_API_VERSION
Definition: version.in.h:49