Mercurial > octave
changeset 23520:9f925aed7d1b
eliminate duplication in subsref methods for function objects
All function objects (built-in, mex, user-defined) have the same
definition for subsref, so provide one implementation in the base
function class instead of separate ones in each class.
* ov-usr-fcn.cc, ov-usr-fcn.h (octave_function::subsref): Define here.
* ov-builtin.cc, ov-builtin.h, ov-fcn.cc, ov-fcn.h, ov-mex-fcn.cc,
ov-mex-fcn.h: Delete all subsref methods except one for scripts.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 22 May 2017 13:45:19 -0400 |
parents | 4fe410bd918d |
children | 551fa3879615 |
files | libinterp/octave-value/ov-builtin.cc libinterp/octave-value/ov-builtin.h libinterp/octave-value/ov-fcn.cc libinterp/octave-value/ov-fcn.h libinterp/octave-value/ov-mex-fcn.cc libinterp/octave-value/ov-mex-fcn.h libinterp/octave-value/ov-usr-fcn.cc libinterp/octave-value/ov-usr-fcn.h |
diffstat | 8 files changed, 57 insertions(+), 169 deletions(-) [+] |
line wrap: on
line diff
--- a/libinterp/octave-value/ov-builtin.cc Fri May 19 16:05:03 2017 -0400 +++ b/libinterp/octave-value/ov-builtin.cc Mon May 22 13:45:19 2017 -0400 @@ -41,51 +41,6 @@ "built-in function"); octave_value_list -octave_builtin::subsref (const std::string& type, - const std::list<octave_value_list>& idx, - int nargout) -{ - octave_value_list retval; - - switch (type[0]) - { - case '(': - { - int tmp_nargout = (type.length () > 1 && nargout == 0) ? 1 : nargout; - - retval = call (tmp_nargout, idx.front ()); - } - break; - - case '{': - case '.': - { - std::string nm = type_name (); - error ("%s cannot be indexed with %c", nm.c_str (), type[0]); - } - break; - - default: - panic_impossible (); - } - - // FIXME: perhaps there should be an - // octave_value_list::next_subsref member function? See also - // octave_user_function::subsref. - // - // FIXME: Note that if a function call returns multiple - // values, and there is further indexing to perform, then we are - // ignoring all but the first value. Is this really what we want to - // do? If it is not, then what should happen for stat("file").size, - // for exmaple? - - if (idx.size () > 1) - retval = retval(0).next_subsref (nargout, type, idx); - - return retval; -} - -octave_value_list octave_builtin::call (int nargout, const octave_value_list& args) { octave_value_list retval;
--- a/libinterp/octave-value/ov-builtin.h Fri May 19 16:05:03 2017 -0400 +++ b/libinterp/octave-value/ov-builtin.h Mon May 22 13:45:19 2017 -0400 @@ -82,17 +82,6 @@ std::string src_file_name (void) const { return file; } - octave_value subsref (const std::string& type, - const std::list<octave_value_list>& idx) - { - octave_value_list tmp = subsref (type, idx, 1); - return tmp.length () > 0 ? tmp(0) : octave_value (); - } - - octave_value_list subsref (const std::string& type, - const std::list<octave_value_list>& idx, - int nargout); - octave_function * function_value (bool = false) { return this; } bool is_builtin_function (void) const { return true; }
--- a/libinterp/octave-value/ov-fcn.cc Fri May 19 16:05:03 2017 -0400 +++ b/libinterp/octave-value/ov-fcn.cc Mon May 22 13:45:19 2017 -0400 @@ -42,3 +42,48 @@ panic_impossible (); return 0; } + +octave_value_list +octave_function::subsref (const std::string& type, + const std::list<octave_value_list>& idx, + int nargout) +{ + octave_value_list retval; + + switch (type[0]) + { + case '(': + { + int tmp_nargout = (type.length () > 1 && nargout == 0) ? 1 : nargout; + + retval = call (tmp_nargout, idx.front ()); + } + break; + + case '{': + case '.': + { + std::string nm = type_name (); + error ("%s cannot be indexed with %c", nm.c_str (), type[0]); + } + break; + + default: + panic_impossible (); + } + + // FIXME: perhaps there should be an + // octave_value_list::next_subsref member function? See also + // octave_user_function::subsref. + // + // FIXME: Note that if a function call returns multiple + // values, and there is further indexing to perform, then we are + // ignoring all but the first value. Is this really what we want to + // do? If it is not, then what should happen for stat("file").size, + // for exmaple? + + if (idx.size () > 1) + retval = retval(0).next_subsref (nargout, type, idx); + + return retval; +}
--- a/libinterp/octave-value/ov-fcn.h Fri May 19 16:05:03 2017 -0400 +++ b/libinterp/octave-value/ov-fcn.h Mon May 22 13:45:19 2017 -0400 @@ -202,6 +202,18 @@ call (int nargout = 0, const octave_value_list& args = octave_value_list ()) = 0; + octave_value subsref (const std::string& type, + const std::list<octave_value_list>& idx) + { + octave_value_list tmp = subsref (type, idx, 1); + return tmp.length () > 0 ? tmp(0) : octave_value (); + } + + octave_value_list + subsref (const std::string& type, + const std::list<octave_value_list>& idx, + int nargout); + protected: octave_function (const std::string& nm,
--- a/libinterp/octave-value/ov-mex-fcn.cc Fri May 19 16:05:03 2017 -0400 +++ b/libinterp/octave-value/ov-mex-fcn.cc Mon May 22 13:45:19 2017 -0400 @@ -77,51 +77,6 @@ return m_sh_lib.time_loaded (); } -octave_value_list -octave_mex_function::subsref (const std::string& type, - const std::list<octave_value_list>& idx, - int nargout) -{ - octave_value_list retval; - - switch (type[0]) - { - case '(': - { - int tmp_nargout = (type.length () > 1 && nargout == 0) ? 1 : nargout; - - retval = call (tmp_nargout, idx.front ()); - } - break; - - case '{': - case '.': - { - std::string nm = type_name (); - error ("%s cannot be indexed with %c", nm.c_str (), type[0]); - } - break; - - default: - panic_impossible (); - } - - // FIXME: perhaps there should be an - // octave_value_list::next_subsref member function? See also - // octave_user_function::subsref. - // - // FIXME: Note that if a function call returns multiple - // values, and there is further indexing to perform, then we are - // ignoring all but the first value. Is this really what we want to - // do? If it is not, then what should happen for stat("file").size, - // for exmaple? - - if (idx.size () > 1) - retval = retval(0).next_subsref (nargout, type, idx); - - return retval; -} - // FIXME: shouldn't this declaration be a header file somewhere? extern octave_value_list call_mex (octave_mex_function& curr_mex_fcn, const octave_value_list& args,
--- a/libinterp/octave-value/ov-mex-fcn.h Fri May 19 16:05:03 2017 -0400 +++ b/libinterp/octave-value/ov-mex-fcn.h Mon May 22 13:45:19 2017 -0400 @@ -59,17 +59,6 @@ ~octave_mex_function (void); - octave_value subsref (const std::string& type, - const std::list<octave_value_list>& idx) - { - octave_value_list tmp = subsref (type, idx, 1); - return tmp.length () > 0 ? tmp(0) : octave_value (); - } - - octave_value_list subsref (const std::string& type, - const std::list<octave_value_list>& idx, - int nargout); - octave_function * function_value (bool = false) { return this; } const octave_function * function_value (bool = false) const { return this; }
--- a/libinterp/octave-value/ov-usr-fcn.cc Fri May 19 16:05:03 2017 -0400 +++ b/libinterp/octave-value/ov-usr-fcn.cc Mon May 22 13:45:19 2017 -0400 @@ -405,45 +405,6 @@ } octave_value_list -octave_user_function::subsref (const std::string& type, - const std::list<octave_value_list>& idx, - int nargout) -{ - octave_value_list retval; - - switch (type[0]) - { - case '(': - { - int tmp_nargout = (type.length () > 1 && nargout == 0) ? 1 : nargout; - - retval = call (tmp_nargout, idx.front ()); - } - break; - - case '{': - case '.': - { - std::string nm = type_name (); - error ("%s cannot be indexed with %c", nm.c_str (), type[0]); - } - break; - - default: - panic_impossible (); - } - - // FIXME: 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 (nargout, type, idx); - - return retval; -} - -octave_value_list octave_user_function::call (int nargout, const octave_value_list& _args) { octave_value_list retval;
--- a/libinterp/octave-value/ov-usr-fcn.h Fri May 19 16:05:03 2017 -0400 +++ b/libinterp/octave-value/ov-usr-fcn.h Mon May 22 13:45:19 2017 -0400 @@ -134,13 +134,6 @@ octave::sys::time time_checked (void) const { return t_checked; } - octave_value subsref (const std::string& type, - const std::list<octave_value_list>& idx) - { - octave_value_list tmp = subsref (type, idx, 1); - return tmp.length () > 0 ? tmp(0) : octave_value (); - } - octave_value_list subsref (const std::string& type, const std::list<octave_value_list>& idx, int nargout); @@ -359,17 +352,6 @@ ? (cname.empty () ? true : cname == dispatch_class ()) : false; } - octave_value subsref (const std::string& type, - const std::list<octave_value_list>& idx) - { - octave_value_list tmp = subsref (type, idx, 1); - return tmp.length () > 0 ? tmp(0) : octave_value (); - } - - octave_value_list subsref (const std::string& type, - const std::list<octave_value_list>& idx, - int nargout); - octave_value_list call (int nargout, const octave_value_list& args); octave::tree_parameter_list * parameter_list (void) { return param_list; }