Changeset 7 for ProtocolBuffers
- Timestamp:
- Oct 9, 2009, 2:15:48 PM (15 years ago)
- Location:
- ProtocolBuffers
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
ProtocolBuffers/Demo.lpi
r6 r7 38 38 <IsPartOfProject Value="True"/> 39 39 <UnitName Value="Demo"/> 40 <CursorPos X=" 66" Y="10"/>40 <CursorPos X="49" Y="10"/> 41 41 <TopLine Value="1"/> 42 42 <EditorIndex Value="3"/> 43 <UsageCount Value="2 0"/>43 <UsageCount Value="21"/> 44 44 <Loaded Value="True"/> 45 45 </Unit0> … … 50 50 <ResourceBaseClass Value="Form"/> 51 51 <UnitName Value="UMainForm"/> 52 <CursorPos X=" 29" Y="41"/>53 <TopLine Value=" 30"/>52 <CursorPos X="41" Y="43"/> 53 <TopLine Value="24"/> 54 54 <EditorIndex Value="0"/> 55 <UsageCount Value="2 0"/>55 <UsageCount Value="21"/> 56 56 <Loaded Value="True"/> 57 57 </Unit1> … … 60 60 <IsPartOfProject Value="True"/> 61 61 <UnitName Value="UProtocolBuffers"/> 62 <CursorPos X=" 48" Y="102"/>63 <TopLine Value=" 85"/>62 <CursorPos X="14" Y="160"/> 63 <TopLine Value="144"/> 64 64 <EditorIndex Value="1"/> 65 <UsageCount Value="2 0"/>65 <UsageCount Value="21"/> 66 66 <Loaded Value="True"/> 67 67 </Unit2> … … 73 73 <TopLine Value="2"/> 74 74 <EditorIndex Value="2"/> 75 <UsageCount Value="2 0"/>75 <UsageCount Value="21"/> 76 76 <Loaded Value="True"/> 77 77 </Unit3> … … 79 79 <JumpHistory Count="30" HistoryIndex="29"> 80 80 <Position1> 81 <Filename Value="UM emoryStreamEx.pas"/>82 <Caret Line=" 49" Column="13" TopLine="36"/>81 <Filename Value="UMainForm.pas"/> 82 <Caret Line="34" Column="14" TopLine="28"/> 83 83 </Position1> 84 84 <Position2> 85 <Filename Value="U MemoryStreamEx.pas"/>86 <Caret Line=" 74" Column="14" TopLine="61"/>85 <Filename Value="UProtocolBuffers.pas"/> 86 <Caret Line="55" Column="18" TopLine="39"/> 87 87 </Position2> 88 88 <Position3> 89 <Filename Value="U MemoryStreamEx.pas"/>90 <Caret Line="4 9" Column="13" TopLine="36"/>89 <Filename Value="UProtocolBuffers.pas"/> 90 <Caret Line="41" Column="3" TopLine="19"/> 91 91 </Position3> 92 92 <Position4> 93 <Filename Value="U MemoryStreamEx.pas"/>94 <Caret Line=" 74" Column="14" TopLine="61"/>93 <Filename Value="UProtocolBuffers.pas"/> 94 <Caret Line="19" Column="3" TopLine="9"/> 95 95 </Position4> 96 96 <Position5> 97 <Filename Value="U MemoryStreamEx.pas"/>98 <Caret Line=" 3" Column="19" TopLine="1"/>97 <Filename Value="UProtocolBuffers.pas"/> 98 <Caret Line="96" Column="34" TopLine="67"/> 99 99 </Position5> 100 100 <Position6> 101 <Filename Value="UM emoryStreamEx.pas"/>102 <Caret Line=" 76" Column="14" TopLine="63"/>101 <Filename Value="UMainForm.pas"/> 102 <Caret Line="42" Column="5" TopLine="15"/> 103 103 </Position6> 104 104 <Position7> 105 <Filename Value="U MemoryStreamEx.pas"/>106 <Caret Line="4 4" Column="14" TopLine="31"/>105 <Filename Value="UProtocolBuffers.pas"/> 106 <Caret Line="49" Column="46" TopLine="23"/> 107 107 </Position7> 108 108 <Position8> 109 <Filename Value="UM emoryStreamEx.pas"/>110 <Caret Line=" 39" Column="14" TopLine="26"/>109 <Filename Value="UMainForm.pas"/> 110 <Caret Line="42" Column="5" TopLine="28"/> 111 111 </Position8> 112 112 <Position9> 113 <Filename Value="U MemoryStreamEx.pas"/>114 <Caret Line=" 106" Column="58" TopLine="98"/>113 <Filename Value="UProtocolBuffers.pas"/> 114 <Caret Line="97" Column="5" TopLine="65"/> 115 115 </Position9> 116 116 <Position10> 117 <Filename Value="UM emoryStreamEx.pas"/>118 <Caret Line=" 76" Column="14" TopLine="63"/>117 <Filename Value="UMainForm.pas"/> 118 <Caret Line="42" Column="9" TopLine="30"/> 119 119 </Position10> 120 120 <Position11> 121 <Filename Value="U MemoryStreamEx.pas"/>122 <Caret Line=" 39" Column="14" TopLine="26"/>121 <Filename Value="UProtocolBuffers.pas"/> 122 <Caret Line="110" Column="11" TopLine="84"/> 123 123 </Position11> 124 124 <Position12> 125 <Filename Value="U MemoryStreamEx.pas"/>126 <Caret Line="1 9" Column="40" TopLine="1"/>125 <Filename Value="UProtocolBuffers.pas"/> 126 <Caret Line="107" Column="1" TopLine="85"/> 127 127 </Position12> 128 128 <Position13> 129 <Filename Value="U ProtocolBuffers.pas"/>130 <Caret Line="4 5" Column="3" TopLine="32"/>129 <Filename Value="UMainForm.pas"/> 130 <Caret Line="43" Column="41" TopLine="30"/> 131 131 </Position13> 132 132 <Position14> 133 <Filename Value="U MainForm.pas"/>134 <Caret Line=" 33" Column="36" TopLine="15"/>133 <Filename Value="UProtocolBuffers.pas"/> 134 <Caret Line="104" Column="1" TopLine="72"/> 135 135 </Position14> 136 136 <Position15> 137 <Filename Value="U MainForm.pas"/>138 <Caret Line=" 41" Column="3" TopLine="16"/>137 <Filename Value="UProtocolBuffers.pas"/> 138 <Caret Line="103" Column="5" TopLine="71"/> 139 139 </Position15> 140 140 <Position16> 141 <Filename Value="U MainForm.pas"/>142 <Caret Line=" 45" Column="46" TopLine="22"/>141 <Filename Value="UProtocolBuffers.pas"/> 142 <Caret Line="38" Column="3" TopLine="19"/> 143 143 </Position16> 144 144 <Position17> 145 <Filename Value="U MainForm.pas"/>146 <Caret Line=" 3" Column="14" TopLine="1"/>145 <Filename Value="UProtocolBuffers.pas"/> 146 <Caret Line="80" Column="5" TopLine="48"/> 147 147 </Position17> 148 148 <Position18> 149 <Filename Value="U MainForm.pas"/>150 <Caret Line=" 34" Column="14" TopLine="28"/>149 <Filename Value="UProtocolBuffers.pas"/> 150 <Caret Line="154" Column="1" TopLine="122"/> 151 151 </Position18> 152 152 <Position19> 153 153 <Filename Value="UProtocolBuffers.pas"/> 154 <Caret Line=" 55" Column="18" TopLine="39"/>154 <Caret Line="96" Column="44" TopLine="90"/> 155 155 </Position19> 156 156 <Position20> 157 157 <Filename Value="UProtocolBuffers.pas"/> 158 <Caret Line=" 41" Column="3" TopLine="19"/>158 <Caret Line="29" Column="17" TopLine="13"/> 159 159 </Position20> 160 160 <Position21> 161 161 <Filename Value="UProtocolBuffers.pas"/> 162 <Caret Line=" 19" Column="3" TopLine="9"/>162 <Caret Line="53" Column="32" TopLine="37"/> 163 163 </Position21> 164 164 <Position22> 165 165 <Filename Value="UProtocolBuffers.pas"/> 166 <Caret Line=" 96" Column="34" TopLine="67"/>166 <Caret Line="156" Column="18" TopLine="133"/> 167 167 </Position22> 168 168 <Position23> 169 <Filename Value="U MainForm.pas"/>170 <Caret Line=" 42" Column="5" TopLine="15"/>169 <Filename Value="UProtocolBuffers.pas"/> 170 <Caret Line="117" Column="54" TopLine="117"/> 171 171 </Position23> 172 172 <Position24> 173 <Filename Value=" UProtocolBuffers.pas"/>174 <Caret Line=" 49" Column="46" TopLine="23"/>173 <Filename Value="Demo.lpr"/> 174 <Caret Line="10" Column="66" TopLine="1"/> 175 175 </Position24> 176 176 <Position25> 177 <Filename Value=" UMainForm.pas"/>178 <Caret Line=" 42" Column="5" TopLine="28"/>177 <Filename Value="Demo.lpr"/> 178 <Caret Line="19" Column="51" TopLine="1"/> 179 179 </Position25> 180 180 <Position26> 181 <Filename Value=" UProtocolBuffers.pas"/>182 <Caret Line=" 97" Column="5" TopLine="65"/>181 <Filename Value="Demo.lpr"/> 182 <Caret Line="10" Column="39" TopLine="1"/> 183 183 </Position26> 184 184 <Position27> 185 <Filename Value="U MainForm.pas"/>186 <Caret Line=" 42" Column="9" TopLine="30"/>185 <Filename Value="UProtocolBuffers.pas"/> 186 <Caret Line="156" Column="39" TopLine="140"/> 187 187 </Position27> 188 188 <Position28> 189 189 <Filename Value="UProtocolBuffers.pas"/> 190 <Caret Line=" 110" Column="11" TopLine="84"/>190 <Caret Line="55" Column="47" TopLine="39"/> 191 191 </Position28> 192 192 <Position29> 193 193 <Filename Value="UProtocolBuffers.pas"/> 194 <Caret Line="10 7" Column="1" TopLine="85"/>194 <Caret Line="104" Column="5" TopLine="72"/> 195 195 </Position29> 196 196 <Position30> 197 <Filename Value="U MainForm.pas"/>198 <Caret Line=" 43" Column="41" TopLine="30"/>197 <Filename Value="UProtocolBuffers.pas"/> 198 <Caret Line="103" Column="36" TopLine="101"/> 199 199 </Position30> 200 200 </JumpHistory> -
ProtocolBuffers/UMainForm.pas
r6 r7 33 33 Text: string; 34 34 I: Integer; 35 NewItem: TPBItem; 35 36 begin 36 37 Stream := TMemoryStreamEx.Create; 37 38 PB := TProtocolBuffer.Create; 38 39 with PB do begin 39 BaseMessage := TPBMessage.Create; 40 BaseMessage.Name := 'SampleMessage'; 41 BaseMessage.Tag := $aaaa; 40 with BaseMessage do begin 41 Name := 'SampleMessage'; 42 NewItem := TPBIntegerItem.Create; 43 TPBIntegerItem(NewItem).Value := $5555555; 44 Items.Add(NewItem); 45 end; 42 46 SaveToStream(Stream); 43 47 end; -
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.