Changeset 349 for trunk/ItemList.pas
- Timestamp:
- Dec 27, 2024, 10:29:54 AM (11 days ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/ItemList.pas
r347 r349 36 36 end; 37 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; 52 end; 38 TBaseItemList = class; 53 39 54 40 { TItem } … … 87 73 class function GetClassSysName: string; virtual; 88 74 class function GetClassName: string; virtual; 89 function GetReferenceList(Index: Integer): IList<TItem>; virtual;75 function GetReferenceList(Index: Integer): TBaseItemList; virtual; 90 76 constructor Create; virtual; 91 77 end; … … 93 79 TItemClass = class of TItem; 94 80 95 IItemList = interface(IList<TItem>) 96 end; 81 { TBaseItemList } 82 83 TBaseItemList = class 84 public 85 type 86 TAddEvent = function (constref AValue: TItem): SizeInt of object; 87 TGetCountEvent = function: SizeInt of object; 88 TSetItemEvent = procedure(Index: SizeInt; AValue: TItem) of object; 89 TGetNameEvent = function: string of object; 90 TGetItemEvent = function(Index: SizeInt): TItem of object; 91 TGetItemFieldsEvent = function: TItemFields of object; 92 TRemoveEvent = function(constref AValue: TItem): SizeInt of object; 93 TGetNextAvailableNameEvent = function(Name: string): string of object; 94 TCreateItemEvent = function(Name: string = ''): TItem of object; 95 private 96 FOnAdd: TAddEvent; 97 FOnCreateItem: TCreateItemEvent; 98 FOnGetCount: TGetCountEvent; 99 FOnGetItem: TGetItemEvent; 100 FOnGetItemFields: TGetItemFieldsEvent; 101 FOnGetName: TGetNameEvent; 102 FOnGetNextAvailableName: TGetNextAvailableNameEvent; 103 FOnRemove: TRemoveEvent; 104 FOnSetItem: TSetItemEvent; 105 procedure SetItem(Index: SizeInt; AValue: TItem); 106 function GetItem(Index: SizeInt): TItem; 107 public 108 function GetName: string; 109 function GetCount: SizeInt; 110 function Remove(constref AValue: TItem): SizeInt; 111 function Add(constref AValue: TItem): SizeInt; 112 function CreateItem(Name: string = ''): TItem; 113 function GetNextAvailableName(Name: string): string; 114 function GetItemFields: TItemFields; 115 property Count: SizeInt read GetCount; 116 property Items[Index: SizeInt]: TItem read GetItem write SetItem; default; 117 property OnAdd: TAddEvent read FOnAdd write FOnAdd; 118 property OnGetCount: TGetCountEvent read FOnGetCount write FOnGetCount; 119 property OnSetItem: TSetItemEvent read FOnSetItem write FOnSetItem; 120 property OnGetItem: TGetItemEvent read FOnGetItem write FOnGetItem; 121 property OnGetName: TGetNameEvent read FOnGetName write FOnGetName; 122 property OnRemove: TRemoveEvent read FOnRemove write FOnRemove; 123 property OnGetItemFields: TGetItemFieldsEvent read FOnGetItemFields write FOnGetItemFields; 124 property OnGetNextAvailableName: TGetNextAvailableNameEvent read 125 FOnGetNextAvailableName write FOnGetNextAvailableName; 126 property OnCreateItem: TCreateItemEvent read FOnCreateItem 127 write FOnCreateItem; 128 end; 97 129 98 130 { TItemList } 99 131 100 TItemList<T: TItem> = class(TObjectList<T> , IList<TItem>)132 TItemList<T: TItem> = class(TObjectList<T>) 101 133 private 134 FBaseItemList: TBaseItemList; 102 135 procedure RecalculateNewId(Reset: Boolean); 103 136 procedure RecalculateItemsId; 104 function GetItem(Index: SizeInt): TItem; 105 procedure SetItem(Index: SizeInt; AValue: TItem); 106 protected 107 function GetCount: SizeInt; override; 137 function BaseGetItem(Index: SizeInt): TItem; 138 procedure BaseSetItem(Index: SizeInt; AValue: TItem); 139 function BaseAdd(constref AValue: TItem): SizeInt; 140 function BaseGetCount: SizeInt; 141 function BaseGetName(A: string): string; 142 function BaseRemove(constref AValue: TItem): SizeInt; 143 function BaseGetItemFields: TItemFields; 144 function BaseCreateItem(Name: string = ''): TItem; 108 145 public 109 146 NewId: Integer; 110 function GetName: string;111 function GetItemFields: TItemFields;112 function Remove(constref AValue: TItem): SizeInt;113 function Add(constref AValue: TItem): SizeInt; overload;114 147 function CreateItem(Name: string = ''): T; virtual; 115 function CreateBaseItem(Name: string = ''): TItem;116 148 function IncrementName(Name: string): string; 117 149 function GetNextAvailableName(Name: string): string; … … 126 158 procedure SaveToNode(Node: TDOMNode); virtual; 127 159 constructor Create(FreeObjects: Boolean = True); 128 function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF}; 129 function _AddRef: Longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF}; 130 function _Release: Longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF}; 160 destructor Destroy; override; 161 property BaseItemList: TBaseItemList read FBaseItemList; 131 162 end; 132 163 … … 210 241 end; 211 242 212 function TItemList<T>. CreateBaseItem(Name: string): TItem;243 function TItemList<T>.BaseCreateItem(Name: string): TItem; 213 244 begin 214 245 Result := TItem(CreateItem(Name)); … … 247 278 begin 248 279 inherited; 280 FBaseItemList := TBaseItemList.Create; 281 FBaseItemList.OnAdd := BaseAdd; 282 FBaseItemList.OnGetCount := BaseGetCount; 283 FBaseItemList.OnSetItem := BaseSetItem; 284 FBaseItemList.OnGetItem := BaseGetItem; 285 FBaseItemList.OnRemove := BaseRemove; 286 FBaseItemList.OnGetItemFields := BaseGetItemFields; 287 FBaseItemList.OnCreateItem := BaseCreateItem; 288 //FBaseItemList.OnGetNextAvailableName := GetNextAvailableName; 289 //FBaseItemList.OnGetName := BaseGetName; 249 290 NewId := 1; 250 291 end; 251 292 252 function TItemList<T>.QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid: tguid; out obj): LongInt; 253 {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF}; 254 begin 255 if GetInterface(IID, Obj) then Result := 0 256 else Result := E_NOINTERFACE; 257 end; 258 259 function TItemList<T>._AddRef: LongInt; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF}; 260 begin 261 Result := -1; 262 end; 263 264 function TItemList<T>._Release: LongInt; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF}; 265 begin 266 Result := -1; 267 end; 268 269 function TItemList<T>.GetCount: SizeInt; 293 destructor TItemList<T>.Destroy; 294 begin 295 FreeAndNil(FBaseItemList); 296 inherited; 297 end; 298 299 function TItemList<T>.BaseGetCount: SizeInt; 270 300 begin 271 301 Result := Count; 272 302 end; 273 303 274 function TItemList<T>. GetName: string;304 function TItemList<T>.BaseGetName(A: string): string; 275 305 begin 276 306 Result := T.GetClassName; 277 307 end; 278 308 279 function TItemList<T>. GetItemFields: TItemFields;309 function TItemList<T>.BaseGetItemFields: TItemFields; 280 310 begin 281 311 Result := T.GetFields; 282 312 end; 283 313 284 function TItemList<T>. Remove(constref AValue: TItem): SizeInt;314 function TItemList<T>.BaseRemove(constref AValue: TItem): SizeInt; 285 315 begin 286 316 Result := inherited Remove(T(AValue)); 287 317 end; 288 318 289 function TItemList<T>. Add(constref AValue: TItem): SizeInt;319 function TItemList<T>.BaseAdd(constref AValue: TItem): SizeInt; 290 320 begin 291 321 Result := inherited Add(T(AValue)); … … 312 342 end; 313 343 314 function TItemList<T>. GetItem(Index: SizeInt): TItem;344 function TItemList<T>.BaseGetItem(Index: SizeInt): TItem; 315 345 begin 316 346 Result := inherited GetItem(Index); 317 347 end; 318 348 319 procedure TItemList<T>. SetItem(Index: SizeInt; AValue: TItem);349 procedure TItemList<T>.BaseSetItem(Index: SizeInt; AValue: TItem); 320 350 begin 321 351 inherited SetItem(Index, T(AValue)); … … 452 482 var 453 483 ReadId: Integer; 454 ReferenceList: IList<TItem>;484 ReferenceList: TBaseItemList; 455 485 begin 456 486 if Field.DataType = dtString then begin … … 471 501 if Field.DataType = dtReference then begin 472 502 ReadId := ReadInteger(Node, Field.SysName, 0); 473 ReferenceList := IList<TItem>(GetReferenceList(Field.Index));503 ReferenceList := GetReferenceList(Field.Index); 474 504 if (ReadId > 0) and Assigned(ReferenceList) then 475 505 SetValueReference(Field.Index, TItem(ReferenceList[ReadId])); … … 714 744 end; 715 745 716 function TItem.GetReferenceList(Index: Integer): IList<TItem>;746 function TItem.GetReferenceList(Index: Integer): TBaseItemList; 717 747 begin 718 748 Result := nil; … … 723 753 end; 724 754 755 { TBaseItemList } 756 757 procedure TBaseItemList.SetItem(Index: SizeInt; AValue: TItem); 758 begin 759 if Assigned(FOnSetItem) then FOnSetItem(Index, AValue) 760 else Exception.Create('Undefined SetItem handler'); 761 end; 762 763 function TBaseItemList.GetName: string; 764 begin 765 if Assigned(FOnGetName) then Result := FOnGetName 766 else Exception.Create('Undefined GetName handler'); 767 end; 768 769 function TBaseItemList.GetCount: SizeInt; 770 begin 771 if Assigned(FOnGetCount) then Result := FOnGetCount 772 else Exception.Create('Undefined GetCount handler'); 773 end; 774 775 function TBaseItemList.GetItem(Index: SizeInt): TItem; 776 begin 777 if Assigned(FOnGetItem) then Result := FOnGetItem(Index) 778 else Exception.Create('Undefined GetItem handler'); 779 end; 780 781 function TBaseItemList.Remove(constref AValue: TItem): SizeInt; 782 begin 783 if Assigned(FOnRemove) then Result := FOnRemove(AValue) 784 else Exception.Create('Undefined Remove handler'); 785 end; 786 787 function TBaseItemList.Add(constref AValue: TItem): SizeInt; 788 begin 789 if Assigned(FOnAdd) then Result := FOnAdd(AValue) 790 else Exception.Create('Undefined Add handler'); 791 end; 792 793 function TBaseItemList.CreateItem(Name: string): TItem; 794 begin 795 if Assigned(FOnCreateItem) then Result := FOnCreateItem(Name) 796 else Exception.Create('Undefined CreateItem handler'); 797 end; 798 799 function TBaseItemList.GetNextAvailableName(Name: string): string; 800 begin 801 if Assigned(FOnGetNextAvailableName) then Result := FOnGetNextAvailableName(Name) 802 else Exception.Create('Undefined GetNextAvailableName handler'); 803 end; 804 805 function TBaseItemList.GetItemFields: TItemFields; 806 begin 807 if Assigned(FOnGetItemFields) then Result := FOnGetItemFields 808 else Exception.Create('Undefined GetItemFields handler'); 809 end; 810 725 811 end. 726 812
Note:
See TracChangeset
for help on using the changeset viewer.