GNU Octave  8.1.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-2023 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 
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_nested (const std::shared_ptr<stack_frame>&) const
76  {
77  return false;
78  }
79 
80  virtual bool is_weak_nested (void) const { return false; }
81 
82  virtual bool is_class_simple (void) const { return false; }
83 
84  virtual bool is_anonymous (void) const { return false; }
85 
86  virtual bool is_weak_anonymous (void) const { return false; }
87 
88  virtual octave_value make_weak_nested_handle (void) const;
89 
90  virtual octave_value make_weak_anonymous_handle (void) const;
91 
92  std::string fcn_name (void) const { return m_name; }
93 
94  std::string file (void) const { return m_file; }
95 
97  subsref (const std::string& type, const std::list<octave_value_list>& idx,
98  int nargout);
99 
100  virtual octave_value_list
101  call (int nargout, const octave_value_list& args) = 0;
102 
103  // FIXME: These must go away. They don't do the right thing for
104  // scoping or overloads.
105  virtual octave_function * function_value (bool = false)
106  {
107  return nullptr;
108  }
109 
110  virtual octave_user_function * user_function_value (bool = false)
111  {
112  return nullptr;
113  }
114 
115  virtual octave_value fcn_val (void) { return octave_value (); }
116 
117  virtual octave_value workspace (void) const { return octave_value (); }
118 
119  // Should be const.
120  virtual octave_scalar_map info (void) { return octave_scalar_map (); }
121 
122  virtual void set_dispatch_class (const std::string& /*class_name*/) { }
123 
124  virtual std::string get_dispatch_class (void) const { return ""; }
125 
126  octave_value convert_to_str_internal (bool pad, bool force, char type) const;
127 
128  virtual bool save_ascii (std::ostream& os);
129 
130  virtual bool load_ascii (std::istream& is);
131 
132  virtual bool save_binary (std::ostream& os, bool save_as_floats);
133 
134  virtual bool load_binary (std::istream& is, bool swap,
136 
137  virtual bool save_hdf5 (octave_hdf5_id loc_id, const char *name,
138  bool save_as_floats);
139 
140  virtual bool load_hdf5 (octave_hdf5_id& group_hid,
141  octave_hdf5_id& space_hid,
142  octave_hdf5_id& type_hid);
143 
144  virtual void print_raw (std::ostream&, bool /*pr_as_read_syntax*/,
145  int /*current_print_indent_level*/) const
146  { }
147 
148  // Function handles are printed without a newline by default.
149  virtual bool print_as_scalar (void) const { return true; }
150 
151  virtual bool
152  set_fcn (const std::string& /*octaveroot*/, const std::string& /*fpath*/)
153  {
154  return false;
155  }
156 
157 protected:
158 
159  void warn_load (const char *file_type) const;
160  void warn_save (const char *file_type) const;
161 
162  void unimplemented (const char *op, const char *fmt) const;
163 
164  // The name of the handle, not including the "@", or the text of the
165  // anonymous function.
166  std::string m_name;
167 
168  // The name of the file where the named function was defined.
169  std::string m_file;
170 };
171 
173 
174 class
175 OCTINTERP_API
177 {
178 public:
179 
180  static const std::string anonymous;
181 
182  // Creates an invalid function handle. Used to create generic
183  // function handle objects when loading function handles. Further
184  // dispatch happens in the octave_fcn_handle load/save functions.
185  octave_fcn_handle (void);
186 
187  // Create a handle to a built-in or internal function.
188  octave_fcn_handle (const octave_value& fcn);
189 
190  // Create a simple function handle that is not bound to a function.
191  // Lookup happens when a function call is attempted.
192  octave_fcn_handle (const std::string& name);
193 
194  // Create a simple function handle that is bound to a function.
195  octave_fcn_handle (const octave_value& fcn, const std::string& name);
196 
197  // Create a function handle that might be bound to a class method.
198  octave_fcn_handle (const std::string& class_nm, const std::string& meth_nm);
199 
200  // Create a function handle bound to a class method.
201  octave_fcn_handle (const octave_value& fcn, const std::string& class_nm,
202  const std::string& meth_nm);
203 
204  // Create a function handle bound to a class method.
205  octave_fcn_handle (const octave_value& obj, const octave_value& fcn,
206  const std::string& class_nm,
207  const std::string& meth_nm);
208 
209  // Create a function handle bound to a scoped function.
210  octave_fcn_handle (const octave_value& fcn, const std::string& name,
211  const std::list<std::string>& parentage);
212 
213  // Create a handle to a nested function.
214  octave_fcn_handle (const octave_value& fcn, const std::string& name,
215  const std::shared_ptr<octave::stack_frame>& closure_frames);
216 
217  // Create an anonymous function handle with local variable values
218  // provided in LOCAL_VARS.
219  octave_fcn_handle (const octave_value& fcn,
220  const octave::stack_frame::local_vars_map& local_vars,
221  const std::shared_ptr<octave::stack_frame>& closure_frames
222  = std::shared_ptr<octave::stack_frame> ());
223 
224  octave_fcn_handle (octave::base_fcn_handle *rep);
225 
227 
228  ~octave_fcn_handle (void) = default;
229 
230  octave_base_value * clone (void) const
231  {
232  return new octave_fcn_handle (*this);
233  }
234 
236  {
237  return new octave_fcn_handle ();
238  }
239 
240  // We don't need to override all three forms of subsref. The using
241  // declaration will avoid warnings about partially-overloaded virtual
242  // functions.
244 
245  octave_value subsref (const std::string& type,
246  const std::list<octave_value_list>& idx)
247  {
248  octave_value_list tmp = subsref (type, idx, 1);
249  return tmp.length () > 0 ? tmp(0) : octave_value ();
250  }
251 
252  octave_value_list subsref (const std::string& type,
253  const std::list<octave_value_list>& idx,
254  int nargout)
255  {
256  return m_rep->subsref (type, idx, nargout);
257  }
258 
259  octave_value_list call (int nargout, const octave_value_list& args);
260 
261  bool is_defined (void) const { return true; }
262 
264 
265  bool is_function_handle (void) const { return true; }
266 
267  bool is_internal (void) const { return m_rep->is_internal (); }
268 
269  bool is_simple (void) const { return m_rep->is_simple (); }
270 
271  bool is_scoped (void) const { return m_rep->is_scoped (); }
272 
273  bool is_nested (void) const { return m_rep->is_nested (); }
274 
275  bool is_nested (const std::shared_ptr<octave::stack_frame>& frame) const
276  {
277  return m_rep->is_nested (frame);
278  }
279 
280  bool is_weak_nested (void) const { return m_rep->is_weak_nested (); }
281 
282  bool is_class_simple (void) const { return m_rep->is_class_simple (); }
283 
284  bool is_anonymous (void) const { return m_rep->is_anonymous (); }
285 
286  bool is_weak_anonymous (void) const { return m_rep->is_weak_anonymous (); }
287 
289  {
290  return m_rep->make_weak_nested_handle ();
291  }
292 
294  {
295  return m_rep->make_weak_anonymous_handle ();
296  }
297 
298  dim_vector dims (void) const;
299 
300  // FIXME: These must go away. They don't do the right thing for
301  // scoping or overloads.
303  {
304  return m_rep->function_value ();
305  }
306 
308  {
309  return m_rep->user_function_value ();
310  }
311 
312  octave_fcn_handle * fcn_handle_value (bool = false) { return this; }
313 
314  octave_value fcn_val (void) { return m_rep->fcn_val (); }
315 
316  // FCN_NAME should be eliminated.
317  std::string fcn_name (void) const { return m_rep->fcn_name (); }
318 
319  octave_value workspace (void) const
320  {
321  return m_rep->workspace ();
322  }
323 
324  octave_scalar_map info (void) { return m_rep->info (); }
325 
326  void set_dispatch_class (const std::string& class_name)
327  {
328  m_rep->set_dispatch_class (class_name);
329  }
330 
331  std::string get_dispatch_class (void) const
332  {
333  return m_rep->get_dispatch_class ();
334  }
335 
336  octave_value convert_to_str_internal (bool pad, bool force, char type) const
337  {
338  return m_rep->convert_to_str_internal (pad, force, type);
339  }
340 
341  bool save_ascii (std::ostream& os);
342 
343  bool load_ascii (std::istream& is);
344 
345  bool save_binary (std::ostream& os, bool save_as_floats);
346 
347  bool load_binary (std::istream& is, bool swap,
349 
350  bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
351 
352  bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
353 
354  void print (std::ostream& os, bool pr_as_read_syntax = false);
355 
356  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
357 
358  // Simple function handles are printed without a newline.
359  bool print_as_scalar (void) const { return m_rep->print_as_scalar (); }
360 
361  friend bool
362  is_equal_to (const octave_fcn_handle& fh1, const octave_fcn_handle& fh2);
363 
364 private:
365 
366  std::shared_ptr<octave::base_fcn_handle> m_rep;
367 
368  octave::base_fcn_handle * get_rep (void) const { return m_rep.get (); }
369 
371 };
372 
373 extern bool
374 is_equal_to (const octave_fcn_handle& fh1, const octave_fcn_handle& fh2);
375 
376 #endif
OCTAVE_END_NAMESPACE(octave)
virtual bool save_binary(std::ostream &os, bool save_as_floats)
virtual bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
virtual octave_value make_weak_anonymous_handle(void) const
virtual octave_value workspace(void) const
virtual octave_value fcn_val(void)
std::string m_name
virtual bool is_weak_nested(void) const
Definition: ov-fcn-handle.h:80
virtual bool print_as_scalar(void) const
void unimplemented(const char *op, const char *fmt) const
virtual base_fcn_handle * clone(void) const =0
std::string fcn_name(void) const
Definition: ov-fcn-handle.h:92
virtual octave_scalar_map info(void)
virtual bool is_nested(const std::shared_ptr< stack_frame > &) const
Definition: ov-fcn-handle.h:75
virtual bool save_ascii(std::ostream &os)
virtual void set_dispatch_class(const std::string &)
virtual std::string type(void) const =0
void warn_save(const char *file_type) const
virtual bool load_binary(std::istream &is, bool swap, mach_info::float_format fmt)
virtual octave_value make_weak_nested_handle(void) const
virtual bool is_scoped(void) const
Definition: ov-fcn-handle.h:71
virtual bool load_hdf5(octave_hdf5_id &group_hid, octave_hdf5_id &space_hid, octave_hdf5_id &type_hid)
virtual bool is_simple(void) const
Definition: ov-fcn-handle.h:69
base_fcn_handle(const base_fcn_handle &)=default
virtual octave_value_list call(int nargout, const octave_value_list &args)=0
virtual bool is_weak_anonymous(void) const
Definition: ov-fcn-handle.h:86
virtual ~base_fcn_handle(void)=default
void warn_load(const char *file_type) const
virtual bool is_anonymous(void) const
Definition: ov-fcn-handle.h:84
virtual octave_function * function_value(bool=false)
virtual bool is_nested(void) const
Definition: ov-fcn-handle.h:73
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:82
virtual bool load_ascii(std::istream &is)
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int nargout)
std::string file(void) const
Definition: ov-fcn-handle.h:94
octave_value convert_to_str_internal(bool pad, bool force, char type) const
virtual bool is_internal(void) const
Definition: ov-fcn-handle.h:67
virtual std::string get_dispatch_class(void) const
virtual octave_user_function * user_function_value(bool=false)
virtual bool set_fcn(const std::string &, const std::string &)
std::string m_file
virtual void print_raw(std::ostream &, bool, int) const
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-base.cc:200
virtual octave_function * function_value(bool silent=false)
Definition: ov-base.cc:940
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_value make_weak_nested_handle(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_weak_anonymous(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_value make_weak_anonymous_handle(void) 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)
bool is_weak_nested(void) const
std::string get_dispatch_class(void) const
bool is_nested(const std::shared_ptr< octave::stack_frame > &frame) const
void set_dispatch_class(const std::string &class_name)
octave_user_function * user_function_value(bool=false)
Definition: ov-usr-fcn.h:228
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:1419
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
float_format
Definition: mach-info.h:38
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:181
builtin_type_t
Definition: ov-base.h:83
@ btyp_func_handle
Definition: ov-base.h:100
bool is_equal_to(const octave_fcn_handle &fh1, const octave_fcn_handle &fh2)