Changeset 13 for ProtocolBuffers
- Timestamp:
- Oct 23, 2009, 8:12:34 AM (15 years ago)
- Location:
- ProtocolBuffers
- Files:
-
- 1 deleted
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
ProtocolBuffers/Demo.lpi
r12 r13 50 50 <ResourceBaseClass Value="Form"/> 51 51 <UnitName Value="UMainForm"/> 52 <CursorPos X=" 42" Y="159"/>52 <CursorPos X="35" Y="159"/> 53 53 <TopLine Value="146"/> 54 54 <EditorIndex Value="0"/> -
ProtocolBuffers/UProtocolBuffers.pas
r12 r13 30 30 DefaultString: string; 31 31 DefaultInteger: Integer; 32 DefaultDouble: Double; 32 33 constructor Create; 33 34 destructor Destroy; override; … … 46 47 procedure SaveVariantToStream(Stream: TStream; Value: Integer); 47 48 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; 48 53 procedure SaveLengthDelimitedToStream(Stream: TStream; Block: TStream); 49 54 procedure LoadLengthDelimitedFromStream(Stream: TStream; Block: TStream); … … 70 75 TPBIntegerItem = class(TPBItem) 71 76 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; 72 95 procedure SaveToStream(Stream: TStream; Definition: TPBDefinition); override; 73 96 procedure LoadFromStream(Stream: TStream; Definition: TPBDefinition); override; … … 167 190 Items[I] := TPBIntegerItem.Create; 168 191 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; 169 200 end else 170 201 if TPBDefinition(Definition.Items[I]).ItemType = itString then begin … … 239 270 TPBStringItem(NewItem).LoadFromStream(Stream, Definition.Items[ItemIndex]) 240 271 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 241 280 if TPBDefinition(Definition.Items[ItemIndex]).ItemType = itMessage then begin 242 281 NewItem := TPBMessageItem.Create; … … 254 293 if ItemHead.WireType = wtVariant then 255 294 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) 256 299 else if ItemHead.WireType = wtLengthDelimited then 257 300 TempItem.LoadLengthDelimitedFromStream(Stream, TempStream); … … 375 418 end; 376 419 420 procedure TPBItem.SaveFixed32ToStream(Stream: TStream; Value: Cardinal); 421 begin 422 TMemoryStreamEx(Stream).WriteCardinal(Value); 423 end; 424 425 function TPBItem.LoadFixed32FromStream(Stream: TStream): Cardinal; 426 begin 427 Result := TMemoryStreamEx(Stream).ReadCardinal; 428 end; 429 430 procedure TPBItem.SaveFixed64ToStream(Stream: TStream; Value: QWord); 431 begin 432 TMemoryStreamEx(Stream).WriteInt64(Value); 433 end; 434 435 function TPBItem.LoadFixed64FromStream(Stream: TStream): QWord; 436 begin 437 Result := TMemoryStreamEx(Stream).ReadInt64; 438 end; 439 377 440 procedure TPBItem.SaveLengthDelimitedToStream(Stream: TStream; Block: TStream); 378 441 begin … … 462 525 case ItemType of 463 526 itInteger: Result := wtVariant; 464 itFloat: Result := wt 64bit;465 itDouble: Result := wt 32bit;527 itFloat: Result := wt32bit; 528 itDouble: Result := wt64bit; 466 529 itString: Result := wtLengthDelimited; 467 530 itMessage: Result := wtLengthDelimited; … … 496 559 TPBIntegerItem(Items[I]).Value := Definition.DefaultInteger; 497 560 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 498 569 if Definition.ItemType = itString then begin 499 570 Items[I] := TPBStringItem.Create; … … 550 621 end; 551 622 623 { TPBFloatItem } 624 625 procedure TPBFloatItem.SaveToStream(Stream: TStream; Definition: TPBDefinition 626 ); 627 begin 628 SaveHeadToStream(Stream, Definition); 629 SaveFixed32ToStream(Stream, Cardinal(Value)); 630 end; 631 632 procedure TPBFloatItem.LoadFromStream(Stream: TStream; Definition: TPBDefinition 633 ); 634 begin 635 inherited; 636 Value := Single(LoadFixed32FromStream(Stream)); 637 end; 638 639 constructor TPBFloatItem.Create; 640 begin 641 642 end; 643 644 procedure TPBFloatItem.Assign(Source: TPBItem); 645 begin 646 if Source is TPBFloatItem then 647 Value := TPBFloatItem(Source).Value; 648 end; 649 650 { TPBDoubleItem } 651 652 procedure TPBDoubleItem.SaveToStream(Stream: TStream; Definition: TPBDefinition 653 ); 654 begin 655 SaveHeadToStream(Stream, Definition); 656 SaveFixed64ToStream(Stream, QWord(Value)); 657 end; 658 659 procedure TPBDoubleItem.LoadFromStream(Stream: TStream; 660 Definition: TPBDefinition); 661 begin 662 inherited; 663 Value := Double(LoadFixed64FromStream(Stream)); 664 end; 665 666 constructor TPBDoubleItem.Create; 667 begin 668 669 end; 670 671 procedure TPBDoubleItem.Assign(Source: TPBItem); 672 begin 673 if Source is TPBDoubleItem then 674 Value := TPBDoubleItem(Source).Value; 675 end; 676 552 677 end. 553 678
Note:
See TracChangeset
for help on using the changeset viewer.