[fpc-devel] PB2PAS: Proto Compiler for FPC

Amir amir at aavani.net
Thu Feb 25 21:48:45 CET 2021


Hi there,

   I would like to share the proto compiler I developed and get some 
feedback on it as well.

   The compiler's code is located https://github.com/AmirAavani/PB2PAS. 
The generated code(units) uses the units in 
https://github.com/AmirAavani/my-units/tree/master/PB2PAS.
   I attached two proto files and the generated unit files as well 
(Please see the ReadME file in the repo for more information).

The proto compiler works with proto3 
syntax(https://developers.google.com/protocol-buffers/docs/proto3). 
Right now, it does not support "bytes" (one could use string instead). 
Also, it does not support "service" as well (WIP).

It would be great if you could go through the output/code and let me 
know what you think (or if you have any comments). I have some specific 
questions:

1) For Repeated Fields, the message class will have the following three 
properties:
   constFieldName, MutableFeildName, and FieldName[Index: Integer].

Their definition looks like this:

property ConstFieldName: TFieldName read FFieldName;
property MutableFieldName: TFieldName read GetOrCreate write FFiledName;
property FieldName[Index: Integer]: TFieldName read GetFiledName;

Is there a better way to handle this. Specifically is there anyway to 
disable MsgObj.ConstFieldName.Add(...)?

2) A similar question about Getters for Non-simple fields (Fields that 
are messages, OneOf, etc). The code generates the following properties, 
per this kind of fields:
property FieldName: TFieldName read FFieldName write FFieldName;
property MutableFieldName: TFieldName read GetOrCreateFieldName;

Should I drop "write FFieldName" from the property FieldName?

3) I do not have a good answer for this one: Should the the items in a 
Map/List be freed in the class destructor?
   In the current implementation, the destructor frees the Maps/Lists 
and all their members. The issue is uncommenting  line 178 of Main.pp 
(attached) would cause a segfault. I had a couple of ideas about how to 
handle this, but none of them is perfect. Wondering what you think?

Regards,
Amir
-------------- next part --------------
syntax = "proto3";

package test;
option go_package = "Samples;testpb";

import "Samples/test_dep1.proto";

message MyMessage {
  message A {
    int32 a_id = 1;
    SubMessage a_sub_message = 2;
  }
  A a = 1100;
  map<int32, A> id_a_map = 1101;

  oneof MyOneOf {
    AnEnum an_enum = 12;
    deptest.AnEnum deptest_an_enum = 13;
    string is_country = 1;
    string is_state = 2;
    string is_county = 3;
    string is_city = 4;
    string is_street = 5;
    string is_str = 6;
    int32 is_i = 8;
    float is_f = 9;
    double is_d = 10;
    A is_a = 11;
  }
  message SubMessage {
    int32 id = 1;
  }
  enum AnEnum {
    NONE = 0;
    V1 = 1;
    V4 = 4;
  }
  repeated string rep_str = 122;
  repeated int32 rep_i = 123;
  repeated float ref_f = 124;
  repeated double rep_d = 125;
  repeated A rep_a=  126;

  double dd = 200;
  float f = 201;
  int32 i32 = 202;
  int64 i64 = 203;
  uint32 ui32 = 204;
  uint64 ui64 = 205;
  sint32 si32 = 206;
  sint64 si64 = 207;
  fixed32 f32 = 208;
  fixed64 f64 = 209;
  sfixed32 s32 = 210;
  sfixed64 s64 = 211;
  bool _b = 212;
  string an_s = 213;

  repeated double rdd = 300;
  repeated float rf = 301;
  repeated int32 ri32 = 302;
  repeated int64 ri64 = 303;
  repeated uint32 rui32 = 304;
  repeated uint64 rui64 = 305;
  repeated sint32 rsi32 = 306;
  repeated sint64 rsi64 = 307;
  repeated fixed32 rf32 = 308;
  repeated fixed64 rf64 = 309;
  repeated sfixed32 rs32 = 310;
  repeated sfixed64 rs64 = 311;
  repeated bool rb = 312;
  repeated string rs = 313;
  repeated AnEnum aenums = 314;

  map<int32, SubMessage> id_sub_message_map = 420;
  map<int32, int32> an_int_int_map = 421;
  // map<int32, AnEnum> an_int_an_enum_map = 127;
  // map<int32, deptest.AnEnum> an_int_to_b_num_map = 128;
  map<int32, int32> b_int_int_map = 429;

}
-------------- next part --------------
syntax = "proto3";

package deptest;
option go_package = "Samples;testpb";

enum AnEnum {
  V0 = 0;
  V1 = 1;
  V2 = 2;
}
-------------- next part --------------
unit TestUnit;
{$Mode objfpc}

interface

uses
 TestDep1Unit, classes, fgl, sysutils, ProtoHelperUnit, ProtoHelperListsUnit, ProtoStreamUnit, GenericCollectionUnit;

