Guest User

Untitled

a guest
Sep 11th, 2020
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. TBuildingList = class
  2. private
  3. FBuildings: TList;
  4. function GetCount: Integer;
  5. function GeTBuilding(Index: Integer): TBuilding;
  6. function GetAsJSON: string;
  7. procedure SetAsJSON(const Value: string);
  8. public
  9. constructor Create;
  10. destructor Destroy; override;
  11. procedure Clear;
  12. procedure Add(aBuilding: TBuilding);
  13. procedure Assign(Src: TBuildingList);
  14. procedure LoadFromJSON(const Str: string); overload;
  15. procedure LoadFromJSON(Js: ISuperObject); overload;
  16. procedure SaveToJSON(Js: ISuperObject); overload;
  17. function SaveToJSON: ISuperObject; overload;
  18. function IndexOf(ID: Integer): Integer; overload;
  19. function IndexOf(aItem: TBuilding): Integer; overload;
  20. function ItemByID(ID: Integer): TBuilding;
  21. procedure Delete(Index: Integer); overload;
  22. procedure Delete(aItem: TBuilding); overload;
  23.  
  24. property Count: Integer read GetCount;
  25. property Building[Index: Integer]: TBuilding read GeTBuilding; default;
  26. property AsJSON: string read GetAsJSON write SetAsJSON;
  27. end;
  28.  
  29.  
  30. ............................
  31.  
  32. { TBuildingList }
  33.  
  34. constructor TBuildingList.Create;
  35. begin
  36. FBuildings := TList.Create;
  37. end;
  38.  
  39. procedure TBuildingList.Delete(Index: Integer);
  40. begin
  41. if (Index < 0) or (Index >= Count) then
  42. raise Exception.Create(ErrItemNotFound);
  43.  
  44. TBuilding(FBuildings[Index]).Free;
  45. FBuildings.Delete(Index);
  46. end;
  47.  
  48. procedure TBuildingList.Delete(aItem: TBuilding);
  49. begin
  50. Delete(IndexOf(aItem));
  51. end;
  52.  
  53. destructor TBuildingList.Destroy;
  54. begin
  55. Clear;
  56. FBuildings.Free;
  57. inherited;
  58. end;
  59.  
  60. procedure TBuildingList.Add(aBuilding: TBuilding);
  61. begin
  62. FBuildings.Add(aBuilding)
  63. end;
  64.  
  65. procedure TBuildingList.Assign(Src: TBuildingList);
  66. var
  67. I: Integer;
  68. begin
  69. Clear;
  70. for I := 0 to Src.Count - 1 do
  71. Add(Src[I]);
  72. end;
  73.  
  74. procedure TBuildingList.Clear;
  75. var
  76. I: Integer;
  77. begin
  78. for I := 0 to FBuildings.Count - 1 do
  79. Building[I].Free;
  80. FBuildings.Clear;
  81. end;
  82.  
  83.  
  84. function TBuildingList.GetAsJSON: string;
  85. begin
  86. Result := SaveToJSON.AsJSon;
  87. end;
  88.  
  89. function TBuildingList.GetCount: Integer;
  90. begin
  91. Result := FBuildings.Count;
  92. end;
  93.  
  94. function TBuildingList.IndexOf(ID: Integer): Integer;
  95. var
  96. I: Integer;
  97. begin
  98. for I := 0 to Count - 1 do
  99. if Building[I].ID = ID then
  100. begin
  101. Result := I;
  102. Exit;
  103. end;
  104. Result := -1;
  105. end;
  106.  
  107. function TBuildingList.GeTBuilding(Index: Integer): TBuilding;
  108. begin
  109. if (Index >= 0) and (Index < Count) then
  110. Result := TBuilding(FBuildings[Index])
  111. else
  112. Result := nil;
  113. end;
  114.  
  115. function TBuildingList.IndexOf(aItem: TBuilding): Integer;
  116. begin
  117. Result := FBuildings.IndexOf(aItem);
  118. end;
  119.  
  120. function TBuildingList.ItemByID(ID: Integer): TBuilding;
  121. begin
  122. Result := Building[IndexOf(ID)];
  123. end;
  124.  
  125. procedure TBuildingList.LoadFromJSON(const Str: string);
  126. begin
  127. if Length(Str) > 0 then
  128. LoadFromJSON(SO(Str))
  129. else
  130. Clear;
  131. end;
  132.  
  133. procedure TBuildingList.LoadFromJSON(Js: ISuperObject);
  134. var
  135. Arr: TSuperArray;
  136. Building: TBuilding;
  137. I: Integer;
  138. begin
  139. if (Js = nil) or (Js.DataType <> stArray) then
  140. raise Exception.Create(ErrInvalidParam);
  141.  
  142. Clear;
  143. Arr := Js.AsArray;
  144. for I := 0 to Count - 1 do
  145. begin
  146. Building := TBuilding.Create;
  147. try
  148. Building.LoadFromJSON(Arr[I]);
  149. Add(Building);
  150. except
  151. Building.Free;
  152. raise;
  153. end;
  154. end;
  155. end;
  156.  
  157. function TBuildingList.SaveToJSON: ISuperObject;
  158. begin
  159. Result := TSuperObject.Create(stArray);
  160. SaveToJSON(Result);
  161. end;
  162.  
  163.  
  164. procedure TBuildingList.SetAsJSON(const Value: string);
  165. begin
  166. LoadFromJSON(Value);
  167. end;
  168.  
  169.  
  170. procedure TBuildingList.SaveToJSON(Js: ISuperObject);
  171. var
  172. Arr: TSuperArray;
  173. I: Integer;
  174. begin
  175. if (Js = nil) or (Js.DataType <> stArray) then
  176. raise Exception.Create(ErrInvalidParam);
  177.  
  178. Arr := Js.AsArray;
  179. for I := 0 to Count - 1 do
  180. Arr.Add(Building[I].SaveToJSON);
  181. end;
Advertisement
Add Comment
Please, Sign In to add comment