trtti1.pp 17 KB

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