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
ov-fcn-handle.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2003-2017 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 the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 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 <http://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 // Function handles.
39 
40 class
43 {
44 private:
45 
46  typedef std::map<std::string, octave_value> str_ov_map;
47 
48 public:
49 
50  static const std::string anonymous;
51 
53  : fcn (), nm (), has_overloads (false), overloads () { }
54 
56  : fcn (), nm (n), has_overloads (false), overloads () { }
57 
58  octave_fcn_handle (const octave_value& f, const std::string& n = anonymous);
59 
61  : octave_base_value (fh), fcn (fh.fcn), nm (fh.nm),
62  has_overloads (fh.has_overloads), overloads ()
63  {
64  for (int i = 0; i < btyp_num_types; i++)
65  builtin_overloads[i] = fh.builtin_overloads[i];
66 
67  overloads = fh.overloads;
68  }
69 
70  ~octave_fcn_handle (void) { }
71 
72  octave_base_value *clone (void) const
73  { return new octave_fcn_handle (*this); }
75  { return new octave_fcn_handle (); }
76 
78  const std::list<octave_value_list>& idx)
79  {
80  octave_value_list tmp = subsref (type, idx, 1);
81  return tmp.length () > 0 ? tmp(0) : octave_value ();
82  }
83 
85  const std::list<octave_value_list>& idx,
86  int nargout);
87 
89  const std::list<octave_value_list>& idx,
90  int nargout,
91  const std::list<octave_lvalue>* lvalue_list);
92 
94  do_multi_index_op (int nargout, const octave_value_list& args);
95 
97  do_multi_index_op (int nargout, const octave_value_list& args,
98  const std::list<octave_lvalue>* lvalue_list);
99 
100  bool is_defined (void) const { return true; }
101 
102  bool is_function_handle (void) const { return true; }
103 
105 
106  bool is_overloaded (void) const { return has_overloads; }
107 
108  dim_vector dims (void) const;
109 
111  { return fcn.function_value (); }
112 
114  { return fcn.user_function_value (); }
115 
116  octave_fcn_handle *fcn_handle_value (bool = false) { return this; }
117 
118  octave_value fcn_val (void) const { return fcn; }
119 
120  std::string fcn_name (void) const { return nm; }
121 
122  void set_overload (builtin_type_t btyp, const octave_value& ov_fcn)
123  {
124  if (btyp != btyp_unknown)
125  {
126  has_overloads = true;
127  builtin_overloads[btyp] = ov_fcn;
128  }
129 
130  }
131 
132  void set_overload (const std::string& dispatch_type,
133  const octave_value& ov_fcn)
134  {
135  has_overloads = true;
136  overloads[dispatch_type] = ov_fcn;
137  }
138 
139  bool is_equal_to (const octave_fcn_handle&) const;
140 
141  bool save_ascii (std::ostream& os);
142 
143  bool load_ascii (std::istream& is);
144 
145  bool save_binary (std::ostream& os, bool& save_as_floats);
146 
147  bool load_binary (std::istream& is, bool swap,
149 
150  bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
151 
152  bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
153 
154  void print (std::ostream& os, bool pr_as_read_syntax = false);
155 
156  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
157 
158  // Simple function handles are printed without a newline.
159  bool print_as_scalar (void) const { return nm != anonymous; }
160 
161 private:
162 
163  bool set_fcn (const std::string &octaveroot, const std::string& fpath);
164 
166 
167 protected:
168 
169  // The function we are handling.
171 
172  // The name of the handle, including the "@".
174 
175  // Whether the function is overloaded at all.
177 
178  // Overloads for builtin types. We use array to make lookup faster.
179  octave_value builtin_overloads[btyp_num_types];
180 
181  // Overloads for other classes.
182  str_ov_map overloads;
183 
184  friend octave_value make_fcn_handle (const std::string &, bool);
185 };
186 
188  bool local_funcs = true);
189 
190 class
193 {
194 private:
195  // Private ctor.
196  octave_fcn_binder (const octave_value& f, const octave_value& root,
197  const octave_value_list& templ,
198  const std::vector<int>& mask, int exp_nargin);
199 
200 public:
201 
202  // Factory method.
203  static octave_fcn_handle *maybe_binder (const octave_value& f);
204 
206  do_multi_index_op (int nargout, const octave_value_list& args);
207 
209  do_multi_index_op (int nargout, const octave_value_list& args,
210  const std::list<octave_lvalue>* lvalue_list);
211 
212 protected:
213 
216  std::vector<int> arg_mask;
218 };
219 #endif
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-fcn-handle.h:77
octave_fcn_handle(const octave_fcn_handle &fh)
Definition: ov-fcn-handle.h:60
static const std::string anonymous
Definition: ov-fcn-handle.h:50
octave_fcn_handle * fcn_handle_value(bool=false)
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
octave_idx_type length(void) const
Definition: ovl.h:96
idx subsref(val, idx) esult
Definition: ov.cc:3080
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:46
octave_value fcn_val(void) const
bool is_overloaded(void) const
builtin_type_t
Definition: ov-base.h:61
octave_fcn_handle(const std::string &n)
Definition: ov-fcn-handle.h:55
octave_function * fcn
Definition: ov-class.cc:1743
builtin_type_t builtin_type(void) const
bool swap
Definition: load-save.cc:725
JNIEnv void * args
Definition: ov-java.cc:67
#define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
Definition: ov-base.h:148
str_ov_map overloads
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_EXPORT octave_value_list isdir nd deftypefn *std::string nm
Definition: utils.cc:941
virtual octave_user_function * user_function_value(bool silent=false)
Definition: ov-base.cc:923
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
#define OCTINTERP_API
Definition: mexproto.h:69
octave_value root_handle
bool is_defined(void) const
bool save_as_floats
Definition: load-save.cc:1581
double tmp
Definition: data.cc:6300
octave_value_list arg_template
is false
Definition: cellfun.cc:398
octave_value builtin_overloads[btyp_num_types]
int64_t octave_hdf5_id
idx type
Definition: ov.cc:3129
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
octave_base_value * clone(void) const
Definition: ov-fcn-handle.h:72
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
void set_overload(const std::string &dispatch_type, const octave_value &ov_fcn)
bool print_as_scalar(void) const
octave_value fcn
octave_base_value * empty_clone(void) const
Definition: ov-fcn-handle.h:74
void set_overload(builtin_type_t btyp, const octave_value &ov_fcn)
bool is_function_handle(void) const
virtual octave_function * function_value(bool silent=false)
Definition: ov-base.cc:912
write the output to stdout if nargout is
Definition: load-save.cc:1576
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:854
octave_function * function_value(bool=false)
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))
std::string fcn_name(void) const
octave_user_function * user_function_value(bool=false)