trtti1.pp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. Program trtti1;
  2. {$Mode Delphi}
  3. {$M+}
  4. Uses
  5. Typinfo;
  6. Const TypeNames : Array [TTYpeKind] of string[15] =
  7. ('Unknown','Integer','Char','Enumeration',
  8. 'Float','Set','Method','ShortString','LongString',
  9. 'AnsiString','WideString','Variant','Array','Record',
  10. 'Interface','Class','Object','WideChar','Bool','Int64','QWord',
  11. 'DynamicArray','RawInterface');
  12. Const OrdinalTypes = [tkInteger,tkChar,tkENumeration,tkbool];
  13. Type
  14. TMyEnum = (meFirst,meSecond,meThird);
  15. TMyTestObject = Class(TObject)
  16. Private
  17. FBoolean : Boolean;
  18. FByte : Byte;
  19. FChar : Char;
  20. FWord : Word;
  21. FInteger : Integer;
  22. Flongint : Longint;
  23. FCardinal : Cardinal;
  24. FReal : Real;
  25. FExtended : Extended;
  26. FMyEnum : TMyEnum;
  27. FAnsiString : AnsiSTring;
  28. FObj : TObject;
  29. FStored : Boolean;
  30. Function GetBoolean : Boolean;
  31. Function GetByte : Byte;
  32. Function GetChar : Char;
  33. Function GetWord : Word;
  34. Function GetInteger : Integer;
  35. Function GetLongint : Longint;
  36. Function GetCardinal : Cardinal;
  37. Function GetReal : Real;
  38. Function GetExtended : Extended;
  39. Function GetAnsiString : AnsiString;
  40. Function GetMyEnum : TMyEnum;
  41. Procedure SetBoolean ( Value : Boolean);
  42. Procedure SetByte ( Value : Byte );
  43. Procedure SetChar ( Value : Char );
  44. Procedure SetWord ( Value : Word );
  45. Procedure SetInteger ( Value : Integer );
  46. Procedure SetLongint ( Value : Longint );
  47. Procedure SetCardinal ( Value : Cardinal );
  48. Procedure SetReal ( Value : Real );
  49. Procedure SetExtended ( Value : Extended );
  50. Procedure SetAnsiString ( Value : AnsiString );
  51. Procedure SetMyEnum ( Value : TMyEnum );
  52. Function GetVirtualBoolean : Boolean; virtual;
  53. Function GetVirtualByte : Byte; virtual;
  54. Function GetVirtualChar : Char; virtual;
  55. Function GetVirtualWord : Word; virtual;
  56. Function GetVirtualInteger : Integer; virtual;
  57. Function GetVirtualLongint : Longint; virtual;
  58. Function GetVirtualCardinal : Cardinal; virtual;
  59. Function GetVirtualReal : Real; virtual;
  60. Function GetVirtualExtended : Extended; virtual;
  61. Function GetVirtualAnsiString : AnsiString; virtual;
  62. Function GetVirtualMyEnum : TMyEnum; virtual;
  63. Procedure SetVirtualBoolean ( Value : Boolean); virtual;
  64. Procedure SetVirtualByte ( Value : Byte ); virtual;
  65. Procedure SetVirtualChar ( Value : Char ); virtual;
  66. Procedure SetVirtualWord ( Value : Word ); virtual;
  67. Procedure SetVirtualInteger ( Value : Integer ); virtual;
  68. Procedure SetVirtualLongint ( Value : Longint ); virtual;
  69. Procedure SetVirtualCardinal ( Value : Cardinal ); virtual;
  70. Procedure SetVirtualReal ( Value : Real ); virtual;
  71. Procedure SetVirtualExtended ( Value : Extended ); virtual;
  72. Procedure SetVirtualAnsiString ( Value : AnsiString ); virtual;
  73. Procedure SetVirtualMyEnum ( Value : TMyEnum ); virtual;
  74. Function GetStaticStored : Boolean;
  75. Function GetVirtualStored : Boolean;virtual;
  76. Public
  77. Constructor Create;
  78. Destructor Destroy;override;
  79. Published
  80. Property ObjField: TObject read FObj write FObj;
  81. Property BooleanField : Boolean Read FBoolean Write FBoolean;
  82. Property ByteField : Byte Read FByte Write FByte;
  83. Property CharField : Char Read FChar Write FChar;
  84. Property WordField : Word Read FWord Write FWord;
  85. Property IntegerField : Integer Read FInteger Write FInteger;
  86. Property LongintField : Longint Read FLongint Write FLongint;
  87. Property CardinalField : Cardinal Read FCardinal Write FCardinal;
  88. Property RealField : Real Read FReal Write FReal;
  89. Property ExtendedField : Extended Read FExtended Write FExtended;
  90. Property AnsiStringField : AnsiString Read FAnsiString Write FAnsiString;
  91. Property MyEnumField : TMyEnum Read FMyEnum Write FMyEnum;
  92. Property BooleanMethod : Boolean Read GetBoolean Write SetBoolean;
  93. Property ByteMethod : Byte Read GetByte Write SetByte;
  94. Property CharMethod : Char Read GetChar Write SetChar;
  95. Property WordMethod : Word Read GetWord Write SetWord;
  96. Property IntegerMethod : Integer Read GetInteger Write SetInteger;
  97. Property LongintMethod : Longint Read GetLongint Write SetLongint;
  98. Property CardinalMethod : Cardinal Read GetCardinal Write SetCardinal;
  99. Property RealMethod : Real Read GetReal Write SetReal;
  100. Property ExtendedMethod : Extended Read GetExtended Write SetExtended;
  101. Property AnsiStringMethod : AnsiString Read GetAnsiString Write SetAnsiString;
  102. Property MyEnumMethod : TMyEnum Read GetMyEnum Write SetMyEnum;
  103. Property BooleanVirtualMethod : Boolean Read GetVirtualBoolean Write SetVirtualBoolean;
  104. Property ByteVirtualMethod : Byte Read GetVirtualByte Write SetVirtualByte;
  105. Property CharVirtualMethod : Char Read GetVirtualChar Write SetVirtualChar;
  106. Property WordVirtualMethod : Word Read GetVirtualWord Write SetVirtualWord;
  107. Property IntegerVirtualMethod : Integer Read GetVirtualInteger Write SetVirtualInteger;
  108. Property LongintVirtualMethod : Longint Read GetVirtualLongint Write SetVirtualLongint;
  109. Property CardinalVirtualMethod : Cardinal Read GetVirtualCardinal Write SetVirtualCardinal;
  110. Property RealVirtualMethod : Real Read GetVirtualReal Write SetVirtualReal;
  111. Property ExtendedVirtualMethod : Extended Read GetVirtualExtended Write SetVirtualExtended;
  112. Property AnsiStringVirtualMethod : AnsiString Read GetVirtualAnsiString Write SetVirtualAnsiString;
  113. Property MyEnumVirtualMethod : TMyEnum Read GetVirtualMyEnum Write SetVirtualMyEnum;
  114. Property StoredIntegerConstFalse : Longint Read FLongint Stored False;
  115. Property StoredIntegerConstTrue : Longint Read FLongint Stored True;
  116. Property StoredIntegerField : Longint Read FLongint Stored FStored;
  117. Property StoredIntegerMethod : Longint Read Flongint Stored GetStaticStored;
  118. Property StoredIntegerVirtualMethod : Longint Read Flongint Stored GetVirtualStored;
  119. end;
  120. Constructor TMyTestObject.Create;
  121. begin
  122. FBoolean:=true;
  123. FByte:=1; { : Byte;}
  124. FChar:='B'; { : Char; }
  125. FWord:=3; {: Word; }
  126. FInteger:=4; {: Integer; }
  127. Flongint:=5; { : Longint; }
  128. FCardinal:=6; {: Cardinal; }
  129. FReal:=7.0; { : Real;}
  130. FExtended :=8.0; { Extended;}
  131. FMyEnum:=methird; { TMyEnum;}
  132. FAnsiString:='this is an AnsiString';
  133. end;
  134. Destructor TMyTestObject.Destroy;
  135. begin
  136. Inherited Destroy;
  137. end;
  138. Function TMyTestObject.GetBoolean : boolean;
  139. begin
  140. Result:=FBoolean;
  141. end;
  142. Function TMyTestObject.GetByte : Byte;
  143. begin
  144. Result:=FByte;
  145. end;
  146. Function TMyTestObject.GetChar : Char;
  147. begin
  148. Result:=FChar;
  149. end;
  150. Function TMyTestObject.GetWord : Word;
  151. begin
  152. Result:=FWord;
  153. end;
  154. Function TMyTestObject.GetInteger : Integer;
  155. begin
  156. Result:=FInteger;
  157. end;
  158. Function TMyTestObject.GetLongint : Longint;
  159. begin
  160. Result:=FLongint;
  161. end;
  162. Function TMyTestObject.GetCardinal : Cardinal;
  163. begin
  164. Result:=FCardinal;
  165. end;
  166. Function TMyTestObject.GetReal : Real;
  167. begin
  168. Result:=FReal;
  169. end;
  170. Function TMyTestObject.GetExtended : Extended;
  171. begin
  172. Result:=FExtended;
  173. end;
  174. Function TMyTestObject.GetAnsiString : AnsiString;
  175. begin
  176. Result:=FAnsiString;
  177. end;
  178. Function TMyTestObject.GetMyEnum : TMyEnum;
  179. begin
  180. Result:=FMyEnum;
  181. end;
  182. Procedure TMyTestObject.Setboolean ( Value : boolean );
  183. begin
  184. Fboolean:=Value;
  185. end;
  186. Procedure TMyTestObject.SetByte ( Value : Byte );
  187. begin
  188. FByte:=Value;
  189. end;
  190. Procedure TMyTestObject.SetChar ( Value : Char );
  191. begin
  192. FChar:=Value;
  193. end;
  194. Procedure TMyTestObject.SetWord ( Value : Word );
  195. begin
  196. FWord:=Value;
  197. end;
  198. Procedure TMyTestObject.SetInteger ( Value : Integer );
  199. begin
  200. FInteger:=Value;
  201. end;
  202. Procedure TMyTestObject.SetLongint ( Value : Longint );
  203. begin
  204. FLongint:=Value;
  205. end;
  206. Procedure TMyTestObject.SetCardinal ( Value : Cardinal );
  207. begin
  208. FCardinal:=Value;
  209. end;
  210. Procedure TMyTestObject.SetReal ( Value : Real );
  211. begin
  212. FReal:=Value;
  213. end;
  214. Procedure TMyTestObject.SetExtended ( Value : Extended );
  215. begin
  216. FExtended:=Value;
  217. end;
  218. Procedure TMyTestObject.SetAnsiString ( Value : AnsiString );
  219. begin
  220. FAnsiString:=Value;
  221. end;
  222. Procedure TMyTestObject.SetMyEnum ( Value : TMyEnum );
  223. begin
  224. FMyEnum:=Value;
  225. end;
  226. Function TMyTestObject.GetVirtualBoolean : boolean;
  227. begin
  228. Result:=FBoolean;
  229. end;
  230. Function TMyTestObject.GetVirtualByte : Byte;
  231. begin
  232. Result:=FByte;
  233. end;
  234. Function TMyTestObject.GetVirtualChar : Char;
  235. begin
  236. Result:=FChar;
  237. end;
  238. Function TMyTestObject.GetVirtualWord : Word;
  239. begin
  240. Result:=FWord;
  241. end;
  242. Function TMyTestObject.GetVirtualInteger : Integer;
  243. begin
  244. Result:=FInteger;
  245. end;
  246. Function TMyTestObject.GetVirtualLongint : Longint;
  247. begin
  248. Result:=FLongint;
  249. end;
  250. Function TMyTestObject.GetVirtualCardinal : Cardinal;
  251. begin
  252. Result:=FCardinal;
  253. end;
  254. Function TMyTestObject.GetVirtualReal : Real;
  255. begin
  256. Result:=FReal;
  257. end;
  258. Function TMyTestObject.GetVirtualExtended : Extended;
  259. begin
  260. Result:=FExtended;
  261. end;
  262. Function TMyTestObject.GetVirtualAnsiString : AnsiString;
  263. begin
  264. Result:=FAnsiString;
  265. end;
  266. Function TMyTestObject.GetVirtualMyEnum : TMyEnum;
  267. begin
  268. Result:=FMyEnum;
  269. end;
  270. Procedure TMyTestObject.SetVirtualboolean ( Value : boolean );
  271. begin
  272. Fboolean:=Value;
  273. end;
  274. Procedure TMyTestObject.SetVirtualByte ( Value : Byte );
  275. begin
  276. FByte:=Value;
  277. end;
  278. Procedure TMyTestObject.SetVirtualChar ( Value : Char );
  279. begin
  280. FChar:=Value;
  281. end;
  282. Procedure TMyTestObject.SetVirtualWord ( Value : Word );
  283. begin
  284. FWord:=Value;
  285. end;
  286. Procedure TMyTestObject.SetVirtualInteger ( Value : Integer );
  287. begin
  288. FInteger:=Value;
  289. end;
  290. Procedure TMyTestObject.SetVirtualLongint ( Value : Longint );
  291. begin
  292. FLongint:=Value;
  293. end;
  294. Procedure TMyTestObject.SetVirtualCardinal ( Value : Cardinal );
  295. begin
  296. FCardinal:=Value;
  297. end;
  298. Procedure TMyTestObject.SetVirtualReal ( Value : Real );
  299. begin
  300. FReal:=Value;
  301. end;
  302. Procedure TMyTestObject.SetVirtualExtended ( Value : Extended );
  303. begin
  304. FExtended:=Value;
  305. end;
  306. Procedure TMyTestObject.SetVirtualAnsiString ( Value : AnsiString );
  307. begin
  308. FAnsiString:=Value;
  309. end;
  310. Procedure TMyTestObject.SetVirtualMyEnum ( Value : TMyEnum );
  311. begin
  312. FMyEnum:=Value;
  313. end;
  314. Function TMyTestObject.GetStaticStored : Boolean;
  315. begin
  316. Result:=False;
  317. end;
  318. Function TMyTestObject.GetVirtualStored : Boolean;
  319. begin
  320. Result:=False;
  321. end;
  322. Procedure DumpMem ( PL : PByte );
  323. Var I,j : longint;
  324. begin
  325. For I:=1 to 16 do
  326. begin
  327. Write ((I-1)*16:3,' :');
  328. For J:=1 to 10 do
  329. begin
  330. If (PL^>31) and (PL^<129) then
  331. Write(' ',CHar(PL^))
  332. else
  333. Write (PL^:3);
  334. Write (' ');
  335. inc(pl);
  336. end;
  337. writeln;
  338. end;
  339. end;
  340. Function ProcType (PP : Byte) : String;
  341. begin
  342. Case PP and 3 of
  343. ptfield : Result:='from Field';
  344. ptstatic : Result:='with static method';
  345. ptVirtual : Result:='with virtual method';
  346. ptconst : Result:='with Const';
  347. end;
  348. end;
  349. Procedure DumpTypeInfo (O : TMyTestObject);
  350. Var
  351. PT : PTypeData;
  352. PI : PTypeInfo;
  353. I : Longint;
  354. PP : PPropList;
  355. begin
  356. PI:=O.ClassInfo;
  357. Writeln ('Type kind : ',TypeNames[PI^.Kind]);
  358. Writeln ('Type name : ',PI^.Name);
  359. PT:=GetTypeData(PI);
  360. //DumpMem(PByte(PI));
  361. If PT^.ParentInfo=Nil then
  362. Writeln ('Object has no parent info')
  363. else
  364. Writeln ('Object has parent info');
  365. Writeln ('Property Count : ',PT^.PropCount);
  366. Writeln ('Unit name : ',PT^.UnitName);
  367. GetMem (PP,PT^.PropCount*SizeOf(Pointer));
  368. GetPropInfos(PI,PP);
  369. For I:=0 to PT^.PropCount-1 do
  370. If PP^[i]<>Nil then
  371. With PP^[I]^ do
  372. begin
  373. Writeln ('Property name : ',Name);
  374. Writeln (' Type kind: ',TypeNames[PropType^.Kind]);
  375. Writeln (' Type Name: ',PropType^.Name);
  376. If GetProc=Nil then Write ('No');
  377. Writeln (' Getproc available');
  378. If SetProc=Nil then Write ('No');
  379. Writeln (' Setproc available');
  380. If StoredProc=Nil then Write ('No');
  381. Writeln (' Storedproc available');
  382. Writeln (' Get property ',proctype(Propprocs));
  383. Writeln (' Set Property ',proctype(propprocs shr 2));
  384. Writeln (' Stored Property ',proctype(propprocs shr 4));
  385. Writeln (' Default : ',Default,' Index : ',Index);
  386. Writeln (' NameIndex : ',NameIndex);
  387. end;
  388. end;
  389. Procedure PrintObject ( Obj: TMyTestObject);
  390. begin
  391. With Obj do
  392. begin
  393. Writeln ('Field properties :');
  394. Writeln ('Property booleanField : ',booleanField);
  395. Writeln ('Property ByteField : ',ByteField);
  396. Writeln ('Property CharField : ',CharField);
  397. Writeln ('Property WordField : ',WordField);
  398. Writeln ('Property IntegerField : ',IntegerField);
  399. Writeln ('Property LongintField : ',LongintField);
  400. Writeln ('Property CardinalField : ',CardinalField);
  401. Writeln ('Property RealField : ',RealField);
  402. Writeln ('Property ExtendedField : ',ExtendedFIeld);
  403. Writeln ('Property AnsiStringField : ',AnsiStringField);
  404. Writeln ('Property MyEnumField : ',ord(MyEnumField));
  405. Writeln ('Method properties :');
  406. Writeln ('Property booleanMethod : ',BooleanMethod);
  407. Writeln ('Property ByteMethod : ',ByteMethod);
  408. Writeln ('Property CharMethod : ',CharMethod);
  409. Writeln ('Property WordMethod : ',WordMethod);
  410. Writeln ('Property IntegerMethod : ',IntegerMethod);
  411. Writeln ('Property LongintMethod : ',LongintMethod);
  412. Writeln ('Property CardinalMethod : ',CardinalMethod);
  413. Writeln ('Property RealMethod : ',RealMethod);
  414. Writeln ('Property ExtendedMethod : ',ExtendedMethod);
  415. Writeln ('Property AnsiStringMethod : ',AnsiStringMethod);
  416. Writeln ('Property MyEnumMethod : ',ord(MyEnumMethod));
  417. Writeln ('VirtualMethod properties :');
  418. Writeln ('Property booleanVirtualMethod : ',BooleanVirtualMethod);
  419. Writeln ('Property ByteVirtualMethod : ',ByteVirtualMethod);
  420. Writeln ('Property CharVirtualMethod : ',CharVirtualMethod);
  421. Writeln ('Property WordVirtualMethod : ',WordVirtualMethod);
  422. Writeln ('Property IntegerVirtualMethod : ',IntegerVirtualMethod);
  423. Writeln ('Property LongintVirtualMethod : ',LongintVirtualMethod);
  424. Writeln ('Property CardinalVirtualMethod : ',CardinalVirtualMethod);
  425. Writeln ('Property RealVirtualMethod : ',RealVirtualMethod);
  426. Writeln ('Property ExtendedVirtualMethod : ',ExtendedVirtualMethod);
  427. Writeln ('Property AnsiStringVirtualMethod : ',AnsiStringVirtualMethod);
  428. Writeln ('Property MyEnumVirtualMethod : ',ord(MyEnumVirtualMethod));
  429. end;
  430. end;
  431. Procedure TestGet (O : TMyTestObject);
  432. Var
  433. PT : PTypeData;
  434. PI : PTypeInfo;
  435. I,J : Longint;
  436. PP : PPropList;
  437. prI : PPropInfo;
  438. begin
  439. PI:=O.ClassInfo;
  440. Writeln ('Type kind : ',TypeNames[PI^.Kind]);
  441. Writeln ('Type name : ',PI^.Name);
  442. PT:=GetTypeData(PI);
  443. If PT^.ParentInfo=Nil then
  444. Writeln ('Object has no parent info')
  445. else
  446. Writeln ('Object has parent info');
  447. Writeln ('Property Count : ',PT^.PropCount);
  448. Writeln ('Unit name : ',PT^.UnitName);
  449. GetMem (PP,PT^.PropCount*SizeOf(Pointer));
  450. GetPropInfos(PI,PP);
  451. For I:=0 to PT^.PropCount-1 do
  452. begin
  453. pri:=PP^[i];
  454. With Pri^ do
  455. begin
  456. Write ('(Examining ',name,' : Type : ',TypeNames[PropType^.Kind],', ');
  457. If (Proptype^.kind in Ordinaltypes) Then
  458. begin
  459. J:=GetOrdProp(O,pri);
  460. Write ('Value : ',j);
  461. If PropType^.Kind=tkenumeration then
  462. Write ('(=',GetEnumName(Proptype,J),')')
  463. end
  464. else
  465. Case pri^.proptype^.kind of
  466. tkfloat : begin
  467. Write ('Value : ');
  468. Flush(output);
  469. Write(GetFloatProp(O,pri))
  470. end;
  471. tkAstring : begin
  472. Write ('value : ');
  473. flush (output);
  474. Write(GetStrProp(O,Pri));
  475. end;
  476. else
  477. Write ('Untested type:',ord(pri^.proptype^.kind));
  478. end;
  479. Writeln (')');
  480. end;
  481. end;
  482. end;
  483. Var O : TMyTestObject;
  484. begin
  485. O:=TMyTestObject.Create;
  486. DumpTypeInfo(O);
  487. PrintObject(O);
  488. testget(o);
  489. end.