trtti1.pp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. Program trtti1;
  2. {$Mode Delphi}
  3. {$M+}
  4. Uses
  5. Rttiobj,Typinfo;
  6. Procedure DumpMem ( PL : PByte );
  7. Var I,j : longint;
  8. begin
  9. For I:=1 to 16 do
  10. begin
  11. Write ((I-1)*16:3,' :');
  12. For J:=1 to 10 do
  13. begin
  14. If (PL^>31) and (PL^<129) then
  15. Write(' ',CHar(PL^))
  16. else
  17. Write (PL^:3);
  18. Write (' ');
  19. inc(pl);
  20. end;
  21. writeln;
  22. end;
  23. end;
  24. Function ProcType (PP : Byte) : String;
  25. begin
  26. Case PP and 3 of
  27. ptfield : Result:='from Field';
  28. ptstatic : Result:='with static method';
  29. ptVirtual : Result:='with virtual method';
  30. ptconst : Result:='with Const';
  31. end;
  32. end;
  33. Procedure DumpTypeInfo (O : TMyTestObject);
  34. Var
  35. PT : PTypeData;
  36. PI : PTypeInfo;
  37. I : Longint;
  38. PP : PPropList;
  39. begin
  40. PI:=O.ClassInfo;
  41. Writeln ('Type kind : ',TypeNames[PI^.Kind]);
  42. Writeln ('Type name : ',PI^.Name);
  43. PT:=GetTypeData(PI);
  44. //DumpMem(PByte(PI));
  45. If PT^.ParentInfo=Nil then
  46. Writeln ('Object has no parent info')
  47. else
  48. Writeln ('Object has parent info');
  49. Writeln ('Property Count : ',PT^.PropCount);
  50. Writeln ('Unit name : ',PT^.UnitName);
  51. GetMem (PP,PT^.PropCount*SizeOf(Pointer));
  52. GetPropInfos(PI,PP);
  53. For I:=0 to PT^.PropCount-1 do
  54. If PP^[i]<>Nil then
  55. With PP^[I]^ do
  56. begin
  57. Writeln ('Property name : ',Name);
  58. Writeln (' Type kind: ',TypeNames[PropType^.Kind]);
  59. Writeln (' Type Name: ',PropType^.Name);
  60. If GetProc=Nil then Write ('No');
  61. Writeln (' Getproc available');
  62. If SetProc=Nil then Write ('No');
  63. Writeln (' Setproc available');
  64. If StoredProc=Nil then Write ('No');
  65. Writeln (' Storedproc available');
  66. Writeln (' Get property ',proctype(Propprocs));
  67. Writeln (' Set Property ',proctype(propprocs shr 2));
  68. Writeln (' Stored Property ',proctype(propprocs shr 4));
  69. Writeln (' Default : ',Default,' Index : ',Index);
  70. Writeln (' NameIndex : ',NameIndex);
  71. end;
  72. end;
  73. Procedure PrintObject ( Obj: TMyTestObject);
  74. begin
  75. With Obj do
  76. begin
  77. Writeln ('Field properties :');
  78. Writeln ('Property booleanField : ',booleanField);
  79. Writeln ('Property ByteField : ',ByteField);
  80. Writeln ('Property CharField : ',CharField);
  81. Writeln ('Property WordField : ',WordField);
  82. Writeln ('Property IntegerField : ',IntegerField);
  83. Writeln ('Property LongintField : ',LongintField);
  84. Writeln ('Property CardinalField : ',CardinalField);
  85. Writeln ('Property RealField : ',RealField);
  86. Writeln ('Property ExtendedField : ',ExtendedFIeld);
  87. Writeln ('Property AnsiStringField : ',AnsiStringField);
  88. Writeln ('Property MyEnumField : ',ord(MyEnumField));
  89. Writeln ('Method properties :');
  90. Writeln ('Property booleanMethod : ',BooleanMethod);
  91. Writeln ('Property ByteMethod : ',ByteMethod);
  92. Writeln ('Property CharMethod : ',CharMethod);
  93. Writeln ('Property WordMethod : ',WordMethod);
  94. Writeln ('Property IntegerMethod : ',IntegerMethod);
  95. Writeln ('Property LongintMethod : ',LongintMethod);
  96. Writeln ('Property CardinalMethod : ',CardinalMethod);
  97. Writeln ('Property RealMethod : ',RealMethod);
  98. Writeln ('Property ExtendedMethod : ',ExtendedMethod);
  99. Writeln ('Property AnsiStringMethod : ',AnsiStringMethod);
  100. Writeln ('Property MyEnumMethod : ',ord(MyEnumMethod));
  101. Writeln ('VirtualMethod properties :');
  102. Writeln ('Property booleanVirtualMethod : ',BooleanVirtualMethod);
  103. Writeln ('Property ByteVirtualMethod : ',ByteVirtualMethod);
  104. Writeln ('Property CharVirtualMethod : ',CharVirtualMethod);
  105. Writeln ('Property WordVirtualMethod : ',WordVirtualMethod);
  106. Writeln ('Property IntegerVirtualMethod : ',IntegerVirtualMethod);
  107. Writeln ('Property LongintVirtualMethod : ',LongintVirtualMethod);
  108. Writeln ('Property CardinalVirtualMethod : ',CardinalVirtualMethod);
  109. Writeln ('Property RealVirtualMethod : ',RealVirtualMethod);
  110. Writeln ('Property ExtendedVirtualMethod : ',ExtendedVirtualMethod);
  111. Writeln ('Property AnsiStringVirtualMethod : ',AnsiStringVirtualMethod);
  112. Writeln ('Property MyEnumVirtualMethod : ',ord(MyEnumVirtualMethod));
  113. end;
  114. end;
  115. Procedure TestGet (O : TMyTestObject);
  116. Var
  117. PT : PTypeData;
  118. PI : PTypeInfo;
  119. I,J : Longint;
  120. PP : PPropList;
  121. prI : PPropInfo;
  122. begin
  123. PI:=O.ClassInfo;
  124. Writeln ('Type kind : ',TypeNames[PI^.Kind]);
  125. Writeln ('Type name : ',PI^.Name);
  126. PT:=GetTypeData(PI);
  127. If PT^.ParentInfo=Nil then
  128. Writeln ('Object has no parent info')
  129. else
  130. Writeln ('Object has parent info');
  131. Writeln ('Property Count : ',PT^.PropCount);
  132. Writeln ('Unit name : ',PT^.UnitName);
  133. GetMem (PP,PT^.PropCount*SizeOf(Pointer));
  134. GetPropInfos(PI,PP);
  135. For I:=0 to PT^.PropCount-1 do
  136. begin
  137. pri:=PP^[i];
  138. With Pri^ do
  139. begin
  140. Write ('(Examining ',name,' : Type : ',TypeNames[PropType^.Kind],', ');
  141. If (Proptype^.kind in Ordinaltypes) Then
  142. begin
  143. J:=GetOrdProp(O,pri);
  144. Write ('Value : ',j);
  145. If PropType^.Kind=tkenumeration then
  146. Write ('(=',GetEnumName(Proptype,J),')')
  147. end
  148. else
  149. Case pri^.proptype^.kind of
  150. tkfloat : begin
  151. Write ('Value : ');
  152. Flush(output);
  153. Write(GetFloatProp(O,pri))
  154. end;
  155. tkAstring : begin
  156. Write ('value : ');
  157. flush (output);
  158. Write(GetStrProp(O,Pri));
  159. end;
  160. else
  161. Write ('Untested type:',ord(pri^.proptype^.kind));
  162. end;
  163. Writeln (')');
  164. end;
  165. end;
  166. end;
  167. Var O : TMyTestObject;
  168. begin
  169. O:=TMyTestObject.Create;
  170. DumpTypeInfo(O);
  171. PrintObject(O);
  172. testget(o);
  173. end.