type
  TMyMessage = class;

  // message MyMessage
  { TMyMessage }
  TMyMessage = class(TBaseMessage)
  type
    // AnEnum
    TAnEnum = (
      ANENUM_NONE = 0,
      ANENUM_V1 = 1,
      ANENUM_V4 = 4
    );

  // Forward Declarations.
  public type
    TMyOneOf = class;
  public type
    TA = class;
    TSubMessage = class;

  public type
    TMyOneOf = Class(TBaseOneOf)
    private
      function GetAnEnum: TAnEnum;
      procedure SetAnEnum(_AnEnum: TAnEnum);
      function GetDeptestAnEnum: TestDep1Unit.TAnEnum;
      procedure SetDeptestAnEnum(_DeptestAnEnum: TestDep1Unit.TAnEnum);
      function GetIsCountry: AnsiString;
      procedure SetIsCountry(_IsCountry: AnsiString);
      function GetIsState: AnsiString;
      procedure SetIsState(_IsState: AnsiString);
      function GetIsCounty: AnsiString;
      procedure SetIsCounty(_IsCounty: AnsiString);
      function GetIsCity: AnsiString;
      procedure SetIsCity(_IsCity: AnsiString);
      function GetIsStreet: AnsiString;
      procedure SetIsStreet(_IsStreet: AnsiString);
      function GetIsStr: AnsiString;
      procedure SetIsStr(_IsStr: AnsiString);
      function GetIsI: Int32;
      procedure SetIsI(_IsI: Int32);
      function GetIsF: Single;
      procedure SetIsF(_IsF: Single);
      function GetIsD: Double;
      procedure SetIsD(_IsD: Double);
      function GetIsA: TA;
      procedure SetIsA(_IsA: TA);

    public
      property AnEnum: TAnEnum read GetAnEnum write SetAnEnum;
      property DeptestAnEnum: TestDep1Unit.TAnEnum read GetDeptestAnEnum write SetDeptestAnEnum;
      property IsCountry: AnsiString read GetIsCountry write SetIsCountry;
      property IsState: AnsiString read GetIsState write SetIsState;
      property IsCounty: AnsiString read GetIsCounty write SetIsCounty;
      property IsCity: AnsiString read GetIsCity write SetIsCity;
      property IsStreet: AnsiString read GetIsStreet write SetIsStreet;
      property IsStr: AnsiString read GetIsStr write SetIsStr;
      property IsI: Int32 read GetIsI write SetIsI;
      property IsF: Single read GetIsF write SetIsF;
      property IsD: Double read GetIsD write SetIsD;
      property IsA: TA read GetIsA write SetIsA;

      constructor Create;
      destructor Destroy; override;
      procedure Clear; override;

    end;

  public type
    // message A
    { TA }
    TA = class(TBaseMessage)
    // Forward Declarations.

    private
      FAId: Int32;

    public
      // int32 a_id = 1;
      property AId: Int32 read FAId write FAId;

    private
      FASubMessage: TSubMessage;
      function GetOrCreateASubMessage: TSubMessage;

    public
      // SubMessage a_sub_message = 2;
      property ASubMessage: TSubMessage read FASubMessage write FASubMessage;
      property MutableASubMessage: TSubMessage read GetOrCreateASubMessage;

    protected 
      procedure SaveToStream(Stream: TProtoStreamWriter); override;
      function LoadFromStream(Stream: TProtoStreamReader; Len: Integer): Boolean; override;

    public
      constructor Create;
      destructor Destroy; override;
      procedure Clear; override;

    end;

  public type
    // message SubMessage
    { TSubMessage }
    TSubMessage = class(TBaseMessage)
    // Forward Declarations.

    private
      FId: Int32;

    public
      // int32 id = 1;
      property Id: Int32 read FId write FId;

    protected 
      procedure SaveToStream(Stream: TProtoStreamWriter); override;
      function LoadFromStream(Stream: TProtoStreamReader; Len: Integer): Boolean; override;

    public
      constructor Create;
      destructor Destroy; override;
      procedure Clear; override;

    end;

  public type
    TInt32ToAMap = class(specialize TMapSimpleKeyObjectValue<Int32, TA>)

  private
    function LoadFromStream(Stream: TProtoStreamReader): Boolean;
    procedure SaveToStream(Stream: TProtoStreamWriter);

  end;

  public type
    TInt32ToSubMessageMap = class(specialize TMapSimpleKeyObjectValue<Int32, TSubMessage>)

  private
    function LoadFromStream(Stream: TProtoStreamReader): Boolean;
    procedure SaveToStream(Stream: TProtoStreamWriter);

  end;

  public type
    TInt32ToInt32Map = class(specialize TMap<Int32, Int32>)

  private
    function LoadFromStream(Stream: TProtoStreamReader): Boolean;
    procedure SaveToStream(Stream: TProtoStreamWriter);

  end;

  private
    FA: TA;
    function GetOrCreateA: TA;

  public
    // A a = 1100;
    property A: TA read FA write FA;
    property MutableA: TA read GetOrCreateA;

  private
    FIdAMap: TInt32ToAMap;
    function GetOrCreateIdAMap: TInt32ToAMap;

  public
    // map<int32, A> id_a_map = 1101;
    property IdAMap: TInt32ToAMap read FIdAMap write FIdAMap;
    property MutableIdAMap: TInt32ToAMap read GetOrCreateIdAMap;

  private
    FMyOneOf: TMyOneOf;
    function GetOrCreateMyOneOf: TMyOneOf;

  public
    // MyOneOf MyOneOf = -1;
    property MyOneOf: TMyOneOf read FMyOneOf write FMyOneOf;
    property MutableMyOneOf: TMyOneOf read GetOrCreateMyOneOf;

  public type
    TRepStr =  TAnsiStrings;

  private
    FRepStr: TRepStr;

    function GetRepStr(Index: Integer): AnsiString;
    function GetOrCreateRepStr: TRepStr;

  public
    // repeated string rep_str = 122;
    property RepStr[Index: Integer]: AnsiString read GetRepStr;
    property ConstRepStr: TRepStr read FRepStr;
    property MutableRepStr: TRepStr read GetOrCreateRepStr write FRepStr;

  public type
    TRepI =  TInt32s;

  private
    FRepI: TRepI;

    function GetRepI(Index: Integer): Int32;
    function GetOrCreateRepI: TRepI;

  public
    // repeated int32 rep_i = 123;
    property RepI[Index: Integer]: Int32 read GetRepI;
    property ConstRepI: TRepI read FRepI;
    property MutableRepI: TRepI read GetOrCreateRepI write FRepI;

  public type
    TRefF =  TSingles;

  private
    FRefF: TRefF;

    function GetRefF(Index: Integer): Single;
    function GetOrCreateRefF: TRefF;

  public
    // repeated float ref_f = 124;
    property RefF[Index: Integer]: Single read GetRefF;
    property ConstRefF: TRefF read FRefF;
    property MutableRefF: TRefF read GetOrCreateRefF write FRefF;

  public type
    TRepD =  TDoubles;

  private
    FRepD: TRepD;

    function GetRepD(Index: Integer): Double;
    function GetOrCreateRepD: TRepD;

  public
    // repeated double rep_d = 125;
    property RepD[Index: Integer]: Double read GetRepD;
    property ConstRepD: TRepD read FRepD;
    property MutableRepD: TRepD read GetOrCreateRepD write FRepD;

  public type
    TRepA = specialize TObjectList<TA>;

  private
    FRepA: TRepA;

    function GetRepA(Index: Integer): TA;
    function GetRepA: TRepA;
    function GetOrCreateRepA: TRepA;

  public
    // repeated A rep_a = 126;
    property RepA[Index: Integer]: TA read GetRepA;
    property ConstRepA: TRepA read FRepA;
    property MutableRepA: TRepA read GetOrCreateRepA write FRepA;

  private
    FDd: Double;

  public
    // double dd = 200;
    property Dd: Double read FDd write FDd;

  private
    FF: Single;

  public
    // float f = 201;
    property F: Single read FF write FF;

  private
    FI32: Int32;

  public
    // int32 i32 = 202;
    property I32: Int32 read FI32 write FI32;

  private
    FI64: Int64;

  public
    // int64 i64 = 203;
    property I64: Int64 read FI64 write FI64;

  private
    FUi32: UInt32;

  public
    // uint32 ui32 = 204;
    property Ui32: UInt32 read FUi32 write FUi32;

  private
    FUi64: UInt64;

  public
    // uint64 ui64 = 205;
    property Ui64: UInt64 read FUi64 write FUi64;

  private
    FSi32: Int32;

  public
    // sint32 si32 = 206;
    property Si32: Int32 read FSi32 write FSi32;

  private
    FSi64: Int64;

  public
    // sint64 si64 = 207;
    property Si64: Int64 read FSi64 write FSi64;

  private
    FF32: UInt32;

  public
    // fixed32 f32 = 208;
    property F32: UInt32 read FF32 write FF32;

  private
    FF64: UInt64;

  public
    // fixed64 f64 = 209;
    property F64: UInt64 read FF64 write FF64;

  private
    FS32: Int32;

  public
    // sfixed32 s32 = 210;
    property S32: Int32 read FS32 write FS32;

  private
    FS64: Int64;

  public
    // sfixed64 s64 = 211;
    property S64: Int64 read FS64 write FS64;

  private
    FB: Boolean;

  public
    // bool _b = 212;
    property B: Boolean read FB write FB;

  private
    FAnS: AnsiString;

  public
    // string an_s = 213;
    property AnS: AnsiString read FAnS write FAnS;

  public type
    TRdd =  TDoubles;

  private
    FRdd: TRdd;

    function GetRdd(Index: Integer): Double;
    function GetOrCreateRdd: TRdd;

  public
    // repeated double rdd = 300;
    property Rdd[Index: Integer]: Double read GetRdd;
    property ConstRdd: TRdd read FRdd;
    property MutableRdd: TRdd read GetOrCreateRdd write FRdd;

  public type
    TRf =  TSingles;

  private
    FRf: TRf;

    function GetRf(Index: Integer): Single;
    function GetOrCreateRf: TRf;

  public
    // repeated float rf = 301;
    property Rf[Index: Integer]: Single read GetRf;
    property ConstRf: TRf read FRf;
    property MutableRf: TRf read GetOrCreateRf write FRf;

  public type
    TRi32 =  TInt32s;

  private
    FRi32: TRi32;

    function GetRi32(Index: Integer): Int32;
    function GetOrCreateRi32: TRi32;

  public
    // repeated int32 ri32 = 302;
    property Ri32[Index: Integer]: Int32 read GetRi32;
    property ConstRi32: TRi32 read FRi32;
    property MutableRi32: TRi32 read GetOrCreateRi32 write FRi32;

  public type
    TRi64 =  TInt64s;

  private
    FRi64: TRi64;

    function GetRi64(Index: Integer): Int64;
    function GetOrCreateRi64: TRi64;

  public
    // repeated int64 ri64 = 303;
    property Ri64[Index: Integer]: Int64 read GetRi64;
    property ConstRi64: TRi64 read FRi64;
    property MutableRi64: TRi64 read GetOrCreateRi64 write FRi64;

  public type
    TRui32 =  TUInt32s;

  private
    FRui32: TRui32;

    function GetRui32(Index: Integer): UInt32;
    function GetOrCreateRui32: TRui32;

  public
    // repeated uint32 rui32 = 304;
    property Rui32[Index: Integer]: UInt32 read GetRui32;
    property ConstRui32: TRui32 read FRui32;
    property MutableRui32: TRui32 read GetOrCreateRui32 write FRui32;

  public type
    TRui64 =  TUInt64s;

  private
    FRui64: TRui64;

    function GetRui64(Index: Integer): UInt64;
    function GetOrCreateRui64: TRui64;

  public
    // repeated uint64 rui64 = 305;
    property Rui64[Index: Integer]: UInt64 read GetRui64;
    property ConstRui64: TRui64 read FRui64;
    property MutableRui64: TRui64 read GetOrCreateRui64 write FRui64;

  public type
    TRsi32 =  TInt32s;

  private
    FRsi32: TRsi32;

    function GetRsi32(Index: Integer): Int32;
    function GetOrCreateRsi32: TRsi32;

  public
    // repeated sint32 rsi32 = 306;
    property Rsi32[Index: Integer]: Int32 read GetRsi32;
    property ConstRsi32: TRsi32 read FRsi32;
    property MutableRsi32: TRsi32 read GetOrCreateRsi32 write FRsi32;

  public type
    TRsi64 =  TInt64s;

  private
    FRsi64: TRsi64;

    function GetRsi64(Index: Integer): Int64;
    function GetOrCreateRsi64: TRsi64;

  public
    // repeated sint64 rsi64 = 307;
    property Rsi64[Index: Integer]: Int64 read GetRsi64;
    property ConstRsi64: TRsi64 read FRsi64;
    property MutableRsi64: TRsi64 read GetOrCreateRsi64 write FRsi64;

  public type
    TRf32 =  TUInt32s;

  private
    FRf32: TRf32;

    function GetRf32(Index: Integer): UInt32;
    function GetOrCreateRf32: TRf32;

  public
    // repeated fixed32 rf32 = 308;
    property Rf32[Index: Integer]: UInt32 read GetRf32;
    property ConstRf32: TRf32 read FRf32;
    property MutableRf32: TRf32 read GetOrCreateRf32 write FRf32;

  public type
    TRf64 =  TUInt64s;

  private
    FRf64: TRf64;

    function GetRf64(Index: Integer): UInt64;
    function GetOrCreateRf64: TRf64;

  public
    // repeated fixed64 rf64 = 309;
    property Rf64[Index: Integer]: UInt64 read GetRf64;
    property ConstRf64: TRf64 read FRf64;
    property MutableRf64: TRf64 read GetOrCreateRf64 write FRf64;

  public type
    TRs32 =  TInt32s;

  private
    FRs32: TRs32;

    function GetRs32(Index: Integer): Int32;
    function GetOrCreateRs32: TRs32;

  public
    // repeated sfixed32 rs32 = 310;
    property Rs32[Index: Integer]: Int32 read GetRs32;
    property ConstRs32: TRs32 read FRs32;
    property MutableRs32: TRs32 read GetOrCreateRs32 write FRs32;

  public type
    TRs64 =  TInt64s;

  private
    FRs64: TRs64;

    function GetRs64(Index: Integer): Int64;
    function GetOrCreateRs64: TRs64;

  public
    // repeated sfixed64 rs64 = 311;
    property Rs64[Index: Integer]: Int64 read GetRs64;
    property ConstRs64: TRs64 read FRs64;
    property MutableRs64: TRs64 read GetOrCreateRs64 write FRs64;

  public type
    TRb =  TBooleans;

  private
    FRb: TRb;

    function GetRb(Index: Integer): Boolean;
    function GetOrCreateRb: TRb;

  public
    // repeated bool rb = 312;
    property Rb[Index: Integer]: Boolean read GetRb;
    property ConstRb: TRb read FRb;
    property MutableRb: TRb read GetOrCreateRb write FRb;

  public type
    TRs =  TAnsiStrings;

  private
    FRs: TRs;

    function GetRs(Index: Integer): AnsiString;
    function GetOrCreateRs: TRs;

  public
    // repeated string rs = 313;
    property Rs[Index: Integer]: AnsiString read GetRs;
    property ConstRs: TRs read FRs;
    property MutableRs: TRs read GetOrCreateRs write FRs;

  public type
    TAenums =  TInt32s;

  private
    FAenums: TAenums;

    function GetAenums(Index: Integer): Int32;
    function GetOrCreateAenums: TAenums;

  public
    // repeated AnEnum aenums = 314;
    property Aenums[Index: Integer]: Int32 read GetAenums;
    property ConstAenums: TAenums read FAenums;
    property MutableAenums: TAenums read GetOrCreateAenums write FAenums;

  private
    FIdSubMessageMap: TInt32ToSubMessageMap;
    function GetOrCreateIdSubMessageMap: TInt32ToSubMessageMap;

  public
    // map<int32, SubMessage> id_sub_message_map = 420;
    property IdSubMessageMap: TInt32ToSubMessageMap read FIdSubMessageMap write FIdSubMessageMap;
    property MutableIdSubMessageMap: TInt32ToSubMessageMap read GetOrCreateIdSubMessageMap;

  private
    FAnIntIntMap: TInt32ToInt32Map;
    function GetOrCreateAnIntIntMap: TInt32ToInt32Map;

  public
    // map<int32, int32> an_int_int_map = 421;
    property AnIntIntMap: TInt32ToInt32Map read FAnIntIntMap write FAnIntIntMap;
    property MutableAnIntIntMap: TInt32ToInt32Map read GetOrCreateAnIntIntMap;

  private
    FBIntIntMap: TInt32ToInt32Map;
    function GetOrCreateBIntIntMap: TInt32ToInt32Map;

  public
    // map<int32, int32> b_int_int_map = 429;
    property BIntIntMap: TInt32ToInt32Map read FBIntIntMap write FBIntIntMap;
    property MutableBIntIntMap: TInt32ToInt32Map read GetOrCreateBIntIntMap;

  protected 
    procedure SaveToStream(Stream: TProtoStreamWriter); override;
    function LoadFromStream(Stream: TProtoStreamReader; Len: Integer): Boolean; override;

  public
    constructor Create;
    destructor Destroy; override;
    procedure Clear; override;

  end;



