1 | unit SourceNode;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Generics.Collections;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TDataType = (dtNone, dtString, dtBoolean, dtInteger, dtFloat, dtColor,
|
---|
10 | dtTime, dtDate, dtDateTime, dtEnumeration, dtObject, dtList);
|
---|
11 |
|
---|
12 | { TField }
|
---|
13 |
|
---|
14 | TField = class
|
---|
15 | Index: Integer;
|
---|
16 | DataType: TDataType;
|
---|
17 | Name: string;
|
---|
18 | constructor Create(ADataType: TDataType; AName: string);
|
---|
19 | end;
|
---|
20 |
|
---|
21 | TFields = class(TObjectList<TField>)
|
---|
22 | end;
|
---|
23 |
|
---|
24 | { TSourceNode }
|
---|
25 |
|
---|
26 | TSourceNode = class
|
---|
27 | protected
|
---|
28 | function GetFieldsCount: Integer; virtual;
|
---|
29 | public
|
---|
30 | Parent: TSourceNode;
|
---|
31 | function GetField(Index: Integer): TField; virtual;
|
---|
32 | procedure GetValue(Index: Integer; out Value); virtual;
|
---|
33 | function GetValueInteger(Index: Integer): Integer;
|
---|
34 | function GetValueString(Index: Integer): string;
|
---|
35 | function GetValueBoolean(Index: Integer): Boolean;
|
---|
36 | function GetValueObject(Index: Integer): TObject;
|
---|
37 | function GetValueAsText(Index: Integer): string;
|
---|
38 | procedure SetValue(Index: Integer; var Value); virtual;
|
---|
39 | procedure SetValueInteger(Index: Integer; Value: Integer);
|
---|
40 | procedure SetValueString(Index: Integer; Value: string);
|
---|
41 | procedure SetValueBoolean(Index: Integer; Value: Boolean);
|
---|
42 | procedure SetValueObject(Index: Integer; Value: TObject);
|
---|
43 | function GetFields: TFields;
|
---|
44 | property FieldsCount: Integer read GetFieldsCount;
|
---|
45 | end;
|
---|
46 |
|
---|
47 | { TSourceNodeList }
|
---|
48 |
|
---|
49 | TSourceNodeList<T: TSourceNode> = class(TObjectList<T>)
|
---|
50 | private
|
---|
51 | Parent: TSourceNode;
|
---|
52 | end;
|
---|
53 |
|
---|
54 |
|
---|
55 | implementation
|
---|
56 |
|
---|
57 | resourcestring
|
---|
58 | SUnsupportedValueIndex = 'Unsupported value index %d';
|
---|
59 | SUnsupportedDataType = 'Unsupported field value data type %s';
|
---|
60 | SYes = 'Yes';
|
---|
61 | SNo = 'No';
|
---|
62 |
|
---|
63 | const
|
---|
64 | DataTypeStr: array[TDataType] of string = ('None', 'String', 'Boolean',
|
---|
65 | 'Integer', 'Float', 'Color', 'Time', 'Date', 'DateTime', 'Enumeration',
|
---|
66 | 'Reference', 'List');
|
---|
67 |
|
---|
68 | { TField }
|
---|
69 |
|
---|
70 | constructor TField.Create(ADataType: TDataType; AName: string);
|
---|
71 | begin
|
---|
72 | DataType := ADataType;
|
---|
73 | Name := AName;
|
---|
74 | end;
|
---|
75 |
|
---|
76 | { TSourceNode }
|
---|
77 |
|
---|
78 | procedure TSourceNode.GetValue(Index: Integer; out Value);
|
---|
79 | begin
|
---|
80 | raise Exception.Create(Format(SUnsupportedValueIndex, [Index]));
|
---|
81 | end;
|
---|
82 |
|
---|
83 | function TSourceNode.GetFieldsCount: Integer;
|
---|
84 | begin
|
---|
85 | Result := 0;
|
---|
86 | end;
|
---|
87 |
|
---|
88 | function TSourceNode.GetField(Index: Integer): TField;
|
---|
89 | begin
|
---|
90 | Result := nil;
|
---|
91 | raise Exception.Create(Format(SUnsupportedValueIndex, [Index]));
|
---|
92 | end;
|
---|
93 |
|
---|
94 | procedure TSourceNode.SetValue(Index: Integer; var Value);
|
---|
95 | begin
|
---|
96 | raise Exception.Create(Format(SUnsupportedValueIndex, [Index]));
|
---|
97 | end;
|
---|
98 |
|
---|
99 | function TSourceNode.GetValueInteger(Index: Integer): Integer;
|
---|
100 | begin
|
---|
101 | GetValue(Index, Result);
|
---|
102 | end;
|
---|
103 |
|
---|
104 | function TSourceNode.GetValueString(Index: Integer): string;
|
---|
105 | begin
|
---|
106 | GetValue(Index, Result);
|
---|
107 | end;
|
---|
108 |
|
---|
109 | function TSourceNode.GetValueBoolean(Index: Integer): Boolean;
|
---|
110 | begin
|
---|
111 | GetValue(Index, Result);
|
---|
112 | end;
|
---|
113 |
|
---|
114 | function TSourceNode.GetValueObject(Index: Integer): TObject;
|
---|
115 | begin
|
---|
116 | GetValue(Index, Result);
|
---|
117 | end;
|
---|
118 |
|
---|
119 | function TSourceNode.GetValueAsText(Index: Integer): string;
|
---|
120 | var
|
---|
121 | Field: TField;
|
---|
122 | Item: TObject;
|
---|
123 | begin
|
---|
124 | Field := GetField(Index);
|
---|
125 | try
|
---|
126 | if Field.DataType = dtInteger then Result := IntToStr(GetValueInteger(Index))
|
---|
127 | else if Field.DataType = dtString then Result := GetValueString(Index)
|
---|
128 | //else if Field.DataType = dtEnumeration then Result := Field.EnumStates[Integer(GetValueEnumeration(Index))]
|
---|
129 | else if Field.DataType = dtObject then begin
|
---|
130 | Item := TObject(GetValueObject(Index));
|
---|
131 | if Assigned(Item) then Result := Item.ClassName
|
---|
132 | else Result := '';
|
---|
133 | end else if Field.DataType = dtBoolean then begin
|
---|
134 | if GetValueBoolean(Index) then Result := SYes else Result := SNo;
|
---|
135 | end else
|
---|
136 | raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]]));
|
---|
137 | finally
|
---|
138 | Field.Free;
|
---|
139 | end;
|
---|
140 | end;
|
---|
141 |
|
---|
142 | procedure TSourceNode.SetValueInteger(Index: Integer; Value: Integer);
|
---|
143 | begin
|
---|
144 | SetValue(Index, Value);
|
---|
145 | end;
|
---|
146 |
|
---|
147 | procedure TSourceNode.SetValueString(Index: Integer; Value: string);
|
---|
148 | begin
|
---|
149 | SetValue(Index, Value);
|
---|
150 | end;
|
---|
151 |
|
---|
152 | procedure TSourceNode.SetValueBoolean(Index: Integer; Value: Boolean);
|
---|
153 | begin
|
---|
154 | SetValue(Index, Value);
|
---|
155 | end;
|
---|
156 |
|
---|
157 | procedure TSourceNode.SetValueObject(Index: Integer; Value: TObject);
|
---|
158 | begin
|
---|
159 | SetValue(Index, Value);
|
---|
160 | end;
|
---|
161 |
|
---|
162 | function TSourceNode.GetFields: TFields;
|
---|
163 | var
|
---|
164 | I: Integer;
|
---|
165 | begin
|
---|
166 | Result := TFields.Create;
|
---|
167 | for I := 0 to GetFieldsCount - 1 do
|
---|
168 | Result.Add(GetField(I));
|
---|
169 | end;
|
---|
170 |
|
---|
171 |
|
---|
172 | end.
|
---|
173 |
|
---|