Ignore:
Timestamp:
Oct 9, 2009, 1:44:09 PM (15 years ago)
Author:
george
Message:
  • Přidáno: Generování hlavičky položek se značkou Tag a Wire typem.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ProtocolBuffers/UProtocolBuffers.pas

    r5 r6  
     1// http://code.google.com/intl/cs/apis/protocolbuffers/docs/overview.html
    12unit UProtocolBuffers;
    23
    3 {$mode delphi}{$H+}
     4{$mode delphi}
    45
    56interface
    67
    78uses
    8   Classes, SysUtils; 
     9  Classes, SysUtils;
    910
    1011type
    11   TPBItemType = (itRequired, itOptional, itRepeated);
     12  TPBItemMode = (imRequired, imOptional, imRepeated);
     13  TPBItemType = (itVariant, it64bit, itLengthDelimited, itStartGroup,
     14    itEndGroup, it32bit);
    1215
    1316  TPBEnumeration = class
    1417  end;
     18
     19  { TPBItem }
    1520
    1621  TPBItem = class
     
    1823    Tag: Integer;
    1924    ItemType: TPBItemType;
     25    ItemMode: TPBItemMode;
     26    procedure SaveToStream(Stream: TStream); virtual;
    2027  end;
    2128
     
    3643  end;
    3744
     45  { TPBMessage }
     46
    3847  TPBMessage = class(TPBItem)
    3948    Items: TList; // TList<TPBItem>;
     49    procedure SaveToStream(Stream: TStream); override;
     50    constructor Create;
    4051  end;
    4152
     53  { TProtocolBuffer }
     54
    4255  TProtocolBuffer = class
     56    BaseMessage: TPBMessage;
    4357    procedure LoadFromStream(Stream: TStream);
    4458    procedure SaveToStream(Stream: TStream);
    45     PMMessage: TPBMessage;
     59    destructor Destroy; override;
    4660  end;
    4761
    4862implementation
     63
     64uses
     65  UMemoryStreamEx;
    4966
    5067{ TProtocolBuffer }
     
    5774procedure TProtocolBuffer.SaveToStream(Stream: TStream);
    5875begin
     76  BaseMessage.SaveToStream(Stream);
     77end;
    5978
     79destructor TProtocolBuffer.Destroy;
     80begin
     81  if Assigned(BaseMessage) then BaseMessage.Free;
     82  inherited Destroy;
     83end;
     84
     85{ TPBMessage }
     86
     87procedure TPBMessage.SaveToStream(Stream: TStream);
     88begin
     89  inherited SaveToStream(Stream);
     90end;
     91
     92constructor TPBMessage.Create;
     93begin
     94  ItemType := itLengthDelimited;
     95end;
     96
     97{ TPBItem }
     98
     99procedure TPBItem.SaveToStream(Stream: TStream);
     100var
     101  ByteIndex: Byte;
     102  Data: Byte;
     103begin
     104  with TMemoryStreamEx(Stream) do begin
     105    Data := ((Tag and $f) shl 3) or (Integer(ItemType) and $7);
     106    ByteIndex := 0;
     107    while Tag > (1 shl (ByteIndex * 8 + 4)) do begin
     108      WriteByte(Data or $80);
     109      Data := (Tag shr (ByteIndex * 8 + 4)) and $7f;
     110      Inc(ByteIndex);
     111    end;
     112    WriteByte(Data);
     113  end
    60114end;
    61115
Note: See TracChangeset for help on using the changeset viewer.