implementation

function TMyMessage.TMyOneOf.GetAnEnum: TAnEnum;
begin
  Result := TAnEnum(PUInt64(Self.GetPointerByIndex(0))^)

end;

procedure TMyMessage.TMyOneOf.SetAnEnum(_AnEnum: TAnEnum);
var
  PData: PUInt64;

begin
  PData := new(PUInt64);
  PData^ := UInt64(_AnEnum);
  Self.SetPointerByIndex(0, PData);

end;

function TMyMessage.TMyOneOf.GetDeptestAnEnum: TestDep1Unit.TAnEnum;
begin
  Result := TestDep1Unit.TAnEnum(PUInt64(Self.GetPointerByIndex(1))^)

end;

procedure TMyMessage.TMyOneOf.SetDeptestAnEnum(_DeptestAnEnum: TestDep1Unit.TAnEnum);
var
  PData: PUInt64;

begin
  PData := new(PUInt64);
  PData^ := UInt64(_DeptestAnEnum);
  Self.SetPointerByIndex(1, PData);

end;

function TMyMessage.TMyOneOf.GetIsCountry: AnsiString;
begin
  Result := AnsiString(PAnsiString(Self.GetPointerByIndex(2))^)

end;

procedure TMyMessage.TMyOneOf.SetIsCountry(_IsCountry: AnsiString);
var
  PData: PAnsiString;

