Changeset 6 for ProtocolBuffers/UProtocolBuffers.pas
- Timestamp:
- Oct 9, 2009, 1:44:09 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ProtocolBuffers/UProtocolBuffers.pas
r5 r6 1 // http://code.google.com/intl/cs/apis/protocolbuffers/docs/overview.html 1 2 unit UProtocolBuffers; 2 3 3 {$mode delphi} {$H+}4 {$mode delphi} 4 5 5 6 interface 6 7 7 8 uses 8 Classes, SysUtils; 9 Classes, SysUtils; 9 10 10 11 type 11 TPBItemType = (itRequired, itOptional, itRepeated); 12 TPBItemMode = (imRequired, imOptional, imRepeated); 13 TPBItemType = (itVariant, it64bit, itLengthDelimited, itStartGroup, 14 itEndGroup, it32bit); 12 15 13 16 TPBEnumeration = class 14 17 end; 18 19 { TPBItem } 15 20 16 21 TPBItem = class … … 18 23 Tag: Integer; 19 24 ItemType: TPBItemType; 25 ItemMode: TPBItemMode; 26 procedure SaveToStream(Stream: TStream); virtual; 20 27 end; 21 28 … … 36 43 end; 37 44 45 { TPBMessage } 46 38 47 TPBMessage = class(TPBItem) 39 48 Items: TList; // TList<TPBItem>; 49 procedure SaveToStream(Stream: TStream); override; 50 constructor Create; 40 51 end; 41 52 53 { TProtocolBuffer } 54 42 55 TProtocolBuffer = class 56 BaseMessage: TPBMessage; 43 57 procedure LoadFromStream(Stream: TStream); 44 58 procedure SaveToStream(Stream: TStream); 45 PMMessage: TPBMessage;59 destructor Destroy; override; 46 60 end; 47 61 48 62 implementation 63 64 uses 65 UMemoryStreamEx; 49 66 50 67 { TProtocolBuffer } … … 57 74 procedure TProtocolBuffer.SaveToStream(Stream: TStream); 58 75 begin 76 BaseMessage.SaveToStream(Stream); 77 end; 59 78 79 destructor TProtocolBuffer.Destroy; 80 begin 81 if Assigned(BaseMessage) then BaseMessage.Free; 82 inherited Destroy; 83 end; 84 85 { TPBMessage } 86 87 procedure TPBMessage.SaveToStream(Stream: TStream); 88 begin 89 inherited SaveToStream(Stream); 90 end; 91 92 constructor TPBMessage.Create; 93 begin 94 ItemType := itLengthDelimited; 95 end; 96 97 { TPBItem } 98 99 procedure TPBItem.SaveToStream(Stream: TStream); 100 var 101 ByteIndex: Byte; 102 Data: Byte; 103 begin 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 60 114 end; 61 115
Note:
See TracChangeset
for help on using the changeset viewer.