source: issm/trunk/src/m/kml/kml_object.m@ 6480

Last change on this file since 6480 was 6480, checked in by jschierm, 14 years ago

Eliminated redundant type checking in kml class constructors.

File size: 4.7 KB
Line 
1%
2% definition for the kml_object super (base) class.
3%
4% [kml]=kml_object(varargin)
5%
6% where the optional varargin and defaults are:
7% id (char, object id, '')
8%
9% note that zero arguments constructs a default instance; one
10% argument of the class copies the instance; and two or more
11% arguments constructs a new instance from the arguments.
12%
13classdef kml_object
14 properties
15 id ='';
16 end
17
18 methods
19 function [kml]=kml_object(varargin)
20
21 switch nargin
22
23% create a default object
24
25 case 0
26
27% copy the object or create the object from the input
28
29 otherwise
30 if (nargin == 1) && isa(varargin{1},class(kml))
31 kml=varargin{1};
32
33 else
34 fnames=fieldnames(kml_object());
35
36 for i=1:min(nargin,length(fnames))
37 if isa(varargin{i},class(kml.(fnames{i})))
38 if ~isempty(varargin{i})
39 kml.(fnames{i})=varargin{i};
40 end
41 else
42 if ~isempty(inputname(i))
43 warning('Argument ''%s'' for property ''%s'' is a ''%s'' class object, not ''%s''.',...
44 inputname(i),fnames{i},class(varargin{i}),class(kml.(fnames{i})));
45 else
46 warning('Argument %d for property ''%s'' is a ''%s'' class object, not ''%s''.',...
47 i ,fnames{i},class(varargin{i}),class(kml.(fnames{i})));
48 end
49 end
50 end
51 end
52
53 end
54
55 end
56
57% display the object
58
59 function []=disp(kml)
60
61 for i=1:numel(kml)
62 if strcmp(class(kml),'kml_object')
63 disp(sprintf('class ''%s'' object ''%s%s'' = \n',...
64 class(kml),inputname(1),string_dim(kml,i)));
65 end
66
67 if strcmp(class(kml),'kml_object')
68 disp(sprintf(' id: ''%s''\n',kml(i).id));
69 else
70 disp(sprintf(' id: ''%s''' ,kml(i).id));
71 end
72 end
73
74 end
75
76% return the fieldnames of the object
77
78 function [fnames]=fieldnames(kml)
79
80% fieldnames for a sub (derived) class list those before super (base)
81
82 fnames={'id'};
83
84 end
85
86% set the properties of the object
87
88 function [kml]=set(kml,varargin)
89
90 kmlref=feval(class(kml));
91 fnames=fieldnames(kmlref);
92
93% loop through each parameter in the input list (comparing to the reference
94% object in case property types have been changed)
95
96 for i=1:2:length(varargin)
97 if ismember(varargin{i},fnames) && (i+1 <= length(varargin))
98 if isa(varargin{i+1},class(kmlref.(varargin{i})))
99 kml.(varargin{i})=varargin{i+1};
100 else
101 if ~isempty(inputname(i+1))
102 warning('Argument ''%s'' for property ''%s'' is a ''%s'' class object, not ''%s''.',...
103 inputname(i+2),varargin{i},class(varargin{i+1}),class(kmlref.(varargin{i})));
104 else
105 warning('Argument %d for property ''%s'' is a ''%s'' class object, not ''%s''.',...
106 i+2 ,varargin{i},class(varargin{i+1}),class(kmlref.(varargin{i})));
107 end
108 end
109 else
110 warning('Property ''%s'' for class ''%s'' does not exist.',...
111 varargin{i},class(kmlref));
112 end
113 end
114
115 end
116
117% write the object
118
119 function []=kml_write(kml,fid,indent)
120
121 if ~exist('fid','var') || isempty(fid)
122 fid=1;
123 end
124 if ~exist('indent','var') || isempty(indent)
125 indent='';
126 end
127
128% loop over the objects
129
130 for i=1:numel(kml)
131 if strcmp(class(kml),'kml_object')
132 if ~isempty(kml(i).id)
133 fprintf(fid,'%s<!Object id="%s">\n',indent,kml(i).id);
134 else
135 fprintf(fid,'%s<!Object>\n',indent);
136 end
137 end
138
139 if strcmp(class(kml),'kml_object')
140 fprintf(fid,'%s</!Object>\n',indent);
141 end
142 end
143
144 end
145
146 end
147
148end
149
Note: See TracBrowser for help on using the repository browser.