begin
  PData := new(PAnsiString);
  PData^ := _IsCountry;
  Self.SetPointerByIndex(2, PData);

end;

function TMyMessage.TMyOneOf.GetIsState: AnsiString;
begin
  Result := AnsiString(PAnsiString(Self.GetPointerByIndex(3))^)

end;

procedure TMyMessage.TMyOneOf.SetIsState(_IsState: AnsiString);
var
  PData: PAnsiString;

begin
  PData := new(PAnsiString);
  PData^ := _IsState;
  Self.SetPointerByIndex(3, PData);

end;

function TMyMessage.TMyOneOf.GetIsCounty: AnsiString;
begin
  Result := AnsiString(PAnsiString(Self.GetPointerByIndex(4))^)

end;

procedure TMyMessage.TMyOneOf.SetIsCounty(_IsCounty: AnsiString);
var
  PData: PAnsiString;

begin
  PData := new(PAnsiString);
  PData^ := _IsCounty;
  Self.SetPointerByIndex(4, PData);

end;

function TMyMessage.TMyOneOf.GetIsCity: AnsiString;
begin
  Result := AnsiString(PAnsiString(Self.GetPointerByIndex(5))^)

end;

procedure TMyMessage.TMyOneOf.SetIsCity(_IsCity: AnsiString);
var
  PData: PAnsiString;

begin
  PData := new(PAnsiString);
  PData^ := _IsCity;
  Self.SetPointerByIndex(5, PData);

end;

function TMyMessage.TMyOneOf.GetIsStreet: AnsiString;
begin
  Result := AnsiString(PAnsiString(Self.GetPointerByIndex(6))^)

end;

procedure TMyMessage.TMyOneOf.SetIsStreet(_IsStreet: AnsiString);
var
  PData: PAnsiString;

begin
  PData := new(PAnsiString);
  PData^ := _IsStreet;
  Self.SetPointerByIndex(6, PData);

end;

function TMyMessage.TMyOneOf.GetIsStr: AnsiString;
begin
  Result := AnsiString(PAnsiString(Self.GetPointerByIndex(7))^)

end;

procedure TMyMessage.TMyOneOf.SetIsStr(_IsStr: AnsiString);
var
  PData: PAnsiString;

begin
  PData := new(PAnsiString);
  PData^ := _IsStr;
  Self.SetPointerByIndex(7, PData);

end;

function TMyMessage.TMyOneOf.GetIsI: Int32;
begin
  Result := Int32(PInt32(Self.GetPointerByIndex(8))^)

end;

procedure TMyMessage.TMyOneOf.SetIsI(_IsI: Int32);
var
  PData: PInt32;

begin
  PData := new(PInt32);
  PData^ := _IsI;
  Self.SetPointerByIndex(8, PData);

end;

function TMyMessage.TMyOneOf.GetIsF: Single;
begin
  Result := Single(PSingle(Self.GetPointerByIndex(9))^)

end;

procedure TMyMessage.TMyOneOf.SetIsF(_IsF: Single);
var
  PData: PSingle;

begin
  PData := new(PSingle);
  PData^ := _IsF;
  Self.SetPointerByIndex(9, PData);

end;

function TMyMessage.TMyOneOf.GetIsD: Double;
begin
  Result := Double(PDouble(Self.GetPointerByIndex(10))^)

end;

procedure TMyMessage.TMyOneOf.SetIsD(_IsD: Double);
var
  PData: PDouble;

begin
  PData := new(PDouble);
  PData^ := _IsD;
  Self.SetPointerByIndex(10, PData);

end;

function TMyMessage.TMyOneOf.GetIsA: TA;
begin
  Result := TA(Self.GetPointerByIndex(11))

end;

procedure TMyMessage.TMyOneOf.SetIsA(_IsA: TA);
begin
  Self.SetPointerByIndex(11, _IsA);

end;


constructor TMyMessage.TMyOneOf.Create;
begin
  inherited Create;

end;

destructor TMyMessage.TMyOneOf.Destroy;
begin
  Clear;


  inherited Destroy;

end;

procedure TMyMessage.TMyOneOf.Clear;
begin
  MaybeDispose(PUInt64(GetPointerByIndex(0)));
  MaybeDispose(PUInt64(GetPointerByIndex(1)));
  MaybeDispose(PAnsiString(GetPointerByIndex(2)));
  MaybeDispose(PAnsiString(GetPointerByIndex(3)));
  MaybeDispose(PAnsiString(GetPointerByIndex(4)));
  MaybeDispose(PAnsiString(GetPointerByIndex(5)));
  MaybeDispose(PAnsiString(GetPointerByIndex(6)));
  MaybeDispose(PAnsiString(GetPointerByIndex(7)));
  MaybeDispose(PInt32(GetPointerByIndex(8)));
  MaybeDispose(PSingle(GetPointerByIndex(9)));
  MaybeDispose(PDouble(GetPointerByIndex(10)));
  GetIsA.Free;
  inherited;

end;

function TMyMessage.TA.GetOrCreateASubMessage: TSubMessage;
begin
  if Self = nil then
    Exit(nil);

  if Self.FASubMessage = nil then
    FASubMessage := TSubMessage.Create;

  Result := FASubMessage; 

end;


constructor TMyMessage.TA.Create;
begin
  inherited Create;


end;


destructor TMyMessage.TA.Destroy;
begin
  Self.Clear;

  inherited;
end;

procedure TMyMessage.TA.Clear;
begin
  FreeAndNil(FASubMessage);

  inherited;
end;

procedure TMyMessage.TA.SaveToStream(Stream: TProtoStreamWriter);
begin
    SaveInt32(Stream, AId, 1);

    SaveMessage(Stream, ASubMessage, 2);

end;


function TMyMessage.TA.LoadFromStream(Stream: TProtoStreamReader; Len: Integer): Boolean;
var
  StartPos, FieldNumber, WireType: Integer;

begin
  StartPos := Stream.Position;
  while Stream.Position < StartPos + Len do
  begin
    Stream.ReadTag(FieldNumber, WireType);

    case FieldNumber of
      1:
        AId := LoadInt32(Stream);

      2:
      begin
        if WireType <> 2 then
          Exit(False);
        if not LoadMessage(Stream, MutableASubMessage) then
          Exit(False);
      end;

    end;
  end;

  Result := StartPos + Len = Stream.Position;

end;
constructor TMyMessage.TSubMessage.Create;
begin
  inherited Create;


end;


destructor TMyMessage.TSubMessage.Destroy;
begin
  Self.Clear;

  inherited;
end;

procedure TMyMessage.TSubMessage.Clear;
begin

  inherited;
end;

procedure TMyMessage.TSubMessage.SaveToStream(Stream: TProtoStreamWriter);
begin
    SaveInt32(Stream, Id, 1);

end;


function TMyMessage.TSubMessage.LoadFromStream(Stream: TProtoStreamReader; Len: Integer): Boolean;
var
  StartPos, FieldNumber, WireType: Integer;

begin
  StartPos := Stream.Position;
  while Stream.Position < StartPos + Len do
  begin
    Stream.ReadTag(FieldNumber, WireType);

    case FieldNumber of
      1:
        Id := LoadInt32(Stream);


    end;
  end;

  Result := StartPos + Len = Stream.Position;

end;

function TMyMessage.TInt32ToAMap.LoadFromStream(Stream: TProtoStreamReader): Boolean;
var
  StartPos, Len, f, w, fs: Integer;
  Key: Int32;
  Value: TA;

