testrtti.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. Program testrtti;
  2. Uses Typinfo;
  3. {$M+}
  4. Uses
  5. {$ifdef go32v2}
  6. dpmiexcp,
  7. {$endif}
  8. Typinfo;
  9. Const TypeNames : Array [TTYpeKind] of string[15] =
  10. ('Unknown','Integer','Char','Enumeration',
  11. 'Float','Set','Method','ShortString','LongString',
  12. 'AnsiString','WideString','Variant','Array','Record',
  13. 'Interface','Class','Object','WideChar','Bool');
  14. Const OrdinalTypes = [tkInteger,tkChar,tkENumeration,tkbool];
  15. Type
  16. TMyEnum = (meFirst,meSecond,meThird);
  17. TMyTestObject = Class(TObject)
  18. Private
  19. FBoolean : Boolean;
  20. FByte : Byte;
  21. FChar : Char;
  22. FWord : Word;
  23. FInteger : Integer;
  24. Flongint : Longint;
  25. FCardinal : Cardinal;
  26. FReal : Real;
  27. FExtended : Extended;
  28. FMyEnum : TMyEnum;
  29. FAnsiString : AnsiSTring;
  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. Public
  53. Constructor Create;
  54. Destructor Destroy;override;
  55. Published
  56. Property BooleanField : Boolean Read FBoolean Write FBoolean;
  57. Property ByteField : Byte Read FByte Write FByte;
  58. Property CharField : Char Read FChar Write FChar;
  59. Property WordField : Word Read FWord Write FWord;
  60. Property IntegerField : Integer Read FInteger Write FInteger;
  61. Property LongintField : Longint Read FLongint Write FLongint;
  62. Property CardinalField : Cardinal Read FCardinal Write FCardinal;
  63. Property RealField : Real Read FReal Write FReal;
  64. Property ExtendedField : Extended Read FExtended Write FExtended;
  65. Property AnsiStringField : AnsiString Read FAnsiString Write FAnsiString;
  66. Property MyEnumField : TMyEnum Read FMyEnum Write FMyEnum;
  67. Property BooleanMethod : Boolean Read GetBoolean Write SetBoolean;
  68. Property ByteMethod : Byte Read GetByte Write SetByte;
  69. Property CharMethod : Char Read GetChar Write SetChar;
  70. Property WordMethod : Word Read GetWord Write SetWord;
  71. Property IntegerMethod : Integer Read GetInteger Write SetInteger;
  72. Property LongintMethod : Longint Read GetLongint Write SetLongint;
  73. Property CardinalMethod : Cardinal Read GetCardinal Write SetCardinal;
  74. Property RealMethod : Real Read GetReal Write SetReal;
  75. Property ExtendedMethod : Extended Read GetExtended Write SetExtended;
  76. Property AnsiStringMethod : AnsiString Read GetAnsiString Write SetAnsiString;
  77. Property MyEnumMethod : TMyEnum Read GetMyEnum Write SetMyEnum;
  78. end;
  79. Constructor TMyTestObject.Create;
  80. begin
  81. FBoolean:=true;
  82. FByte:=1; { : Byte;}
  83. FChar:='B'; { : Char; }
  84. FWord:=3; {: Word; }
  85. FInteger:=4; {: Integer; }
  86. Flongint:=5; { : Longint; }
  87. FCardinal:=6; {: Cardinal; }
  88. FReal:=7.0; { : Real;}
  89. FExtended :=8.0; { Extended;}
  90. FMyEnum:=methird; { TMyEnum;}
  91. FAnsiString:='this is an AnsiString';
  92. end;
  93. Destructor TMyTestObject.Destroy;
  94. begin
  95. Inherited Destroy;
  96. end;
  97. Function TMyTestObject.GetBoolean : boolean;
  98. begin
  99. Result:=FBoolean;
  100. end;
  101. Function TMyTestObject.GetByte : Byte;
  102. begin
  103. Result:=FByte;
  104. end;
  105. Function TMyTestObject.GetChar : Char;
  106. begin
  107. Result:=FChar;
  108. end;
  109. Function TMyTestObject.GetWord : Word;
  110. begin
  111. Result:=FWord;
  112. end;
  113. Function TMyTestObject.GetInteger : Integer;
  114. begin
  115. Result:=FInteger;
  116. end;
  117. Function TMyTestObject.GetLongint : Longint;
  118. begin
  119. Result:=FLongint;
  120. end;
  121. Function TMyTestObject.GetCardinal : Cardinal;
  122. begin
  123. Result:=FCardinal;
  124. end;
  125. Function TMyTestObject.GetReal : Real;
  126. begin
  127. Result:=FReal;
  128. end;
  129. Function TMyTestObject.GetExtended : Extended;
  130. begin
  131. Result:=FExtended;
  132. end;
  133. Function TMyTestObject.GetAnsiString : AnsiString;
  134. begin
  135. Result:=FAnsiString;
  136. end;
  137. Function TMyTestObject.GetMyEnum : TMyEnum;
  138. begin
  139. Result:=FMyEnum;
  140. end;
  141. Procedure TMyTestObject.Setboolean ( Value : boolean );
  142. begin
  143. Fboolean:=Value;
  144. end;
  145. Procedure TMyTestObject.SetByte ( Value : Byte );
  146. begin
  147. FByte:=Value;
  148. end;
  149. Procedure TMyTestObject.SetChar ( Value : Char );
  150. begin
  151. FChar:=Value;
  152. end;
  153. Procedure TMyTestObject.SetWord ( Value : Word );
  154. begin
  155. FWord:=Value;
  156. end;
  157. Procedure TMyTestObject.SetInteger ( Value : Integer );
  158. begin
  159. FInteger:=Value;
  160. end;
  161. Procedure TMyTestObject.SetLongint ( Value : Longint );
  162. begin
  163. FLongint:=Value;
  164. end;
  165. Procedure TMyTestObject.SetCardinal ( Value : Cardinal );
  166. begin
  167. FCardinal:=Value;
  168. end;
  169. Procedure TMyTestObject.SetReal ( Value : Real );
  170. begin
  171. FReal:=Value;
  172. end;
  173. Procedure TMyTestObject.SetExtended ( Value : Extended );
  174. begin
  175. FExtended:=Value;
  176. end;
  177. Procedure TMyTestObject.SetAnsiString ( Value : AnsiString );
  178. begin
  179. FAnsiString:=Value;
  180. end;
  181. Procedure TMyTestObject.SetMyEnum ( Value : TMyEnum );
  182. begin
  183. FMyEnum:=Value;
  184. end;
  185. Procedure DumpMem ( PL : PByte );
  186. Var I,j : longint;
  187. begin
  188. For I:=1 to 16 do
  189. begin
  190. Write ((I-1)*16:3,' :');
  191. For J:=1 to 10 do
  192. begin
  193. If (PL^>31) and (PL^<129) then
  194. Write(' ',CHar(PL^))
  195. else
  196. Write (PL^:3);
  197. Write (' ');
  198. inc(pl);
  199. end;
  200. writeln;
  201. end;
  202. end;
  203. Function ProcType (PP : Byte) : String;
  204. begin
  205. Case PP and 3 of
  206. ptfield : Result:='from Field';
  207. ptstatic : Result:='with static method';
  208. ptVirtual : Result:='with virtual method';
  209. ptconst : Result:='with Const';
  210. end;
  211. end;
  212. Procedure DumpTypeInfo (O : TMyTestObject);
  213. Var
  214. PT : PTypeData;
  215. PI : PTypeInfo;
  216. I,J : Longint;
  217. PP : PPropList;
  218. begin
  219. PI:=O.ClassInfo;
  220. Writeln ('Type kind : ',TypeNames[PI^.Kind]);
  221. Writeln ('Type name : ',PI^.Name);
  222. PT:=GetTypeData(PI);
  223. //DumpMem(PByte(PI));
  224. If PT^.ParentInfo=Nil then
  225. Writeln ('Object has no parent info')
  226. else
  227. Writeln ('Object has parent info');
  228. Writeln ('Property Count : ',PT^.PropCount);
  229. Writeln ('Unit name : ',PT^.UnitName);
  230. GetMem (PP,PT^.PropCount*SizeOf(Pointer));
  231. GetPropInfos(PI,PP);
  232. For I:=0 to PT^.PropCount-1 do
  233. If PP^[i]<>Nil then
  234. With PP^[I]^ do
  235. begin
  236. Writeln ('Property name : ',Name);
  237. Writeln (' Type kind: ',TypeNames[PropType^.Kind]);
  238. Writeln (' Type Name: ',PropType^.Name);
  239. If GetProc=Nil then Write ('No');
  240. Writeln (' Getproc available');
  241. If SetProc=Nil then Write ('No');
  242. Writeln (' Setproc available');
  243. If StoredProc=Nil then Write ('No');
  244. Writeln (' Storedproc available');
  245. Writeln (' Get property ',proctype(Propprocs));
  246. Writeln (' Set Property ',proctype(propprocs shr 2));
  247. Writeln (' Stored Property ',proctype(propprocs shr 2));
  248. Writeln (' Default : ',Default,' Index : ',Index);
  249. Writeln (' NameIndex : ',NameIndex);
  250. end;
  251. end;
  252. Procedure PrintObject ( Obj: TMyTestObject);
  253. begin
  254. With Obj do
  255. begin
  256. Writeln ('Field properties :');
  257. Writeln ('Property booleanField : ',booleanField);
  258. Writeln ('Property ByteField : ',ByteField);
  259. Writeln ('Property CharField : ',CharField);
  260. Writeln ('Property WordField : ',WordField);
  261. Writeln ('Property IntegerField : ',IntegerField);
  262. Writeln ('Property LongintField : ',LongintField);
  263. Writeln ('Property CardinalField : ',CardinalField);
  264. Writeln ('Property RealField : ',RealField);
  265. Writeln ('Property ExtendedField : ',ExtendedFIeld);
  266. Writeln ('Property AnsiStringField : ',AnsiStringField);
  267. Writeln ('Property MyEnumField : ',ord(MyEnumField));
  268. Writeln ('Method properties :');
  269. Writeln ('Property booleanMethod : ',BooleanMethod);
  270. Writeln ('Property ByteMethod : ',ByteMethod);
  271. Writeln ('Property CharMethod : ',CharMethod);
  272. Writeln ('Property WordMethod : ',WordMethod);
  273. Writeln ('Property IntegerMethod : ',IntegerMethod);
  274. Writeln ('Property LongintMethod : ',LongintMethod);
  275. Writeln ('Property CardinalMethod : ',CardinalMethod);
  276. Writeln ('Property RealMethod : ',RealMethod);
  277. Writeln ('Property ExtendedMethod : ',ExtendedMethod);
  278. Writeln ('Property AnsiStringMethod : ',AnsiStringMethod);
  279. Writeln ('Property MyEnumMethod : ',ord(MyEnumMethod));
  280. end;
  281. end;
  282. Procedure TestGet (O : TMyTestObject);
  283. Var
  284. PT : PTypeData;
  285. PI : PTypeInfo;
  286. I,J : Longint;
  287. PP : PPropList;
  288. prI : PPropInfo;
  289. begin
  290. PI:=O.ClassInfo;
  291. Writeln ('Type kind : ',TypeNames[PI^.Kind]);
  292. Writeln ('Type name : ',PI^.Name);
  293. PT:=GetTypeData(PI);
  294. If PT^.ParentInfo=Nil then
  295. Writeln ('Object has no parent info')
  296. else
  297. Writeln ('Object has parent info');
  298. Writeln ('Property Count : ',PT^.PropCount);
  299. Writeln ('Unit name : ',PT^.UnitName);
  300. GetMem (PP,PT^.PropCount*SizeOf(Pointer));
  301. GetPropInfos(PI,PP);
  302. For I:=0 to PT^.PropCount-1 do
  303. begin
  304. pri:=PP^[i];
  305. With Pri^ do
  306. begin
  307. Write ('(Examining ',name,' : Type : ',TypeNames[PropType^.Kind],', ');
  308. If (Proptype^.kind in Ordinaltypes) Then
  309. begin
  310. J:=GetOrdProp(O,pri);
  311. Write ('Value : ',j);
  312. If PropType^.Kind=tkenumeration then
  313. Write ('(=',GetEnumName(Proptype,J),')')
  314. end
  315. else
  316. Case pri^.proptype^.kind of
  317. tkfloat : begin
  318. Write ('Value : ');
  319. Flush(output);
  320. Write(GetFloatProp(O,pri))
  321. end;
  322. tkAstring : begin
  323. Write ('value : ');
  324. flush (output);
  325. Write(GetStrProp(O,Pri));
  326. end;
  327. else
  328. Write ('Untested type:',ord(pri^.proptype^.kind));
  329. end;
  330. Writeln (')');
  331. end;
  332. end;
  333. end;
  334. Var O : TMyTestObject;
  335. begin
  336. O:=TMyTestObject.Create;
  337. DumpTypeInfo(O);
  338. PrintObject(O);
  339. testget(o);
  340. end.