Mercurial > hg > octave-nkf
view src/ov-fcn-handle.cc @ 4960:ce01dbd7e026 ss-2-1-58
[project @ 2004-09-02 03:47:49 by jwe]
author | jwe |
---|---|
date | Thu, 02 Sep 2004 03:47:49 +0000 |
parents | 4fc993a4e072 |
children | 8719102eb804 |
line wrap: on
line source
/* Copyright (C) 2003 John W. Eaton This file is part of Octave. Octave is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. Octave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Octave; see the file COPYING. If not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) #pragma implementation #endif #ifdef HAVE_CONFIG_H #include <config.h> #endif #include <iostream> #include "defun.h" #include "error.h" #include "gripes.h" #include "oct-map.h" #include "ov-base.h" #include "ov-fcn-handle.h" #include "pr-output.h" #include "variables.h" DEFINE_OCTAVE_ALLOCATOR (octave_fcn_handle); DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_fcn_handle, "function handle", "function handle"); octave_value_list octave_fcn_handle::subsref (const std::string& type, const std::list<octave_value_list>& idx, int nargout) { octave_value_list retval; switch (type[0]) { case '(': { octave_function *f = function_value (); retval = f->subsref (type, idx, nargout); } break; case '{': case '.': { std::string typ_nm = type_name (); error ("%s cannot be indexed with %c", typ_nm.c_str (), type[0]); } break; default: panic_impossible (); } // XXX FIXME XXX -- perhaps there should be an // octave_value_list::next_subsref member function? See also // octave_builtin::subsref. if (idx.size () > 1) retval = retval(0).next_subsref (type, idx); return retval; } void octave_fcn_handle::print (std::ostream& os, bool pr_as_read_syntax) const { print_raw (os, pr_as_read_syntax); newline (os); } void octave_fcn_handle::print_raw (std::ostream& os, bool pr_as_read_syntax) const { octave_print_internal (os, nm, pr_as_read_syntax, current_print_indent_level ()); } octave_value make_fcn_handle (const std::string& nm) { octave_value retval; octave_value f = lookup_function (nm); if (f.is_function ()) retval = octave_value (new octave_fcn_handle (f, nm)); else error ("error creating function handle \"@%s\"", nm.c_str ()); return retval; } DEFUN (functions, args, , "-*- texinfo -*-\n\ @deftypefn {Built-in Function} {} functions (@var{fcn_handle})\n\ Return a struct containing information about the function handle\n\ @var{fcn_handle}.\n\ @end deftypefn") { octave_value retval; if (args.length () == 1) { octave_fcn_handle *fh = args(0).fcn_handle_value (); if (! error_state) { octave_function *fcn = fh ? fh->function_value (true) : 0; if (fcn) { Octave_map m; std::string fh_nm = fh->fcn_name (); m.assign ("function", fh_nm.substr (1)); if (fcn->is_nested_function ()) m.assign ("type", "subfunction"); else m.assign ("type", "simple"); std::string nm = fcn->fcn_file_name (); if (nm.empty ()) { if (fh_nm == "@<anonymous>") m.assign ("file", "none"); else m.assign ("file", "built-in function"); } else m.assign ("file", nm); retval = m; } else error ("functions: invalid function handle object"); } else error ("functions: argument must be a function handle object"); } else print_usage ("functions"); return retval; } DEFUN (func2str, args, , "-*- texinfo -*-\n\ @deftypefn {Built-in Function} {} func2str (@var{fcn_handle})\n\ Return a string containing the name of the function referenced by\n\ the function handle @var{fcn_handle}.\n\ @end deftypefn") { octave_value retval; if (args.length () == 1) { octave_fcn_handle *fh = args(0).fcn_handle_value (); if (! error_state && fh) { std::string fh_nm = fh->fcn_name (); retval = fh_nm.substr (1); } else error ("func2str: expecting valid function handle as first argument"); } else print_usage ("func2str"); return retval; } DEFUN (str2func, args, , "-*- texinfo -*-\n\ @deftypefn {Built-in Function} {} str2func (@var{fcn_name})\n\ Return a function handle constructed from the string @var{fcn_name}.\n\ @end deftypefn") { octave_value retval; if (args.length () == 1) { std::string nm = args(0).string_value (); if (! error_state) retval = make_fcn_handle (nm); else error ("str2func: expecting string as first argument"); } else print_usage ("str2func"); return retval; } /* ;;; Local Variables: *** ;;; mode: C++ *** ;;; End: *** */