Mercurial > hg > octave-nkf
changeset 13193:a00ff5cedb9b
also look to parent classes for overloaded functions called through handles
* ov-fcn-handle.cc (octave_fcn_handle::do_multi_index_op):
Look for overloads in parent classes if none are found in the
immediate dispatch class.
* test/fcn-handle-derived-resolution: New directory for tests.
* test/Makefile.am: Include fcn-handle-derived-resolution/module.mk.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 22 Sep 2011 17:08:49 -0400 |
parents | 968e89b45bbf |
children | 3e1871badab9 |
files | src/ov-fcn-handle.cc test/Makefile.am test/fcn-handle-derived-resolution/@derived/derived.m test/fcn-handle-derived-resolution/@other/getsize_arrayfun.m test/fcn-handle-derived-resolution/@other/getsize_cellfun.m test/fcn-handle-derived-resolution/@other/getsize_loop.m test/fcn-handle-derived-resolution/@other/other.m test/fcn-handle-derived-resolution/@parent/numel.m test/fcn-handle-derived-resolution/@parent/parent.m test/fcn-handle-derived-resolution/module.mk test/fcn-handle-derived-resolution/test_fcn_handle_derived_resolution.m |
diffstat | 11 files changed, 132 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ov-fcn-handle.cc +++ b/src/ov-fcn-handle.cc @@ -164,7 +164,38 @@ else { str_ov_map::iterator it = overloads.find (dispatch_type); - if (it != overloads.end ()) + + if (it == overloads.end ()) + { + // Try parent classes too. + + std::list<std::string> plist + = symbol_table::parent_classes (dispatch_type); + + std::list<std::string>::const_iterator pit = plist.begin (); + + while (pit != plist.end ()) + { + std::string pname = *pit; + + std::string fnm = fcn_name (); + + octave_value ftmp = symbol_table::find_method (fnm, pname); + + if (ftmp.is_defined ()) + { + set_overload (pname, ftmp); + + out_of_date_check (ftmp, pname, false); + ov_fcn = ftmp; + + break; + } + + pit++; + } + } + else { out_of_date_check (it->second, dispatch_type, false); ov_fcn = it->second;
--- a/test/Makefile.am +++ b/test/Makefile.am @@ -53,6 +53,7 @@ include classes/module.mk include ctor-vs-method/module.mk +include fcn-handle-derived-resolution/module.mk check: test_sparse.m test_bc_overloads.m $(top_builddir)/run-octave --norc --silent --no-history $(srcdir)/fntests.m $(srcdir)
new file mode 100644 --- /dev/null +++ b/test/fcn-handle-derived-resolution/@derived/derived.m @@ -0,0 +1,5 @@ +function r = derived (n) + s.a = n; + p = parent (n); + r = class (s, 'derived', p); +end
new file mode 100644 --- /dev/null +++ b/test/fcn-handle-derived-resolution/@other/getsize_arrayfun.m @@ -0,0 +1,3 @@ +function r = getsize_arrayfun (x) + r = arrayfun (@(i) numel (x(i).d), 1:numel(x), 'uniformoutput', true); +end
new file mode 100644 --- /dev/null +++ b/test/fcn-handle-derived-resolution/@other/getsize_cellfun.m @@ -0,0 +1,3 @@ +function r = getsize_cellfun (x) + r = cellfun (@numel, {x.d}); +end
new file mode 100644 --- /dev/null +++ b/test/fcn-handle-derived-resolution/@other/getsize_loop.m @@ -0,0 +1,7 @@ +function r = getsize_loop (x) + n = numel (x); + r = zeros (1, n); + for i = 1:n + r(i) = numel (x(i).d); + end +end
new file mode 100644 --- /dev/null +++ b/test/fcn-handle-derived-resolution/@other/other.m @@ -0,0 +1,4 @@ +function r = other (n) + s.d = derived (n); + r = class (s, 'other'); +end
new file mode 100644 --- /dev/null +++ b/test/fcn-handle-derived-resolution/@parent/numel.m @@ -0,0 +1,3 @@ +function r = numel (x, varargin) + r = numel (x.a, varargin{:}); +end
new file mode 100644 --- /dev/null +++ b/test/fcn-handle-derived-resolution/@parent/parent.m @@ -0,0 +1,4 @@ +function r = parent (n) + s.a = rand (n, 1); + r = class (s, 'parent'); +end
new file mode 100644 --- /dev/null +++ b/test/fcn-handle-derived-resolution/module.mk @@ -0,0 +1,11 @@ +fcn_handle_derived_resolution_FCN_FILES = \ + fcn-handle-derived-resolution/@derived/derived.m \ + fcn-handle-derived-resolution/@other/getsize_arrayfun.m \ + fcn-handle-derived-resolution/@other/getsize_cellfun.m \ + fcn-handle-derived-resolution/@other/getsize_loop.m \ + fcn-handle-derived-resolution/@other/other.m \ + fcn-handle-derived-resolution/@parent/numel.m \ + fcn-handle-derived-resolution/@parent/parent.m \ + test_fcn_handle_derived_resolution.m + +FCN_FILES += $(fcn_handle_derived_resolution_FCN_FILES)
new file mode 100644 --- /dev/null +++ b/test/fcn-handle-derived-resolution/test_fcn_handle_derived_resolution.m @@ -0,0 +1,59 @@ +## Copyright (C) 2011 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 3 of the License, 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, see +## <http://www.gnu.org/licenses/>. + +%% Test script for legacy OOP. +%% Requires the path to contain the directory ctor-vs-method. +%% +%% Note: This script and all classes are also intended to run +%% in Matlab to test compatibility. Don't break that! + +%!shared +%! clear -classes + +%!test +%! p = parent (7); +%! assert (numel (p), 7) + +%!test +%! d = derived (13); +%! assert (numel (d), 13) + +%!test +%! p = parent (11); +%! f = @numel; +%! assert (f (p), 11) + +%!test +%! d = parent (21); +%! f = @numel; +%! assert (f (d), 21) + +%!test +%! o(1) = other (13); +%! o(2) = other (42); +%! assert (getsize_loop (o), [13, 42]) + +%!test +%! o(1) = other (13); +%! o(2) = other (42); +%! assert (getsize_cellfun (o), [13, 42]) + +%!test +%! o(1) = other (13); +%! o(2) = other (42); +%! assert (getsize_arrayfun (o), [13, 42])