typinfo.pp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1998 by Florian Klaempfl
  5. member of the Free Pascal development team
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. { This unit provides the same functionality as the TypInfo Unit }
  13. { of Delphi }
  14. unit typinfo;
  15. interface
  16. uses objpas;
  17. {
  18. sysutils;
  19. }
  20. // temporary types:
  21. type
  22. ShortSTring=String;
  23. PByte =^Byte;
  24. PBoolean =^Boolean;
  25. {$MINENUMSIZE 1 this saves a lot of memory }
  26. // if you change one of the following enumeration types
  27. // you have also to change the compiler in an appropriate way !
  28. TTypeKind = (tkUnknown,tkInteger,tkChar,tkEnumeration,
  29. tkFloat,tkSet,tkMethod,tkSString,tkLString,tkAString,
  30. tkWString,tkVariant,tkArray,tkRecord,tkInterface,
  31. tkClass,tkObject,tkWChar,tkBool);
  32. TTOrdType = (otSByte,otUByte,otSWord,otUWord,otSLong,otULong);
  33. TFloatType = (ftSingle,ftDouble,ftExtended,ftComp,ftCurr,
  34. ftFixed16,ftFixed32);
  35. {$MINENUMSIZE DEFAULT}
  36. const
  37. ptField = 0;
  38. ptStatic = 1;
  39. ptVirtual = 2;
  40. ptConst = 3;
  41. const
  42. tkString = tkSString;
  43. type
  44. TMethodKind = Byte;
  45. TTypeKinds = set of TTypeKind;
  46. TTypeInfo = record
  47. Kind : TTypeKind;
  48. Name : ShortString;
  49. end;
  50. PTypeInfo = ^TTypeInfo;
  51. PPTypeInfo = ^PTypeInfo;
  52. PTypeData = ^TTypeData;
  53. TTypeData = packed record
  54. case TTypeKind of
  55. tkUnKnown,tkLString,tkWString,tkAString,tkVariant:
  56. ();
  57. tkInteger,tkChar,tkEnumeration,tkWChar:
  58. (OrdType : TTOrdType;
  59. case TTypeKind of
  60. tkInteger,tkChar,tkEnumeration,tkBool,tkWChar : (
  61. MinValue,MaxValue : Longint;
  62. case TTypeKind of
  63. tkEnumeration:
  64. (
  65. BaseType : PTypeInfo;
  66. NameList : ShortString)
  67. );
  68. tkSet:
  69. (CompType : PTypeInfo)
  70. );
  71. tkFloat:
  72. (FloatType : TFloatType);
  73. tkSString:
  74. (MaxLength : Byte);
  75. tkClass:
  76. (ClassType : TClass;
  77. ParentInfo : PTypeInfo;
  78. PropCount : SmallInt;
  79. UnitName : ShortString
  80. );
  81. tkMethod:
  82. ({!!!!!!!}
  83. );
  84. tkInterface:
  85. ({!!!!!!!}
  86. );
  87. end;
  88. PPropInfo = ^TPropInfo;
  89. TPropInfo = packed record
  90. PropType : PTypeInfo;
  91. GetProc : Pointer;
  92. SetProc : Pointer;
  93. StoredProc : Pointer;
  94. Index : Integer;
  95. Default : Longint;
  96. NameIndex : SmallInt;
  97. // contains the type of the Get/Set/Storedproc, see also ptxxx
  98. // bit 0..1 GetProc
  99. // 2..3 SetProc
  100. // 4..5 StoredProc
  101. ProcProcs : Byte;
  102. Name : ShortString;
  103. end;
  104. TProcInfoProc = procedure(PropInfo : PPropInfo) of object;
  105. PPropList = ^TPropList;
  106. TPropList = array[0..65535] of PPropInfo;
  107. const
  108. tkAny = [Low(TTypeKind)..High(TTypeKind)];
  109. tkMethods = [tkMethod];
  110. tkProperties = tkAny-tkMethods-[tkUnknown];
  111. // just skips the id and the name
  112. function GetTypeData(TypeInfo : PTypeInfo) : PTypeData;
  113. // searches in the property PropName
  114. function GetPropInfo(TypeInfo : PTypeInfo;const PropName : string) : PPropInfo;
  115. // returns true, if PropInfo is a stored property
  116. function IsStoredProp(Instance: TObject; PropInfo: PPropInfo): Boolean;
  117. {
  118. procedure GetPropInfos(TypeInfo : PTypeInfo;PropList : PPropList);
  119. function GetPropList(TypeInfo : PTypeInfo;TypeKinds : TTypeKinds;
  120. PropList : PPropList) : Integer;
  121. }
  122. implementation
  123. function GetTypeData(TypeInfo : PTypeInfo) : PTypeData;
  124. begin
  125. GetTypeData:=PTypeData(TypeInfo)+2+PByte(TypeInfo+1)^;
  126. end;
  127. function GetPropInfo(TypeInfo : PTypeInfo;const PropName : string) : PPropInfo;
  128. var
  129. hp : PTypeData;
  130. i : longint;
  131. begin
  132. Result:=Nil;
  133. while Assigned(hp) do
  134. begin
  135. // skip the name
  136. //!! Florian, I added (typeinfo) so it would compile
  137. hp:=GetTypeData(Typeinfo);
  138. // the class info rtti the property rtti follows
  139. // immediatly
  140. Result:=PPropInfo(@hp^.UnitName)+byte(hp^.UnitName[0])+1;
  141. for i:=1 to hp^.PropCount do
  142. begin
  143. // found a property of that name ?
  144. if Result^.Name=PropName then
  145. exit;
  146. // skip to next property
  147. Result:=PPropInfo(@Result^.Name)+byte(Result^.Name[0])+1;
  148. end;
  149. // parent class
  150. //!! Florian, commented out, because the types are wrong
  151. // hp:=hp^.ParentInfo;
  152. end;
  153. end;
  154. function IsStoredProp(Instance: TObject; PropInfo: PPropInfo): Boolean;
  155. type
  156. tbfunction = function : boolean of object;
  157. var
  158. caller : packed record
  159. Instance : Pointer;
  160. Address : Pointer;
  161. end;
  162. begin
  163. caller.Instance:=Instance;
  164. //!! propprocs doesn't exist, changed to procprops
  165. case (PropInfo^.ProcProcs shr 4) and 3 of
  166. 0:
  167. IsStoredProp:=
  168. PBoolean(Pointer(Instance)+Longint(PropInfo^.StoredProc))^;
  169. 1:
  170. begin
  171. caller.Address:=PropInfo^.StoredProc;
  172. // IsStoredProp:=tbfunction(caller);
  173. end;
  174. 2:
  175. begin
  176. caller.Address:=PPointer(PPointer(Instance.ClassType)+Longint(PropInfo^.StoredProc))^;
  177. // IsStoredProp:=tbfunction(caller);
  178. end;
  179. 4:
  180. begin
  181. IsStoredProp:=
  182. LongBool(PropInfo^.StoredProc);
  183. end;
  184. end;
  185. end;
  186. end.
  187. {
  188. $Log$
  189. Revision 1.6 1998-09-08 00:08:36 michael
  190. Made it compilable
  191. Revision 1.5 1998/09/07 23:11:43 florian
  192. + more fields to TTypeInfo added
  193. Revision 1.4 1998/09/07 19:34:47 florian
  194. * constant value is now supported as stored condition
  195. Revision 1.3 1998/09/07 08:32:59 florian
  196. + procedure IsStoredProc added
  197. Revision 1.2 1998/09/06 21:27:05 florian
  198. + some methods and declarations added
  199. Revision 1.1 1998/08/25 22:30:00 florian
  200. + initial revision:
  201. o constants
  202. o basic type data record
  203. }