GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
defun.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2017 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 <http://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 "symtab.h"
46 #include "interpreter.h"
47 #include "variables.h"
48 #include "parse.h"
49 
50 // Print the usage part of the doc string of FCN (user-defined or DEFUN).
51 void
53 {
55  if (cur)
56  print_usage (cur->name ());
57  else
58  error ("print_usage: invalid function");
59 }
60 
61 void
63 {
64  feval ("print_usage", octave_value (name), 0);
65 }
66 
67 void
68 check_version (const std::string& version, const std::string& fcn)
69 {
70  if (version != OCTAVE_API_VERSION)
71  {
72  error ("API version %s found in .oct file function '%s'\n"
73  " does not match the running Octave (API version %s)\n"
74  " this can lead to incorrect results or other failures\n"
75  " you can fix this problem by recompiling this .oct file",
76  version.c_str (), fcn.c_str (), OCTAVE_API_VERSION);
77  }
78 }
79 
80 // Install variables and functions in the symbol tables.
81 
82 void
84  const std::string& file, const std::string& doc,
85  bool /* can_hide_function -- not yet implemented */)
86 {
87  octave_value fcn (new octave_builtin (f, name, file, doc));
88 
90 }
91 
92 void
94  const octave::dynamic_library& shl, const std::string& doc,
95  bool relative)
96 {
97  octave_dld_function *fcn = new octave_dld_function (f, shl, name, doc);
98 
99  if (relative)
100  fcn->mark_relative ();
101 
102  octave_value fval (fcn);
103 
105 }
106 
107 void
108 install_mex_function (void *fptr, bool fmex, const std::string& name,
109  const octave::dynamic_library& shl, bool relative)
110 {
111  octave_mex_function *fcn = new octave_mex_function (fptr, fmex, shl, name);
112 
113  if (relative)
114  fcn->mark_relative ();
115 
116  octave_value fval (fcn);
117 
119 }
120 
121 void
123 {
125 }
126 
129 {
131 
133  if (curr_fcn)
134  {
135  if (curr_fcn->is_dld_function ())
136  {
138  = dynamic_cast<octave_dld_function *> (curr_fcn);
139  retval = dld->get_shlib ();
140  }
141  else if (curr_fcn->is_mex_function ())
142  {
144  = dynamic_cast<octave_mex_function *> (curr_fcn);
145  retval = mex->get_shlib ();
146  }
147  }
148 
149  return retval;
150 }
151 
152 bool defun_isargout (int nargout, int iout)
153 {
154  const std::list<octave_lvalue> *lvalue_list
156  if (iout >= std::max (nargout, 1))
157  return false;
158  else if (lvalue_list)
159  {
160  int k = 0;
161  for (std::list<octave_lvalue>::const_iterator p = lvalue_list->begin ();
162  p != lvalue_list->end (); p++)
163  {
164  if (k == iout)
165  return ! p->is_black_hole ();
166  k += p->numel ();
167  if (k > iout)
168  break;
169  }
170 
171  return true;
172  }
173  else
174  return true;
175 }
176 
177 void defun_isargout (int nargout, int nout, bool *isargout)
178 {
179  const std::list<octave_lvalue> *lvalue_list
181 
182  if (lvalue_list)
183  {
184  int k = 0;
185  for (std::list<octave_lvalue>::const_iterator p = lvalue_list->begin ();
186  p != lvalue_list->end () && k < nout; p++)
187  {
188  if (p->is_black_hole ())
189  isargout[k++] = false;
190  else
191  {
192  int l = std::min (k + p->numel (),
193  static_cast<octave_idx_type> (nout));
194  while (k < l)
195  isargout[k++] = true;
196  }
197  }
198  }
199  else
200  for (int i = 0; i < nout; i++)
201  isargout[i] = true;
202 
203  for (int i = std::max (nargout, 1); i < nout; i++)
204  isargout[i] = false;
205 }
static void install_built_in_function(const std::string &name, const octave_value &fcn)
Definition: symtab.h:1644
virtual bool is_mex_function(void) const
Definition: ov-base.h:469
Definition: mex.cc:2061
For example cd octave end example noindent changes the current working directory to file
Definition: dirfns.cc:120
void print_usage(void)
Definition: defun.cc:52
octave_value_list(* fcn)(const octave_value_list &, int)
Definition: ov-builtin.h:47
void install_mex_function(void *fptr, bool fmex, const std::string &name, const octave::dynamic_library &shl, bool relative)
Definition: defun.cc:108
void install_builtin_function(octave_builtin::fcn f, const std::string &name, const std::string &file, const std::string &doc, bool)
Definition: defun.cc:83
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &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 F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE const F77_DBLE * f
for large enough k
Definition: lu.cc:606
void mark_relative(void)
Definition: ov-fcn.h:159
void error(const char *fmt,...)
Definition: error.cc:570
std::string name(void) const
Definition: ov-fcn.h:163
static octave_function * current(void)
Definition: call-stack.h:108
#define OCTAVE_API_VERSION
Definition: version.h:45
octave_function * fcn
Definition: ov-class.cc:1743
bool defun_isargout(int nargout, int iout)
Definition: defun.cc:152
OCTAVE_EXPORT octave_value_list any number nd example oindent prints the prompt xample Pick a any number!nd example oindent and waits for the user to enter a value The string entered by the user is evaluated as an so it may be a literal a variable name
Definition: input.cc:871
octave::dynamic_library get_current_shlib(void)
Definition: defun.cc:128
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:935
static const std::list< octave_lvalue > * curr_lvalue_list
Definition: ov-builtin.h:94
octave_value retval
Definition: data.cc:6294
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:93
virtual bool is_dld_function(void) const
Definition: ov-base.h:467
feval(ar{f}, 1) esult
Definition: oct-parse.cc:8829
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:228
void alias_builtin(const std::string &alias, const std::string &name)
Definition: defun.cc:122
static void alias_built_in_function(const std::string &alias, const std::string &name)
Definition: symtab.h:1831
void check_version(const std::string &version, const std::string &fcn)
Definition: defun.cc:68
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
p
Definition: lu.cc:138
octave::dynamic_library get_shlib(void) const
Definition: ov-dld-fcn.h:76
FloatComplex(* fptr)(const FloatComplex &, float, int, octave_idx_type &)
Definition: lo-specfun.cc:1724
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:854
octave::dynamic_library get_shlib(void) const
Definition: ov-mex-fcn.h:89
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:205
octave_value_list fval
Definition: ov-struct.cc:2060