Changeset 405


Ignore:
Timestamp:
Aug 14, 2012, 7:04:04 AM (12 years ago)
Author:
chronos
Message:
  • Modified: TemplateGenerics TBinarySerializer now use TListByte class instead TStreamByte.
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • Common/UMemory.pas

    r393 r405  
    2424    constructor Create;
    2525    destructor Destroy; override;
     26    procedure WriteMemory(Position: Integer; Memory: TMemory);
     27    procedure ReadMemory(Position: Integer; Memory: TMemory);
    2628    property Data: PByte read FData;
    2729    property Size: Integer read FSize write SetSize;
     
    108110end;
    109111
     112procedure TMemory.WriteMemory(Position: Integer; Memory: TMemory);
     113begin
     114  Move(Memory.FData, PByte(@FData + Position)^, Memory.Size);
     115end;
     116
     117procedure TMemory.ReadMemory(Position: Integer; Memory: TMemory);
     118begin
     119  Move(PByte(@FData + Position)^, Memory.FData, Memory.Size);
     120end;
     121
    110122end.
    111123
  • CoolStreaming/CoolStreaming.lpk

    r379 r405  
    5050    </Files>
    5151    <Type Value="RunAndDesignTime"/>
    52     <RequiredPkgs Count="3">
     52    <RequiredPkgs Count="2">
    5353      <Item1>
    5454        <PackageName Value="Common"/>
    5555      </Item1>
    5656      <Item2>
    57         <PackageName Value="TemplateGenerics"/>
    58       </Item2>
    59       <Item3>
    6057        <PackageName Value="FCL"/>
    6158        <MinVersion Major="1" Valid="True"/>
    62       </Item3>
     59      </Item2>
    6360    </RequiredPkgs>
    6461    <UsageOptions>
  • CoolStreaming/UVarBlockSerializer.pas

    r384 r405  
    3838    procedure WriteVarStream(AStream: TStream);
    3939    procedure ReadVarStream(AStream: TStream);
     40    procedure WriteVarList(List: TListByte);
     41    procedure ReadVarList(List: TListByte);
    4042    function GetVarSize: Integer;
    4143    function GetVarCount: Integer;
     
    8082    procedure WriteVarStream(Index: Integer; Stream: TStream);
    8183    procedure ReadVarStream(Index: Integer; Stream: TStream);
     84    procedure WriteVarList(Index: Integer; List: TListByte);
     85    procedure ReadVarList(Index: Integer; List: TListByte);
    8286    procedure WriteVarIndexedBlock(Index: Integer; Block: TVarBlockIndexed);
    8387    procedure ReadVarIndexedBlock(Index: Integer; Block: TVarBlockIndexed);
     
    101105    procedure WriteToStream(Stream: TStream);
    102106    procedure ReadFromStream(Stream: TStream);
     107    procedure WriteToList(List: TListByte);
     108    procedure ReadFromList(List: TListByte);
    103109    constructor Create;
    104110    destructor Destroy; override;
     
    413419end;
    414420
     421procedure TVarBlockSerializer.WriteVarList(List: TListByte);
     422var
     423  Mem: TMemoryStream;
     424begin
     425  try
     426    Mem := TMemoryStream.Create;
     427    List.WriteToStream(Mem);
     428    WriteVarStream(Mem);
     429  finally
     430    Mem.Free
     431  end;
     432end;
     433
     434procedure TVarBlockSerializer.ReadVarList(List: TListByte);
     435var
     436  Mem: TMemoryStream;
     437begin
     438  try
     439    Mem := TMemoryStream.Create;
     440    ReadVarStream(Mem);
     441    List.Count := Mem.Size;
     442    List.ReplaceStream(Mem);
     443  finally
     444    Mem.Free
     445  end;
     446end;
     447
    415448function TVarBlockSerializer.GetVarSize: Integer;
    416449var
     
    697730end;
    698731
     732procedure TVarBlockIndexed.WriteVarList(Index: Integer; List: TListByte);
     733begin
     734  CheckItem(Index);
     735  TVarBlockSerializer(Items[Index]).WriteVarList(List);
     736end;
     737
     738procedure TVarBlockIndexed.ReadVarList(Index: Integer; List: TListByte);
     739begin
     740  TVarBlockSerializer(Items[Index]).ReadVarList(List);
     741end;
     742
    699743procedure TVarBlockIndexed.WriteVarIndexedBlock(Index: Integer;
    700744  Block: TVarBlockIndexed);
     
    915959end;
    916960
     961procedure TVarBlockIndexed.WriteToList(List: TListByte);
     962var
     963  Mem: TMemoryStream;
     964begin
     965  try
     966    Mem := TMemoryStream.Create;
     967    WriteToStream(Mem);
     968    List.Count := Mem.Size;
     969    List.ReplaceStream(Mem);
     970  finally
     971    Mem.Free;
     972  end;
     973end;
     974
     975procedure TVarBlockIndexed.ReadFromList(List: TListByte);
     976var
     977  Mem: TMemoryStream;
     978begin
     979  try
     980    Mem := TMemoryStream.Create;
     981    List.WriteToStream(Mem);
     982    ReadFromStream(Mem);
     983  finally
     984    Mem.Free;
     985  end;
     986end;
     987
    917988constructor TVarBlockIndexed.Create;
    918989begin
  • Generics/TemplateGenerics/Additional/UBinarySerializer.pas

    r342 r405  
    11unit UBinarySerializer;
    22
    3 {$mode objfpc}{$H+}
     3{$mode delphi}{$H+}
    44
    55interface
     
    1515  TBinarySerializer = class
    1616  private
    17     FStream: TStreamByte;
     17    FGrow: Boolean;
     18    FList: TListByte;
    1819    FEndianness: TEndianness;
    1920    SwapData: Boolean;
     
    2122    procedure ReverseByteOrder(var Buffer; Count: Integer);
    2223  public
     24    Position: Integer;
     25    OwnsList: Boolean;
     26    procedure Write(var Buffer; Count: Integer); inline;
     27    procedure Read(var Buffer; Count: Integer); inline;
     28
    2329    procedure Assign(Source: TBinarySerializer);
    2430    procedure WriteByte(Data: Byte);
     
    4854    procedure ReadStream(AStream: TStream; Count: Integer);
    4955    procedure ReadStreamPart(AStream: TStream; Count: Integer);
     56    procedure ReadList(List: TListByte; StartIndex, Count: Integer);
    5057    constructor Create; overload;
    51     constructor Create(AStream: TStreamByte); overload;
    5258    procedure Clear;
    5359    destructor Destroy; override;
    5460    property Endianness: TEndianness read FEndianness write SetEndianness;
    55     property Stream: TStreamByte read FStream write FStream;
     61    property List: TListByte read FList write FList;
     62    property Grow: Boolean read FGrow write FGrow;
    5663  end;
    5764
     
    6572  StringLength: Longint;
    6673begin
    67   FStream.ReadBuffer(StringLength, SizeOf(StringLength));
     74  FList.ReadBuffer(StringLength, SizeOf(StringLength));
    6875  Result := ReadString(StringLength);
    6976end;
     
    7582  OldPosition: Integer;
    7683begin
    77   OldPosition := FStream.Position;
     84  OldPosition := Position;
    7885  Result := '';
    7986  I := 1;
    8087  repeat
    81     if FStream.Position >= FStream.Size then Break;
     88    if Position >= FList.Count then Break;
    8289    Data := Chr(ReadByte);
    8390    if Data <> Terminator[I] then begin
     
    8895  if not (I > Length(Terminator)) then begin
    8996    Result := '';
    90     FStream.Position := OldPosition;
     97    Position := OldPosition;
    9198  end;
    9299end;
     
    94101function TBinarySerializer.ReadByte: Byte;
    95102begin
    96   FStream.ReadBuffer(Result, SizeOf(Byte));
     103  Read(Result, SizeOf(Byte));
    97104end;
    98105
    99106function TBinarySerializer.ReadCardinal: Cardinal;
    100107begin
    101   FStream.ReadBuffer(Result, SizeOf(Cardinal));
     108  Read(Result, SizeOf(Cardinal));
    102109  if SwapData then Result := SwapEndian(Result);
    103110end;
     
    105112function TBinarySerializer.ReadInt64: Int64;
    106113begin
    107   FStream.ReadBuffer(Result, SizeOf(Int64));
     114  Read(Result, SizeOf(Int64));
    108115  if SwapData then Result := SwapEndian(Result);
    109116end;
     
    113120  if Length > 0 then begin
    114121    SetLength(Result, Length);
    115     FStream.ReadBuffer(Result[1], Length);
     122    Read(Result[1], Length);
    116123  end else Result := '';
    117124end;
     
    121128  Count: Byte;
    122129begin
    123   FStream.ReadBuffer(Count, 1);
     130  Read(Count, 1);
    124131  Result := ReadString(Count);
    125132end;
     
    131138  if Count > 0 then begin
    132139    SetLength(Buffer, Count);
    133     FStream.ReadBuffer(Buffer[0], Count);
     140    Read(Buffer[0], Count);
    134141    AStream.Size := Count;
    135142    AStream.Position := 0;
     
    144151  if Count > 0 then begin
    145152    SetLength(Buffer, Count);
    146     FStream.ReadBuffer(Buffer[0], Count);
     153    Read(Buffer[0], Count);
    147154    if AStream.Size < (AStream.Position + Count) then
    148155      AStream.Size := AStream.Position + Count;
    149     AStream.Write(Buffer[0], Count);
     156    Write(Buffer[0], Count);
     157  end;
     158end;
     159
     160procedure TBinarySerializer.ReadList(List: TListByte; StartIndex, Count: Integer
     161  );
     162var
     163  Buffer: array of Byte;
     164begin
     165  if Count > (List.Count - StartIndex) then Count := (List.Count - StartIndex); // Limit max. stream size
     166  if Count > 0 then begin
     167    SetLength(Buffer, Count);
     168    Read(Pointer(Buffer)^, Count);
     169    List.ReplaceBuffer(StartIndex, Pointer(Buffer)^, Count);
    150170  end;
    151171end;
     
    159179    SetLength(Buffer, Count);
    160180    AStream.ReadBuffer(Pointer(Buffer)^, Count);
    161     FStream.WriteBuffer(Pointer(Buffer)^, Count);
     181    Write(Pointer(Buffer)^, Count);
    162182  end;
    163183end;
     
    170190  if Count > 0 then begin
    171191    SetLength(Buffer, Count);
    172     List.ReadBuffer(Pointer(Buffer)^, Count);
    173     FStream.WriteBuffer(Pointer(Buffer)^, Count);
     192    List.GetBuffer(StartIndex, PByte(Buffer), Count);
     193    Write(Pointer(Buffer)^, Count);
    174194  end;
    175195end;
     
    179199  inherited;
    180200  Endianness := enLittle;
    181   FStream := nil;
    182 end;
    183 
    184 constructor TBinarySerializer.Create(AStream: TStreamByte);
    185 begin
    186   inherited Create;
    187   Endianness := enLittle;
    188   FStream := AStream;
     201  FList := nil;
     202  FGrow := True;
    189203end;
    190204
    191205procedure TBinarySerializer.Clear;
    192206begin
    193   Stream.Size := 0;
     207  FList.Count := 0;
    194208end;
    195209
    196210destructor TBinarySerializer.Destroy;
    197211begin
     212  if OwnsList then FList.Free;
    198213  inherited Destroy;
    199214end;
     
    206221function TBinarySerializer.ReadDouble: Double;
    207222begin
    208   FStream.ReadBuffer(Result, SizeOf(Double));
     223  Read(Result, SizeOf(Double));
    209224end;
    210225
    211226function TBinarySerializer.ReadSingle: Single;
    212227begin
    213   FStream.ReadBuffer(Result, SizeOf(Single));
     228  Read(Result, SizeOf(Single));
    214229end;
    215230
    216231function TBinarySerializer.ReadWord: Word;
    217232begin
    218   FStream.ReadBuffer(Result, SizeOf(Word));
     233  Read(Result, SizeOf(Word));
    219234  if SwapData then Result := SwapEndian(Result);
    220235end;
     
    246261end;
    247262
     263procedure TBinarySerializer.Write(var Buffer; Count: Integer);
     264var
     265  NewCount: Integer;
     266begin
     267  if FGrow then begin
     268    NewCount := Position + Count;
     269    if FList.Count < NewCount then
     270      FList.Count := NewCount;
     271  end;
     272  FList.ReplaceBuffer(Position, Buffer, Count);
     273  Inc(Position, Count);
     274end;
     275
     276procedure TBinarySerializer.Read(var Buffer; Count: Integer);
     277begin
     278  FList.GetBuffer(Position, Buffer, Count);
     279  Inc(Position, Count);
     280end;
     281
    248282procedure TBinarySerializer.Assign(Source: TBinarySerializer);
    249283begin
    250   FStream := Source.FStream;
     284  FList := Source.FList;
    251285end;
    252286
     
    262296procedure TBinarySerializer.WriteByte(Data: Byte);
    263297begin
    264   FStream.WriteBuffer(Data, SizeOf(Byte));
     298  Write(Data, SizeOf(Byte));
    265299end;
    266300
     
    298332    SetLength(Buffer, Count);
    299333    AStream.ReadBuffer(Pointer(Buffer)^, Count);
    300     FStream.WriteBuffer(Pointer(Buffer)^, Count);
     334    Write(Pointer(Buffer)^, Count);
    301335  end;
    302336end;
  • Generics/TemplateGenerics/Generic/GenericList.inc

    r383 r405  
    3939    procedure SetCount(const AValue: TGListIndex); virtual;
    4040  public
     41    type
     42      PItem = ^TGListItem;
    4143    function CompareMem(P1, P2: Pointer; Length: cardinal): Boolean; inline;
    4244    function Add(Item: TGListItem): TGListIndex;
     
    5759    function GetArray(Index, ACount: TGListIndex): TGListItemArray;
    5860    procedure GetList(List: TGList; Index, ACount: TGListIndex);
     61    procedure GetBuffer(Index: TGListIndex; var Buffer; Count: TGListIndex);
    5962    function Implode(Separator: string; Converter: TGListToStringConverter): string;
    6063    function IndexOf(Item: TGListItem; Start: TGListIndex = 0): TGListIndex; virtual;
     
    7376    procedure ReplaceListPart(Index: TGListIndex; Source: TGList;
    7477      SourceIndex, SourceCount: TGListIndex);
     78    procedure ReplaceBuffer(Index: TGListIndex; var Buffer; Count: TGListIndex);
    7579    procedure Sort(Compare: TGListSortCompare);
    7680    procedure SetArray(Values: array of TGListItem);
     
    102106begin
    103107  FCount := 0;
     108end;
     109
     110procedure TGList.GetBuffer(Index: TGListIndex; var Buffer; Count: TGListIndex);
     111var
     112  P: PItem;
     113  I: TGListIndex;
     114begin
     115  if (Index + Count) > FCount then
     116    raise EListError.CreateFmt(SListIndexError, [Index + Count]);
     117  P := PItem(@Buffer);
     118  I := 0;
     119  while I < Count do begin
     120    P^ := Items[Index + I];
     121    Inc(P, 1);
     122    I := I + 1;
     123  end;
     124end;
     125
     126procedure TGList.ReplaceBuffer(Index: TGListIndex; var Buffer; Count: TGListIndex);
     127var
     128  P: PItem;
     129  I: TGListIndex;
     130begin
     131  if (Index + Count) > FCount then
     132    raise EListError.CreateFmt(SListIndexError, [Index + Count]);
     133  P := PItem(@Buffer);
     134  I := 0;
     135  while I < Count do begin
     136    Items[Index + I] := P^;
     137    Inc(P, 1);
     138    I := I + 1;
     139  end;
    104140end;
    105141
Note: See TracChangeset for help on using the changeset viewer.