]> Creatis software - CreaPhase.git/blob - octave_packages/m/miscellaneous/setappdata.m
update packages
[CreaPhase.git] / octave_packages / m / miscellaneous / setappdata.m
1 ## Copyright (C) 2010-2012 Ben Abbott
2 ##
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 ## GNU General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with Octave; see the file COPYING.  If not, see
15 ## <http://www.gnu.org/licenses/>.
16
17 ## -*- texinfo -*-
18 ## @deftypefn {Function File} {} setappdata (@var{h}, @var{name}, @var{value})
19 ## Set the named application data to @var{value} for the object(s) with
20 ## handle(s) @var{h}.  If the application data with the specified name does
21 ## not exist, it is created.
22 ## @end deftypefn
23
24 ## Author: Ben Abbott <bpabbott@mac.com>
25 ## Created: 2010-07-15
26
27 function setappdata (h, varargin)
28
29   if (! (all (ishandle (h)) && mod (numel (varargin), 2) == 0))
30     error ("setappdata: invalid input");
31   endif
32
33   for nh = 1:numel(h)
34     if (! isfield (get (h(nh)), "__appdata__"))
35       addproperty ("__appdata__", h(nh), "any", struct ());
36     endif
37     appdata = get (h(nh), "__appdata__");
38     for narg = 1:2:numel(varargin)
39       if (iscellstr (varargin{narg}))
40         ## Handle cell arrays like set() does.
41         set (h(nh), "__appdata__", appdata);
42         setappdata (h(nh), vertcat (varargin{narg}', varargin{narg+1}'){:});
43         appdata = get (h(nh), "__appdata__");
44       elseif (ischar (varargin{narg}))
45         appdata.(varargin{narg}) = varargin{narg+1};
46       else
47         error ("setappdata: invalid input");
48       endif
49     endfor
50     set (h(nh), "__appdata__", appdata);
51   endfor
52
53 endfunction
54
55 %!test
56 %! setappdata (0, "hello", "world")
57 %! assert (isappdata (0, "hello"), true)
58 %!assert (getappdata (0, "hello"), "world")
59