# HG changeset patch # User Ben Abbott # Date 1288768247 -28800 # Node ID 1a26199cb21227db8354b6b7cb1dc3ec4c217c51 # Parent 6ead75935ebf0234fb145c3e7d3cdeb978323916 Add new appdata functions. diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,9 @@ +2010-11-03 Ben Abbott + + * miscellaneous/getappdata.m, miscellaneous/isappdata.m, + miscellaneous/rmappdata.m, miscellaneous/setappdata.m: + Add new appdata function. + 2010-11-01 David Bateman * plot/__private__/__contour__.m: Use __go_patch__ rather than patch diff --git a/scripts/miscellaneous/getappdata.m b/scripts/miscellaneous/getappdata.m new file mode 100644 --- /dev/null +++ b/scripts/miscellaneous/getappdata.m @@ -0,0 +1,43 @@ +## Copyright (C) 2010 Ben Abbott +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{value} =} getappdata (@var{h}, @var{name}) +## Returns the @var{value} for named application data for the object(s) with +## handle(s) @var{h}. +## @end deftypefn + +## Author: Ben Abbott +## Created: 2010-07-15 + +function val = getappdata (h, name) + + if (! (all (ishandle (h)) && ischar (name))) + error ("getappdata: invalid input.") + endif + + appdata(numel(h)) = struct(); + for nh = 1:numel(h) + appdata(nh) = get (h(nh), "__appdata__"); + end + if (nh > 1) + val = {appdata.(name)}; + else + val = appdata.(name); + endif + +endfunction + diff --git a/scripts/miscellaneous/isappdata.m b/scripts/miscellaneous/isappdata.m new file mode 100644 --- /dev/null +++ b/scripts/miscellaneous/isappdata.m @@ -0,0 +1,47 @@ +## Copyright (C) 2010 Ben Abbott +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{V} =} isappdata (@var{h}, @var{name}) +## Returns a logical vector indicating whether the named application data +## for the object(s) with handle(s) @var{H} exists. +## @end deftypefn + +## Author: Ben Abbott +## Created: 2010-07-15 + +function res = isappdata (h, name) + + if (! (all (ishandle (h)) && ischar (name))) + error ("isappdata: invalid input.") + endif + + for nh = 1:numel(h) + data = get (h(nh)); + if (isfield (data, "__appdata__") && isfield (data.__appdata__, name)) + res(nh) = true; + else + res(nh) = false; + endif + endfor + +endfunction + +%!test +%! setappdata (0, "hello", "world") +%! assert (isappdata (0, "hello"), true) +%!assert (isappdata (0, "foobar"), false) + diff --git a/scripts/miscellaneous/module.mk b/scripts/miscellaneous/module.mk --- a/scripts/miscellaneous/module.mk +++ b/scripts/miscellaneous/module.mk @@ -24,11 +24,13 @@ miscellaneous/fileparts.m \ miscellaneous/flops.m \ miscellaneous/fullfile.m \ + miscellaneous/getappdata.m \ miscellaneous/getfield.m \ miscellaneous/gunzip.m \ miscellaneous/gzip.m \ miscellaneous/info.m \ miscellaneous/inputname.m \ + miscellaneous/isappdata.m \ miscellaneous/ismac.m \ miscellaneous/ispc.m \ miscellaneous/isunix.m \ @@ -48,8 +50,10 @@ miscellaneous/paren.m \ miscellaneous/parseparams.m \ miscellaneous/perl.m \ + miscellaneous/rmappdata.m \ miscellaneous/run.m \ miscellaneous/semicolon.m \ + miscellaneous/setappdata.m \ miscellaneous/setfield.m \ miscellaneous/substruct.m \ miscellaneous/swapbytes.m \ diff --git a/scripts/miscellaneous/rmappdata.m b/scripts/miscellaneous/rmappdata.m new file mode 100644 --- /dev/null +++ b/scripts/miscellaneous/rmappdata.m @@ -0,0 +1,44 @@ +## Copyright (C) 2010 Ben Abbott +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## . + +## -*- texinfo -*- +## @deftypefn {Function File} {} rmappdata (@var{h}, @var{name}) +## Deletes the named application data for the object(s) with +## handle(s) @var{h}. +## @end deftypefn + +## Author: Ben Abbott +## Created: 2010-07-15 + +function rmappdata (h, varargin) + + if (! (all (ishandle (h)) && iscellstr (varargin))) + error ("rmappdata: invalid input.") + endif + + for nh = 1:numel(h) + appdata = get (h(nh), "__appdata__"); + appdata = rmfield (appdata, varargin); + set (h(nh), "__appdata__", appdata); + endfor + +endfunction + +%!test +%! setappdata (0, "hello", "world") +%! rmappdata (0, "hello") +%! assert (isappdata (0, "hello"), false) + diff --git a/scripts/miscellaneous/setappdata.m b/scripts/miscellaneous/setappdata.m new file mode 100644 --- /dev/null +++ b/scripts/miscellaneous/setappdata.m @@ -0,0 +1,59 @@ +## Copyright (C) 2010 Ben Abbott +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## . + +## -*- texinfo -*- +## @deftypefn {Function File} {} setappdata (@var{h}, @var{name}, @var{value}) +## Sets the named application data to @var{value} for the object(s) with +## handle(s) @var{h}. If the application data with the specified name does +## not exist, it is created. +## @end deftypefn + +## Author: Ben Abbott +## Created: 2010-07-15 + +function setappdata (h, varargin) + + if (! (all (ishandle (h)) && mod (numel (varargin), 2) == 0)) + error ("setappdata: invalid input.") + endif + + for nh = 1:numel(h) + if (! isfield (get (h(nh)), "__appdata__")) + addproperty ("__appdata__", h(nh), "any", struct ()); + end + appdata = get (h(nh), "__appdata__"); + for narg = 1:2:numel(varargin) + if (iscellstr (varargin{narg})) + ## Handle cell arrays like set() does. + set (h(nh), "__appdata__", appdata); + setappdata (h(nh), vertcat (varargin{narg}', varargin{narg+1}'){:}); + appdata = get (h(nh), "__appdata__"); + elseif (ischar (varargin{narg})) + appdata.(varargin{narg}) = varargin{narg+1}; + else + error ("setappdata: invalid input.") + endif + endfor + set (h(nh), "__appdata__", appdata); + endfor + +endfunction + +%!test +%! setappdata (0, "hello", "world") +%! assert (isappdata (0, "hello"), true) +%!assert (getappdata (0, "hello"), "world") +