GNU Octave  6.2.0
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-2021 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 (octave_ov_fcn_handle_h)
27 #define octave_ov_fcn_handle_h 1
28 
29 #include "octave-config.h"
30 
31 #include <iosfwd>
32 #include <list>
33 #include <memory>
34 #include <string>
35 
36 #include "oct-map.h"
37 #include "ov-base.h"
38 #include "ov-fcn.h"
39 #include "ov-typeinfo.h"
40 #include "stack-frame.h"
41 #include "symscope.h"
42 
43 namespace octave
44 {
45  class interpreter;
46  class tree_evaluator;
47 
48  // Function handles.
49 
51  {
52  public:
53 
54  base_fcn_handle (const std::string& name = "",
55  const std::string& file = "")
56  : m_name (name), m_file (file)
57  { }
58 
59  base_fcn_handle (const base_fcn_handle&) = default;
60 
61  virtual ~base_fcn_handle (void) = default;
62 
63  virtual base_fcn_handle * clone (void) const = 0;
64 
65  virtual std::string type (void) const = 0;
66 
67  virtual bool is_internal (void) const { return false; }
68 
69  virtual bool is_simple (void) const { return false; }
70 
71  virtual bool is_scoped (void) const { return false; }
72 
73  virtual bool is_nested (void) const { return false; }
74 
75  virtual bool is_class_simple (void) const { return false; }
76 
77  virtual bool is_anonymous (void) const { return false; }
78 
79  std::string fcn_name (void) const { return m_name; }
80 
81  std::string file (void) const { return m_file; }
82 
84  subsref (const std::string& type, const std::list<octave_value_list>& idx,
85  int nargout);
86 
87  virtual octave_value_list
88  call (int nargout, const octave_value_list& args) = 0;
89 
90  // FIXME: These must go away. They don't do the right thing for
91  // scoping or overloads.
92  virtual octave_function * function_value (bool = false)
93  {
94  return nullptr;
95  }
96 
97  virtual octave_user_function * user_function_value (bool = false)
98  {
99  return nullptr;
100  }
101 
102  virtual octave_value fcn_val (void) { return octave_value (); }
103 
104  virtual octave_value workspace (void) const { return octave_value (); }
105 
106  // Should be const.
107  virtual octave_scalar_map info (void) { return octave_scalar_map (); }
108 
109  virtual void set_dispatch_class (const std::string& /*class_name*/) { }
110 
111  virtual std::string get_dispatch_class (void) const { return ""; }
112 
113  octave_value convert_to_str_internal (bool pad, bool force, char type) const;
114 
115  virtual bool save_ascii (std::ostream& os);
116 
117  virtual bool load_ascii (std::istream& is);
118 
119  virtual bool save_binary (std::ostream& os, bool save_as_floats);
120 
121  virtual bool load_binary (std::istream& is, bool swap,
123 
124  virtual bool save_hdf5 (octave_hdf5_id loc_id, const char *name,
125  bool save_as_floats);
126 
127  virtual bool load_hdf5 (octave_hdf5_id& group_hid,
128  octave_hdf5_id& space_hid,
129  octave_hdf5_id& type_hid);
130 
131  virtual void print_raw (std::ostream&, bool /*pr_as_read_syntax*/,
132  int /*current_print_indent_level*/) const
133  { }
134 
135  // Function handles are printed without a newline by default.
136  virtual bool print_as_scalar (void) const { return true; }
137 
138  virtual bool
139  set_fcn (const std::string& /*octaveroot*/, const std::string& /*fpath*/)
140  {
141  return false;
142  }
143 
144  protected:
145 
146  void warn_load (const char *file_type) const;
147  void warn_save (const char *file_type) const;
148 
149  void unimplemented (const char *op, const char *fmt) const;
150 
151  // The name of the handle, not including the "@", or the text of the
152  // anonymous function.
153  std::string m_name;
154 
155  // The name of the file where the named function was defined.
156  std::string m_file;
157  };
158 }
159 
160 class
161 OCTINTERP_API
163 {
164 public:
165 
166  static const std::string anonymous;
167 
168  // Creates an invalid function handle. Used to create generic
169  // function handle objects when loading function handles. Further
170  // dispatch happens in the octave_fcn_handle load/save functions.
171  octave_fcn_handle (void);
172 
173  // Create a handle to a built-in or internal function.
174  octave_fcn_handle (const octave_value& fcn);
175 
176  // Create a simple function handle that is not bound to a function.
177  // Lookup happens when a function call is attempted.
178  octave_fcn_handle (const std::string& name);
179 
180  // Create a simple function handle that is bound to a function.
181  octave_fcn_handle (const octave_value& fcn, const std::string& name);
182 
183  // Create a function handle that might be bound to a class method.
184  octave_fcn_handle (const std::string& class_nm, const std::string& meth_nm);
185 
186  // Create a function handle bound to a class method.
187  octave_fcn_handle (const octave_value& fcn, const std::string& class_nm,
188  const std::string& meth_nm);
189 
190  // Create a function handle bound to a class method.
191  octave_fcn_handle (const octave_value& obj, const octave_value& fcn,
192  const std::string& class_nm,
193  const std::string& meth_nm);
194 
195  // Create a function handle bound to a scoped function.
196  octave_fcn_handle (const octave_value& fcn, const std::string& name,
197  const std::list<std::string>& parentage);
198 
199  // Create a handle to a nested function.
200  octave_fcn_handle (const octave_value& fcn, const std::string& name,
201  const std::shared_ptr<octave::stack_frame>& closure_frames);
202 
203  // Create an anonymous function handle with local variable values
204  // provided in LOCAL_VARS.
205  octave_fcn_handle (const octave_value& fcn,
206  const octave::stack_frame::local_vars_map& local_vars);
207 
209 
210  ~octave_fcn_handle (void) = default;
211 
212  octave_base_value * clone (void) const
213  {
214  return new octave_fcn_handle (*this);
215  }
216 
218  {
219  return new octave_fcn_handle ();
220  }
221 
222  // We don't need to override all three forms of subsref. The using
223  // declaration will avoid warnings about partially-overloaded virtual
224  // functions.
226 
227  octave_value subsref (const std::string& type,
228  const std::list<octave_value_list>& idx)
229  {
230  octave_value_list tmp = subsref (type, idx, 1);
231  return tmp.length () > 0 ? tmp(0) : octave_value ();
232  }
233 
234  octave_value_list subsref (const std::string& type,
235  const std::list<octave_value_list>& idx,
236  int nargout)
237  {
238  return m_rep->subsref (type, idx, nargout);
239  }
240 
241  octave_value_list call (int nargout, const octave_value_list& args);
242 
243  bool is_defined (void) const { return true; }
244 
246 
247  bool is_function_handle (void) const { return true; }
248 
249  bool is_internal (void) const { return m_rep->is_internal (); }
250 
251  bool is_simple (void) const { return m_rep->is_simple (); }
252 
253  bool is_scoped (void) const { return m_rep->is_scoped (); }
254 
255  bool is_nested (void) const { return m_rep->is_nested (); }
256 
257  bool is_class_simple (void) const { return m_rep->is_class_simple (); }
258 
259  bool is_anonymous (void) const { return m_rep->is_anonymous (); }
260 
261  dim_vector dims (void) const;
262 
263  // FIXME: These must go away. They don't do the right thing for
264  // scoping or overloads.
266  {
267  return m_rep->function_value ();
268  }
269 
271  {
272  return m_rep->user_function_value ();
273  }
274 
275  octave_fcn_handle * fcn_handle_value (bool = false) { return this; }
276 
277  octave_value fcn_val (void) { return m_rep->fcn_val (); }
278 
279  // FCN_NAME should be eliminated.
280  std::string fcn_name (void) const { return m_rep->fcn_name (); }
281 
282  octave_value workspace (void) const
283  {
284  return m_rep->workspace ();
285  }
286 
287  octave_scalar_map info (void) { return m_rep->info (); }
288 
289  void set_dispatch_class (const std::string& class_name)
290  {
291  m_rep->set_dispatch_class (class_name);
292  }
293 
294  std::string get_dispatch_class (void) const
295  {
296  return m_rep->get_dispatch_class ();
297  }
298 
299  octave_value convert_to_str_internal (bool pad, bool force, char type) const
300  {
301  return m_rep->convert_to_str_internal (pad, force, type);
302  }
303 
304  bool save_ascii (std::ostream& os);
305 
306  bool load_ascii (std::istream& is);
307 
308  bool save_binary (std::ostream& os, bool save_as_floats);
309 
310  bool load_binary (std::istream& is, bool swap,
312 
313  bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
314 
315  bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
316 
317  void print (std::ostream& os, bool pr_as_read_syntax = false);
318 
319  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
320 
321  // Simple function handles are printed without a newline.
322  bool print_as_scalar (void) const { return m_rep->print_as_scalar (); }
323 
324  friend bool
325  is_equal_to (const octave_fcn_handle& fh1, const octave_fcn_handle& fh2);
326 
327 private:
328 
329  std::shared_ptr<octave::base_fcn_handle> m_rep;
330 
332 
333  octave::base_fcn_handle * get_rep (void) const { return m_rep.get (); }
334 
336 };
337 
338 extern bool
339 is_equal_to (const octave_fcn_handle& fh1, const octave_fcn_handle& fh2);
340 
341 namespace octave
342 {
343  OCTAVE_DEPRECATED (6, "use 'tree_evaluator::make_fcn_handle' instead")
344  extern octave_value
345  make_fcn_handle (interpreter& interp, const std::string& name);
346 }
347 
348 #endif
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:95
octave_value convert_to_str_internal(bool pad, bool force, char type) const
void warn_save(const char *file_type) const
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int nargout)
virtual octave_value_list call(int nargout, const octave_value_list &args)=0
std::string fcn_name(void) const
Definition: ov-fcn-handle.h:79
virtual octave_scalar_map info(void)
virtual bool print_as_scalar(void) const
virtual bool save_binary(std::ostream &os, bool save_as_floats)
virtual octave_function * function_value(bool=false)
Definition: ov-fcn-handle.h:92
virtual ~base_fcn_handle(void)=default
virtual octave_value fcn_val(void)
virtual octave_user_function * user_function_value(bool=false)
Definition: ov-fcn-handle.h:97
base_fcn_handle(const std::string &name="", const std::string &file="")
Definition: ov-fcn-handle.h:54
virtual bool is_class_simple(void) const
Definition: ov-fcn-handle.h:75
std::string file(void) const
Definition: ov-fcn-handle.h:81
virtual bool load_hdf5(octave_hdf5_id &group_hid, octave_hdf5_id &space_hid, octave_hdf5_id &type_hid)
virtual void print_raw(std::ostream &, bool, int) const
virtual std::string get_dispatch_class(void) const
virtual void set_dispatch_class(const std::string &)
virtual bool is_anonymous(void) const
Definition: ov-fcn-handle.h:77
virtual bool is_internal(void) const
Definition: ov-fcn-handle.h:67
virtual base_fcn_handle * clone(void) const =0
virtual octave_value workspace(void) const
virtual std::string type(void) const =0
base_fcn_handle(const base_fcn_handle &)=default
virtual bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
virtual bool load_binary(std::istream &is, bool swap, mach_info::float_format fmt)
virtual bool is_scoped(void) const
Definition: ov-fcn-handle.h:71
void unimplemented(const char *op, const char *fmt) const
virtual bool load_ascii(std::istream &is)
virtual bool is_simple(void) const
Definition: ov-fcn-handle.h:69
virtual bool set_fcn(const std::string &, const std::string &)
virtual bool is_nested(void) const
Definition: ov-fcn-handle.h:73
void warn_load(const char *file_type) const
virtual bool save_ascii(std::ostream &os)
std::map< std::string, octave_value > local_vars_map
Definition: stack-frame.h:112
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-base.cc:201
virtual octave_function * function_value(bool silent=false)
Definition: ov-base.cc:873
builtin_type_t builtin_type(void) const
static const std::string anonymous
std::string fcn_name(void) const
octave_base_value * empty_clone(void) const
bool is_defined(void) const
octave_scalar_map info(void)
bool is_simple(void) const
std::shared_ptr< octave::base_fcn_handle > m_rep
bool is_function_handle(void) const
bool print_as_scalar(void) const
bool is_internal(void) const
~octave_fcn_handle(void)=default
octave_base_value * clone(void) const
bool is_class_simple(void) const
octave_user_function * user_function_value(bool=false)
octave::base_fcn_handle * get_rep(void) const
octave_value fcn_val(void)
bool is_anonymous(void) const
octave_value_list call(int nargout, const octave_value_list &args)
octave_value convert_to_str_internal(bool pad, bool force, char type) const
octave_fcn_handle * fcn_handle_value(bool=false)
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
bool is_scoped(void) const
octave_value workspace(void) const
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int nargout)
bool is_nested(void) const
octave_function * function_value(bool=false)
std::string get_dispatch_class(void) const
void set_dispatch_class(const std::string &class_name)
octave_user_function * user_function_value(bool=false)
Definition: ov-usr-fcn.h:231
octave_idx_type length(void) const
Definition: ovl.h:113
octave_value convert_to_str_internal(bool pad, bool force, char type) const
Definition: ov.h:1221
QString name
static octave_value make_fcn_handle(const octave_value &fcn, const std::string &meth_name, const std::string &class_name)
Definition: cdef-class.cc:66
int64_t octave_hdf5_id
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
#define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
Definition: ov-base.h:158
builtin_type_t
Definition: ov-base.h:72
@ btyp_func_handle
Definition: ov-base.h:89
bool is_equal_to(const octave_fcn_handle &fh1, const octave_fcn_handle &fh2)