tests.rtti.types.pas 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. TRTTIRecord = Record
  151. a,b : Integer;
  152. end;
  153. PRTTIRecord = ^TRTTIRecord;
  154. TRecordRttiClass = Class(TObject)
  155. Private
  156. FRecordProp : TRTTIRecord;
  157. Public
  158. Property RecordProp : TRTTIRecord read FRecordProp write FRecordProp;
  159. end;
  160. {$M+}
  161. TFieldRTTI = Class(TObject)
  162. private
  163. FPrivateA: Integer;
  164. Property PrivateA : Integer Read FPrivateA Write FPrivateA;
  165. strict private
  166. FPrivateB: Integer;
  167. Property PrivateB : Integer Read FPrivateB Write FPrivateB;
  168. Protected
  169. FProtectedA: Integer;
  170. Property ProtectedA : Integer Read FProtectedA Write FProtectedA;
  171. Strict Protected
  172. FProtectedB: Integer;
  173. Property ProtectedB : Integer Read FProtectedB Write FProtectedB;
  174. Public
  175. FPublicA: Integer;
  176. FPublicB: Integer;
  177. Property PublicA : Integer Read FPublicA Write FPublicA;
  178. Property PublicB : Integer Read FPublicA Write FPublicB;
  179. Private
  180. FPublishedA: Integer;
  181. FPublishedB: Integer;
  182. Published
  183. FPublishedC: TFieldRTTI;
  184. FPublishedD: TFieldRTTI;
  185. Property PublishedA : Integer Read FPublishedA Write FPublishedA;
  186. Property PublishedB : Integer Read FPublishedA Write FPublishedB;
  187. end;
  188. TFieldRTTIChild = class(TFieldRTTI)
  189. Public
  190. FPublicC : Integer;
  191. end;
  192. { TMethodClassRTTI }
  193. TMethodClassRTTI = Class (TObject)
  194. private
  195. Procedure PrivateMethodA;
  196. strict private
  197. Procedure PrivateMethodB; virtual;
  198. private
  199. Procedure PrivateMethodC; virtual; abstract;
  200. protected
  201. Procedure ProtectedMethodA;
  202. strict protected
  203. Procedure ProtectedMethodB; virtual;
  204. protected
  205. Procedure ProtectedMethodC; virtual; abstract;
  206. public
  207. Procedure PublicMethodA;
  208. Procedure PublicMethodB; virtual;
  209. Procedure PublicMethodC; virtual; abstract;
  210. published
  211. Procedure PublishedMethodA(a : Integer);
  212. Procedure PublishedMethodB; virtual;
  213. Procedure PublishedMethodC; virtual; abstract;
  214. end;
  215. { TAdditionalMethodClassRTTI }
  216. TAdditionalMethodClassRTTI = class(TMethodClassRTTI)
  217. public
  218. Procedure PublicAdditionalMethod;
  219. end;
  220. // Use different names, so we can distinguish RTTI in asm file...
  221. TRecordFieldRTTI = record
  222. private
  223. FRPrivateA: Integer;
  224. FRPrivateB: Integer;
  225. Property RPrivateA : Integer Read FRPrivateA Write FRPrivateA;
  226. Property RPrivateB : Integer Read FRPrivateB Write FRPrivateB;
  227. Public
  228. FRPublicA: Integer;
  229. FRPublicB: Integer;
  230. Property RPublicA : Integer Read FRPublicA Write FRPublicA;
  231. Property RPublicB : Integer Read FRPublicA Write FRPublicB;
  232. end;
  233. TRecordFieldRTTIMixed = record
  234. private
  235. FRPrivateA: Integer;
  236. FRPrivateB: Integer;
  237. Property RPrivateA : Integer Read FRPrivateA Write FRPrivateA;
  238. Property RPrivateB : Integer Read FRPrivateB Write FRPrivateB;
  239. Public
  240. FRPublicA: Integer;
  241. FRPublicB: Integer;
  242. Property RPublicA : Integer Read FRPublicA Write FRPublicA;
  243. Property RPublicB : Integer Read FRPublicA Write FRPublicB;
  244. Procedure DoA;
  245. end;
  246. // Use different names, so we can distinguish RTTI in asm file...
  247. { TRecordMethodRTTI }
  248. TRecordMethodRTTI = record
  249. a,b,c : Integer;
  250. private
  251. Procedure PrivateMethodA;
  252. Procedure PrivateMethodB;
  253. Public
  254. Procedure PublicMethodA;
  255. Procedure PublicMethodB(I : Integer);
  256. end;
  257. implementation
  258. { TTestValueClass }
  259. function TTestValueClass.GetAInteger: integer;
  260. begin
  261. result := FAInteger;
  262. end;
  263. function TTestValueClass.GetAString: string;
  264. begin
  265. result := FAString;
  266. end;
  267. function TTestValueClass.GetABoolean: boolean;
  268. begin
  269. result := FABoolean;
  270. end;
  271. function TTestValueClass.GetAShortString: ShortString;
  272. begin
  273. Result := FAShortString;
  274. end;
  275. procedure TTestValueClass.SetWriteOnly(AValue: integer);
  276. begin
  277. // Do nothing
  278. end;
  279. { TMyAttribute }
  280. constructor TMyAttribute.create(const avalue: string);
  281. begin
  282. FValue:=aValue;
  283. end;
  284. { TMethodClassRTTI }
  285. procedure TMethodClassRTTI.PrivateMethodA;
  286. begin
  287. end;
  288. procedure TMethodClassRTTI.PrivateMethodB;
  289. begin
  290. end;
  291. procedure TMethodClassRTTI.ProtectedMethodA;
  292. begin
  293. end;
  294. procedure TMethodClassRTTI.ProtectedMethodB;
  295. begin
  296. end;
  297. procedure TMethodClassRTTI.PublicMethodA;
  298. begin
  299. end;
  300. procedure TMethodClassRTTI.PublicMethodB;
  301. begin
  302. end;
  303. procedure TMethodClassRTTI.PublishedMethodA(a : Integer);
  304. begin
  305. end;
  306. procedure TMethodClassRTTI.PublishedMethodB;
  307. begin
  308. end;
  309. { TAdditionalMethodClassRTTI }
  310. procedure TAdditionalMethodClassRTTI.PublicAdditionalMethod;
  311. begin
  312. end;
  313. {$ifdef fpc}
  314. class operator TManagedRecOp.AddRef(var a: TManagedRecOp);
  315. begin
  316. end;
  317. {$endif}
  318. { TRecordMethodRTTI }
  319. procedure TRecordMethodRTTI.PrivateMethodA;
  320. begin
  321. //
  322. end;
  323. procedure TRecordMethodRTTI.PrivateMethodB;
  324. begin
  325. //
  326. end;
  327. procedure TRecordMethodRTTI.PublicMethodA;
  328. begin
  329. //
  330. end;
  331. procedure TRecordMethodRTTI.PublicMethodB(I : Integer);
  332. begin
  333. //
  334. end;
  335. Procedure TRecordFieldRTTIMixed.DoA;
  336. begin
  337. //
  338. end;
  339. end.