Mercurial > hg > octave-lyh
diff src/ov-fcn-handle.cc @ 4343:db5e0814277a
[project @ 2003-02-20 16:44:16 by jwe]
author | jwe |
---|---|
date | Thu, 20 Feb 2003 16:46:37 +0000 |
parents | |
children | d39de791ef9c |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/src/ov-fcn-handle.cc @@ -0,0 +1,177 @@ +/* + +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 "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"); + +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 +{ + indent (os); + os << "@" << name (); +} + +octave_value +make_fcn_handle (const std::string& nm) +{ + octave_value retval; + + octave_function *f = lookup_function (nm); + + if (f) + { + octave_fcn_handle fh (f, nm); + + retval = octave_value (fh); + } + 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.function_value (true); + + if (fcn) + { + Octave_map m; + + m ["function"](0) = fh.name (); + + if (fcn->is_nested_function ()) + m ["type"](0) = "subfunction"; + else + m ["type"](0) = "simple"; + + std::string nm = fcn->fcn_file_name (); + + if (nm.empty ()) + m ["file"](0) = "built-in function"; + else + m ["file"](0) = 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) + retval = fh.name (); + else + error ("func2str: expecting 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: *** +*/