Changeset 356


Ignore:
Timestamp:
Dec 30, 2024, 10:10:26 PM (3 days ago)
Author:
chronos
Message:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/ItemList.pas

    r355 r356  
    8686    TGetCountEvent = function: SizeInt of object;
    8787    TSetItemEvent = procedure(Index: SizeInt; AValue: TItem) of object;
    88     TGetNameEvent = function: string of object;
     88    TGetNameEvent = procedure (out Name: string) of object;
    8989    TGetItemEvent = function(Index: SizeInt): TItem of object;
    9090    TGetItemFieldsEvent = function: TItemFields of object;
    9191    TRemoveEvent = function(constref AValue: TItem): SizeInt of object;
    92     TGetNextAvailableNameEvent = function(Name: string): string of object;
     92    TGetNextAvailableNameEvent = procedure (Name: string; out NewName: string) of object;
    9393    TCreateItemEvent = function(Name: string = ''): TItem of object;
    9494  private
     
    138138    function BaseAdd(constref AValue: TItem): SizeInt;
    139139    function BaseGetCount: SizeInt;
    140     function BaseGetName(A: string): string;
     140    procedure BaseGetName(out Name: string);
    141141    function BaseRemove(constref AValue: TItem): SizeInt;
    142142    function BaseGetItemFields: TItemFields;
    143143    function BaseCreateItem(Name: string = ''): TItem;
     144    procedure BaseGetNextAvailableName(Name: string; out NewName: string);
    144145  public
    145146    NewId: Integer;
    146147    function CreateItem(Name: string = ''): T; virtual;
    147148    function IncrementName(Name: string): string;
    148     function GetNextAvailableName(Name: string): string;
    149149    function FindById(Id: Integer): T;
    150150    function FindByName(Name: string): T;
     
    285285  FBaseItemList.OnGetItemFields := BaseGetItemFields;
    286286  FBaseItemList.OnCreateItem := BaseCreateItem;
    287   //FBaseItemList.OnGetNextAvailableName := GetNextAvailableName;
    288   //FBaseItemList.OnGetName := BaseGetName;
     287  FBaseItemList.OnGetNextAvailableName := BaseGetNextAvailableName;
     288  FBaseItemList.OnGetName := BaseGetName;
    289289  NewId := 1;
    290290end;
     
    301301end;
    302302
    303 function TItemList<T>.BaseGetName(A: string): string;
    304 begin
    305   Result := T.GetClassName;
     303procedure TItemList<T>.BaseGetName(out Name: string);
     304begin
     305  Name := T.GetClassName;
    306306end;
    307307
     
    364364end;
    365365
    366 function TItemList<T>.GetNextAvailableName(Name: string): string;
    367 begin
    368   Result := Name;
    369   while Assigned(FindByName(Result)) do
    370     Result := IncrementName(Result);
     366procedure TItemList<T>.BaseGetNextAvailableName(Name: string; out
     367  NewName: string);
     368begin
     369  NewName := Name;
     370  while Assigned(FindByName(NewName)) do
     371    NewName := IncrementName(NewName);
    371372end;
    372373
     
    757758begin
    758759  if Assigned(FOnSetItem) then FOnSetItem(Index, AValue)
    759     else Exception.Create('Undefined SetItem handler');
     760    else raise Exception.Create('Undefined SetItem handler');
    760761end;
    761762
    762763function TBaseItemList.GetName: string;
    763 begin
    764   if Assigned(FOnGetName) then Result := FOnGetName
    765     else Exception.Create('Undefined GetName handler');
     764var
     765  Name: string;
     766begin
     767  if Assigned(FOnGetName) then begin
     768    FOnGetName(Name);
     769    Result := Name;
     770  end else raise Exception.Create('Undefined GetName handler');
    766771end;
    767772
     
    769774begin
    770775  if Assigned(FOnGetCount) then Result := FOnGetCount
    771     else Exception.Create('Undefined GetCount handler');
     776    else raise Exception.Create('Undefined GetCount handler');
    772777end;
    773778
     
    775780begin
    776781  if Assigned(FOnGetItem) then Result := FOnGetItem(Index)
    777     else Exception.Create('Undefined GetItem handler');
     782    else raise Exception.Create('Undefined GetItem handler');
    778783end;
    779784
     
    781786begin
    782787  if Assigned(FOnRemove) then Result := FOnRemove(AValue)
    783     else Exception.Create('Undefined Remove handler');
     788    else raise Exception.Create('Undefined Remove handler');
    784789end;
    785790
     
    787792begin
    788793  if Assigned(FOnAdd) then Result := FOnAdd(AValue)
    789     else Exception.Create('Undefined Add handler');
     794    else raise Exception.Create('Undefined Add handler');
    790795end;
    791796
     
    793798begin
    794799  if Assigned(FOnCreateItem) then Result := FOnCreateItem(Name)
    795     else Exception.Create('Undefined CreateItem handler');
     800    else raise Exception.Create('Undefined CreateItem handler');
    796801end;
    797802
    798803function TBaseItemList.GetNextAvailableName(Name: string): string;
    799 begin
    800   if Assigned(FOnGetNextAvailableName) then Result := FOnGetNextAvailableName(Name)
    801     else Exception.Create('Undefined GetNextAvailableName handler');
     804var
     805  NewName: string;
     806begin
     807  if Assigned(FOnGetNextAvailableName) then begin
     808    FOnGetNextAvailableName(Name, NewName);
     809    Result := NewName;
     810  end else raise Exception.Create('Undefined GetNextAvailableName handler');
    802811end;
    803812
     
    805814begin
    806815  if Assigned(FOnGetItemFields) then Result := FOnGetItemFields
    807     else Exception.Create('Undefined GetItemFields handler');
     816    else raise Exception.Create('Undefined GetItemFields handler');
    808817end;
    809818
Note: See TracChangeset for help on using the changeset viewer.