GNU Octave  8.1.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-2023 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 
52 
53 // Print the usage part of the doc string of FCN (user-defined or DEFUN).
54 void
56 {
58 
59  const octave_function *cur = tw.current_function ();
60 
61  if (cur)
62  print_usage (cur->name ());
63  else
64  error ("print_usage: invalid function");
65 }
66 
67 void
68 print_usage (const std::string& name)
69 {
70  feval ("print_usage", octave_value (name), 0);
71 }
72 
73 void
74 check_version (const std::string& version, const std::string& fcn)
75 {
76  if (version != OCTAVE_API_VERSION)
77  {
78  error ("API version %s found in .oct file function '%s'\n"
79  " does not match the running Octave (API version %s)\n"
80  " this can lead to incorrect results or other failures\n"
81  " you can fix this problem by recompiling this .oct file",
82  version.c_str (), fcn.c_str (), OCTAVE_API_VERSION);
83  }
84 }
85 
86 // Install variables and functions in the symbol tables.
87 
88 void
90  const dynamic_library& shl, const std::string& doc,
91  bool relative)
92 {
93  octave_dld_function *fcn = new octave_dld_function (f, shl, name, doc);
94 
95  if (relative)
96  fcn->mark_relative ();
97 
98  octave_value fval (fcn);
99 
100  symbol_table& symtab = __get_symbol_table__ ();
101 
102  symtab.install_built_in_function (name, fval);
103 }
104 
105 void
107  const dynamic_library& shl, const std::string& doc,
108  bool relative)
109 {
110  octave_dld_function *fcn = new octave_dld_function (m, shl, name, doc);
111 
112  if (relative)
113  fcn->mark_relative ();
114 
115  octave_value fval (fcn);
116 
117  symbol_table& symtab = __get_symbol_table__ ();
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 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  symbol_table& symtab = __get_symbol_table__ ();
134 
135  symtab.install_built_in_function (name, fval);
136 }
137 
140 {
141  dynamic_library retval;
142 
144 
145  octave_function *curr_fcn = tw.current_function ();
146 
147  if (curr_fcn)
148  {
149  if (curr_fcn->is_dld_function ())
150  {
152  = dynamic_cast<octave_dld_function *> (curr_fcn);
153  retval = dld->get_shlib ();
154  }
155  else if (curr_fcn->is_mex_function ())
156  {
158  = dynamic_cast<octave_mex_function *> (curr_fcn);
159  retval = mex->get_shlib ();
160  }
161  }
162 
163  return retval;
164 }
165 
OCTAVE_END_NAMESPACE(octave)
Definition: mex.cc:3412
virtual bool is_mex_function(void) const
Definition: ov-base.h:523
virtual bool is_dld_function(void) const
Definition: ov-base.h:521
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:97
void mark_relative(void)
Definition: ov-fcn.h:204
std::string name(void) const
Definition: ov-fcn.h:208
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:2519
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void install_dld_function(octave_dld_function::fcn f, const std::string &name, const dynamic_library &shl, const std::string &doc, bool relative)
Definition: defun.cc:89
dynamic_library get_current_shlib(void)
Definition: defun.cc:139
void install_mex_function(void *fptr, bool fmex, const std::string &name, const dynamic_library &shl, bool relative)
Definition: defun.cc:123
void print_usage(void)
Definition: defun.cc:55
void check_version(const std::string &version, const std::string &fcn)
Definition: defun.cc:74
void error(const char *fmt,...)
Definition: error.cc:979
symbol_table & __get_symbol_table__(void)
tree_evaluator & __get_evaluator__(void)
F77_RET_T const F77_DBLE const F77_DBLE * f
FloatComplex(* fptr)(const FloatComplex &, float, int, octave_idx_type &)
Definition: lo-specfun.cc:1102
T octave_idx_type m
Definition: mx-inlines.cc:773
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:10370