begin
  Len := Stream.ReadVarUInt32;
  StartPos := Stream.Position;
  fs := 0;

  while Stream.Position < StartPos + Len do
  begin
    Stream.ReadTag(f, w);

    if f = 1 then
    begin
      Key := Loadint32(Stream)
    end
    else if f = 2 then
    begin
      Value := TA.Create;

      if not LoadMessage(Stream, Value) then
      begin
        Exit(False);
      end

    end
    else
      Exit(False);

    fs := fs xor f;
    if fs = 3 then
    begin
      Self.Add(Key, Value);
      fs := 0;

    end;

  end;

  Result := StartPos + Len = Stream.Position;
end;

procedure TMyMessage.TInt32ToAMap.SaveToStream(Stream: TProtoStreamWriter);
var
  it: TMyMessage.TInt32ToAMap.TPairEnumerator;
  SizeNode: TLinkListNode;

begin
  if (Self = nil) or (Self.Count = 0) then
    Exit;

  it := Self.GetEnumerator;
  while it.MoveNext do
  begin
    Stream.WriteTag(1101, WIRETYPE_LENGTH_DELIMITED);
    SizeNode := Stream.AddIntervalNode;
    SaveInt32(Stream, it.Current.Key, 1);
    SaveMessage(Stream, it.Current.Value, 2);
    SizeNode.WriteLength(SizeNode.TotalSize);

  end;
  it.Free;

end;



function TMyMessage.TInt32ToSubMessageMap.LoadFromStream(Stream: TProtoStreamReader): Boolean;
var
  StartPos, Len, f, w, fs: Integer;
  Key: Int32;
  Value: TSubMessage;

begin
  Len := Stream.ReadVarUInt32;
  StartPos := Stream.Position;
  fs := 0;

  while Stream.Position < StartPos + Len do
  begin
    Stream.ReadTag(f, w);

    if f = 1 then
    begin
      Key := Loadint32(Stream)
    end
    else if f = 2 then
    begin
      Value := TSubMessage.Create;

      if not LoadMessage(Stream, Value) then
      begin
        Exit(False);
      end

    end
    else
      Exit(False);

    fs := fs xor f;
    if fs = 3 then
    begin
      Self.Add(Key, Value);
      fs := 0;

    end;

  end;

  Result := StartPos + Len = Stream.Position;
end;

procedure TMyMessage.TInt32ToSubMessageMap.SaveToStream(Stream: TProtoStreamWriter);
var
  it: TMyMessage.TInt32ToSubMessageMap.TPairEnumerator;
  SizeNode: TLinkListNode;

begin
  if (Self = nil) or (Self.Count = 0) then
    Exit;

  it := Self.GetEnumerator;
  while it.MoveNext do
  begin
    Stream.WriteTag(420, WIRETYPE_LENGTH_DELIMITED);
    SizeNode := Stream.AddIntervalNode;
    SaveInt32(Stream, it.Current.Key, 1);
    SaveMessage(Stream, it.Current.Value, 2);
    SizeNode.WriteLength(SizeNode.TotalSize);

  end;
  it.Free;

end;



function TMyMessage.TInt32ToInt32Map.LoadFromStream(Stream: TProtoStreamReader): Boolean;
var
  StartPos, Len, f, w, fs: Integer;
  Key: Int32;
  Value: Int32;

begin
  Len := Stream.ReadVarUInt32;
  StartPos := Stream.Position;
  fs := 0;

  while Stream.Position < StartPos + Len do
  begin
    Stream.ReadTag(f, w);

    if f = 1 then
    begin
      Key := Loadint32(Stream)
    end
    else if f = 2 then
    begin
      Value := Loadint32(Stream)
    end
    else
      Exit(False);

    fs := fs xor f;
    if fs = 3 then
    begin
      Self.Add(Key, Value);
      fs := 0;

    end;

  end;

  Result := StartPos + Len = Stream.Position;
end;

procedure TMyMessage.TInt32ToInt32Map.SaveToStream(Stream: TProtoStreamWriter);
var
  it: TMyMessage.TInt32ToInt32Map.TPairEnumerator;
  SizeNode: TLinkListNode;

begin
  if (Self = nil) or (Self.Count = 0) then
    Exit;

  it := Self.GetEnumerator;
  while it.MoveNext do
  begin
    Stream.WriteTag(421, WIRETYPE_LENGTH_DELIMITED);
    SizeNode := Stream.AddIntervalNode;
    SaveInt32(Stream, it.Current.Key, 1);
    SaveInt32(Stream, it.Current.Value, 2);
    SizeNode.WriteLength(SizeNode.TotalSize);

  end;
  it.Free;

end;


function TMyMessage.GetOrCreateA: TA;
begin
  if Self = nil then
    Exit(nil);

  if Self.FA = nil then
    FA := TA.Create;

  Result := FA; 

end;


function TMyMessage.GetOrCreateIdAMap: TInt32ToAMap;
begin
  if Self = nil then
    Exit(nil);

  if Self.FIdAMap = nil then
    FIdAMap := TInt32ToAMap.Create;

  Result := FIdAMap; 

end;


function TMyMessage.GetOrCreateMyOneOf: TMyOneOf;
begin
  if Self = nil then
    Exit(nil);

  if Self.FMyOneOf = nil then
    FMyOneOf := TMyOneOf.Create;

  Result := FMyOneOf; 

end;



function TMyMessage.GetRepStr(Index: Integer): AnsiString;
begin
  Result := FRepStr[Index];

end;

function TMyMessage.GetOrCreateRepStr: TRepStr;

begin
  if FRepStr = nil then
    FRepStr := TRepStr.Create;
  Result := FRepStr;

end;


function TMyMessage.GetRepI(Index: Integer): Int32;
begin
  Result := FRepI[Index];

end;

function TMyMessage.GetOrCreateRepI: TRepI;

begin
  if FRepI = nil then
    FRepI := TRepI.Create;
  Result := FRepI;

end;


function TMyMessage.GetRefF(Index: Integer): Single;
begin
  Result := FRefF[Index];

end;

function TMyMessage.GetOrCreateRefF: TRefF;

begin
  if FRefF = nil then
    FRefF := TRefF.Create;
  Result := FRefF;

end;


function TMyMessage.GetRepD(Index: Integer): Double;
begin
  Result := FRepD[Index];

end;

function TMyMessage.GetOrCreateRepD: TRepD;

begin
  if FRepD = nil then
    FRepD := TRepD.Create;
  Result := FRepD;

end;


function TMyMessage.GetRepA(Index: Integer): TA;
begin
  Result := FRepA[Index];

end;

function TMyMessage.GetRepA: TRepA;
begin
  if Self = nil then
    Exit(nil);

  if FRepA = nil then
    Exit(nil);

  Result := FRepA; 

end;

function TMyMessage.GetOrCreateRepA: TRepA;
begin
  if Self = nil then
    Exit(nil);

  if Self.FRepA = nil then
    FRepA := TRepA.Create;

  Result := FRepA; 

end;



function TMyMessage.GetRdd(Index: Integer): Double;
begin
  Result := FRdd[Index];

end;

function TMyMessage.GetOrCreateRdd: TRdd;

begin
  if FRdd = nil then
    FRdd := TRdd.Create;
  Result := FRdd;

end;


function TMyMessage.GetRf(Index: Integer): Single;
begin
  Result := FRf[Index];

end;

function TMyMessage.GetOrCreateRf: TRf;

begin
  if FRf = nil then
    FRf := TRf.Create;
  Result := FRf;

end;


function TMyMessage.GetRi32(Index: Integer): Int32;
begin
  Result := FRi32[Index];

end;

function TMyMessage.GetOrCreateRi32: TRi32;

begin
  if FRi32 = nil then
    FRi32 := TRi32.Create;
  Result := FRi32;

end;


function TMyMessage.GetRi64(Index: Integer): Int64;
begin
  Result := FRi64[Index];

end;

function TMyMessage.GetOrCreateRi64: TRi64;

