-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmtobject.m
81 lines (70 loc) · 2.67 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
73
74
75
76
77
78
79
80
81
classdef cmtobject
% CMTOBJECT provides some base CMT functionality for classes.
% Copyright 2015 the ConfMapTk project developers. See the COPYRIGHT
% file at the top-level directory of this distribution and at
% https://github.com/ehkropf/ConfMapTk.
%
% This file is part of ConfMapTk. It is subject to the license terms in
% the LICENSE file found in the top-level directory of this distribution
% and at https://github.com/ehkropf/ConfMapTk. No part of ConfMapTk,
% including this file, may be copied, modified, propagated, or distributed
% except according to the terms contained in the LICENSE file.
%
% 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 LICENSE
% file.
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