Mercurial > octave
changeset 28167:925fe981a25c
isprop.m: Add support for classdef objects (bug #58012).
* isprop.m: Document behavior for classdef objects.
Use isobject() to detect classdef object and implement new code branch.
Add BIST test for new behavior.
author | Rik <rik@octave.org> |
---|---|
date | Sat, 21 Mar 2020 20:04:02 -0700 |
parents | fab3eeb1fc7e |
children | d5dfbd5c80d6 |
files | scripts/plot/util/isprop.m |
diffstat | 1 files changed, 31 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/plot/util/isprop.m Sat Mar 21 19:30:32 2020 -0700 +++ b/scripts/plot/util/isprop.m Sat Mar 21 20:04:02 2020 -0700 @@ -31,29 +31,42 @@ ## logical array indicating whether each handle has the property @var{prop}. ## ## For plotting, @var{obj} is a handle to a graphics object. Otherwise, -## @var{obj} should be an instance of a class. -## @seealso{get, set, ismethod, isobject} +## @var{obj} should be an instance of a class. @code{isprop} reports whether +## the class defines a property, but @code{Access} permissions or visibility +## restrictions (@code{Hidden = true}) may prevent use by the programmer. +## @seealso{get, set, properties, ismethod, isobject} ## @end deftypefn function res = isprop (obj, prop) if (nargin != 2) print_usage (); - elseif (! ischar (prop)) + endif + + if (! ischar (prop)) error ("isprop: PROP name must be a string"); endif - warning ("error", "Octave:abbreviated-property-match", "local"); + if (isobject (obj)) + ## Separate code for classdef objects because Octave doesn't handle arrays + ## of objects and so can't use the generic code. + warning ("off", "Octave:classdef-to-struct", "local"); + + all_props = __fieldnames__ (obj); + res = any (strcmp (prop, all_props)); + else + warning ("error", "Octave:abbreviated-property-match", "local"); - res = false (size (obj)); - for i = 1:numel (res) - if (ishghandle (obj(i))) - try - v = get (obj(i), prop); - res(i) = true; - end_try_catch - endif - endfor + res = false (size (obj)); + for i = 1:numel (res) + if (ishghandle (obj(i))) + try + get (obj(i), prop); + res(i) = true; + end_try_catch + endif + endfor + endif endfunction @@ -63,6 +76,11 @@ %!assert (isprop (zeros (2, 3), "visible"), true (2, 3)) %!assert (isprop ([-2, -1, 0], "visible"), [false, false, true]) +%!test +%! m = containers.Map (); +%! assert (isprop (m, "KeyType")); +%! assert (! isprop (m, "FooBar")); + %!error isprop () %!error isprop (1) %!error isprop (1,2,3)