tests.rtti.types.pas 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. unit tests.rtti.types;
  2. {$ifdef fpc}
  3. {$mode objfpc}{$H+}
  4. {$modeswitch advancedrecords}
  5. {$modeswitch prefixedattributes}
  6. {$endif}
  7. interface
  8. uses
  9. Classes, SysUtils;
  10. Type
  11. {$M+}
  12. TGetClassProperties = class
  13. private
  14. FPubPropRO: integer;
  15. FPubPropRW: integer;
  16. published
  17. property PubPropRO: integer read FPubPropRO;
  18. property PubPropRW: integer read FPubPropRW write FPubPropRW;
  19. property PubPropSetRO: integer read FPubPropRO;
  20. property PubPropSetRW: integer read FPubPropRW write FPubPropRW;
  21. end;
  22. TGetClassPropertiesSub = class(TGetClassProperties)
  23. end;
  24. {$M+}
  25. ITestInterface = interface
  26. procedure Test;
  27. function Test2: LongInt;
  28. procedure Test3(aArg1: LongInt; const aArg2: AnsiString; var aArg3: Boolean; out aArg4: Word);
  29. function Test4(aArg1: array of LongInt; aArg2: array of const): AnsiString;
  30. end;
  31. {$M-}
  32. TManagedRec = record
  33. s: string;
  34. end;
  35. {$ifdef fpc}
  36. TManagedRecOp = record
  37. class operator AddRef(var a: TManagedRecOp);
  38. end;
  39. {$endif}
  40. TNonManagedRec = record
  41. i: Integer;
  42. end;
  43. TManagedObj = object
  44. i: IInterface;
  45. end;
  46. TNonManagedObj = object
  47. d: double;
  48. end;
  49. TTestEnum = (te1, te2, te3, te4, te5);
  50. TTestSet = set of TTestEnum;
  51. TTestProc = procedure;
  52. TTestFunc1 = function: LongInt;
  53. TTestFunc2 = function(aArg1: LongInt; aArg2: array of LongInt): String;
  54. TTestMethod = procedure of object;
  55. TTestMethod1 = function: LongInt of object;
  56. TTestMethod2 = function(aArg1: LongInt; aArg2: array of LongInt): String of object;
  57. TTestHelper = class helper for TObject
  58. end;
  59. TTestRecord = record
  60. Value1: LongInt;
  61. Value2: String;
  62. end;
  63. PTestRecord = ^TTestRecord;
  64. TArrayOfString = array[0..0] of string;
  65. TArrayOfManagedRec = array[0..0] of TManagedRec;
  66. TArrayOfNonManagedRec = array[0..0] of TNonManagedRec;
  67. TArrayOfByte = array[0..0] of byte;
  68. TArrayOfLongintDyn = array of LongInt;
  69. TArrayOfLongintStatic = array[0..3] of LongInt;
  70. TArrayOfLongint2DStatic = array[0..3, 2..4] of LongInt;
  71. TTestDynArray = array of Integer;
  72. TTestEnumeration = (en1, en2, en3, en4);
  73. {$M-}
  74. { TTestValueClass }
  75. {$M+}
  76. TTestValueClass = class
  77. private
  78. FAArray: TTestDynArray;
  79. FAChar: AnsiChar;
  80. FAComp: Comp;
  81. FACurrency: Currency;
  82. FADouble: Double;
  83. FAEnumeration: TTestEnumeration;
  84. FAExtended: Extended;
  85. FAInteger: integer;
  86. FAObject: TObject;
  87. FASingle: Single;
  88. FAString: string;
  89. FABoolean: boolean;
  90. FAShortString: ShortString;
  91. FAUnknown: IUnknown;
  92. FAWideChar: WideChar;
  93. function GetAInteger: integer;
  94. function GetAString: string;
  95. function GetABoolean: boolean;
  96. function GetAShortString: ShortString;
  97. procedure SetWriteOnly(AValue: integer);
  98. published
  99. property AArray: TTestDynArray read FAArray write FAArray;
  100. property AEnumeration: TTestEnumeration read FAEnumeration write FAEnumeration;
  101. property AInteger: Integer read FAInteger write FAInteger;
  102. property AString: string read FAString write FAString;
  103. property ASingle: Single read FASingle write FASingle;
  104. property ADouble: Double read FADouble write FADouble;
  105. property AExtended: Extended read FAExtended write FAExtended;
  106. property ACurrency: Currency read FACurrency write FACurrency;
  107. property AObject: TObject read FAObject write FAObject;
  108. property AUnknown: IUnknown read FAUnknown write FAUnknown;
  109. property AComp: Comp read FAComp write FAComp;
  110. property ABoolean: boolean read FABoolean write FABoolean;
  111. property AShortString: ShortString read FAShortString write FAShortString;
  112. property AGetInteger: Integer read GetAInteger;
  113. property AGetString: string read GetAString;
  114. property AGetBoolean: boolean read GetABoolean;
  115. property AGetShortString: ShortString read GetAShortString;
  116. property AWriteOnly: integer write SetWriteOnly;
  117. property AChar: AnsiChar read FAChar write FAChar;
  118. property AWideChar: WideChar read FAWideChar write FAWideChar;
  119. end;
  120. {$M-}
  121. {$ifdef fpc}
  122. {$PUSH}
  123. {$INTERFACES CORBA}
  124. ICORBATest = interface
  125. end;
  126. {$POP}
  127. {$endif}
  128. { TMyAttribute }
  129. TMyAttribute = class(TCustomAttribute)
  130. private
  131. FValue: string;
  132. public
  133. constructor create(const avalue : string);
  134. property value : string read FValue;
  135. end;
  136. { TMyAnnotatedClass }
  137. [TMyAttribute('something')]
  138. TMyAnnotatedClass = class
  139. private
  140. FSomething: String;
  141. Published
  142. Property Something : String Read FSomething Write FSomeThing;
  143. end;
  144. {$RTTI EXPLICIT
  145. PROPERTIES([vcPrivate,vcProtected,vcPublic,vcPublished])
  146. FIELDS([vcPrivate,vcProtected,vcPublic,vcPublished])
  147. METHODS([vcPrivate,vcProtected,vcPublic,vcPublished])}
  148. Type
  149. { TFieldRTTI }
  150. {$M+}
  151. TFieldRTTI = Class(TObject)
  152. private
  153. FPrivateA: Integer;
  154. Property PrivateA : Integer Read FPrivateA Write FPrivateA;
  155. strict private
  156. FPrivateB: Integer;
  157. Property PrivateB : Integer Read FPrivateB Write FPrivateB;
  158. Protected
  159. FProtectedA: Integer;
  160. Property ProtectedA : Integer Read FProtectedA Write FProtectedA;
  161. Strict Protected
  162. FProtectedB: Integer;
  163. Property ProtectedB : Integer Read FProtectedB Write FProtectedB;
  164. Public
  165. FPublicA: Integer;
  166. FPublicB: Integer;
  167. Property PublicA : Integer Read FPublicA Write FPublicA;
  168. Property PublicB : Integer Read FPublicA Write FPublicB;
  169. Private
  170. FPublishedA: Integer;
  171. FPublishedB: Integer;
  172. Published
  173. FPublishedC: TFieldRTTI;
  174. FPublishedD: TFieldRTTI;
  175. Property PublishedA : Integer Read FPublishedA Write FPublishedA;
  176. Property PublishedB : Integer Read FPublishedA Write FPublishedB;
  177. end;
  178. TFieldRTTIChild = class(TFieldRTTI)
  179. Public
  180. FPublicC : Integer;
  181. end;
  182. { TMethodClassRTTI }
  183. TMethodClassRTTI = Class (TObject)
  184. private
  185. Procedure PrivateMethodA;
  186. strict private
  187. Procedure PrivateMethodB; virtual;
  188. private
  189. Procedure PrivateMethodC; virtual; abstract;
  190. protected
  191. Procedure ProtectedMethodA;
  192. strict protected
  193. Procedure ProtectedMethodB; virtual;
  194. protected
  195. Procedure ProtectedMethodC; virtual; abstract;
  196. public
  197. Procedure PublicMethodA;
  198. Procedure PublicMethodB; virtual;
  199. Procedure PublicMethodC; virtual; abstract;
  200. published
  201. Procedure PublishedMethodA(a : Integer);
  202. Procedure PublishedMethodB; virtual;
  203. Procedure PublishedMethodC; virtual; abstract;
  204. end;
  205. { TAdditionalMethodClassRTTI }
  206. TAdditionalMethodClassRTTI = class(TMethodClassRTTI)
  207. public
  208. Procedure PublicAdditionalMethod;
  209. end;
  210. // Use different names, so we can distinguish RTTI in asm file...
  211. TRecordFieldRTTI = record
  212. private
  213. FRPrivateA: Integer;
  214. FRPrivateB: Integer;
  215. Property RPrivateA : Integer Read FRPrivateA Write FRPrivateA;
  216. Property RPrivateB : Integer Read FRPrivateB Write FRPrivateB;
  217. Public
  218. FRPublicA: Integer;
  219. FRPublicB: Integer;
  220. Property RPublicA : Integer Read FRPublicA Write FRPublicA;
  221. Property RPublicB : Integer Read FRPublicA Write FRPublicB;
  222. end;
  223. TRecordFieldRTTIMixed = record
  224. private
  225. FRPrivateA: Integer;
  226. FRPrivateB: Integer;
  227. Property RPrivateA : Integer Read FRPrivateA Write FRPrivateA;
  228. Property RPrivateB : Integer Read FRPrivateB Write FRPrivateB;
  229. Public
  230. FRPublicA: Integer;
  231. FRPublicB: Integer;
  232. Property RPublicA : Integer Read FRPublicA Write FRPublicA;
  233. Property RPublicB : Integer Read FRPublicA Write FRPublicB;
  234. Procedure DoA;
  235. end;
  236. // Use different names, so we can distinguish RTTI in asm file...
  237. { TRecordMethodRTTI }
  238. TRecordMethodRTTI = record
  239. a,b,c : Integer;
  240. private
  241. Procedure PrivateMethodA;
  242. Procedure PrivateMethodB;
  243. Public
  244. Procedure PublicMethodA;
  245. Procedure PublicMethodB(I : Integer);
  246. end;
  247. implementation
  248. { TTestValueClass }
  249. function TTestValueClass.GetAInteger: integer;
  250. begin
  251. result := FAInteger;
  252. end;
  253. function TTestValueClass.GetAString: string;
  254. begin
  255. result := FAString;
  256. end;
  257. function TTestValueClass.GetABoolean: boolean;
  258. begin
  259. result := FABoolean;
  260. end;
  261. function TTestValueClass.GetAShortString: ShortString;
  262. begin
  263. Result := FAShortString;
  264. end;
  265. procedure TTestValueClass.SetWriteOnly(AValue: integer);
  266. begin
  267. // Do nothing
  268. end;
  269. { TMyAttribute }
  270. constructor TMyAttribute.create(const avalue: string);
  271. begin
  272. FValue:=aValue;
  273. end;
  274. { TMethodClassRTTI }
  275. procedure TMethodClassRTTI.PrivateMethodA;
  276. begin
  277. end;
  278. procedure TMethodClassRTTI.PrivateMethodB;
  279. begin
  280. end;
  281. procedure TMethodClassRTTI.ProtectedMethodA;
  282. begin
  283. end;
  284. procedure TMethodClassRTTI.ProtectedMethodB;
  285. begin
  286. end;
  287. procedure TMethodClassRTTI.PublicMethodA;
  288. begin
  289. end;
  290. procedure TMethodClassRTTI.PublicMethodB;
  291. begin
  292. end;
  293. procedure TMethodClassRTTI.PublishedMethodA(a : Integer);
  294. begin
  295. end;
  296. procedure TMethodClassRTTI.PublishedMethodB;
  297. begin
  298. end;
  299. { TAdditionalMethodClassRTTI }
  300. procedure TAdditionalMethodClassRTTI.PublicAdditionalMethod;
  301. begin
  302. end;
  303. {$ifdef fpc}
  304. class operator TManagedRecOp.AddRef(var a: TManagedRecOp);
  305. begin
  306. end;
  307. {$endif}
  308. { TRecordMethodRTTI }
  309. procedure TRecordMethodRTTI.PrivateMethodA;
  310. begin
  311. //
  312. end;
  313. procedure TRecordMethodRTTI.PrivateMethodB;
  314. begin
  315. //
  316. end;
  317. procedure TRecordMethodRTTI.PublicMethodA;
  318. begin
  319. //
  320. end;
  321. procedure TRecordMethodRTTI.PublicMethodB(I : Integer);
  322. begin
  323. //
  324. end;
  325. Procedure TRecordFieldRTTIMixed.DoA;
  326. begin
  327. //
  328. end;
  329. end.