GNU Octave  4.4.1
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-2018 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <sstream>
28 #include <iostream>
29 #include <string>
30 
31 #include "call-stack.h"
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 "pt-eval.h"
46 #include "interpreter-private.h"
47 #include "interpreter.h"
48 #include "symtab.h"
49 #include "variables.h"
50 #include "parse.h"
51 
52 // Print the usage part of the doc string of FCN (user-defined or DEFUN).
53 void
55 {
57 
58  const octave_function *cur = cs.current ();
59 
60  if (cur)
61  print_usage (cur->name ());
62  else
63  error ("print_usage: invalid function");
64 }
65 
66 void
68 {
69  octave::feval ("print_usage", octave_value (name), 0);
70 }
71 
72 void
73 check_version (const std::string& version, const std::string& fcn)
74 {
75  if (version != OCTAVE_API_VERSION)
76  {
77  error ("API version %s found in .oct file function '%s'\n"
78  " does not match the running Octave (API version %s)\n"
79  " this can lead to incorrect results or other failures\n"
80  " you can fix this problem by recompiling this .oct file",
81  version.c_str (), fcn.c_str (), OCTAVE_API_VERSION);
82  }
83 }
84 
85 // Install variables and functions in the symbol tables.
86 
87 void
89  const std::string& file, const std::string& doc,
90  bool /* can_hide_function -- not yet implemented */)
91 {
92  octave_value fcn (new octave_builtin (f, name, file, doc));
93 
94  octave::symbol_table& symtab
95  = octave::__get_symbol_table__ ("install_builtin_function");
96 
98 }
99 
100 void
102  const std::string& file, const std::string& doc,
103  bool /* can_hide_function -- not yet implemented */)
104 {
105  octave_value fcn (new octave_builtin (m, name, file, doc));
106 
107  octave::symbol_table& symtab
108  = octave::__get_symbol_table__ ("install_builtin_function");
109 
111 }
112 
113 void
115  const octave::dynamic_library& shl, const std::string& doc,
116  bool relative)
117 {
118  octave_dld_function *fcn = new octave_dld_function (f, shl, name, doc);
119 
120  if (relative)
121  fcn->mark_relative ();
122 
123  octave_value fval (fcn);
124 
125  octave::symbol_table& symtab
126  = octave::__get_symbol_table__ ("install_dld_function");
127 
128  symtab.install_built_in_function (name, fval);
129 }
130 
131 void
133  const octave::dynamic_library& shl, const std::string& doc,
134  bool relative)
135 {
136  octave_dld_function *fcn = new octave_dld_function (m, shl, name, doc);
137 
138  if (relative)
139  fcn->mark_relative ();
140 
141  octave_value fval (fcn);
142 
143  octave::symbol_table& symtab
144  = octave::__get_symbol_table__ ("install_dld_function");
145 
146  symtab.install_built_in_function (name, fval);
147 }
148 
149 void
150 install_mex_function (void *fptr, bool fmex, const std::string& name,
151  const octave::dynamic_library& shl, bool relative)
152 {
153  octave_mex_function *fcn = new octave_mex_function (fptr, fmex, shl, name);
154 
155  if (relative)
156  fcn->mark_relative ();
157 
158  octave_value fval (fcn);
159 
160  octave::symbol_table& symtab
161  = octave::__get_symbol_table__ ("install_mex_function");
162 
163  symtab.install_built_in_function (name, fval);
164 }
165 
166 void
168 {
169  octave::symbol_table& symtab = octave::__get_symbol_table__ ("alias_builtin");
170 
171  symtab.alias_built_in_function (alias, name);
172 }
173 
174 void
176 {
177  octave::symbol_table& symtab
178  = octave::__get_symbol_table__ ("install_builtin_dispatch");
179 
180  symtab.install_built_in_dispatch (name, klass);
181 }
182 
185 {
187 
188  octave::call_stack& cs = octave::__get_call_stack__ ("get_current_shlib");
189 
190  octave_function *curr_fcn = cs.current ();
191 
192  if (curr_fcn)
193  {
194  if (curr_fcn->is_dld_function ())
195  {
197  = dynamic_cast<octave_dld_function *> (curr_fcn);
198  retval = dld->get_shlib ();
199  }
200  else if (curr_fcn->is_mex_function ())
201  {
203  = dynamic_cast<octave_mex_function *> (curr_fcn);
204  retval = mex->get_shlib ();
205  }
206  }
207 
208  return retval;
209 }
210 
211 bool
212 defun_isargout (int nargout, int iout)
213 {
214  octave::tree_evaluator& tw = octave::__get_evaluator__ ("defun_isargout");
215 
216  return tw.isargout (nargout, iout);
217 }
218 
219 void
220 defun_isargout (int nargout, int nout, bool *isargout)
221 {
222  octave::tree_evaluator& tw = octave::__get_evaluator__ ("defun_isargout");
223 
224  return tw.isargout (nargout, nout, isargout);
225 }
OCTINTERP_API octave_value_list feval(const std::string &name, const octave_value_list &args=octave_value_list(), int nargout=0)
void install_built_in_dispatch(const std::string &name, const std::string &klass)
Definition: symtab.h:491
Definition: mex.cc:2117
For example cd octave end example noindent changes the current working directory to file
Definition: dirfns.cc:124
void print_usage(void)
Definition: defun.cc:54
virtual bool is_dld_function(void) const
Definition: ov-base.h:479
void install_mex_function(void *fptr, bool fmex, const std::string &name, const octave::dynamic_library &shl, bool relative)
Definition: defun.cc:150
void install_builtin_function(octave_builtin::fcn f, const std::string &name, const std::string &file, const std::string &doc, bool)
Definition: defun.cc:88
void alias_built_in_function(const std::string &alias, const std::string &name)
Definition: symtab.h:474
void install_built_in_function(const std::string &name, const octave_value &fcn)
Definition: symtab.h:347
bool isargout(int nargout, int iout) const
Definition: pt-eval.cc:483
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE * f
void mark_relative(void)
Definition: ov-fcn.h:178
octave_value_list(* fcn)(const octave_value_list &, int)
Definition: ov-builtin.h:60
void error(const char *fmt,...)
Definition: error.cc:578
#define OCTAVE_API_VERSION
Definition: version.in.h:45
octave_function * fcn
Definition: ov-class.cc:1754
virtual bool is_mex_function(void) const
Definition: ov-base.h:481
octave::call_stack & cs
Definition: ov-class.cc:1752
bool defun_isargout(int nargout, int iout)
Definition: defun.cc:212
symbol_table & __get_symbol_table__(const std::string &who)
nd deftypefn *std::string name
Definition: sysdep.cc:647
octave::dynamic_library get_current_shlib(void)
Definition: defun.cc:184
OCTAVE_EXPORT octave_value_list return the number of command line arguments passed to Octave If called with the optional argument the function xample nargout(@histc)
Definition: ov-usr-fcn.cc:997
octave_value retval
Definition: data.cc:6246
octave_value_list(* meth)(octave::interpreter &, const octave_value_list &, int)
Definition: ov-builtin.h:57
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:114
octave_function * current(void) const
Definition: call-stack.h:97
void alias_builtin(const std::string &alias, const std::string &name)
Definition: defun.cc:167
void check_version(const std::string &version, const std::string &fcn)
Definition: defun.cc:73
FloatComplex(* fptr)(const FloatComplex &, float, int, octave_idx_type &)
Definition: lo-specfun.cc:1129
call_stack & __get_call_stack__(const std::string &who)
octave::dynamic_library get_shlib(void) const
Definition: ov-dld-fcn.h:93
std::string name(void) const
Definition: ov-fcn.h:182
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
tree_evaluator & __get_evaluator__(const std::string &who)
void install_builtin_dispatch(const std::string &name, const std::string &klass)
Definition: defun.cc:175