GNU Octave  4.0.0
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-2015 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 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <sstream>
28 #include <iostream>
29 #include <string>
30 
31 #include "defun.h"
32 #include "dynamic-ld.h"
33 #include "error.h"
34 #include "help.h"
35 #include "ov.h"
36 #include "ov-builtin.h"
37 #include "ov-dld-fcn.h"
38 #include "ov-fcn.h"
39 #include "ov-mex-fcn.h"
40 #include "ov-usr-fcn.h"
41 #include "oct-obj.h"
42 #include "oct-lvalue.h"
43 #include "pager.h"
44 #include "symtab.h"
45 #include "toplev.h"
46 #include "variables.h"
47 #include "parse.h"
48 
49 // Print the usage part of the doc string of FCN (user-defined or DEFUN).
50 void
52 {
54  if (cur)
55  print_usage (cur->name ());
56  else
57  error ("print_usage: invalid function");
58 }
59 
60 void
61 print_usage (const std::string& name)
62 {
63  feval ("print_usage", octave_value (name), 0);
64 }
65 
66 void
67 check_version (const std::string& version, const std::string& fcn)
68 {
69  if (version != OCTAVE_API_VERSION)
70  {
71  error ("API version %s found in .oct file function '%s'\n"
72  " does not match the running Octave (API version %s)\n"
73  " this can lead to incorrect results or other failures\n"
74  " you can fix this problem by recompiling this .oct file",
75  version.c_str (), fcn.c_str (), OCTAVE_API_VERSION);
76  }
77 }
78 
79 // Install variables and functions in the symbol tables.
80 
81 void
83  const std::string& file, const std::string& doc,
84  bool /* can_hide_function -- not yet implemented */)
85 {
86  octave_value fcn (new octave_builtin (f, name, file, doc));
87 
89 }
90 
91 void
93  const octave_shlib& shl, const std::string& doc,
94  bool relative)
95 {
96  octave_dld_function *fcn = new octave_dld_function (f, shl, name, doc);
97 
98  if (relative)
99  fcn->mark_relative ();
100 
101  octave_value fval (fcn);
102 
104 }
105 
106 void
107 install_mex_function (void *fptr, bool fmex, const std::string& name,
108  const octave_shlib& shl, bool relative)
109 {
110  octave_mex_function *fcn = new octave_mex_function (fptr, fmex, shl, name);
111 
112  if (relative)
113  fcn->mark_relative ();
114 
115  octave_value fval (fcn);
116 
118 }
119 
120 void
121 alias_builtin (const std::string& alias, const std::string& name)
122 {
124 }
125 
128 {
129  octave_shlib retval;
130 
132  if (curr_fcn)
133  {
134  if (curr_fcn->is_dld_function ())
135  {
137  = dynamic_cast<octave_dld_function *> (curr_fcn);
138  retval = dld->get_shlib ();
139  }
140  else if (curr_fcn->is_mex_function ())
141  {
143  = dynamic_cast<octave_mex_function *> (curr_fcn);
144  retval = mex->get_shlib ();
145  }
146  }
147 
148  return retval;
149 }
150 
151 bool defun_isargout (int nargout, int iout)
152 {
153  const std::list<octave_lvalue> *lvalue_list
155  if (iout >= std::max (nargout, 1))
156  return false;
157  else if (lvalue_list)
158  {
159  int k = 0;
160  for (std::list<octave_lvalue>::const_iterator p = lvalue_list->begin ();
161  p != lvalue_list->end (); p++)
162  {
163  if (k == iout)
164  return ! p->is_black_hole ();
165  k += p->numel ();
166  if (k > iout)
167  break;
168  }
169 
170  return true;
171  }
172  else
173  return true;
174 }
175 
176 void defun_isargout (int nargout, int nout, bool *isargout)
177 {
178  const std::list<octave_lvalue> *lvalue_list
180 
181  if (lvalue_list)
182  {
183  int k = 0;
184  for (std::list<octave_lvalue>::const_iterator p = lvalue_list->begin ();
185  p != lvalue_list->end () && k < nout; p++)
186  {
187  if (p->is_black_hole ())
188  isargout[k++] = false;
189  else
190  {
191  int l = std::min (k + p->numel (),
192  static_cast<octave_idx_type> (nout));
193  while (k < l)
194  isargout[k++] = true;
195  }
196  }
197  }
198  else
199  for (int i = 0; i < nout; i++)
200  isargout[i] = true;
201 
202  for (int i = std::max (nargout, 1); i < nout; i++)
203  isargout[i] = false;
204 }
205 
static void install_built_in_function(const std::string &name, const octave_value &fcn)
Definition: symtab.h:1629
virtual bool is_mex_function(void) const
Definition: ov-base.h:449
Definition: mex.cc:2005
void print_usage(void)
Definition: defun.cc:51
octave_value_list(* fcn)(const octave_value_list &, int)
Definition: ov-builtin.h:45
void install_builtin_function(octave_builtin::fcn f, const std::string &name, const std::string &file, const std::string &doc, bool)
Definition: defun.cc:82
octave_shlib get_current_shlib(void)
Definition: defun.cc:127
void mark_relative(void)
Definition: ov-fcn.h:157
FloatComplex(* fptr)(const FloatComplex &, float, int, octave_idx_type &)
Definition: lo-specfun.cc:1732
void error(const char *fmt,...)
Definition: error.cc:476
std::string name(void) const
Definition: ov-fcn.h:161
octave_value_list feval(const std::string &name, const octave_value_list &args, int nargout)
Definition: oct-parse.cc:8625
static octave_function * current(void)
Definition: toplev.h:146
#define OCTAVE_API_VERSION
Definition: version.h:43
bool defun_isargout(int nargout, int iout)
Definition: defun.cc:151
F77_RET_T const double const double * f
static const std::list< octave_lvalue > * curr_lvalue_list
Definition: ov-builtin.h:92
octave_shlib get_shlib(void) const
Definition: ov-mex-fcn.h:89
void install_dld_function(octave_dld_function::fcn f, const std::string &name, const octave_shlib &shl, const std::string &doc, bool relative)
Definition: defun.cc:92
octave_shlib get_shlib(void) const
Definition: ov-dld-fcn.h:76
virtual bool is_dld_function(void) const
Definition: ov-base.h:447
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:233
void alias_builtin(const std::string &alias, const std::string &name)
Definition: defun.cc:121
static void alias_built_in_function(const std::string &alias, const std::string &name)
Definition: symtab.h:1808
void check_version(const std::string &version, const std::string &fcn)
Definition: defun.cc:67
void install_mex_function(void *fptr, bool fmex, const std::string &name, const octave_shlib &shl, bool relative)
Definition: defun.cc:107
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:210