]> Creatis software - CreaPhase.git/blob - octave_packages/m/general/isa.m
update packages
[CreaPhase.git] / octave_packages / m / general / isa.m
1 ## Copyright (C) 2004-2012 John W. Eaton
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} {} isa (@var{obj}, @var{class})
21 ## Return true if @var{obj} is an object from the class @var{class}.
22 ## @seealso{class, typeinfo}
23 ## @end deftypefn
24
25 ## Author: Paul Kienzle <pkienzle@users.sf.net>
26 ## Adapted-by: jwe
27
28 function retval = isa (obj, cname)
29
30   if (nargin != 2)
31     print_usage ();
32   endif
33
34   persistent float_classes = {"double", "single"};
35
36   persistent fnum_classes = {"double", "single", ...
37                              "uint8", "uint16", "uint32", "uint64", ...
38                              "int8", "int16", "int32", "int64"};
39
40   if (strcmp (cname, "float"))
41     retval = any (strcmp (class (obj), float_classes));
42   elseif (strcmp (cname, "numeric"))
43     retval = any (strcmp (class (obj), fnum_classes));
44   else
45     class_of_x = class (obj);
46     retval = strcmp (class_of_x, cname);
47     if (! retval && isobject (obj))
48       retval = __isa_parent__ (obj, cname);
49     endif
50   endif
51
52 endfunction
53
54 %!assert (isa ("char", "float"), false)
55 %!assert (isa (logical (1), "float"), false)
56 %!assert (isa (double (13), "float"), true)
57 %!assert (isa (single (13), "float"), true)
58 %!assert (isa (int8 (13), "float"), false)
59 %!assert (isa (int16 (13), "float"), false)
60 %!assert (isa (int32 (13), "float"), false)
61 %!assert (isa (int64 (13), "float"), false)
62 %!assert (isa (uint8 (13), "float"), false)
63 %!assert (isa (uint16 (13), "float"), false)
64 %!assert (isa (uint32 (13), "float"), false)
65 %!assert (isa (uint64 (13), "float"), false)
66 %!assert (isa ("char", "numeric"), false)
67 %!assert (isa (logical (1), "numeric"), false)
68 %!assert (isa (double (13), "numeric"), true)
69 %!assert (isa (single (13), "numeric"), true)
70 %!assert (isa (int8 (13), "numeric"), true)
71 %!assert (isa (int16 (13), "numeric"), true)
72 %!assert (isa (int32 (13), "numeric"), true)
73 %!assert (isa (int64 (13), "numeric"), true)
74 %!assert (isa (uint8 (13), "numeric"), true)
75 %!assert (isa (uint16 (13), "numeric"), true)
76 %!assert (isa (uint32 (13), "numeric"), true)
77 %!assert (isa (uint64 (13), "numeric"), true)
78
79 %!assert (isa (double (13), "double"));
80 %!assert (isa (single (13), "single"));
81 %!assert (isa (int8 (13), "int8"));
82 %!assert (isa (int16 (13), "int16"));
83 %!assert (isa (int32 (13), "int32"));
84 %!assert (isa (int64 (13), "int64"));
85 %!assert (isa (uint8 (13), "uint8"));
86 %!assert (isa (uint16 (13), "uint16"));
87 %!assert (isa (uint32 (13), "uint32"));
88 %!assert (isa (uint64 (13), "uint64"));
89 %!assert (isa ("string", "char"));
90 %!assert (isa (true, "logical"));
91 %!assert (isa (false, "logical"));
92 %!assert (isa ({1, 2}, "cell"));
93 %!test
94 %! a.b = 1;
95 %! assert (isa (a, "struct"));
96