Changeset 322 for Generics/NativeGenerics/Units/GenericStream.pas
- Timestamp:
- Feb 8, 2012, 9:47:54 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Generics/NativeGenerics/Units/GenericStream.pas
r320 r322 24 24 procedure Write(Item: TItem); virtual; abstract; 25 25 procedure WriteArray(Item: array of TItem); virtual; abstract; 26 procedure WriteList(List: TGList<TItem>); virtual; abstract; 26 27 function Read: TItem; virtual; abstract; 27 28 function ReadArray(Count: TIndex): TItemArray; virtual; abstract; … … 35 36 end; 36 37 38 TGMemoryStream<TItem> = class(TGStream<TItem>) 39 private 40 FList: TGList<TItem>; 41 FPosition: TIndex; 42 public 43 procedure Assign(Source: TGStream<TItem>); override; 44 procedure Write(Item: TItem); override; 45 procedure WriteArray(Values: array of TItem); override; 46 procedure WriteList(List: TGList<TItem>); override; 47 function Read: TItem; override; 48 function ReadArray(Count: TIndex): TItemArray; override; 49 function ReadList(List: TGList<TItem>; Count: TIndex): TIndex; 50 function Insert(Count: TIndex): Integer; override; 51 function Remove(Count: TIndex): Integer; override; 52 function Seek(Offset: TIndex; Origin: TSeekOrigin = soCurrent): TIndex; override; 53 constructor Create; override; 54 destructor Destroy; override; 55 property List: TGList<TItem> read FList; 56 end; 57 37 58 38 59 implementation 39 60 61 62 { TGStream } 40 63 41 64 procedure TGStream<TItem>.Assign(Source: TGStream<TItem>); … … 85 108 end; 86 109 110 { TMemoryStreamByte } 111 112 procedure TGMemoryStream<TItem>.Assign(Source: TGStream<TItem>); 113 begin 114 inherited; 115 if Source is TGMemoryStream<TItem> then begin 116 FList.Assign(TGMemoryStream<TItem>(Source).FList); 117 FPosition := TGMemoryStream<TItem>(Source).FPosition; 118 end; 119 end; 120 121 procedure TGMemoryStream<TItem>.Write(Item: TItem); 122 begin 123 if FList.Count < (FPosition + 1) then 124 FList.Count := FPosition + 1; 125 FList[FPosition] := Item; 126 Inc(FPosition); 127 end; 128 129 procedure TGMemoryStream<TItem>.WriteArray(Values: array of TItem); 130 begin 131 if FList.Count < (FPosition + Length(Values)) then 132 FList.Count := FPosition + Length(Values); 133 FList.ReplaceArray(FPosition, Values); 134 Inc(FPosition, Length(Values)); 135 end; 136 137 procedure TGMemoryStream<TItem>.WriteList(List: TGList<TItem>); 138 begin 139 FList.ReplaceList(FPosition, List); 140 end; 141 142 function TGMemoryStream<TItem>.Read: TItem; 143 begin 144 Result := FList[FPosition]; 145 Inc(FPosition); 146 end; 147 148 function TGMemoryStream<TItem>.ReadArray(Count: TIndex): TItemArray; 149 begin 150 Result := FList.GetArray(FPosition, Count); 151 end; 152 153 function TGMemoryStream<TItem>.ReadList(List: TGList<TItem>; Count: TIndex): TIndex; 154 begin 155 if (FPosition + Count) > FList.Count then 156 Count := FList.Count - FPosition; 157 FList.GetList(List, FPosition, Count); 158 Result := Count; 159 end; 160 161 function TGMemoryStream<TItem>.Insert(Count: TIndex): TIndex; 162 begin 163 FList.InsertCount(FPosition, Count); 164 Result := Count; 165 end; 166 167 function TGMemoryStream<TItem>.Remove(Count: TIndex): TIndex; 168 begin 169 Result := FList.Count - FPosition; 170 if Count < Result then Result := Count; 171 FList.DeleteItems(FPosition, Count); 172 end; 173 174 function TGMemoryStream<TItem>.Seek(Offset: TIndex; Origin: TSeekOrigin): TIndex; 175 begin 176 case Origin of 177 soBeginning: FPosition := Offset; 178 soCurrent: FPosition := FPosition + Offset; 179 soEnd: FPosition := FList.Count + Offset; 180 end; 181 if FPosition > FList.Count then FPosition := FList.Count; 182 if FPosition < 0 then FPosition := 0; 183 Result := FPosition; 184 end; 185 186 constructor TGMemoryStream<TItem>.Create; 187 begin 188 inherited; 189 FList := TGList<TItem>.Create; 190 end; 191 192 destructor TGMemoryStream<TItem>.Destroy; 193 begin 194 FList.Free; 195 inherited Destroy; 196 end; 197 198 87 199 end.
Note:
See TracChangeset
for help on using the changeset viewer.