annotate scripts/plot/util/isprop.m @ 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 a4268efb7334
children d8318c12d903
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
27919
1891570abac8 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27918
diff changeset
3 ## Copyright (C) 2010-2020 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
11374
d67d44f47949 isprop.m: New function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
7 ##
d67d44f47949 isprop.m: New function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
8 ## This file is part of Octave.
d67d44f47949 isprop.m: New function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24423
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
11374
d67d44f47949 isprop.m: New function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24423
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## (at your option) any later version.
11374
d67d44f47949 isprop.m: New function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
14 ##
d67d44f47949 isprop.m: New function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
d67d44f47949 isprop.m: New function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
11374
d67d44f47949 isprop.m: New function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
19 ##
d67d44f47949 isprop.m: New function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
d67d44f47949 isprop.m: New function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24423
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
11374
d67d44f47949 isprop.m: New function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
25
d67d44f47949 isprop.m: New function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 19697
diff changeset
27 ## @deftypefn {} {@var{res} =} isprop (@var{obj}, "@var{prop}")
18637
2631484789cf doc: Improve docstrings for isobject, ismethod, isprop.
Rik <rik@octave.org>
parents: 18533
diff changeset
28 ## Return true if @var{prop} is a property of the object @var{obj}.
17488
278ef6bd821d isprop.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
29 ##
18637
2631484789cf doc: Improve docstrings for isobject, ismethod, isprop.
Rik <rik@octave.org>
parents: 18533
diff changeset
30 ## @var{obj} may also be an array of objects in which case @var{res} will be a
17488
278ef6bd821d isprop.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
31 ## logical array indicating whether each handle has the property @var{prop}.
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 18637
diff changeset
32 ##
18637
2631484789cf doc: Improve docstrings for isobject, ismethod, isprop.
Rik <rik@octave.org>
parents: 18533
diff changeset
33 ## For plotting, @var{obj} is a handle to a graphics object. Otherwise,
28167
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
34 ## @var{obj} should be an instance of a class. @code{isprop} reports whether
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
35 ## the class defines a property, but @code{Access} permissions or visibility
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
36 ## restrictions (@code{Hidden = true}) may prevent use by the programmer.
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
37 ## @seealso{get, set, properties, ismethod, isobject}
11374
d67d44f47949 isprop.m: New function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
38 ## @end deftypefn
d67d44f47949 isprop.m: New function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
39
22765
01aae08a0105 maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
40 function res = isprop (obj, prop)
17122
eaab03308c0b doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents: 14868
diff changeset
41
17488
278ef6bd821d isprop.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
42 if (nargin != 2)
11374
d67d44f47949 isprop.m: New function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
43 print_usage ();
28167
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
44 endif
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
45
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
46 if (! ischar (prop))
17488
278ef6bd821d isprop.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
47 error ("isprop: PROP name must be a string");
11374
d67d44f47949 isprop.m: New function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
48 endif
d67d44f47949 isprop.m: New function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
49
28167
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
50 if (isobject (obj))
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
51 ## Separate code for classdef objects because Octave doesn't handle arrays
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
52 ## of objects and so can't use the generic code.
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
53 warning ("off", "Octave:classdef-to-struct", "local");
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
54
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
55 all_props = __fieldnames__ (obj);
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
56 res = any (strcmp (prop, all_props));
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
57 else
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
58 warning ("error", "Octave:abbreviated-property-match", "local");
24691
b658e0fa0cb3 maint: Remove trailing whitespace from files.
Rik <rik@octave.org>
parents: 24685
diff changeset
59
28167
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
60 res = false (size (obj));
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
61 for i = 1:numel (res)
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
62 if (ishghandle (obj(i)))
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
63 try
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
64 get (obj(i), prop);
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
65 res(i) = true;
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
66 end_try_catch
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
67 endif
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
68 endfor
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
69 endif
24691
b658e0fa0cb3 maint: Remove trailing whitespace from files.
Rik <rik@octave.org>
parents: 24685
diff changeset
70
11374
d67d44f47949 isprop.m: New function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
71 endfunction
d67d44f47949 isprop.m: New function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
72
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
73
11374
d67d44f47949 isprop.m: New function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
74 %!assert (isprop (0, "foobar"), false)
d67d44f47949 isprop.m: New function.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
75 %!assert (isprop (0, "screenpixelsperinch"), true)
13214
7715aca4bce1 Allow an nd-array of handles when calling isprop.m.
Ben Abbott <bpabbott@mac.com>
parents: 11587
diff changeset
76 %!assert (isprop (zeros (2, 3), "visible"), true (2, 3))
24403
b9462090773a legend.m: Setting DisplayName to "" should delete legend (bug #52641).
Rik <rik@octave.org>
parents: 23220
diff changeset
77 %!assert (isprop ([-2, -1, 0], "visible"), [false, false, true])
13214
7715aca4bce1 Allow an nd-array of handles when calling isprop.m.
Ben Abbott <bpabbott@mac.com>
parents: 11587
diff changeset
78
28167
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
79 %!test
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
80 %! m = containers.Map ();
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
81 %! assert (isprop (m, "KeyType"));
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
82 %! assert (! isprop (m, "FooBar"));
925fe981a25c isprop.m: Add support for classdef objects (bug #58012).
Rik <rik@octave.org>
parents: 27978
diff changeset
83
17488
278ef6bd821d isprop.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
84 %!error isprop ()
278ef6bd821d isprop.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
85 %!error isprop (1)
278ef6bd821d isprop.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
86 %!error isprop (1,2,3)
278ef6bd821d isprop.m: Overhaul function.
Rik <rik@octave.org>
parents: 17122
diff changeset
87 %!error <PROP name must be a string> isprop (0, {"visible"})