Changeset 13 for ProtocolBuffers


Ignore:
Timestamp:
Oct 23, 2009, 8:12:34 AM (15 years ago)
Author:
george
Message:
  • Added: Support for Single and Double float data types.
Location:
ProtocolBuffers
Files:
1 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • ProtocolBuffers/Demo.lpi

    r12 r13  
    5050        <ResourceBaseClass Value="Form"/>
    5151        <UnitName Value="UMainForm"/>
    52         <CursorPos X="42" Y="159"/>
     52        <CursorPos X="35" Y="159"/>
    5353        <TopLine Value="146"/>
    5454        <EditorIndex Value="0"/>
  • ProtocolBuffers/UProtocolBuffers.pas

    r12 r13  
    3030    DefaultString: string;
    3131    DefaultInteger: Integer;
     32    DefaultDouble: Double;
    3233    constructor Create;
    3334    destructor Destroy; override;
     
    4647    procedure SaveVariantToStream(Stream: TStream; Value: Integer);
    4748    function LoadVariantFromStream(Stream: TStream): Integer;
     49    procedure SaveFixed32ToStream(Stream: TStream; Value: Cardinal);
     50    function LoadFixed32FromStream(Stream: TStream): Cardinal;
     51    procedure SaveFixed64ToStream(Stream: TStream; Value: QWord);
     52    function LoadFixed64FromStream(Stream: TStream): QWord;
    4853    procedure SaveLengthDelimitedToStream(Stream: TStream; Block: TStream);
    4954    procedure LoadLengthDelimitedFromStream(Stream: TStream; Block: TStream);
     
    7075  TPBIntegerItem = class(TPBItem)
    7176    Value: Integer;
     77    procedure SaveToStream(Stream: TStream; Definition: TPBDefinition); override;
     78    procedure LoadFromStream(Stream: TStream; Definition: TPBDefinition); override;
     79    constructor Create;
     80    procedure Assign(Source: TPBItem); override;
     81  end;
     82
     83  { TPBFloatItem }
     84  TPBFloatItem = class(TPBItem)
     85    Value: Single;
     86    procedure SaveToStream(Stream: TStream; Definition: TPBDefinition); override;
     87    procedure LoadFromStream(Stream: TStream; Definition: TPBDefinition); override;
     88    constructor Create;
     89    procedure Assign(Source: TPBItem); override;
     90  end;
     91
     92  { TPBDoubleItem }
     93  TPBDoubleItem = class(TPBItem)
     94    Value: Double;
    7295    procedure SaveToStream(Stream: TStream; Definition: TPBDefinition); override;
    7396    procedure LoadFromStream(Stream: TStream; Definition: TPBDefinition); override;
     
    167190      Items[I] := TPBIntegerItem.Create;
    168191      TPBIntegerItem(Items[I]).Value := TPBDefinition(Definition.Items[I]).DefaultInteger;
     192    end else
     193    if TPBDefinition(Definition.Items[I]).ItemType = itFloat then begin
     194      Items[I] := TPBFloatItem.Create;
     195      TPBFloatItem(Items[I]).Value := TPBDefinition(Definition.Items[I]).DefaultDouble;
     196    end else
     197    if TPBDefinition(Definition.Items[I]).ItemType = itDouble then begin
     198      Items[I] := TPBDoubleItem.Create;
     199      TPBDoubleItem(Items[I]).Value := TPBDefinition(Definition.Items[I]).DefaultDouble;
    169200    end else
    170201    if TPBDefinition(Definition.Items[I]).ItemType = itString then begin
     
    239270          TPBStringItem(NewItem).LoadFromStream(Stream, Definition.Items[ItemIndex])
    240271        end else
     272        if TPBDefinition(Definition.Items[ItemIndex]).ItemType = itFloat then begin
     273          NewItem := TPBFloatItem.Create;
     274          TPBFloatItem(NewItem).LoadFromStream(Stream, Definition.Items[ItemIndex])
     275        end else
     276        if TPBDefinition(Definition.Items[ItemIndex]).ItemType = itDouble then begin
     277          NewItem := TPBDoubleItem.Create;
     278          TPBDoubleItem(NewItem).LoadFromStream(Stream, Definition.Items[ItemIndex])
     279        end else
    241280        if TPBDefinition(Definition.Items[ItemIndex]).ItemType = itMessage then begin
    242281          NewItem := TPBMessageItem.Create;
     
    254293      if ItemHead.WireType = wtVariant then
    255294        TempItem.LoadVariantFromStream(Stream)
     295      else if ItemHead.WireType = wt32bit then
     296        TempItem.LoadFixed32FromStream(Stream)
     297      else if ItemHead.WireType = wt64bit then
     298        TempItem.LoadFixed64FromStream(Stream)
    256299      else if ItemHead.WireType = wtLengthDelimited then
    257300        TempItem.LoadLengthDelimitedFromStream(Stream, TempStream);
     
    375418end;
    376419
     420procedure TPBItem.SaveFixed32ToStream(Stream: TStream; Value: Cardinal);
     421begin
     422  TMemoryStreamEx(Stream).WriteCardinal(Value);
     423end;
     424
     425function TPBItem.LoadFixed32FromStream(Stream: TStream): Cardinal;
     426begin
     427  Result := TMemoryStreamEx(Stream).ReadCardinal;
     428end;
     429
     430procedure TPBItem.SaveFixed64ToStream(Stream: TStream; Value: QWord);
     431begin
     432  TMemoryStreamEx(Stream).WriteInt64(Value);
     433end;
     434
     435function TPBItem.LoadFixed64FromStream(Stream: TStream): QWord;
     436begin
     437  Result := TMemoryStreamEx(Stream).ReadInt64;
     438end;
     439
    377440procedure TPBItem.SaveLengthDelimitedToStream(Stream: TStream; Block: TStream);
    378441begin
     
    462525  case ItemType of
    463526    itInteger: Result := wtVariant;
    464     itFloat: Result := wt64bit;
    465     itDouble: Result := wt32bit;
     527    itFloat: Result := wt32bit;
     528    itDouble: Result := wt64bit;
    466529    itString: Result := wtLengthDelimited;
    467530    itMessage: Result := wtLengthDelimited;
     
    496559      TPBIntegerItem(Items[I]).Value := Definition.DefaultInteger;
    497560    end else
     561    if Definition.ItemType = itFloat then begin
     562      Items[I] := TPBFloatItem.Create;
     563      TPBFloatItem(Items[I]).Value := Definition.DefaultDouble;
     564    end else
     565    if Definition.ItemType = itDouble then begin
     566      Items[I] := TPBDoubleItem.Create;
     567      TPBDoubleItem(Items[I]).Value := Definition.DefaultDouble;
     568    end else
    498569    if Definition.ItemType = itString then begin
    499570      Items[I] := TPBStringItem.Create;
     
    550621end;
    551622
     623{ TPBFloatItem }
     624
     625procedure TPBFloatItem.SaveToStream(Stream: TStream; Definition: TPBDefinition
     626  );
     627begin
     628  SaveHeadToStream(Stream, Definition);
     629  SaveFixed32ToStream(Stream, Cardinal(Value));
     630end;
     631
     632procedure TPBFloatItem.LoadFromStream(Stream: TStream; Definition: TPBDefinition
     633  );
     634begin
     635  inherited;
     636  Value := Single(LoadFixed32FromStream(Stream));
     637end;
     638
     639constructor TPBFloatItem.Create;
     640begin
     641
     642end;
     643
     644procedure TPBFloatItem.Assign(Source: TPBItem);
     645begin
     646  if Source is TPBFloatItem then
     647    Value := TPBFloatItem(Source).Value;
     648end;
     649
     650{ TPBDoubleItem }
     651
     652procedure TPBDoubleItem.SaveToStream(Stream: TStream; Definition: TPBDefinition
     653  );
     654begin
     655  SaveHeadToStream(Stream, Definition);
     656  SaveFixed64ToStream(Stream, QWord(Value));
     657end;
     658
     659procedure TPBDoubleItem.LoadFromStream(Stream: TStream;
     660  Definition: TPBDefinition);
     661begin
     662  inherited;
     663  Value := Double(LoadFixed64FromStream(Stream));
     664end;
     665
     666constructor TPBDoubleItem.Create;
     667begin
     668
     669end;
     670
     671procedure TPBDoubleItem.Assign(Source: TPBItem);
     672begin
     673  if Source is TPBDoubleItem then
     674    Value := TPBDoubleItem(Source).Value;
     675end;
     676
    552677end.
    553678
Note: See TracChangeset for help on using the changeset viewer.