begin
  if FRi64 = nil then
    FRi64 := TRi64.Create;
  Result := FRi64;

end;


function TMyMessage.GetRui32(Index: Integer): UInt32;
begin
  Result := FRui32[Index];

end;

function TMyMessage.GetOrCreateRui32: TRui32;

begin
  if FRui32 = nil then
    FRui32 := TRui32.Create;
  Result := FRui32;

end;


function TMyMessage.GetRui64(Index: Integer): UInt64;
begin
  Result := FRui64[Index];

end;

function TMyMessage.GetOrCreateRui64: TRui64;

begin
  if FRui64 = nil then
    FRui64 := TRui64.Create;
  Result := FRui64;

end;


function TMyMessage.GetRsi32(Index: Integer): Int32;
begin
  Result := FRsi32[Index];

end;

function TMyMessage.GetOrCreateRsi32: TRsi32;

begin
  if FRsi32 = nil then
    FRsi32 := TRsi32.Create;
  Result := FRsi32;

end;


function TMyMessage.GetRsi64(Index: Integer): Int64;
begin
  Result := FRsi64[Index];

end;

function TMyMessage.GetOrCreateRsi64: TRsi64;

begin
  if FRsi64 = nil then
    FRsi64 := TRsi64.Create;
  Result := FRsi64;

end;


function TMyMessage.GetRf32(Index: Integer): UInt32;
begin
  Result := FRf32[Index];

end;

function TMyMessage.GetOrCreateRf32: TRf32;

begin
  if FRf32 = nil then
    FRf32 := TRf32.Create;
  Result := FRf32;

end;


function TMyMessage.GetRf64(Index: Integer): UInt64;
begin
  Result := FRf64[Index];

end;

function TMyMessage.GetOrCreateRf64: TRf64;

begin
  if FRf64 = nil then
    FRf64 := TRf64.Create;
  Result := FRf64;

end;


function TMyMessage.GetRs32(Index: Integer): Int32;
begin
  Result := FRs32[Index];

end;

function TMyMessage.GetOrCreateRs32: TRs32;

begin
  if FRs32 = nil then
    FRs32 := TRs32.Create;
  Result := FRs32;

end;


function TMyMessage.GetRs64(Index: Integer): Int64;
begin
  Result := FRs64[Index];

end;

function TMyMessage.GetOrCreateRs64: TRs64;

begin
  if FRs64 = nil then
    FRs64 := TRs64.Create;
  Result := FRs64;

end;


function TMyMessage.GetRb(Index: Integer): Boolean;
begin
  Result := FRb[Index];

end;

function TMyMessage.GetOrCreateRb: TRb;

begin
  if FRb = nil then
    FRb := TRb.Create;
  Result := FRb;

end;


function TMyMessage.GetRs(Index: Integer): AnsiString;
begin
  Result := FRs[Index];

end;

function TMyMessage.GetOrCreateRs: TRs;

begin
  if FRs = nil then
    FRs := TRs.Create;
  Result := FRs;

end;


function TMyMessage.GetAenums(Index: Integer): Int32;
begin
  Result := FAenums[Index];

end;

function TMyMessage.GetOrCreateAenums: TAenums;

begin
  if FAenums = nil then
    FAenums := TAenums.Create;
  Result := FAenums;

end;

function TMyMessage.GetOrCreateIdSubMessageMap: TInt32ToSubMessageMap;
begin
  if Self = nil then
    Exit(nil);

  if Self.FIdSubMessageMap = nil then
    FIdSubMessageMap := TInt32ToSubMessageMap.Create;

  Result := FIdSubMessageMap; 

end;


function TMyMessage.GetOrCreateAnIntIntMap: TInt32ToInt32Map;
begin
  if Self = nil then
    Exit(nil);

  if Self.FAnIntIntMap = nil then
    FAnIntIntMap := TInt32ToInt32Map.Create;

  Result := FAnIntIntMap; 

end;


function TMyMessage.GetOrCreateBIntIntMap: TInt32ToInt32Map;
begin
  if Self = nil then
    Exit(nil);

  if Self.FBIntIntMap = nil then
    FBIntIntMap := TInt32ToInt32Map.Create;

  Result := FBIntIntMap; 

end;


constructor TMyMessage.Create;
begin
  inherited Create;


end;


destructor TMyMessage.Destroy;
begin
  Self.Clear;

  inherited;
end;

procedure TMyMessage.Clear;
begin
  FreeAndNil(FA);
  FreeAndNil(FIdAMap);
  FreeAndNil(FMyOneOf);
  FreeAndNil(FRepStr);
  FreeAndNil(FRepI);
  FreeAndNil(FRefF);
  FreeAndNil(FRepD);
  FreeAndNil(FRepA);
  FreeAndNil(FRdd);
  FreeAndNil(FRf);
  FreeAndNil(FRi32);
  FreeAndNil(FRi64);
  FreeAndNil(FRui32);
  FreeAndNil(FRui64);
  FreeAndNil(FRsi32);
  FreeAndNil(FRsi64);
  FreeAndNil(FRf32);
  FreeAndNil(FRf64);
  FreeAndNil(FRs32);
  FreeAndNil(FRs64);
  FreeAndNil(FRb);
  FreeAndNil(FRs);
  FreeAndNil(FAenums);
  FreeAndNil(FIdSubMessageMap);
  FreeAndNil(FAnIntIntMap);
  FreeAndNil(FBIntIntMap);

  inherited;
end;

procedure TMyMessage.SaveToStream(Stream: TProtoStreamWriter);
var
  _Aenums: Int32;


begin
  SaveMessage(Stream, A, 1100);

  if IdAMap <> nil then
    IdAMap.SaveToStream(Stream);

  if MyOneOf <> nil then
  begin
    if MyOneOf.GetPointerByIndex(0) <> nil then
      SaveInt32(Stream, Ord(MyOneOf.AnEnum), 12);
    if MyOneOf.GetPointerByIndex(1) <> nil then
      SaveInt32(Stream, Ord(MyOneOf.DeptestAnEnum), 13);
    if MyOneOf.GetPointerByIndex(2) <> nil then
      SaveString(Stream, MyOneOf.IsCountry, 1);
    if MyOneOf.GetPointerByIndex(3) <> nil then
      SaveString(Stream, MyOneOf.IsState, 2);
    if MyOneOf.GetPointerByIndex(4) <> nil then
      SaveString(Stream, MyOneOf.IsCounty, 3);
    if MyOneOf.GetPointerByIndex(5) <> nil then
      SaveString(Stream, MyOneOf.IsCity, 4);
    if MyOneOf.GetPointerByIndex(6) <> nil then
      SaveString(Stream, MyOneOf.IsStreet, 5);
    if MyOneOf.GetPointerByIndex(7) <> nil then
      SaveString(Stream, MyOneOf.IsStr, 6);
    if MyOneOf.GetPointerByIndex(8) <> nil then
      SaveInt32(Stream, MyOneOf.IsI, 8);
    if MyOneOf.GetPointerByIndex(9) <> nil then
      SaveFloat(Stream, MyOneOf.IsF, 9);
    if MyOneOf.GetPointerByIndex(10) <> nil then
      SaveDouble(Stream, MyOneOf.IsD, 10);
    if MyOneOf.GetPointerByIndex(11) <> nil then
      SaveMessage(Stream, MyOneOf.IsA, 11);
  end;

  SaveRepeatedString(Stream, FRepStr, 122);

  SaveRepeatedInt32(Stream, FRepI, 123);

  SaveRepeatedFloat(Stream, FRefF, 124);

  SaveRepeatedDouble(Stream, FRepD, 125);

  specialize SaveRepeatedMessage<TA>(Stream, FRepA, 126);

  SaveDouble(Stream, Dd, 200);

  SaveFloat(Stream, F, 201);

  SaveInt32(Stream, I32, 202);

  SaveInt64(Stream, I64, 203);

  SaveUint32(Stream, Ui32, 204);

  SaveUint64(Stream, Ui64, 205);

  SaveSint32(Stream, Si32, 206);

  SaveSint64(Stream, Si64, 207);

  SaveFixed32(Stream, F32, 208);

  SaveFixed64(Stream, F64, 209);

  SaveSfixed32(Stream, S32, 210);

  SaveSfixed64(Stream, S64, 211);

  SaveBool(Stream, B, 212);

  SaveString(Stream, AnS, 213);

  SaveRepeatedDouble(Stream, FRdd, 300);

  SaveRepeatedFloat(Stream, FRf, 301);

  SaveRepeatedInt32(Stream, FRi32, 302);

  SaveRepeatedInt64(Stream, FRi64, 303);

  SaveRepeatedUint32(Stream, FRui32, 304);

  SaveRepeatedUint64(Stream, FRui64, 305);

  SaveRepeatedSint32(Stream, FRsi32, 306);

  SaveRepeatedSint64(Stream, FRsi64, 307);

  SaveRepeatedFixed32(Stream, FRf32, 308);

  SaveRepeatedFixed64(Stream, FRf64, 309);

  SaveRepeatedSfixed32(Stream, FRs32, 310);

  SaveRepeatedSfixed64(Stream, FRs64, 311);

  SaveRepeatedBool(Stream, FRb, 312);

  SaveRepeatedString(Stream, FRs, 313);

  if  FAenums <> nil then
  begin
    for _Aenums in FAenums do
      SaveInt32(Stream, Int32(_Aenums), 314);
  end;


  if IdSubMessageMap <> nil then
    IdSubMessageMap.SaveToStream(Stream);

  if AnIntIntMap <> nil then
    AnIntIntMap.SaveToStream(Stream);

  if BIntIntMap <> nil then
    BIntIntMap.SaveToStream(Stream);

