GNU Octave  3.8.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-2013 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 #include "oct-alloc.h"
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 
55  octave_fcn_handle (const std::string& n)
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); }
74  octave_base_value *empty_clone (void) const
75  { return new octave_fcn_handle (); }
76 
77  octave_value subsref (const std::string& type,
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 
84  octave_value_list subsref (const std::string& type,
85  const std::list<octave_value_list>& idx,
86  int nargout);
87 
88  octave_value_list subsref (const std::string& type,
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 { static dim_vector dv (1, 1); return dv; }
109 
110  octave_function *function_value (bool = false)
111  { return fcn.function_value (); }
112 
113  octave_user_function *user_function_value (bool = false)
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 #if defined (HAVE_HDF5)
151  bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
152 
153  bool load_hdf5 (hid_t loc_id, const char *name);
154 #endif
155 
156  void print (std::ostream& os, bool pr_as_read_syntax = false) const;
157 
158  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
159 
160  // Simple function handles are printed without a newline.
161  bool print_as_scalar (void) const { return nm != anonymous; }
162 
163 private:
164 
165  bool set_fcn (const std::string &octaveroot, const std::string& fpath);
166 
168 
170 
171 protected:
172 
173  // The function we are handling.
175 
176  // The name of the handle, including the "@".
177  std::string nm;
178 
179  // Whether the function is overloaded at all.
181 
182  // Overloads for builtin types. We use array to make lookup faster.
183  octave_value builtin_overloads[btyp_num_types];
184 
185  // Overloads for other classes.
186  str_ov_map overloads;
187 
188  friend octave_value make_fcn_handle (const std::string &, bool);
189 };
190 
191 extern octave_value make_fcn_handle (const std::string& nm,
192  bool local_funcs = true);
193 
194 class
197 {
198 private:
199  // Private ctor.
200  octave_fcn_binder (const octave_value& f, const octave_value& root,
201  const octave_value_list& templ,
202  const std::vector<int>& mask, int exp_nargin);
203 
204 public:
205 
206  // Factory method.
207  static octave_fcn_handle *maybe_binder (const octave_value& f);
208 
210  do_multi_index_op (int nargout, const octave_value_list& args);
211 
213  do_multi_index_op (int nargout, const octave_value_list& args,
214  const std::list<octave_lvalue>* lvalue_list);
215 
216 protected:
217 
220  std::vector<int> arg_mask;
222 };
223 #endif