Mercurial > hg > octave-nkf
comparison scripts/general/fieldnames.m @ 15781:c33594eefda7
Add fieldnames.m which extensds fieldnames() to work on Java objects.
Deprecate javafields. Rename old C++ fieldnames to __fieldnames__.
* scripts/deprecated/javafields.m: Moved from scripts/java. Added deprecated
warning.
* scripts/java/javafields.m: Moved to scripts/deprecated.
* scripts/general/fieldnames.m: New m-file which accepts Java, structure,
or Octave objects as inputs.
* libinterp/octave-value/ov-struct.cc(Ffieldnames): Renamed fieldnames to
__fieldnames__ to avoid class with fieldnames.m.
* scripts/deprecated/module.mk: Added javafields to deprecated build.
* scripts/general/module.mk: Added fieldnames.m to build.
* scripts/java/module.mk: Removed javafields.m from build.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 13 Dec 2012 10:57:04 -0800 |
parents | |
children | 921912c92102 |
comparison
equal
deleted
inserted
replaced
15780:f25101b1e37f | 15781:c33594eefda7 |
---|---|
1 ## Copyright (C) 2012 Rik Wehbring | |
2 ## | |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {@var{names} =} fieldnames (@var{struct}) | |
21 ## @deftypefnx {Function File} {@var{names} =} fieldnames (@var{obj}) | |
22 ## @deftypefnx {Function File} {@var{names} =} fieldnames (@var{javaobj}) | |
23 ## @deftypefnx {Function File} {@var{names} =} fieldnames ("@var{jclassname}") | |
24 ## Return a cell array of strings with the names of the fields in the | |
25 ## specified input. | |
26 ## | |
27 ## When the input is a structure @var{struct}, the names are the elements | |
28 ## of the structure. | |
29 ## | |
30 ## When the input is an Octave object @var{obj}, the names are the public | |
31 ## properties of the object. | |
32 ## | |
33 ## When the input is a Java object @var{javaobj} or Java classname | |
34 ## @var{jclassname}) the name are the public data elements of the object or | |
35 ## class. | |
36 ## @seealso{struct, methods} | |
37 ## @end deftypefn | |
38 | |
39 function names = fieldnames (obj) | |
40 | |
41 if (nargin != 1) | |
42 print_usage (); | |
43 endif | |
44 | |
45 if (isstruct (obj) || isobject (obj)) | |
46 ## Call internal C++ function for structs or Octave objects | |
47 names = __fieldnames__ (obj); | |
48 elseif (isjava (obj) || ischar (obj)) | |
49 names_str = java_invoke ("org.octave.ClassHelper", "getFields", obj); | |
50 names = strsplit (names_str, ';'); | |
51 else | |
52 error ("fieldnames: Invalid input argument"); | |
53 endif | |
54 | |
55 endfunction | |
56 | |
57 | |
58 ## test preservation of fieldname order | |
59 %!test | |
60 %! x(3).d=1; x(2).a=2; x(1).b=3; x(2).c=3; | |
61 %! assert (fieldnames (x), {"d"; "a"; "b"; "c"}); | |
62 | |
63 ## test empty structure | |
64 %!test | |
65 %! s = struct (); | |
66 %! assert (fieldnames (s), cell (0, 1)); | |
67 | |
68 ## test Java classname | |
69 %!testif HAVE_JAVA | |
70 %! names = fieldnames ("java.lang.Double"); | |
71 %! search = strfind (names, "java.lang.Double.MAX_VALUE"); | |
72 %! assert (! isempty ([search{:}])); | |
73 |