-
Notifications
You must be signed in to change notification settings - Fork 9
/
cmtobject.m
72 lines (60 loc) · 2.08 KB
/
cmtobject.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
classdef cmtobject
% CMTOBJECT provides some base CMT functionality for classes.
% This file is a part of the CMToolkit.
% It is licensed under the BSD 3-clause license.
% (See LICENSE.)
% Copyright Toby Driscoll, 2014.
methods
function prefs = get(this, property)
% Retrieve preference values for module (class) stored in app data
% facility.
prefs = getappdata(0, 'cmt_prefs');
module = class(this);
if isempty(prefs) || ~isfield(prefs, module)
% Set defaults.
if isa(property, 'optset')
set(this, property);
prefs = getappdata(0, 'cmt_prefs');
else
error('CMT:NotDefined', ...
'No default values given to set for class %s.', ...
module)
end
end
if isfield(prefs, module)
prefs = prefs.(module);
else
% This shouldn't actually happen.
error('CMT:NotDefined', ...
'Unable to retreive app data for class %s.', ...
module)
end
if nargin > 1 && ischar(property)
if isprop(prefs, property)
prefs = prefs.(property);
else
error('CMT:NotDefined', ...
'Property %s not defined for class %s%.', ...
property, module)
end
end
end
function set(this, varargin)
% Store preferences for module (class) in app data facility.
prefs = getappdata(0, 'cmt_prefs');
module = class(this);
if numel(varargin) == 1
if isa(varargin{1}, 'optset')
% Set default.
prefs.(module) = varargin{1};
varargin = {};
else
error('CMT:InvalidArgument', ...
'Expected an optset object.')
end
end
prefs.(module) = set(prefs.(module), varargin{:});
setappdata(0, 'cmt_prefs', prefs);
end
end
end