testrtti.pp 11 KB

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