Changeset 6 for ProtocolBuffers
- Timestamp:
- Oct 9, 2009, 1:44:09 PM (15 years ago)
- Location:
- ProtocolBuffers
- Files:
-
- 1 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
ProtocolBuffers/Demo.lpi
r5 r6 33 33 </Item1> 34 34 </RequiredPackages> 35 <Units Count=" 3">35 <Units Count="4"> 36 36 <Unit0> 37 37 <Filename Value="Demo.lpr"/> 38 38 <IsPartOfProject Value="True"/> 39 39 <UnitName Value="Demo"/> 40 <CursorPos X=" 49" Y="10"/>40 <CursorPos X="66" Y="10"/> 41 41 <TopLine Value="1"/> 42 <EditorIndex Value=" 2"/>42 <EditorIndex Value="3"/> 43 43 <UsageCount Value="20"/> 44 44 <Loaded Value="True"/> … … 50 50 <ResourceBaseClass Value="Form"/> 51 51 <UnitName Value="UMainForm"/> 52 <CursorPos X=" 12" Y="11"/>53 <TopLine Value=" 1"/>52 <CursorPos X="29" Y="41"/> 53 <TopLine Value="30"/> 54 54 <EditorIndex Value="0"/> 55 55 <UsageCount Value="20"/> … … 60 60 <IsPartOfProject Value="True"/> 61 61 <UnitName Value="UProtocolBuffers"/> 62 <CursorPos X=" 5" Y="10"/>63 <TopLine Value=" 1"/>62 <CursorPos X="48" Y="102"/> 63 <TopLine Value="85"/> 64 64 <EditorIndex Value="1"/> 65 65 <UsageCount Value="20"/> 66 66 <Loaded Value="True"/> 67 67 </Unit2> 68 <Unit3> 69 <Filename Value="UMemoryStreamEx.pas"/> 70 <IsPartOfProject Value="True"/> 71 <UnitName Value="UMemoryStreamEx"/> 72 <CursorPos X="5" Y="18"/> 73 <TopLine Value="2"/> 74 <EditorIndex Value="2"/> 75 <UsageCount Value="20"/> 76 <Loaded Value="True"/> 77 </Unit3> 68 78 </Units> 69 <JumpHistory Count=" 1" HistoryIndex="0">79 <JumpHistory Count="30" HistoryIndex="29"> 70 80 <Position1> 71 <Filename Value="UProtocolBuffers.pas"/> 81 <Filename Value="UMemoryStreamEx.pas"/> 82 <Caret Line="49" Column="13" TopLine="36"/> 83 </Position1> 84 <Position2> 85 <Filename Value="UMemoryStreamEx.pas"/> 86 <Caret Line="74" Column="14" TopLine="61"/> 87 </Position2> 88 <Position3> 89 <Filename Value="UMemoryStreamEx.pas"/> 90 <Caret Line="49" Column="13" TopLine="36"/> 91 </Position3> 92 <Position4> 93 <Filename Value="UMemoryStreamEx.pas"/> 94 <Caret Line="74" Column="14" TopLine="61"/> 95 </Position4> 96 <Position5> 97 <Filename Value="UMemoryStreamEx.pas"/> 98 <Caret Line="3" Column="19" TopLine="1"/> 99 </Position5> 100 <Position6> 101 <Filename Value="UMemoryStreamEx.pas"/> 102 <Caret Line="76" Column="14" TopLine="63"/> 103 </Position6> 104 <Position7> 105 <Filename Value="UMemoryStreamEx.pas"/> 106 <Caret Line="44" Column="14" TopLine="31"/> 107 </Position7> 108 <Position8> 109 <Filename Value="UMemoryStreamEx.pas"/> 110 <Caret Line="39" Column="14" TopLine="26"/> 111 </Position8> 112 <Position9> 113 <Filename Value="UMemoryStreamEx.pas"/> 114 <Caret Line="106" Column="58" TopLine="98"/> 115 </Position9> 116 <Position10> 117 <Filename Value="UMemoryStreamEx.pas"/> 118 <Caret Line="76" Column="14" TopLine="63"/> 119 </Position10> 120 <Position11> 121 <Filename Value="UMemoryStreamEx.pas"/> 122 <Caret Line="39" Column="14" TopLine="26"/> 123 </Position11> 124 <Position12> 125 <Filename Value="UMemoryStreamEx.pas"/> 126 <Caret Line="19" Column="40" TopLine="1"/> 127 </Position12> 128 <Position13> 129 <Filename Value="UProtocolBuffers.pas"/> 130 <Caret Line="45" Column="3" TopLine="32"/> 131 </Position13> 132 <Position14> 133 <Filename Value="UMainForm.pas"/> 134 <Caret Line="33" Column="36" TopLine="15"/> 135 </Position14> 136 <Position15> 137 <Filename Value="UMainForm.pas"/> 138 <Caret Line="41" Column="3" TopLine="16"/> 139 </Position15> 140 <Position16> 141 <Filename Value="UMainForm.pas"/> 142 <Caret Line="45" Column="46" TopLine="22"/> 143 </Position16> 144 <Position17> 145 <Filename Value="UMainForm.pas"/> 72 146 <Caret Line="3" Column="14" TopLine="1"/> 73 </Position1> 147 </Position17> 148 <Position18> 149 <Filename Value="UMainForm.pas"/> 150 <Caret Line="34" Column="14" TopLine="28"/> 151 </Position18> 152 <Position19> 153 <Filename Value="UProtocolBuffers.pas"/> 154 <Caret Line="55" Column="18" TopLine="39"/> 155 </Position19> 156 <Position20> 157 <Filename Value="UProtocolBuffers.pas"/> 158 <Caret Line="41" Column="3" TopLine="19"/> 159 </Position20> 160 <Position21> 161 <Filename Value="UProtocolBuffers.pas"/> 162 <Caret Line="19" Column="3" TopLine="9"/> 163 </Position21> 164 <Position22> 165 <Filename Value="UProtocolBuffers.pas"/> 166 <Caret Line="96" Column="34" TopLine="67"/> 167 </Position22> 168 <Position23> 169 <Filename Value="UMainForm.pas"/> 170 <Caret Line="42" Column="5" TopLine="15"/> 171 </Position23> 172 <Position24> 173 <Filename Value="UProtocolBuffers.pas"/> 174 <Caret Line="49" Column="46" TopLine="23"/> 175 </Position24> 176 <Position25> 177 <Filename Value="UMainForm.pas"/> 178 <Caret Line="42" Column="5" TopLine="28"/> 179 </Position25> 180 <Position26> 181 <Filename Value="UProtocolBuffers.pas"/> 182 <Caret Line="97" Column="5" TopLine="65"/> 183 </Position26> 184 <Position27> 185 <Filename Value="UMainForm.pas"/> 186 <Caret Line="42" Column="9" TopLine="30"/> 187 </Position27> 188 <Position28> 189 <Filename Value="UProtocolBuffers.pas"/> 190 <Caret Line="110" Column="11" TopLine="84"/> 191 </Position28> 192 <Position29> 193 <Filename Value="UProtocolBuffers.pas"/> 194 <Caret Line="107" Column="1" TopLine="85"/> 195 </Position29> 196 <Position30> 197 <Filename Value="UMainForm.pas"/> 198 <Caret Line="43" Column="41" TopLine="30"/> 199 </Position30> 74 200 </JumpHistory> 75 201 </ProjectOptions> -
ProtocolBuffers/Demo.lpr
r5 r6 8 8 {$ENDIF}{$ENDIF} 9 9 Interfaces, // this includes the LCL widgetset 10 Forms, UMainForm, LResources, UProtocolBuffers 10 Forms, UMainForm, LResources, UProtocolBuffers, UMemoryStreamEx 11 11 { you can add units after this }; 12 12 -
ProtocolBuffers/UMainForm.lfm
r5 r6 1 1 object MainForm: TMainForm 2 Left = 28 83 Height = 3 004 Top = 1 545 Width = 4 002 Left = 281 3 Height = 319 4 Top = 189 5 Width = 448 6 6 Caption = 'Demo ProtocolBuffers' 7 ClientHeight = 319 8 ClientWidth = 448 9 OnCreate = FormCreate 10 OnDestroy = FormDestroy 7 11 LCLVersion = '0.9.29' 12 object Memo1: TMemo 13 Left = 9 14 Height = 272 15 Top = 9 16 Width = 433 17 TabOrder = 0 18 end 8 19 end -
ProtocolBuffers/UMainForm.lrs
r5 r6 1 { Toto je automaticky generovaný zdrojový soubor lazarusu }2 3 4 1 LazarusResources.Add('TMainForm','FORMDATA',[ 5 'TPF0'#9'TMainForm'#8'MainForm'#4'Left'#3' '#1#6'Height'#3','#1#3'Top'#3#154#0 6 +#5'Width'#3#144#1#7'Caption'#6#20'Demo ProtocolBuffers'#10'LCLVersion'#6#6'0' 7 +'.9.29'#0#0 2 'TPF0'#9'TMainForm'#8'MainForm'#4'Left'#3#25#1#6'Height'#3'?'#1#3'Top'#3#189#0 3 +#5'Width'#3#192#1#7'Caption'#6#20'Demo ProtocolBuffers'#12'ClientHeight'#3'?' 4 +#1#11'ClientWidth'#3#192#1#8'OnCreate'#7#10'FormCreate'#9'OnDestroy'#7#11'Fo' 5 +'rmDestroy'#10'LCLVersion'#6#6'0.9.29'#0#5'TMemo'#5'Memo1'#4'Left'#2#9#6'Hei' 6 +'ght'#3#16#1#3'Top'#2#9#5'Width'#3#177#1#8'TabOrder'#2#0#0#0#0 8 7 ]); -
ProtocolBuffers/UMainForm.pas
r5 r6 1 1 unit UMainForm; 2 2 3 {$mode objfpc}{$H+}3 {$mode delphi}{$H+} 4 4 5 5 interface 6 6 7 7 uses 8 Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs; 8 Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, 9 StdCtrls, UProtocolBuffers, UMemoryStreamEx; 9 10 10 11 type 12 { TMainForm } 11 13 TMainForm = class(TForm) 14 Memo1: TMemo; 15 procedure FormCreate(Sender: TObject); 16 procedure FormDestroy(Sender: TObject); 12 17 private 13 18 { private declarations } 14 19 public 15 { public declarations }20 PB: TProtocolBuffer; 16 21 end; 17 22 … … 21 26 implementation 22 27 28 { TMainForm } 29 30 procedure TMainForm.FormCreate(Sender: TObject); 31 var 32 Stream: TMemoryStreamEx; 33 Text: string; 34 I: Integer; 35 begin 36 Stream := TMemoryStreamEx.Create; 37 PB := TProtocolBuffer.Create; 38 with PB do begin 39 BaseMessage := TPBMessage.Create; 40 BaseMessage.Name := 'SampleMessage'; 41 BaseMessage.Tag := $aaaa; 42 SaveToStream(Stream); 43 end; 44 45 Stream.Position := 0; 46 Text := ''; 47 for I := 1 to Stream.Size do begin 48 Text := Text + IntToHex(Stream.ReadByte, 2) + ' '; 49 end; 50 Memo1.Lines.Text := Text; 51 Stream.Free; 52 end; 53 54 procedure TMainForm.FormDestroy(Sender: TObject); 55 begin 56 PB.Free; 57 end; 58 23 59 initialization 24 60 {$I UMainForm.lrs} -
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.