ov-fcn-handle.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2003-2012 John W. Eaton
00004 Copyright (C) 2009 VZLU Prague
00005 
00006 This file is part of Octave.
00007 
00008 Octave is free software; you can redistribute it and/or modify it
00009 under the terms of the GNU General Public License as published by the
00010 Free Software Foundation; either version 3 of the License, or (at your
00011 option) any later version.
00012 
00013 Octave is distributed in the hope that it will be useful, but WITHOUT
00014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00015 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00016 for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with Octave; see the file COPYING.  If not, see
00020 <http://www.gnu.org/licenses/>.
00021 
00022 */
00023 
00024 #if !defined (octave_fcn_handle_h)
00025 #define octave_fcn_handle_h 1
00026 
00027 #include <iosfwd>
00028 #include <string>
00029 #include <memory>
00030 
00031 #include "oct-alloc.h"
00032 
00033 #include "ov-base.h"
00034 #include "ov-base-mat.h"
00035 #include "ov-fcn.h"
00036 #include "ov-typeinfo.h"
00037 
00038 // Function handles.
00039 
00040 class
00041 OCTINTERP_API
00042 octave_fcn_handle : public octave_base_value
00043 {
00044 private:
00045 
00046   typedef std::map<std::string, octave_value> str_ov_map;
00047 
00048 public:
00049 
00050   static const std::string anonymous;
00051 
00052   octave_fcn_handle (void)
00053     : fcn (), nm (), has_overloads (false), overloads () { }
00054 
00055   octave_fcn_handle (const std::string& n)
00056     : fcn (), nm (n), has_overloads (false), overloads () { }
00057 
00058   octave_fcn_handle (const octave_value& f,  const std::string& n = anonymous);
00059 
00060   octave_fcn_handle (const octave_fcn_handle& fh)
00061     : octave_base_value (fh), fcn (fh.fcn), nm (fh.nm),
00062     has_overloads (fh.has_overloads), overloads ()
00063    {
00064      for (int i = 0; i < btyp_num_types; i++)
00065        builtin_overloads[i] = fh.builtin_overloads[i];
00066 
00067      overloads = fh.overloads;
00068    }
00069 
00070   ~octave_fcn_handle (void) { }
00071 
00072   octave_base_value *clone (void) const { return new octave_fcn_handle (*this); }
00073   octave_base_value *empty_clone (void) const { return new octave_fcn_handle (); }
00074 
00075   octave_value subsref (const std::string& type,
00076                         const std::list<octave_value_list>& idx)
00077     {
00078       octave_value_list tmp = subsref (type, idx, 1);
00079       return tmp.length () > 0 ? tmp(0) : octave_value ();
00080     }
00081 
00082   octave_value_list subsref (const std::string& type,
00083                              const std::list<octave_value_list>& idx,
00084                              int nargout);
00085 
00086   octave_value_list subsref (const std::string& type,
00087                              const std::list<octave_value_list>& idx,
00088                              int nargout, const std::list<octave_lvalue>* lvalue_list);
00089 
00090   octave_value_list
00091   do_multi_index_op (int nargout, const octave_value_list& args);
00092 
00093   octave_value_list
00094   do_multi_index_op (int nargout, const octave_value_list& args,
00095                      const std::list<octave_lvalue>* lvalue_list);
00096 
00097   bool is_defined (void) const { return true; }
00098 
00099   bool is_function_handle (void) const { return true; }
00100 
00101   builtin_type_t builtin_type (void) const { return btyp_func_handle; }
00102 
00103   bool is_overloaded (void) const { return has_overloads; }
00104 
00105   dim_vector dims (void) const { static dim_vector dv (1, 1); return dv; }
00106 
00107   octave_function *function_value (bool = false)
00108     { return fcn.function_value (); }
00109 
00110   octave_user_function *user_function_value (bool = false)
00111     { return fcn.user_function_value (); }
00112 
00113   octave_fcn_handle *fcn_handle_value (bool = false) { return this; }
00114 
00115   octave_value fcn_val (void) const { return fcn; }
00116 
00117   std::string fcn_name (void) const { return nm; }
00118 
00119   void set_overload (builtin_type_t btyp, const octave_value& ov_fcn)
00120     {
00121       if (btyp != btyp_unknown)
00122         {
00123           has_overloads = true;
00124           builtin_overloads[btyp] = ov_fcn;
00125         }
00126 
00127     }
00128 
00129   void set_overload (const std::string& dispatch_type, const octave_value& ov_fcn)
00130     {
00131       has_overloads = true;
00132       overloads[dispatch_type] = ov_fcn;
00133     }
00134 
00135   bool is_equal_to (const octave_fcn_handle&) const;
00136 
00137   bool save_ascii (std::ostream& os);
00138 
00139   bool load_ascii (std::istream& is);
00140 
00141   bool save_binary (std::ostream& os, bool& save_as_floats);
00142 
00143   bool load_binary (std::istream& is, bool swap,
00144                     oct_mach_info::float_format fmt);
00145 
00146 #if defined (HAVE_HDF5)
00147   bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
00148 
00149   bool load_hdf5 (hid_t loc_id, const char *name);
00150 #endif
00151 
00152   void print (std::ostream& os, bool pr_as_read_syntax = false) const;
00153 
00154   void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
00155 
00156   // Simple function handles are printed without a newline.
00157   bool print_as_scalar (void) const { return nm != anonymous; }
00158 
00159 private:
00160 
00161   bool set_fcn (const std::string &octaveroot, const std::string& fpath);
00162 
00163   DECLARE_OCTAVE_ALLOCATOR
00164 
00165   DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
00166 
00167 protected:
00168 
00169   // The function we are handling.
00170   octave_value fcn;
00171 
00172   // The name of the handle, including the "@".
00173   std::string nm;
00174 
00175   // Whether the function is overloaded at all.
00176   bool has_overloads;
00177 
00178   // Overloads for builtin types. We use array to make lookup faster.
00179   octave_value builtin_overloads[btyp_num_types];
00180 
00181   // Overloads for other classes.
00182   str_ov_map overloads;
00183 
00184   friend octave_value make_fcn_handle (const std::string &, bool);
00185 };
00186 
00187 extern octave_value make_fcn_handle (const std::string& nm,
00188                                      bool local_funcs = true);
00189 
00190 class
00191 OCTINTERP_API
00192 octave_fcn_binder : public octave_fcn_handle
00193 {
00194 private:
00195   // Private ctor.
00196   octave_fcn_binder (const octave_value& f, const octave_value& root,
00197                      const octave_value_list& templ,
00198                      const std::vector<int>& mask, int exp_nargin);
00199 
00200 public:
00201 
00202   // Factory method.
00203   static octave_fcn_handle *maybe_binder (const octave_value& f);
00204 
00205   octave_value_list
00206   do_multi_index_op (int nargout, const octave_value_list& args);
00207 
00208   octave_value_list
00209   do_multi_index_op (int nargout, const octave_value_list& args,
00210                      const std::list<octave_lvalue>* lvalue_list);
00211 
00212 protected:
00213 
00214   octave_value root_handle;
00215   octave_value_list arg_template;
00216   std::vector<int> arg_mask;
00217   int expected_nargin;
00218 };
00219 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines