THRIFT-5749 Option to enable RTTI info (2nd attempt)
Client: Delphi
Patch: Jens Geyer
diff --git a/compiler/cpp/src/thrift/generate/t_delphi_generator.cc b/compiler/cpp/src/thrift/generate/t_delphi_generator.cc
index fbf6709..8bf9a3f 100644
--- a/compiler/cpp/src/thrift/generate/t_delphi_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_delphi_generator.cc
@@ -1672,9 +1672,11 @@
     generate_delphi_doc(out, tstruct);
     if(rtti_) {
       indent(out) << "{$TYPEINFO ON}" << endl;
-      indent(out) << "{$RTTI INHERIT}" << endl;
+      indent(out) << "{$RTTI EXPLICIT METHODS([vcPublic, vcPublished]) PROPERTIES([vcPublic, vcPublished])}" << endl;
+      indent(out) << struct_intf_name << " = interface(IBaseWithTypeInfo)" << endl;
+    } else {
+      indent(out) << struct_intf_name << " = interface(IBase)" << endl;
     }
-    indent(out) << struct_intf_name << " = interface(IBase)" << endl;
     indent_up();
 
     generate_guid(out);
@@ -3177,13 +3179,15 @@
     return "";
   case t_base_type::TYPE_STRING:
     if (tbase->is_binary()) {
-      if (ansistr_binary_) {
+      if (ansistr_binary_)
         return "System.AnsiString";
-      } else {
-        return com_types_ ? "IThriftBytes" : "SysUtils.TBytes";
-      }
+      if( com_types_)
+        return "IThriftBytes";
+      if( rtti_)
+        return "Thrift.Protocol.TThriftBytes";  // has TypeInfo
+      return  "SysUtils.TBytes";
     } else {
-      return com_types_ ? "System.WideString" : "System.string";
+      return com_types_ ? "System.WideString" : "System.UnicodeString";
     }
   case t_base_type::TYPE_UUID:
     return "System.TGuid";
diff --git a/lib/delphi/src/Thrift.Protocol.pas b/lib/delphi/src/Thrift.Protocol.pas
index 0134ddf..f5cb454 100644
--- a/lib/delphi/src/Thrift.Protocol.pas
+++ b/lib/delphi/src/Thrift.Protocol.pas
@@ -197,6 +197,17 @@
 
   IThriftBytes = interface; // forward
 
+  {$TYPEINFO ON}
+  TThriftBytes = packed record  // can't use SysUtils.TBytes because it has no typinfo -> E2134
+    data : System.TArray<System.Byte>;
+
+    class operator Implicit(aRec : SysUtils.TBytes) : TThriftBytes;
+    class operator Implicit(aRec : TThriftBytes) : SysUtils.TBytes;
+    function Length : Integer;
+  end;
+  {$IFNDEF TYPEINFO_WAS_ON} {$TYPEINFO OFF} {$ENDIF}
+
+
   IProtocol = interface
     ['{6067A28E-15BF-4C9D-9A6F-D991BB3DCB85}']
     function GetTransport: ITransport;
@@ -336,13 +347,18 @@
     constructor Create( const aTransport : ITransport); virtual;
   end;
 
-  {$TYPEINFO ON}
+  {.$TYPEINFO ON}  // big NO -> may cause E2134 due to Delphis stupidity on enums vs TypeInfo
   {$RTTI EXPLICIT METHODS([vcPublic, vcPublished]) PROPERTIES([vcPublic, vcPublished])}
   IBase = interface( ISupportsToString)
     ['{AFF6CECA-5200-4540-950E-9B89E0C1C00C}']
     procedure Read( const prot: IProtocol);
     procedure Write( const prot: IProtocol);
   end;
+
+  {$TYPEINFO ON}
+  {$RTTI EXPLICIT METHODS([vcPublic, vcPublished]) PROPERTIES([vcPublic, vcPublished])}
+  IBaseWithTypeInfo = interface( IBase) end;
+
   {$RTTI EXPLICIT METHODS([]) PROPERTIES([]) FIELDS([])}
   {$IFNDEF TYPEINFO_WAS_ON} {$TYPEINFO OFF} {$ENDIF}
 
@@ -578,6 +594,30 @@
 end;
 
 
+//--- TThriftBytes ----------------------------------------------------------------------
+
+
+class operator TThriftBytes.Implicit(aRec : SysUtils.TBytes) : TThriftBytes;
+begin
+  ASSERT( @result.data = @result);         // must be first field
+  ASSERT( SizeOf(aRec) = SizeOf(result));  // must be the only field
+  result := TThriftBytes(aRec);
+end;
+
+
+class operator TThriftBytes.Implicit(aRec : TThriftBytes) : SysUtils.TBytes;
+begin
+  ASSERT( @aRec.data = @aRec);             // must be first field
+  ASSERT( SizeOf(aRec) = SizeOf(result));  // must be the only field
+  result := SysUtils.TBytes(aRec.data);
+end;
+
+
+function TThriftBytes.Length : Integer;
+begin
+  result := System.Length(data);
+end;
+
 
 { TProtocolRecursionTrackerImpl }
 
diff --git a/lib/delphi/test/serializer/TestSerializer.dproj b/lib/delphi/test/serializer/TestSerializer.dproj
index 1d98d3a..383e04a 100644
--- a/lib/delphi/test/serializer/TestSerializer.dproj
+++ b/lib/delphi/test/serializer/TestSerializer.dproj
@@ -101,7 +101,7 @@
 		<Import Condition="Exists('$(BDS)\Bin\CodeGear.Delphi.Targets')" Project="$(BDS)\Bin\CodeGear.Delphi.Targets"/>
 		<Import Condition="Exists('$(APPDATA)\Embarcadero\$(BDSAPPDATABASEDIR)\$(PRODUCTVERSION)\UserTools.proj')" Project="$(APPDATA)\Embarcadero\$(BDSAPPDATABASEDIR)\$(PRODUCTVERSION)\UserTools.proj"/>
 		<PropertyGroup>
-			<PreBuildEvent><![CDATA[thrift.exe -r -gen delphi:com_types ..\keywords\ReservedKeywords.thrift
+			<PreBuildEvent><![CDATA[thrift.exe -r -gen delphi:com_types,rtti ..\keywords\ReservedKeywords.thrift
 thrift.exe -r -gen delphi:com_types ..\..\..\..\test\DebugProtoTest.thrift]]></PreBuildEvent>
 		</PropertyGroup>
 		<ProjectExtensions>
diff --git a/lib/delphi/test/server.dproj b/lib/delphi/test/server.dproj
index 151f7ee..8e9b99d 100644
--- a/lib/delphi/test/server.dproj
+++ b/lib/delphi/test/server.dproj
@@ -123,7 +123,7 @@
 						<VersionInfoKeys Name="Comments"/>
 					</VersionInfoKeys>
 					<Parameters>
-						<Parameters Name="RunParams">--protocol=compact  </Parameters>
+						<Parameters Name="RunParams">--protocol=compact</Parameters>
 					</Parameters>
 				</Delphi.Personality>
 				<Platforms>
diff --git a/lib/delphi/test/skip/skiptest_version2.dproj b/lib/delphi/test/skip/skiptest_version2.dproj
index 3192d28..1493637 100644
--- a/lib/delphi/test/skip/skiptest_version2.dproj
+++ b/lib/delphi/test/skip/skiptest_version2.dproj
@@ -97,7 +97,7 @@
 		<Import Condition="Exists('$(BDS)\Bin\CodeGear.Delphi.Targets')" Project="$(BDS)\Bin\CodeGear.Delphi.Targets"/>
 		<Import Condition="Exists('$(APPDATA)\Embarcadero\$(BDSAPPDATABASEDIR)\$(PRODUCTVERSION)\UserTools.proj')" Project="$(APPDATA)\Embarcadero\$(BDSAPPDATABASEDIR)\$(PRODUCTVERSION)\UserTools.proj"/>
 		<PropertyGroup>
-			<PreBuildEvent><![CDATA[thrift.exe -r -gen delphi idl\skiptest_version_2.thrift]]></PreBuildEvent>
+			<PreBuildEvent><![CDATA[thrift.exe -r -gen delphi:rtti idl\skiptest_version_2.thrift]]></PreBuildEvent>
 		</PropertyGroup>
 		<ProjectExtensions>
 			<Borland.Personality>Delphi.Personality.12</Borland.Personality>