GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-fcn-handle.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2003-2018 John W. Eaton
4 Copyright (C) 2009 VZLU Prague
5 
6 This file is part of Octave.
7 
8 Octave is free software: you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <https://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if ! defined (octave_ov_fcn_handle_h)
25 #define octave_ov_fcn_handle_h 1
26 
27 #include "octave-config.h"
28 
29 #include <iosfwd>
30 #include <string>
31 #include <memory>
32 
33 #include "ov-base.h"
34 #include "ov-base-mat.h"
35 #include "ov-fcn.h"
36 #include "ov-typeinfo.h"
37 
38 namespace octave
39 {
40  class tree_evaluator;
41 }
42 
43 // Function handles.
44 
45 class
46 OCTINTERP_API
48 {
49 private:
50 
51  typedef std::map<std::string, octave_value> str_ov_map;
52 
53 public:
54 
55  static const std::string anonymous;
56 
58  : fcn (), nm (), has_overloads (false), overloads () { }
59 
61  : fcn (), nm (n), has_overloads (false), overloads () { }
62 
63  octave_fcn_handle (const octave_value& f, const std::string& n = anonymous);
64 
66  : octave_base_value (fh), fcn (fh.fcn), nm (fh.nm),
67  has_overloads (fh.has_overloads), overloads ()
68  {
69  for (int i = 0; i < btyp_num_types; i++)
70  builtin_overloads[i] = fh.builtin_overloads[i];
71 
72  overloads = fh.overloads;
73  }
74 
75  ~octave_fcn_handle (void) = default;
76 
77  octave_base_value * clone (void) const
78  { return new octave_fcn_handle (*this); }
80  { return new octave_fcn_handle (); }
81 
83  const std::list<octave_value_list>& idx)
84  {
85  octave_value_list tmp = subsref (type, idx, 1);
86  return tmp.length () > 0 ? tmp(0) : octave_value ();
87  }
88 
90  const std::list<octave_value_list>& idx,
91  int nargout);
92 
93  bool is_defined (void) const { return true; }
94 
95  bool is_function_handle (void) const { return true; }
96 
97  builtin_type_t builtin_type (void) const { return btyp_func_handle; }
98 
99  bool is_overloaded (void) const { return has_overloads; }
100 
101  dim_vector dims (void) const;
102 
104  { return fcn.function_value (); }
105 
107  { return fcn.user_function_value (); }
108 
109  octave_fcn_handle * fcn_handle_value (bool = false) { return this; }
110 
111  octave_value fcn_val (void) const { return fcn; }
112 
113  std::string fcn_name (void) const { return nm; }
114 
115  void set_overload (builtin_type_t btyp, const octave_value& ov_fcn)
116  {
117  if (btyp != btyp_unknown)
118  {
119  has_overloads = true;
120  builtin_overloads[btyp] = ov_fcn;
121  }
122 
123  }
124 
125  void set_overload (const std::string& dispatch_type,
126  const octave_value& ov_fcn)
127  {
128  has_overloads = true;
129  overloads[dispatch_type] = ov_fcn;
130  }
131 
132  bool is_equal_to (const octave_fcn_handle&) const;
133 
134  bool save_ascii (std::ostream& os);
135 
136  bool load_ascii (std::istream& is);
137 
138  bool save_binary (std::ostream& os, bool& save_as_floats);
139 
140  bool load_binary (std::istream& is, bool swap,
142 
143  bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
144 
145  bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
146 
147  void print (std::ostream& os, bool pr_as_read_syntax = false);
148 
149  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
150 
151  // Simple function handles are printed without a newline.
152  bool print_as_scalar (void) const { return nm != anonymous; }
153 
154 private:
155 
156  bool set_fcn (const std::string& octaveroot, const std::string& fpath);
157 
159 
160 protected:
161 
162  // The function we are handling.
164 
165  // The name of the handle, including the "@".
167 
168  // Whether the function is overloaded at all.
170 
171  // Overloads for builtin types. We use array to make lookup faster.
172  octave_value builtin_overloads[btyp_num_types];
173 
174  // Overloads for other classes.
176 
177  bool parse_anon_fcn_handle (const std::string& fcn_text);
178 
179  virtual octave_value_list call (int nargout, const octave_value_list& args);
180 
181  friend octave_value make_fcn_handle (const std::string &, bool);
182 };
183 
185  bool local_funcs = true);
186 
187 class
188 OCTINTERP_API
190 {
191 private:
192  // Private ctor.
193  octave_fcn_binder (const octave_value& f, const octave_value& root,
194  const octave_value_list& templ,
195  const std::vector<int>& mask, int exp_nargin);
196 
197 public:
198 
199  // Factory method.
200  static octave_fcn_handle * maybe_binder (const octave_value& f,
202 
203 protected:
204 
205  octave_value_list call (int nargout, const octave_value_list& args);
206 
209  std::vector<int> arg_mask;
211 };
212 #endif
octave_value fcn_val(void) const
bool print_as_scalar(void) const
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-fcn-handle.h:82
octave_fcn_handle(const octave_fcn_handle &fh)
Definition: ov-fcn-handle.h:65
static const std::string anonymous
Definition: ov-fcn-handle.h:55
bool is_defined(void) const
Definition: ov-fcn-handle.h:93
octave_base_value * clone(void) const
Definition: ov-fcn-handle.h:77
octave_fcn_handle * fcn_handle_value(bool=false)
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
idx subsref(val, idx) esult
Definition: ov.cc:3065
std::vector< int > arg_mask
octave_value make_fcn_handle(const std::string &nm, bool local_funcs=true)
std::map< std::string, octave_value > str_ov_map
Definition: ov-fcn-handle.h:51
builtin_type_t builtin_type(void) const
Definition: ov-fcn-handle.h:97
builtin_type_t
Definition: ov-base.h:71
octave_fcn_handle(const std::string &n)
Definition: ov-fcn-handle.h:60
octave_function * fcn
Definition: ov-class.cc:1754
bool is_overloaded(void) const
Definition: ov-fcn-handle.h:99
bool swap
Definition: load-save.cc:738
bool is_function_handle(void) const
Definition: ov-fcn-handle.h:95
#define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
Definition: ov-base.h:158
str_ov_map overloads
nd deftypefn *std::string name
Definition: sysdep.cc:647
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
Definition: utils.cc:975
std::string fcn_name(void) const
virtual octave_user_function * user_function_value(bool silent=false)
Definition: ov-base.cc:880
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 root_handle
bool save_as_floats
Definition: load-save.cc:1617
double tmp
Definition: data.cc:6252
octave_value_list arg_template
is false
Definition: cellfun.cc:400
octave_value builtin_overloads[btyp_num_types]
int64_t octave_hdf5_id
idx type
Definition: ov.cc:3114
the exceeded dimensions are set to if fewer subscripts than dimensions are the exceeding dimensions are merged into the final requested dimension For consider the following dims
Definition: sub2ind.cc:255
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
void set_overload(const std::string &dispatch_type, const octave_value &ov_fcn)
octave_base_value * empty_clone(void) const
Definition: ov-fcn-handle.h:79
octave_value fcn
for i
Definition: data.cc:5264
void set_overload(builtin_type_t btyp, const octave_value &ov_fcn)
virtual octave_function * function_value(bool silent=false)
Definition: ov-base.cc:871
write the output to stdout if nargout is
Definition: load-save.cc:1612
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
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
octave::stream os
Definition: file-io.cc:627
octave_function * function_value(bool=false)
octave_user_function * user_function_value(bool=false)