changeset 11180:1a26199cb212

Add new appdata functions.
author Ben Abbott <bpabbott@mac.com>
date Wed, 03 Nov 2010 15:10:47 +0800
parents 6ead75935ebf
children a512c537b38e
files scripts/ChangeLog scripts/miscellaneous/getappdata.m scripts/miscellaneous/isappdata.m scripts/miscellaneous/module.mk scripts/miscellaneous/rmappdata.m scripts/miscellaneous/setappdata.m
diffstat 6 files changed, 203 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Mon Aug 30 13:49:07 2010 -0500
+++ b/scripts/ChangeLog	Wed Nov 03 15:10:47 2010 +0800
@@ -1,3 +1,9 @@
+2010-11-03  Ben Abbott <bpabbott@mac.com>
+
+	* miscellaneous/getappdata.m, miscellaneous/isappdata.m,
+	miscellaneous/rmappdata.m, miscellaneous/setappdata.m:
+	Add new appdata function.
+
 2010-11-01  David Bateman  <dbateman@free.fr>
 
 	* plot/__private__/__contour__.m: Use __go_patch__ rather than patch
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/miscellaneous/getappdata.m	Wed Nov 03 15:10:47 2010 +0800
@@ -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
+## <http://www.gnu.org/licenses/>.
+
+## -*- 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 <bpabbott@mac.com>
+## 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
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/miscellaneous/isappdata.m	Wed Nov 03 15:10:47 2010 +0800
@@ -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
+## <http://www.gnu.org/licenses/>.
+
+## -*- 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 <bpabbott@mac.com>
+## 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)
+
--- a/scripts/miscellaneous/module.mk	Mon Aug 30 13:49:07 2010 -0500
+++ b/scripts/miscellaneous/module.mk	Wed Nov 03 15:10:47 2010 +0800
@@ -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 \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/miscellaneous/rmappdata.m	Wed Nov 03 15:10:47 2010 +0800
@@ -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
+## <http://www.gnu.org/licenses/>.
+
+## -*- 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 <bpabbott@mac.com>
+## 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)
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/miscellaneous/setappdata.m	Wed Nov 03 15:10:47 2010 +0800
@@ -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
+## <http://www.gnu.org/licenses/>.
+
+## -*- 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 <bpabbott@mac.com>
+## 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")
+