end;


function TMyMessage.LoadFromStream(Stream: TProtoStreamReader; Len: Integer): Boolean;
var
  StartPos, FieldNumber, WireType: Integer;

begin
  StartPos := Stream.Position;
  while Stream.Position < StartPos + Len do
  begin
    Stream.ReadTag(FieldNumber, WireType);

    case FieldNumber of
    1100:
    begin
      if WireType <> 2 then
        Exit(False);
      if not LoadMessage(Stream, MutableA) then
        Exit(False);
    end;
    1101:
    begin
      if WireType <> 2 then
        Exit(False);
      if not MutableIdAMap.LoadFromStream(Stream) then
        Exit(False);
    end;

    12: MutableMyOneOf.AnEnum := TAnEnum(LoadInt32(Stream));

    13: MutableMyOneOf.DeptestAnEnum := TestDep1Unit.TAnEnum(LoadInt32(Stream));
    1:
      MutableMyOneOf.IsCountry := LoadString(Stream);

    2:
      MutableMyOneOf.IsState := LoadString(Stream);

    3:
      MutableMyOneOf.IsCounty := LoadString(Stream);

    4:
      MutableMyOneOf.IsCity := LoadString(Stream);

    5:
      MutableMyOneOf.IsStreet := LoadString(Stream);

    6:
      MutableMyOneOf.IsStr := LoadString(Stream);

    8:
      MutableMyOneOf.IsI := LoadInt32(Stream);

    9:
      MutableMyOneOf.IsF := LoadFloat(Stream);

    10:
      MutableMyOneOf.IsD := LoadDouble(Stream);

    11:
    begin
      if WireType <> 2 then
        Exit(False);
      if not LoadMessage(Stream, MutableMyOneOf.IsA) then
        Exit(False);
    end;
    122: 
      if not LoadRepeatedString(Stream, MutableRepStr) then
        Exit(False);

    123: 
      if not LoadRepeatedInt32(Stream, MutableRepI) then
        Exit(False);

    124: 
      if not LoadRepeatedFloat(Stream, MutableRefF) then
        Exit(False);

    125: 
      if not LoadRepeatedDouble(Stream, MutableRepD) then
        Exit(False);

    126: 
      if not (specialize LoadRepeatedMessage<TA>(Stream, MutableRepA)) then
        Exit(False);

    200:
      Dd := LoadDouble(Stream);

    201:
      F := LoadFloat(Stream);

    202:
      I32 := LoadInt32(Stream);

    203:
      I64 := LoadInt64(Stream);

    204:
      Ui32 := LoadUint32(Stream);

    205:
      Ui64 := LoadUint64(Stream);

    206:
      Si32 := LoadSint32(Stream);

    207:
      Si64 := LoadSint64(Stream);

    208:
      F32 := LoadFixed32(Stream);

    209:
      F64 := LoadFixed64(Stream);

    210:
      S32 := LoadSfixed32(Stream);

    211:
      S64 := LoadSfixed64(Stream);

    212:
      B := LoadBool(Stream);

    213:
      AnS := LoadString(Stream);

    300: 
      if not LoadRepeatedDouble(Stream, MutableRdd) then
        Exit(False);

    301: 
      if not LoadRepeatedFloat(Stream, MutableRf) then
        Exit(False);

    302: 
      if not LoadRepeatedInt32(Stream, MutableRi32) then
        Exit(False);

    303: 
      if not LoadRepeatedInt64(Stream, MutableRi64) then
        Exit(False);

    304: 
      if not LoadRepeatedUint32(Stream, MutableRui32) then
        Exit(False);

    305: 
      if not LoadRepeatedUint64(Stream, MutableRui64) then
        Exit(False);

    306: 
      if not LoadRepeatedSint32(Stream, MutableRsi32) then
        Exit(False);

    307: 
      if not LoadRepeatedSint64(Stream, MutableRsi64) then
        Exit(False);

    308: 
      if not LoadRepeatedFixed32(Stream, MutableRf32) then
        Exit(False);

    309: 
      if not LoadRepeatedFixed64(Stream, MutableRf64) then
        Exit(False);

    310: 
      if not LoadRepeatedSfixed32(Stream, MutableRs32) then
        Exit(False);

    311: 
      if not LoadRepeatedSfixed64(Stream, MutableRs64) then
        Exit(False);

    312: 
      if not LoadRepeatedBool(Stream, MutableRb) then
        Exit(False);

    313: 
      if not LoadRepeatedString(Stream, MutableRs) then
        Exit(False);

    314: 
        if not LoadRepeatedInt32(Stream, MutableAenums) then          Exit(False);

    420:
    begin
      if WireType <> 2 then
        Exit(False);
      if not MutableIdSubMessageMap.LoadFromStream(Stream) then
        Exit(False);
    end;
    421:
    begin
      if WireType <> 2 then
        Exit(False);
      if not MutableAnIntIntMap.LoadFromStream(Stream) then
        Exit(False);
    end;
    429:
    begin
      if WireType <> 2 then
        Exit(False);
      if not MutableBIntIntMap.LoadFromStream(Stream) then
        Exit(False);
    end;

    end;
  end;

  Result := StartPos + Len = Stream.Position;

end;


end.
-------------- next part --------------
unit TestDep1Unit;
{$Mode objfpc}

interface

uses
 classes, fgl, sysutils, ProtoHelperUnit, ProtoHelperListsUnit, ProtoStreamUnit, GenericCollectionUnit;

type
  // AnEnum
  TAnEnum = (
    ANENUM_V0 = 0,
    ANENUM_V1 = 1,
    ANENUM_V2 = 2
  );



implementation




end.
-------------- next part --------------
program main;
{$R+}
{$O+}
uses
  SysUtils, Classes, TestUnit, ProtoHelperUnit;

procedure LoadFromFile(aFilename: AnsiString; var e: TMyMessage);
begin
  WriteLN(Format('Loading from %s', [aFilename]));
  e.Free;
  e := TMyMessage.Create;
  if not e.LoadFromStream(TFileStream.Create(aFilename, fmOpenRead)) then
  begin
    WriteLn('Error while loading ', aFilename);
    Halt(2);
    
  end;

