Changeset 6 for ProtocolBuffers


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.
Location:
ProtocolBuffers
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • ProtocolBuffers/Demo.lpi

    r5 r6  
    3333      </Item1>
    3434    </RequiredPackages>
    35     <Units Count="3">
     35    <Units Count="4">
    3636      <Unit0>
    3737        <Filename Value="Demo.lpr"/>
    3838        <IsPartOfProject Value="True"/>
    3939        <UnitName Value="Demo"/>
    40         <CursorPos X="49" Y="10"/>
     40        <CursorPos X="66" Y="10"/>
    4141        <TopLine Value="1"/>
    42         <EditorIndex Value="2"/>
     42        <EditorIndex Value="3"/>
    4343        <UsageCount Value="20"/>
    4444        <Loaded Value="True"/>
     
    5050        <ResourceBaseClass Value="Form"/>
    5151        <UnitName Value="UMainForm"/>
    52         <CursorPos X="12" Y="11"/>
    53         <TopLine Value="1"/>
     52        <CursorPos X="29" Y="41"/>
     53        <TopLine Value="30"/>
    5454        <EditorIndex Value="0"/>
    5555        <UsageCount Value="20"/>
     
    6060        <IsPartOfProject Value="True"/>
    6161        <UnitName Value="UProtocolBuffers"/>
    62         <CursorPos X="5" Y="10"/>
    63         <TopLine Value="1"/>
     62        <CursorPos X="48" Y="102"/>
     63        <TopLine Value="85"/>
    6464        <EditorIndex Value="1"/>
    6565        <UsageCount Value="20"/>
    6666        <Loaded Value="True"/>
    6767      </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>
    6878    </Units>
    69     <JumpHistory Count="1" HistoryIndex="0">
     79    <JumpHistory Count="30" HistoryIndex="29">
    7080      <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"/>
    72146        <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>
    74200    </JumpHistory>
    75201  </ProjectOptions>
  • ProtocolBuffers/Demo.lpr

    r5 r6  
    88  {$ENDIF}{$ENDIF}
    99  Interfaces, // this includes the LCL widgetset
    10   Forms, UMainForm, LResources, UProtocolBuffers
     10  Forms, UMainForm, LResources, UProtocolBuffers, UMemoryStreamEx
    1111  { you can add units after this };
    1212
  • ProtocolBuffers/UMainForm.lfm

    r5 r6  
    11object MainForm: TMainForm
    2   Left = 288
    3   Height = 300
    4   Top = 154
    5   Width = 400
     2  Left = 281
     3  Height = 319
     4  Top = 189
     5  Width = 448
    66  Caption = 'Demo ProtocolBuffers'
     7  ClientHeight = 319
     8  ClientWidth = 448
     9  OnCreate = FormCreate
     10  OnDestroy = FormDestroy
    711  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
    819end
  • ProtocolBuffers/UMainForm.lrs

    r5 r6  
    1 { Toto je automaticky generovaný zdrojový soubor lazarusu }
    2 
    3 
    41LazarusResources.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
    87]);
  • ProtocolBuffers/UMainForm.pas

    r5 r6  
    11unit UMainForm;
    22
    3 {$mode objfpc}{$H+}
     3{$mode delphi}{$H+}
    44
    55interface
    66
    77uses
    8   Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs;
     8  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
     9  StdCtrls, UProtocolBuffers, UMemoryStreamEx;
    910
    1011type
     12  { TMainForm }
    1113  TMainForm = class(TForm)
     14    Memo1: TMemo;
     15    procedure FormCreate(Sender: TObject);
     16    procedure FormDestroy(Sender: TObject);
    1217  private
    1318    { private declarations }
    1419  public
    15     { public declarations }
     20    PB: TProtocolBuffer;
    1621  end;
    1722
     
    2126implementation
    2227
     28{ TMainForm }
     29
     30procedure TMainForm.FormCreate(Sender: TObject);
     31var
     32  Stream: TMemoryStreamEx;
     33  Text: string;
     34  I: Integer;
     35begin
     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;
     52end;
     53
     54procedure TMainForm.FormDestroy(Sender: TObject);
     55begin
     56  PB.Free;
     57end;
     58
    2359initialization
    2460  {$I UMainForm.lrs}
  • 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.