testrtti.pp 11 KB

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