end;

var
  e: TMyMessage;
  s: TMyMessage.TSubMessage;

begin
  e := TMyMessage.Create;

  e.Dd := 1.234;
  e.SaveToStream(TFileStream.Create('/tmp/e1~', fmCreate));
  LoadFromFile('/tmp/e1~', e);

  e.F := 1.235;
  e.SaveToStream(TFileStream.Create('/tmp/e2~', fmCreate));
  LoadFromFile('/tmp/e2~', e);

  e.I32 := 202;
  e.SaveToStream(TFileStream.Create('/tmp/e3~', fmCreate));
  LoadFromFile('/tmp/e3~', e);

  e.I64 := -203;
  e.SaveToStream(TFileStream.Create('/tmp/e4~', fmCreate));
  LoadFromFile('/tmp/e4~', e);

  e.Ui32 := 204;
  e.SaveToStream(TFileStream.Create('/tmp/e5~', fmCreate));
  LoadFromFile('/tmp/e5~', e);

  e.Ui64 := 205;
  e.SaveToStream(TFileStream.Create('/tmp/e6~', fmCreate));
  LoadFromFile('/tmp/e6~', e);

  e.Si32 := -206;
  e.SaveToStream(TFileStream.Create('/tmp/e7~', fmCreate));
  LoadFromFile('/tmp/e7~', e);

  e.Si64 := 207;
  e.SaveToStream(TFileStream.Create('/tmp/e8~', fmCreate));
  LoadFromFile('/tmp/e8~', e);

  e.F32 := 208;
  e.SaveToStream(TFileStream.Create('/tmp/e9~', fmCreate));
  LoadFromFile('/tmp/e9~', e);

  e.F64 := 209;
  e.SaveToStream(TFileStream.Create('/tmp/e10~', fmCreate));
  LoadFromFile('/tmp/e10~', e);

  e.B := True;
  e.SaveToStream(TFileStream.Create('/tmp/e11~', fmCreate));
  LoadFromFile('/tmp/e11~', e);

  e.B := False;
  e.SaveToStream(TFileStream.Create('/tmp/e12~', fmCreate));
  LoadFromFile('/tmp/e12~', e);

  e.AnS := 'testing';
  e.SaveToStream(TFileStream.Create('/tmp/e13~', fmCreate));
  LoadFromFile('/tmp/e13~', e);

  e.MutableRdd.Add(300);   e.MutableRdd.Add(-300);
  e.SaveToStream(TFileStream.Create('/tmp/e14~', fmCreate));
  LoadFromFile('/tmp/e14~', e);

  e.MutableRf.Add(301); e.MutableRf.Add(-301);
  e.SaveToStream(TFileStream.Create('/tmp/e15~', fmCreate));
  LoadFromFile('/tmp/e15~', e);

  e.MutableRi32.Add(302); e.MutableRi32.Add(-302);
  e.SaveToStream(TFileStream.Create('/tmp/e16~', fmCreate));
  LoadFromFile('/tmp/e16~', e);

  e.MutableRi64.Add(303); e.MutableRi64.Add(-303);
  e.SaveToStream(TFileStream.Create('/tmp/e17~', fmCreate));
  LoadFromFile('/tmp/e17~', e);

  e.MutableRui32.Add(304); e.MutableRui32.Add(304304);
  e.SaveToStream(TFileStream.Create('/tmp/e18~', fmCreate));
  LoadFromFile('/tmp/e18~', e);

  e.MutableRui64.Add(305); e.MutableRui64.Add(305305305);
  e.SaveToStream(TFileStream.Create('/tmp/e19~', fmCreate));
  LoadFromFile('/tmp/e19~', e);

  e.MutableRsi32.Add(306); e.MutableRsi32.Add(-306306);
  e.SaveToStream(TFileStream.Create('/tmp/e20~', fmCreate));
  LoadFromFile('/tmp/e20~', e);

  e.MutableRsi64.Add(307); e.MutableRsi64.Add(-307307307);
  e.SaveToStream(TFileStream.Create('/tmp/e21~', fmCreate));
  LoadFromFile('/tmp/e21~', e);

  e.MutableRf32.Add(308); e.MutableRf32.Add(308308);
  e.SaveToStream(TFileStream.Create('/tmp/e22~', fmCreate));
  LoadFromFile('/tmp/e22~', e);

  e.MutableRf64.Add(309); e.MutableRf64.Add(+309309309);
  e.SaveToStream(TFileStream.Create('/tmp/e23~', fmCreate));
  LoadFromFile('/tmp/e23~', e);

  e.MutableRs32.Add(310); e.MutableRs32.Add(310310);
  e.SaveToStream(TFileStream.Create('/tmp/e24~', fmCreate));
  LoadFromFile('/tmp/e24~', e);

  e.MutableRs64.Add(311); e.MutableRs64.Add(-311311311);
  e.SaveToStream(TFileStream.Create('/tmp/e25~', fmCreate));
  LoadFromFile('/tmp/e25~', e);

  e.MutableRb.Add(False); e.MutableRb.Add(True); e.MutableRb.Add(True);
  e.SaveToStream(TFileStream.Create('/tmp/e26~', fmCreate));
  LoadFromFile('/tmp/e26~', e);

  e.MutableRs.Add('testing'); e.MutableRs.Add('-testingtesting');
  e.SaveToStream(TFileStream.Create('/tmp/e27~', fmCreate));
  LoadFromFile('/tmp/e27~', e);

  e.MutableAnIntIntMap.Add(1, 1);
  e.SaveToStream(TFileStream.Create('/tmp/e28~', fmCreate));
  LoadFromFile('/tmp/e28~', e);
  e.AnIntIntMap.Add(3, -102345); 
  e.MutableAnIntIntMap.Add(2, 102345); 
  e.SaveToStream(TFileStream.Create('/tmp/e29~', fmCreate));
  LoadFromFile('/tmp/e29~', e);

  e.IdSubMessageMap := TMyMessage.TInt32ToSubMessageMap.Create;
  e.IdSubMessageMap.Add(1, TMyMessage.TSubMessage.Create);
  e.IdSubMessageMap[1].Id := 1;
  e.SaveToStream(TFileStream.Create('/tmp/e30~', fmCreate));
  LoadFromFile('/tmp/e30~', e);

  e.IdSubMessageMap.Add(20, TMyMessage.TSubMessage.Create); 
  e.IdSubMessageMap[20].Id := 20;
  e.SaveToStream(TFileStream.Create('/tmp/e31~', fmCreate));
  LoadFromFile('/tmp/e31~', e);

  e.MyOneOf := TMyMessage.TMyOneOf.Create;
  e.MyOneOf.IsCity := 'Sari';
  e.SaveToStream(TFileStream.Create('/tmp/e32~', fmCreate));
  LoadFromFile('/tmp/e32~', e);

  e.MyOneOf.IsCountry := 'Iran';
  e.SaveToStream(TFileStream.Create('/tmp/e33~', fmCreate));
  LoadFromFile('/tmp/e33~', e);

	s := TMyMessage.TSubMessage.Create;
	s.Id := 234;
  e.A := TMyMessage.TA.Create;
	e.A.AId := 234;
	e.A.ASubMessage := s;
  e.SaveToStream(TFileStream.Create('/tmp/e34~', fmCreate));
  LoadFromFile('/tmp/e34~', e);

	e.IdAMap := TMyMessage.TInt32ToAMap.Create;
	e.IdAMap.Add(245, TMyMessage.TA.Create);
	e.IdAMap[245].Aid := 245;
  e.SaveToStream(TFileStream.Create('/tmp/e35~', fmCreate));
  LoadFromFile('/tmp/e35~', e);

	// e.IdAMap[234] := e.A;
 
  e.Free;
end.


More information about the fpc-devel mailing list