IdContainers.pas 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. unit IdContainers;
  2. {********************************************************************}
  3. {* IdContainers.pas *}
  4. {* *}
  5. {* Provides compatibility with the Contnr.pas unit from *}
  6. {* Delphi 5 not found in Delphi 4. *}
  7. {* *}
  8. {* Based on ideas from the Borland VCL Contnr.pas interface. *}
  9. {* *}
  10. {********************************************************************}
  11. {
  12. $Log: 10109: IdContainers.pas
  13. {
  14. { Rev 1.0 2002.11.12 10:33:52 PM czhower
  15. }
  16. Revision 1.0 2001-02-20 02:02:09-05 dsiders
  17. Initial revision
  18. }
  19. interface
  20. uses
  21. SysUtils, Classes;
  22. type
  23. {TIdObjectList}
  24. TIdObjectList = class(TList)
  25. private
  26. FOwnsObjects: Boolean;
  27. protected
  28. function GetItem(AIndex: Integer): TObject;
  29. procedure SetItem(AIndex: Integer; AObject: TObject);
  30. procedure Notify(AItemPtr: Pointer; AAction: TListNotification); override;
  31. public
  32. constructor Create; overload;
  33. constructor Create(AOwnsObjects: Boolean); overload;
  34. function Add(AObject: TObject): Integer;
  35. function FindInstanceOf(AClassRef: TClass; AMatchExact: Boolean = True; AStartPos: Integer = 0): Integer;
  36. function IndexOf(AObject: TObject): Integer;
  37. function Remove(AObject: TObject): Integer;
  38. procedure Insert(AIndex: Integer; AObject: TObject);
  39. property Items[AIndex: Integer]: TObject read GetItem write SetItem; default;
  40. property OwnsObjects: Boolean read FOwnsObjects write FOwnsObjects;
  41. end;
  42. implementation
  43. {TIdObjectList}
  44. function TIdObjectList.Add(AObject: TObject): Integer;
  45. begin
  46. Result := inherited Add(AObject);
  47. end;
  48. constructor TIdObjectList.Create;
  49. begin
  50. inherited Create;
  51. FOwnsObjects := True;
  52. end;
  53. constructor TIdObjectList.Create(AOwnsObjects: Boolean);
  54. begin
  55. inherited Create;
  56. FOwnsObjects := AOwnsObjects;
  57. end;
  58. function TIdObjectList.FindInstanceOf(AClassRef: TClass;
  59. AMatchExact: Boolean = True; AStartPos: Integer = 0): Integer;
  60. var
  61. iPos: Integer;
  62. bIsAMatch: Boolean;
  63. begin
  64. Result := -1; // indicates item is not in object list
  65. for iPos := AStartPos to Count - 1 do
  66. begin
  67. bIsAMatch :=
  68. ((not AMatchExact) and Items[iPos].InheritsFrom(AClassRef)) or
  69. (AMatchExact and (Items[iPos].ClassType = AClassRef));
  70. if (bIsAMatch) then
  71. begin
  72. Result := iPos;
  73. break;
  74. end;
  75. end;
  76. end;
  77. function TIdObjectList.GetItem(AIndex: Integer): TObject;
  78. begin
  79. Result := inherited Items[AIndex];
  80. end;
  81. function TIdObjectList.IndexOf(AObject: TObject): Integer;
  82. begin
  83. Result := inherited IndexOf(AObject);
  84. end;
  85. procedure TIdObjectList.Insert(AIndex: Integer; AObject: TObject);
  86. begin
  87. inherited Insert(AIndex, AObject);
  88. end;
  89. procedure TIdObjectList.Notify(AItemPtr: Pointer; AAction: TListNotification);
  90. begin
  91. if (OwnsObjects and (AAction = lnDeleted)) then
  92. TObject(AItemPtr).Free;
  93. inherited Notify(AItemPtr, AAction);
  94. end;
  95. function TIdObjectList.Remove(AObject: TObject): Integer;
  96. begin
  97. Result := inherited Remove(AObject);
  98. end;
  99. procedure TIdObjectList.SetItem(AIndex: Integer; AObject: TObject);
  100. begin
  101. inherited Items[AIndex] := AObject;
  102. end;
  103. end.