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
ov-fcn-handle.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2003-2015 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 <iosfwd>
28 #include <string>
29 #include <memory>
30 
31 
32 #include "ov-base.h"
33 #include "ov-base-mat.h"
34 #include "ov-fcn.h"
35 #include "ov-typeinfo.h"
36 
37 // Function handles.
38 
39 class
42 {
43 private:
44 
45  typedef std::map<std::string, octave_value> str_ov_map;
46 
47 public:
48 
49  static const std::string anonymous;
50 
52  : fcn (), nm (), has_overloads (false), overloads () { }
53 
54  octave_fcn_handle (const std::string& n)
55  : fcn (), nm (n), has_overloads (false), overloads () { }
56 
57  octave_fcn_handle (const octave_value& f, const std::string& n = anonymous);
58 
60  : octave_base_value (fh), fcn (fh.fcn), nm (fh.nm),
61  has_overloads (fh.has_overloads), overloads ()
62  {
63  for (int i = 0; i < btyp_num_types; i++)
64  builtin_overloads[i] = fh.builtin_overloads[i];
65 
66  overloads = fh.overloads;
67  }
68 
69  ~octave_fcn_handle (void) { }
70 
71  octave_base_value *clone (void) const
72  { return new octave_fcn_handle (*this); }
74  { return new octave_fcn_handle (); }
75 
76  octave_value subsref (const std::string& type,
77  const std::list<octave_value_list>& idx)
78  {
79  octave_value_list tmp = subsref (type, idx, 1);
80  return tmp.length () > 0 ? tmp(0) : octave_value ();
81  }
82 
83  octave_value_list subsref (const std::string& type,
84  const std::list<octave_value_list>& idx,
85  int nargout);
86 
87  octave_value_list subsref (const std::string& type,
88  const std::list<octave_value_list>& idx,
89  int nargout,
90  const std::list<octave_lvalue>* lvalue_list);
91 
93  do_multi_index_op (int nargout, const octave_value_list& args);
94 
96  do_multi_index_op (int nargout, const octave_value_list& args,
97  const std::list<octave_lvalue>* lvalue_list);
98 
99  bool is_defined (void) const { return true; }
100 
101  bool is_function_handle (void) const { return true; }
102 
104 
105  bool is_overloaded (void) const { return has_overloads; }
106 
107  dim_vector dims (void) const { static dim_vector dv (1, 1); return dv; }
108 
110  { return fcn.function_value (); }
111 
113  { return fcn.user_function_value (); }
114 
115  octave_fcn_handle *fcn_handle_value (bool = false) { return this; }
116 
117  octave_value fcn_val (void) const { return fcn; }
118 
119  std::string fcn_name (void) const { return nm; }
120 
121  void set_overload (builtin_type_t btyp, const octave_value& ov_fcn)
122  {
123  if (btyp != btyp_unknown)
124  {
125  has_overloads = true;
126  builtin_overloads[btyp] = ov_fcn;
127  }
128 
129  }
130 
131  void set_overload (const std::string& dispatch_type,
132  const octave_value& ov_fcn)
133  {
134  has_overloads = true;
135  overloads[dispatch_type] = ov_fcn;
136  }
137 
138  bool is_equal_to (const octave_fcn_handle&) const;
139 
140  bool save_ascii (std::ostream& os);
141 
142  bool load_ascii (std::istream& is);
143 
144  bool save_binary (std::ostream& os, bool& save_as_floats);
145 
146  bool load_binary (std::istream& is, bool swap,
148 
149  bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
150 
151  bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
152 
153  void print (std::ostream& os, bool pr_as_read_syntax = false);
154 
155  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
156 
157  // Simple function handles are printed without a newline.
158  bool print_as_scalar (void) const { return nm != anonymous; }
159 
160 private:
161 
162  bool set_fcn (const std::string &octaveroot, const std::string& fpath);
163 
164 
166 
167 protected:
168 
169  // The function we are handling.
171 
172  // The name of the handle, including the "@".
173  std::string nm;
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 
187 extern octave_value make_fcn_handle (const std::string& nm,
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:76
octave_fcn_handle(const octave_fcn_handle &fh)
Definition: ov-fcn-handle.h:59
octave_user_function * user_function_value(bool=false)
Definition: ov-usr-fcn.h:194
static const std::string anonymous
Definition: ov-fcn-handle.h:49
octave_fcn_handle * fcn_handle_value(bool=false)
dim_vector dims(void) const
octave_idx_type length(void) const
Definition: oct-obj.h:89
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:45
octave_value fcn_val(void) const
bool is_overloaded(void) const
builtin_type_t
Definition: ov-base.h:59
octave_fcn_handle(const std::string &n)
Definition: ov-fcn-handle.h:54
builtin_type_t builtin_type(void) const
#define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
Definition: ov-base.h:142
str_ov_map overloads
F77_RET_T const double const double * f
#define OCTINTERP_API
Definition: mexproto.h:66
octave_value root_handle
bool is_defined(void) const
Definition: ov-fcn-handle.h:99
octave_value_list arg_template
octave_value builtin_overloads[btyp_num_types]
octave_base_value * clone(void) const
Definition: ov-fcn-handle.h:71
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:73
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:992
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)