source: branches/xpascal/SourceNode.pas

Last change on this file was 232, checked in by chronos, 11 months ago
  • Fixed: Generation of var function parameters.
File size: 4.3 KB
Line 
1unit SourceNode;
2
3interface
4
5uses
6 Classes, SysUtils, Generics.Collections;
7
8type
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
55implementation
56
57resourcestring
58 SUnsupportedValueIndex = 'Unsupported value index %d';
59 SUnsupportedDataType = 'Unsupported field value data type %s';
60 SYes = 'Yes';
61 SNo = 'No';
62
63const
64 DataTypeStr: array[TDataType] of string = ('None', 'String', 'Boolean',
65 'Integer', 'Float', 'Color', 'Time', 'Date', 'DateTime', 'Enumeration',
66 'Reference', 'List');
67
68{ TField }
69
70constructor TField.Create(ADataType: TDataType; AName: string);
71begin
72 DataType := ADataType;
73 Name := AName;
74end;
75
76{ TSourceNode }
77
78procedure TSourceNode.GetValue(Index: Integer; out Value);
79begin
80 raise Exception.Create(Format(SUnsupportedValueIndex, [Index]));
81end;
82
83function TSourceNode.GetFieldsCount: Integer;
84begin
85 Result := 0;
86end;
87
88function TSourceNode.GetField(Index: Integer): TField;
89begin
90 Result := nil;
91 raise Exception.Create(Format(SUnsupportedValueIndex, [Index]));
92end;
93
94procedure TSourceNode.SetValue(Index: Integer; var Value);
95begin
96 raise Exception.Create(Format(SUnsupportedValueIndex, [Index]));
97end;
98
99function TSourceNode.GetValueInteger(Index: Integer): Integer;
100begin
101 GetValue(Index, Result);
102end;
103
104function TSourceNode.GetValueString(Index: Integer): string;
105begin
106 GetValue(Index, Result);
107end;
108
109function TSourceNode.GetValueBoolean(Index: Integer): Boolean;
110begin
111 GetValue(Index, Result);
112end;
113
114function TSourceNode.GetValueObject(Index: Integer): TObject;
115begin
116 GetValue(Index, Result);
117end;
118
119function TSourceNode.GetValueAsText(Index: Integer): string;
120var
121 Field: TField;
122 Item: TObject;
123begin
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;
140end;
141
142procedure TSourceNode.SetValueInteger(Index: Integer; Value: Integer);
143begin
144 SetValue(Index, Value);
145end;
146
147procedure TSourceNode.SetValueString(Index: Integer; Value: string);
148begin
149 SetValue(Index, Value);
150end;
151
152procedure TSourceNode.SetValueBoolean(Index: Integer; Value: Boolean);
153begin
154 SetValue(Index, Value);
155end;
156
157procedure TSourceNode.SetValueObject(Index: Integer; Value: TObject);
158begin
159 SetValue(Index, Value);
160end;
161
162function TSourceNode.GetFields: TFields;
163var
164 I: Integer;
165begin
166 Result := TFields.Create;
167 for I := 0 to GetFieldsCount - 1 do
168 Result.Add(GetField(I));
169end;
170
171
172end.
173
Note: See TracBrowser for help on using the repository browser.