Changeset 7 for ProtocolBuffers/UProtocolBuffers.pas
- Timestamp:
- Oct 9, 2009, 2:15:48 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ProtocolBuffers/UProtocolBuffers.pas
r6 r7 18 18 19 19 { TPBItem } 20 21 20 TPBItem = class 22 21 Name: string; … … 24 23 ItemType: TPBItemType; 25 24 ItemMode: TPBItemMode; 25 procedure SaveVariantToStream(Stream: TStream; Value: Integer); 26 26 procedure SaveToStream(Stream: TStream); virtual; 27 end; 28 29 TPBMessage = class; 30 27 procedure LoadFromStream(Stream: TStream); virtual; 28 function LoadVariantFromStream(Stream: TStream); 29 end; 30 31 TPBMessageItem = class; 32 33 { TPBStringItem } 31 34 TPBStringItem = class(TPBItem) 32 35 Value: string; 33 36 end; 34 37 38 { TPBIntegerItem } 35 39 TPBIntegerItem = class(TPBItem) 36 40 Value: Integer; 37 end; 38 41 procedure SaveToStream(Stream: TStream); override; 42 constructor Create; 43 end; 44 45 { TPBMessageItem } 39 46 TPBMessageItem = class(TPBItem) 40 Tag: Integer;41 Name: string;42 ItemType: TPBItemType;43 end;44 45 { TPBMessage }46 47 TPBMessage = class(TPBItem)48 47 Items: TList; // TList<TPBItem>; 49 48 procedure SaveToStream(Stream: TStream); override; 49 procedure LoadFromStream(Stream: TStream); override; 50 50 constructor Create; 51 destructor Destroy; override; 51 52 end; 52 53 53 54 { TProtocolBuffer } 54 55 55 TProtocolBuffer = class 56 BaseMessage: TPBMessage ;56 BaseMessage: TPBMessageItem; 57 57 procedure LoadFromStream(Stream: TStream); 58 58 procedure SaveToStream(Stream: TStream); 59 constructor Create; 59 60 destructor Destroy; override; 60 61 end; … … 69 70 procedure TProtocolBuffer.LoadFromStream(Stream: TStream); 70 71 begin 71 72 BaseMessage.LoadFromStream(Stream); 72 73 end; 73 74 … … 75 76 begin 76 77 BaseMessage.SaveToStream(Stream); 78 end; 79 80 constructor TProtocolBuffer.Create; 81 begin 82 BaseMessage := TPBMessageItem.Create; 77 83 end; 78 84 … … 83 89 end; 84 90 85 { TPBMessage } 86 87 procedure TPBMessage.SaveToStream(Stream: TStream); 91 { TPBMessageItem } 92 93 procedure TPBMessageItem.SaveToStream(Stream: TStream); 94 var 95 I: Integer; 88 96 begin 89 97 inherited SaveToStream(Stream); 90 end; 91 92 constructor TPBMessage.Create; 98 for I := 0 to Items.Count - 1 do 99 TPBItem(Items[I]).SaveToStream(Stream); 100 end; 101 102 procedure TPBMessageItem.LoadFromStream(Stream: TStream); 103 begin 104 inherited LoadFromStream(Stream); 105 end; 106 107 constructor TPBMessageItem.Create; 93 108 begin 94 109 ItemType := itLengthDelimited; 110 Items := TList.Create; 111 end; 112 113 destructor TPBMessageItem.Destroy; 114 var 115 I: Integer; 116 begin 117 for I := 0 to Items.Count - 1 do 118 TPBItem(Items[I]).Free; 119 Items.Free; 120 inherited Destroy; 95 121 end; 96 122 97 123 { TPBItem } 98 124 99 procedure TPBItem.Save ToStream(Stream: TStream);125 procedure TPBItem.SaveVariantToStream(Stream: TStream; Value: Integer); 100 126 var 101 127 ByteIndex: Byte; … … 103 129 begin 104 130 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 begin131 Data := Value and $7f; 132 ByteIndex := 1; 133 while Value > (1 shl (ByteIndex * 7)) do begin 108 134 WriteByte(Data or $80); 109 Data := ( Tag shr (ByteIndex * 8 + 4)) and $7f;135 Data := (Value shr (ByteIndex * 7)) and $7f; 110 136 Inc(ByteIndex); 111 137 end; … … 114 140 end; 115 141 142 procedure TPBItem.SaveToStream(Stream: TStream); 143 var 144 ByteIndex: Byte; 145 Data: Byte; 146 begin 147 with TMemoryStreamEx(Stream) do begin 148 Data := ((Tag and $f) shl 3) or (Integer(ItemType) and $7); 149 ByteIndex := 0; 150 while Tag > (1 shl (ByteIndex * 7 + 4)) do begin 151 WriteByte(Data or $80); 152 Data := (Tag shr (ByteIndex * 7 + 4)) and $7f; 153 Inc(ByteIndex); 154 end; 155 WriteByte(Data); 156 end 157 end; 158 159 procedure TPBItem.LoadFromStream(Stream: TStream); 160 var 161 Data: Byte; 162 ByteIndex: Byte; 163 begin 164 Data := TMemoryStreamEx(Stream).ReadByte; 165 ItemType := TPBItemType(Data and 3); 166 Tag := (Data shr 3) and $f; 167 ByteIndex := 0; 168 while Data > $7f do begin 169 Data := TMemoryStreamEx(Stream).ReadByte; 170 Tag := Tag or ((Data and $7f) shl (ByteIndex * 7 + 4)) 171 Inc(ByteIndex); 172 end; 173 end; 174 175 function TPBItem.LoadVariantFromStream(Stream: TStream); 176 var 177 Data: Byte; 178 ByteIndex: Byte; 179 begin 180 Data := TMemoryStreamEx(Stream).ReadByte; 181 Tag := Data and $7f; 182 ByteIndex := 1; 183 while Data > $7f do begin 184 Data := TMemoryStreamEx(Stream).ReadByte; 185 Tag := Tag or ((Data and $7f) shl (ByteIndex * 7)) 186 Inc(ByteIndex); 187 end; 188 end; 189 190 { TPBIntegerItem } 191 192 procedure TPBIntegerItem.SaveToStream(Stream: TStream); 193 begin 194 inherited SaveToStream(Stream); 195 SaveVariantToStream(Stream, Value); 196 end; 197 198 constructor TPBIntegerItem.Create; 199 begin 200 ItemType := itVariant; 201 end; 202 116 203 end. 117 204
Note:
See TracChangeset
for help on using the changeset viewer.