Changeset 344 for trunk/ItemList.pas
- Timestamp:
- Dec 23, 2024, 8:16:05 AM (10 hours ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/ItemList.pas
r342 r344 7 7 8 8 type 9 TItemList= class;9 //TItemList<T> = class; 10 10 TUndefinedEnum = (eeNone); 11 11 … … 34 34 function AddField(Index: Integer; SysName, Name: string; DataType: TDataType): TItemField; 35 35 function SearchByIndex(Index: Integer): TItemField; 36 end; 37 38 { IList } 39 40 IList<T> = interface 41 function GetName: string; 42 function GetCount: SizeInt; 43 function GetItem(Index: SizeInt): T; 44 function Remove(constref AValue: T): SizeInt; 45 function Add(constref AValue: T): SizeInt; 46 function CreateBaseItem(Name: string = ''): T; 47 function GetNextAvailableName(Name: string): string; 48 function GetItemFields: TItemFields; 49 procedure SetItem(Index: SizeInt; AValue: T); 50 property Count: SizeInt read GetCount; 51 property Items[Index: SizeInt]: T read GetItem write SetItem; default; 36 52 end; 37 53 … … 71 87 class function GetClassSysName: string; virtual; 72 88 class function GetClassName: string; virtual; 73 function GetReferenceList(Index: Integer): TItemList; virtual;89 function GetReferenceList(Index: Integer): IList<TItem>; virtual; 74 90 constructor Create; virtual; 75 91 end; … … 77 93 TItemClass = class of TItem; 78 94 95 IItemList = interface(IList<TItem>) 96 end; 97 79 98 { TItemList } 80 99 81 TItemList = class(TObjectList<TItem>)100 TItemList<T: TItem> = class(TObjectList<T>, IList<TItem>) 82 101 private 83 102 procedure RecalculateNewId(Reset: Boolean); 84 103 procedure RecalculateItemsId; 104 function GetItem(Index: SizeInt): TItem; 105 procedure SetItem(Index: SizeInt; AValue: TItem); 106 protected 107 function GetCount: SizeInt; override; 85 108 public 109 type 110 var 86 111 NewId: Integer; 87 class function GetItemClass: TItemClass; virtual; 112 function GetName: string; 113 function GetItemFields: TItemFields; 114 function Remove(constref AValue: TItem): SizeInt; 115 function Add(constref AValue: TItem): SizeInt; 116 function CreateItem(Name: string = ''): T; virtual; 117 function CreateBaseItem(Name: string = ''): TItem; 88 118 function IncrementName(Name: string): string; 89 119 function GetNextAvailableName(Name: string): string; 90 function FindById(Id: Integer): T Item;91 function FindByName(Name: string): T Item;120 function FindById(Id: Integer): T; 121 function FindByName(Name: string): T; 92 122 function GetNewId: Integer; 93 123 function ToString: string; override; 94 procedure Assign(Source: TItemList); virtual; 95 function Compare(ItemList: TItemList): Boolean; virtual; 96 function AddItem(Name: string = ''): TItem; virtual; 97 function CreateItem(Name: string = ''): TItem; virtual; 124 procedure Assign(Source: TItemList<T>); virtual; 125 function Compare(ItemList: TItemList<T>): Boolean; virtual; 126 function AddItem(Name: string = ''): T; virtual; 98 127 procedure LoadFromNode(Node: TDOMNode); virtual; 99 128 procedure SaveToNode(Node: TDOMNode); virtual; 100 129 constructor Create(FreeObjects: Boolean = True); 130 function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF}; 131 function _AddRef: Longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF}; 132 function _Release: Longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF}; 101 133 end; 102 134 … … 145 177 { TItemList } 146 178 147 procedure TItemList .Assign(Source: TItemList);179 procedure TItemList<T>.Assign(Source: TItemList<T>); 148 180 var 149 181 I: Integer; … … 155 187 end; 156 188 157 function TItemList .Compare(ItemList: TItemList): Boolean;189 function TItemList<T>.Compare(ItemList: TItemList<T>): Boolean; 158 190 var 159 191 I: Integer; … … 167 199 end; 168 200 169 function TItemList.AddItem(Name: string): TItem; 170 begin 171 Result := CreateItem(Name); 201 function TItemList<T>.AddItem(Name: string): T; 202 begin 203 Result := T.Create; 204 Result.Name := Name; 172 205 Result.Id := GetNewId; 173 206 Add(Result); 174 207 end; 175 208 176 function TItemList .CreateItem(Name: string): TItem;177 begin 178 Result := GetItemClass.Create;209 function TItemList<T>.CreateItem(Name: string): T; 210 begin 211 Result := T.Create; 179 212 Result.Name := Name; 180 213 end; 181 214 182 procedure TItemList.LoadFromNode(Node: TDOMNode); 215 function TItemList<T>.CreateBaseItem(Name: string): TItem; 216 begin 217 Result := TItem(CreateItem(Name)); 218 end; 219 220 procedure TItemList<T>.LoadFromNode(Node: TDOMNode); 183 221 var 184 222 Node2: TDOMNode; 185 NewItem: T Item;223 NewItem: T; 186 224 begin 187 225 Count := 0; 188 226 Node2 := Node.FirstChild; 189 while Assigned(Node2) and (Node2.NodeName = UnicodeString( GetItemClass.GetClassSysName)) do begin227 while Assigned(Node2) and (Node2.NodeName = UnicodeString(T.GetClassSysName)) do begin 190 228 NewItem := CreateItem; 191 229 NewItem.LoadFromNode(Node2); … … 195 233 end; 196 234 197 procedure TItemList .SaveToNode(Node: TDOMNode);235 procedure TItemList<T>.SaveToNode(Node: TDOMNode); 198 236 var 199 237 I: Integer; … … 203 241 for I := 0 to Count - 1 do 204 242 with TItem(Items[I]) do begin 205 NewNode2 := Node.OwnerDocument.CreateElement(UnicodeString( GetItemClass.GetClassSysName));243 NewNode2 := Node.OwnerDocument.CreateElement(UnicodeString(T.GetClassSysName)); 206 244 Node.AppendChild(NewNode2); 207 245 SaveToNode(NewNode2); … … 209 247 end; 210 248 211 constructor TItemList .Create(FreeObjects: Boolean);249 constructor TItemList<T>.Create(FreeObjects: Boolean); 212 250 begin 213 251 inherited; … … 215 253 end; 216 254 217 procedure TItemList.RecalculateNewId(Reset: Boolean); 255 function TItemList<T>.QueryInterface(constref iid: tguid; out obj): longint; 256 stdcall; 257 begin 258 end; 259 260 function TItemList<T>._AddRef: Longint; stdcall; 261 begin 262 end; 263 264 function TItemList<T>._Release: Longint; stdcall; 265 begin 266 end; 267 268 function TItemList<T>.GetCount: SizeInt; 269 begin 270 Result := Count; 271 end; 272 273 function TItemList<T>.GetName: string; 274 begin 275 Result := T.GetClassName; 276 end; 277 278 function TItemList<T>.GetItemFields: TItemFields; 279 begin 280 Result := T.GetFields; 281 end; 282 283 function TItemList<T>.Remove(constref AValue: TItem): SizeInt; 284 begin 285 Result := inherited Remove(T(AValue)); 286 end; 287 288 function TItemList<T>.Add(constref AValue: TItem): SizeInt; 289 begin 290 Result := inherited Add(T(AValue)); 291 end; 292 293 procedure TItemList<T>.RecalculateNewId(Reset: Boolean); 218 294 var 219 295 I: Integer; … … 226 302 end; 227 303 228 procedure TItemList .RecalculateItemsId;304 procedure TItemList<T>.RecalculateItemsId; 229 305 var 230 306 I: Integer; … … 235 311 end; 236 312 237 class function TItemList.GetItemClass: TItemClass; 238 begin 239 Result := TItem; 240 end; 241 242 function TItemList.IncrementName(Name: string): string; 313 function TItemList<T>.GetItem(Index: SizeInt): TItem; 314 begin 315 Result := inherited GetItem(Index); 316 end; 317 318 procedure TItemList<T>.SetItem(Index: SizeInt; AValue: TItem); 319 begin 320 inherited SetItem(Index, T(AValue)); 321 end; 322 323 function TItemList<T>.IncrementName(Name: string): string; 243 324 var 244 325 I: Integer; … … 253 334 end; 254 335 255 function TItemList .GetNextAvailableName(Name: string): string;336 function TItemList<T>.GetNextAvailableName(Name: string): string; 256 337 begin 257 338 Result := Name; … … 260 341 end; 261 342 262 function TItemList .FindById(Id: Integer): TItem;343 function TItemList<T>.FindById(Id: Integer): T; 263 344 var 264 345 I: Integer; … … 270 351 end; 271 352 272 function TItemList .FindByName(Name: string): TItem;353 function TItemList<T>.FindByName(Name: string): T; 273 354 var 274 355 I: Integer; … … 280 361 end; 281 362 282 function TItemList .GetNewId: Integer;363 function TItemList<T>.GetNewId: Integer; 283 364 begin 284 365 Result := NewId; … … 286 367 end; 287 368 288 function TItemList .ToString: string;369 function TItemList<T>.ToString: string; 289 370 var 290 371 I: Integer; … … 370 451 var 371 452 ReadId: Integer; 372 ReferenceList: TItemList;453 ReferenceList: IList<TItem>; 373 454 begin 374 455 if Field.DataType = dtString then begin … … 389 470 if Field.DataType = dtReference then begin 390 471 ReadId := ReadInteger(Node, Field.SysName, 0); 391 ReferenceList := GetReferenceList(Field.Index);472 ReferenceList := IList<TItem>(GetReferenceList(Field.Index)); 392 473 if (ReadId > 0) and Assigned(ReferenceList) then 393 474 SetValueReference(Field.Index, TItem(ReferenceList[ReadId])); … … 632 713 end; 633 714 634 function TItem.GetReferenceList(Index: Integer): TItemList;715 function TItem.GetReferenceList(Index: Integer): IList<TItem>; 635 716 begin 636 717 Result := nil;
Note:
See TracChangeset
for help on using the changeset viewer.