typinfo.pp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. PropProcs : 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(TypeInfo) do
  134. begin
  135. // skip the name
  136. hp:=GetTypeData(Typeinfo);
  137. // the class info rtti the property rtti follows
  138. // immediatly
  139. Result:=PPropInfo(@hp^.UnitName)+byte(hp^.UnitName[0])+1;
  140. for i:=1 to hp^.PropCount do
  141. begin
  142. // found a property of that name ?
  143. if Result^.Name=PropName then
  144. exit;
  145. // skip to next property
  146. Result:=PPropInfo(@Result^.Name)+byte(Result^.Name[0])+1;
  147. end;
  148. // parent class
  149. Typeinfo:=hp^.ParentInfo;
  150. end;
  151. end;
  152. function IsStoredProp(Instance: TObject; PropInfo: PPropInfo): Boolean;
  153. type
  154. tbfunction = function : boolean of object;
  155. var
  156. caller : packed record
  157. Instance : Pointer;
  158. Address : Pointer;
  159. end;
  160. begin
  161. caller.Instance:=Instance;
  162. case (PropInfo^.PropProcs shr 4) and 3 of
  163. 0:
  164. IsStoredProp:=
  165. PBoolean(Pointer(Instance)+Longint(PropInfo^.StoredProc))^;
  166. 1:
  167. begin
  168. caller.Address:=PropInfo^.StoredProc;
  169. // IsStoredProp:=tbfunction(caller);
  170. end;
  171. 2:
  172. begin
  173. caller.Address:=PPointer(PPointer(Instance.ClassType)+Longint(PropInfo^.StoredProc))^;
  174. // IsStoredProp:=tbfunction(caller);
  175. end;
  176. 4:
  177. begin
  178. IsStoredProp:=
  179. LongBool(PropInfo^.StoredProc);
  180. end;
  181. end;
  182. end;
  183. end.
  184. {
  185. $Log$
  186. Revision 1.7 1998-09-08 09:52:31 florian
  187. * small problems fixed
  188. Revision 1.6 1998/09/08 00:08:36 michael
  189. Made it compilable
  190. Revision 1.5 1998/09/07 23:11:43 florian
  191. + more fields to TTypeInfo added
  192. Revision 1.4 1998/09/07 19:34:47 florian
  193. * constant value is now supported as stored condition
  194. Revision 1.3 1998/09/07 08:32:59 florian
  195. + procedure IsStoredProc added
  196. Revision 1.2 1998/09/06 21:27:05 florian
  197. + some methods and declarations added
  198. Revision 1.1 1998/08/25 22:30:00 florian
  199. + initial revision:
  200. o constants
  201. o basic type data record
  202. }