# HG changeset patch # User Rik # Date 1584846242 25200 # Node ID 925fe981a25c11e2d7e31a9977aa76648731fd8d # Parent fab3eeb1fc7ec970898bf353bfc9da72b5e5062d 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. diff -r fab3eeb1fc7e -r 925fe981a25c scripts/plot/util/isprop.m --- 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)