typinfo.tex 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. % The TYPINFO unit
  3. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4. \chapter{The TYPINFO unit}
  5. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6. % Constants, Types and variables
  7. \section{Constants, Types and variables}
  8. \subsection{Constants}
  9. The following constants are used in the implementation section of the unit.
  10. \begin{verbatim}
  11. BooleanIdents: array[Boolean] of String = ('False', 'True');
  12. DotSep: String = '.';
  13. \end{verbatim}
  14. The following constants determine the access method for the \var{Stored}
  15. identifier of a property as used in the \var{PropProcs} field of the
  16. \var{TPropInfo} record:
  17. \begin{verbatim}
  18. ptField = 0;
  19. ptStatic = 1;
  20. ptVirtual = 2;
  21. ptConst = 3;
  22. \end{verbatim}
  23. The following typed constants are used for easy selection of property types.
  24. \begin{verbatim}
  25. tkAny = [Low(TTypeKind)..High(TTypeKind)];
  26. tkMethods = [tkMethod];
  27. tkProperties = tkAny-tkMethods-[tkUnknown];
  28. \end{verbatim}
  29. \subsection{types}
  30. The following pointer types are defined:
  31. \begin{verbatim}
  32. PShortString =^ShortString;
  33. PByte =^Byte;
  34. PWord =^Word;
  35. PLongint =^Longint;
  36. PBoolean =^Boolean;
  37. PSingle =^Single;
  38. PDouble =^Double;
  39. PExtended =^Extended;
  40. PComp =^Comp;
  41. PFixed16 =^Fixed16;
  42. Variant = Pointer;
  43. \end{verbatim}
  44. The \var{TTypeKind} determines the type of a property:
  45. \begin{verbatim}
  46. TTypeKind = (tkUnknown,tkInteger,tkChar,tkEnumeration,
  47. tkFloat,tkSet,tkMethod,tkSString,tkLString,tkAString,
  48. tkWString,tkVariant,tkArray,tkRecord,tkInterface,
  49. tkClass,tkObject,tkWChar,tkBool,tkInt64,tkQWord,
  50. tkDynArray,tkInterfaceRaw);
  51. tkString = tkSString;
  52. \end{verbatim}
  53. \var{tkString} is an alias that is introduced for Delphi compatibility.
  54. If the property is and ordinal type, then \var{TTOrdType} determines the
  55. size and sign of the ordinal type:
  56. \begin{verbatim}
  57. TTOrdType = (otSByte,otUByte,otSWord,otUWord,otSLong,otULong);
  58. \end{verbatim}
  59. The size of a float type is determined by \var{TFloatType}:
  60. \begin{verbatim}
  61. TFloatType = (ftSingle,ftDouble,ftExtended,ftComp,ftCurr,
  62. ftFixed16,ftFixed32);
  63. \end{verbatim}
  64. A method property (e.g. an event) can have one of several types:
  65. \begin{verbatim}
  66. TMethodKind = (mkProcedure,mkFunction,mkConstructor,mkDestructor,
  67. mkClassProcedure, mkClassFunction);
  68. \end{verbatim}
  69. The kind of parameter to a method is determined by \var{TParamFlags}:
  70. \begin{verbatim}
  71. TParamFlags = set of (pfVar,pfConst,pfArray,pfAddress,pfReference,pfOut);
  72. \end{verbatim}
  73. Interfaces are described further with \var{TntfFlags}:
  74. \begin{verbatim}
  75. TIntfFlags = set of (ifHasGuid,ifDispInterface,ifDispatch);
  76. \end{verbatim}
  77. The following defines a set of \var{TTypeKind}:
  78. \begin{verbatim}
  79. TTypeKinds = set of TTypeKind;
  80. \end{verbatim}
  81. The \var{TypeInfo} function returns a pointer to a \var{TTypeInfo} record:
  82. \begin{verbatim}
  83. TTypeInfo = record
  84. Kind : TTypeKind;
  85. Name : ShortString;
  86. end;
  87. PTypeInfo = ^TTypeInfo;
  88. PPTypeInfo = ^PTypeInfo;
  89. \end{verbatim}
  90. Note that the Name is stored with as much bytes as needed to store the name,
  91. it is not padded to 255 characters.
  92. The type data immediatly follows the \var{TTypeInfo} record as a \var{TTypeData} record:
  93. \begin{verbatim}
  94. PTypeData = ^TTypeData;
  95. TTypeData = packed record
  96. case TTypeKind of
  97. tkUnKnown,tkLString,tkWString,tkAString,tkVariant:
  98. ();
  99. tkInteger,tkChar,tkEnumeration,tkWChar:
  100. (OrdType : TTOrdType;
  101. case TTypeKind of
  102. tkInteger,tkChar,tkEnumeration,tkBool,tkWChar : (
  103. MinValue,MaxValue : Longint;
  104. case TTypeKind of
  105. tkEnumeration: (
  106. BaseType : PTypeInfo;
  107. NameList : ShortString
  108. )
  109. );
  110. tkSet: (
  111. CompType : PTypeInfo
  112. )
  113. );
  114. tkFloat: (
  115. FloatType : TFloatType
  116. );
  117. tkSString:
  118. (MaxLength : Byte);
  119. tkClass:
  120. (ClassType : TClass;
  121. ParentInfo : PTypeInfo;
  122. PropCount : SmallInt;
  123. UnitName : ShortString
  124. );
  125. tkMethod:
  126. (MethodKind : TMethodKind;
  127. ParamCount : Byte;
  128. ParamList : array[0..1023] of Char
  129. {in reality ParamList is a array[1..ParamCount] of:
  130. record
  131. Flags : TParamFlags;
  132. ParamName : ShortString;
  133. TypeName : ShortString;
  134. end;
  135. followed by
  136. ResultType : ShortString}
  137. );
  138. tkInt64:
  139. (MinInt64Value, MaxInt64Value: Int64);
  140. tkQWord:
  141. (MinQWordValue, MaxQWordValue: QWord);
  142. tkInterface:
  143. ();
  144. end;
  145. \end{verbatim}
  146. If the typeinfo kind is \var{tkClass}, then the property
  147. information follows the \var{UnitName} string, as an array of \var{TPropInfo} records.
  148. The \var{TPropData} record is not used, but is provided for completeness.
  149. \begin{verbatim}
  150. TPropData = packed record
  151. PropCount : Word;
  152. PropList : record end;
  153. end;
  154. \end{verbatim}
  155. The \var{TPropInfo} record describes one published property of a class:
  156. \begin{verbatim}
  157. PPropInfo = ^TPropInfo;
  158. TPropInfo = packed record
  159. PropType : PTypeInfo;
  160. GetProc : Pointer;
  161. SetProc : Pointer;
  162. StoredProc : Pointer;
  163. Index : Integer;
  164. Default : Longint;
  165. NameIndex : SmallInt;
  166. PropProcs : Byte;
  167. Name : ShortString;
  168. end;
  169. \end{verbatim}
  170. The \var{Name} field is stored not with 255 characters, but with just as many characters
  171. as required to store the name.
  172. \begin{verbatim}
  173. TProcInfoProc = procedure(PropInfo : PPropInfo) of object;
  174. \end{verbatim}
  175. The following pointer and array types are used for typecasts:
  176. \begin{verbatim}
  177. PPropList = ^TPropList;
  178. TPropList = array[0..65535] of PPropInfo;
  179. \end{verbatim}
  180. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  181. % Functions and procedures
  182. \section{Functions and Procedures}
  183. \begin{function}{GetTypeData}
  184. \Declaration
  185. Function GetTypeData(TypeInfo : PTypeInfo) : PTypeData;
  186. \Description
  187. \var{GetTypeData} returns a pointer to the \var{TTypeData} record that
  188. follows after the \var{TTypeInfo} record pointed to by \var{TypeInfo}.
  189. It essentially skips the \var{Kind} and \var{Name} fields in the
  190. \var{TTypeInfo} record.
  191. \Errors
  192. None.
  193. \SeeAlso
  194. \end{function}
  195. \begin{function}{GetPropInfo}
  196. \Declaration
  197. function GetPropInfo(TypeInfo: PTypeInfo;
  198. const PropName: string) : PPropInfo;
  199. \Description
  200. \var{GetPropInfo} returns a pointer to the \var{TPropInfo} record for a the \var{PropName}
  201. property of a class that has \var{TypeInfo} as class info.
  202. \Errors
  203. If the property \var{PropName} does not exist, \var{Nil} is returned.
  204. \SeeAlso
  205. \seep{GetPropInfos},\seef{GetPropList}
  206. \end{function}
  207. \begin{procedure}{GetPropInfos}
  208. \Declaration
  209. Procedure GetPropInfos(TypeInfo: PTypeInfo;PropList: PPropList);
  210. \Description
  211. \var{GetPropInfos} stores pointers to the property information of all published
  212. properties of a class with class info \var{TypeInfo} in the list pointed to by
  213. \var{Proplist}. The \var{PropList} pointer must point to a memory location that
  214. contains enough space to hold all properties of the class and its parent classes.
  215. \Errors
  216. No checks are done to see whether \var{PropList} points to a memory area that
  217. is big enough to hold all pointers.
  218. \SeeAlso
  219. \seef{GetPropInfo},\seef{GetPropList}
  220. \end{procedure}
  221. \begin{function}{GetPropList}
  222. \Declaration
  223. Function GetPropList(TypeInfo : PTypeInfo;
  224. TypeKinds : TTypeKinds;
  225. PropList : PPropList) : Integer;
  226. \Description
  227. \var{GetPropList} stores pointers to property information of the class with class
  228. info \var{TypeInfo} for properties of kind \var{TypeKinds} in the list pointed to
  229. by \var{Proplist}. \var{PropList} must contain enough space to hold all properties.
  230. The function returns the number of pointers that matched the criteria and were stored
  231. in \var{PropList}.
  232. \Errors
  233. No checks are done to see whether \var{PropList} points to a memory area that is big enough
  234. to hold all pointers.
  235. \SeeAlso
  236. \seep{GetPropInfos}, \seef{GetPropInfo}
  237. \end{function}
  238. \begin{function}{IsStoredProp}
  239. \Declaration
  240. Function IsStoredProp(Instance : TObject;PropInfo : PPropInfo) : Boolean;
  241. \Description
  242. \var{IsStoredProp} returns \var{True} if the \var{Stored} modifier evaluates
  243. to \var{True} for the property described by \var{PropInfo} for object
  244. \var{Instance}. It returns \var{False} otherwise. If the function returns
  245. \var{True}, this indicates that the property should be written when
  246. streaming the object \var{Instance}.
  247. If there was no \var{stored} modifier in the declaration of the property,
  248. \var{True} will be returned.
  249. \Errors
  250. No checking is done whether \var{Instance} is non-nil, or whether
  251. \var{PropInfo} describes a valid property of \var{Instance}.
  252. \SeeAlso
  253. \end{function}
  254. \begin{function}{GetOrdProp}
  255. \Declaration
  256. Function GetOrdProp(Instance : TObject;PropInfo : PPropInfo) : Longint;
  257. \Description
  258. \var{GetOrdProp} returns the value of the ordinal property described by
  259. \var{PropInfo} for the object \var{Instance}. The value is returned as a
  260. longint, which should be typecasted to the needed type.
  261. Ordinal properties that can be retrieved include:
  262. \begin{description}
  263. \item[Integers and subranges of integers] The value of the integer will be
  264. returned.
  265. \item[Enumerated types and subranges of enumerated types] The ordinal value
  266. of the enumerated type will be returned.
  267. \item[Sets] If the base type of the set has less than 31 possible values.
  268. If a bit is set in the return value, then the corresponding element of the
  269. base ordinal class of the set type must be included in the set.
  270. \end{description}
  271. \Errors
  272. No checking is done whether \var{Instance} is non-nil, or whether
  273. \var{PropInfo} describes a valid ordinal property of \var{Instance}
  274. \SeeAlso
  275. \seep{SetOrdProp}, \seef{GetStrProp}, \seef{GetFloatProp},
  276. \seef{GetInt64Prop},\seef{GetMethodProp}
  277. \end{function}
  278. \begin{procedure}{SetOrdProp}
  279. \Declaration
  280. Procedure SetOrdProp(Instance : TObject;PropInfo : PPropInfo; Value : Longint);
  281. \Description
  282. \var{SetOrdProp} assigns \var{Value} to the the ordinal property described by
  283. \var{PropInfo} for the object \var{Instance}.
  284. Ordinal properties that can be set include:
  285. \begin{description}
  286. \item[Integers and subranges of integers] The actual value of the integer must be
  287. passed.
  288. \item[Enumerated types and subranges of enumerated types] The ordinal value
  289. of the enumerated type must be passed.
  290. \item[Subrange types] of integers or enumerated types. Here the ordinal
  291. value must be passed.
  292. \item[Sets] If the base type of the set has less than 31 possible values.
  293. For each possible value; the corresponding bit of \var{Value} must be set.
  294. \end{description}
  295. \Errors
  296. No checking is done whether \var{Instance} is non-nil, or whether
  297. \var{PropInfo} describes a valid ordinal property of \var{Instance}.
  298. No range checking is performed.
  299. \SeeAlso
  300. \seef{GetOrdProp}, \seep{SetStrProp}, \seep{SetFloatProp},
  301. \seep{SetInt64Prop},\seep{SetMethodProp}
  302. \end{procedure}
  303. \begin{function}{GetStrProp}
  304. \Declaration
  305. Function GetStrProp(Instance : TObject;PropInfo : PPropInfo) : Ansistring;
  306. \Description
  307. \var{GetStrProp} returns the value of the string property described by
  308. \var{PropInfo} for object \var{Instance}.
  309. \Errors
  310. No checking is done whether \var{Instance} is non-nil, or whether
  311. \var{PropInfo} describes a valid string property of \var{Instance}.
  312. \SeeAlso
  313. \seep{SetStrProp}, \seef{GetOrdProp}, \seef{GetFloatProp},
  314. \seef{GetInt64Prop},\seef{GetMethodProp}
  315. \end{function}
  316. \begin{procedure}{SetStrProp}
  317. \Declaration
  318. procedure SetStrProp(Instance : TObject;PropInfo : PPropInfo;
  319. const Value : Ansistring);
  320. \Description
  321. \var{GetStrProp} assigns \var{Value} to the string property described by
  322. \var{PropInfo} for object \var{Instance}.
  323. \Errors
  324. No checking is done whether \var{Instance} is non-nil, or whether
  325. \var{PropInfo} describes a valid string property of \var{Instance}.
  326. \SeeAlso
  327. \seef{GetStrProp}, \seep{SetOrdProp}, \seep{SetFloatProp},
  328. \seep{SetInt64Prop},\seep{SetMethodProp}
  329. \end{procedure}
  330. \begin{function}{GetFloatProp}
  331. \Declaration
  332. Function GetFloatProp(Instance : TObject;PropInfo : PPropInfo) : Extended;
  333. \Description
  334. \var{GetFloatProp} returns the value of the float property described by
  335. \var{PropInfo} for the object \var{Instance}. All float types are converted
  336. to extended.
  337. \Errors
  338. No checking is done whether \var{Instance} is non-nil, or whether
  339. \var{PropInfo} describes a valid float property of \var{Instance}.
  340. \SeeAlso
  341. \seep{SetFloatProp}, \seef{GetOrdProp}, \seef{GetStrProp},
  342. \seef{GetInt64Prop},\seef{GetMethodProp}
  343. \end{function}
  344. \begin{procedure}{SetFloatProp}
  345. \Declaration
  346. Procedure SetFloatProp(Instance : TObject;
  347. PropInfo : PPropInfo;
  348. Value : Extended);
  349. \Description
  350. \var{SetFloatProp} assigns \var{Value} to the property described by
  351. \var{PropInfo} for the object \var{Instance}.
  352. \Errors
  353. No checking is done whether \var{Instance} is non-nil, or whether
  354. \var{PropInfo} describes a valid float property of \var{Instance}.
  355. \SeeAlso
  356. \seef{GetFloatProp}, \seep{SetOrdProp}, \seep{SetStrProp},
  357. \seep{SetInt64Prop},\seep{SetMethodProp}
  358. \end{procedure}
  359. \begin{function}{GetVariantProp}
  360. \Declaration
  361. Function GetVariantProp(Instance : TObject;PropInfo : PPropInfo): Variant;
  362. \Description
  363. Not yet implemented. Provided for Delphi compatibility only.
  364. \Errors
  365. \SeeAlso
  366. \end{function}
  367. \begin{procedure}{SetVariantProp}
  368. \Declaration
  369. Procedure SetVariantProp(Instance : TObject;
  370. PropInfo : PPropInfo;
  371. Const Value: Variant);
  372. \Description
  373. Not yet implemented. Provided for Delphi compatibility only.
  374. \Errors
  375. \SeeAlso
  376. \end{procedure}
  377. \begin{function}{GetMethodProp}
  378. \Declaration
  379. Function GetMethodProp(Instance : TObject;PropInfo : PPropInfo) : TMethod;
  380. \Description
  381. \var{GetMethodProp} returns the method the property described by
  382. \var{PropInfo} for object \var{Instance}.
  383. The return type \var{TMethod} is defined in the \file{SysUtils} unit as:
  384. \begin{verbatim}
  385. TMethod = packed record
  386. Code, Data: Pointer;
  387. end;
  388. \end{verbatim}
  389. \var{Data} points to the instance of the class with the method \var{Code}.
  390. \Errors
  391. No checking is done whether \var{Instance} is non-nil, or whether
  392. \var{PropInfo} describes a valid method property of \var{Instance}.
  393. \SeeAlso
  394. \seep{SetMethodProp}, \seef{GetOrdProp}, \seef{GetStrProp},
  395. \seef{GetFloatProp}, \seef{GetInt64Prop}
  396. \end{function}
  397. \begin{procedure}{SetMethodProp}
  398. \Declaration
  399. Procedure SetMethodProp(Instance : TObject;PropInfo : PPropInfo; const Value : TMethod);
  400. \Description
  401. \var{SetMethodProp} assigns \var{Value} to the method the property described
  402. by \var{PropInfo} for object \var{Instance}.
  403. The type \var{TMethod} of the \var{Value} parameter is defined in the
  404. \file{SysUtils} unit as:
  405. \begin{verbatim}
  406. TMethod = packed record
  407. Code, Data: Pointer;
  408. end;
  409. \end{verbatim}
  410. \var{Data} should point to the instance of the class with the method \var{Code}.
  411. \Errors
  412. No checking is done whether \var{Instance} is non-nil, or whether
  413. \var{PropInfo} describes a valid method property of \var{Instance}.
  414. \SeeAlso
  415. \seef{GetMethodProp}, \seep{SetOrdProp}, \seep{SetStrProp},
  416. \seep{SetFloatProp}, \seep{SetInt64Prop}
  417. \end{procedure}
  418. \begin{function}{GetInt64Prop}
  419. \Declaration
  420. Function GetInt64Prop(Instance: TObject; PropInfo: PPropInfo): Int64;
  421. \Description
  422. \var{GetInt64Prop} returns the value of the property of type
  423. \var{Int64} that is described by \var{PropInfo} for the object
  424. \var{Instance}.
  425. \Errors
  426. No checking is done whether \var{Instance} is non-nil, or whether
  427. \var{PropInfo} describes a valid \var{Int64} property of \var{Instance}.
  428. \SeeAlso
  429. \seep{SetInt64Prop}, \seef{GetOrdProp}, \seef{GetStrProp},
  430. \seef{GetFloatProp}, \seef{GetMethodProp}
  431. \end{function}
  432. \begin{procedure}{SetInt64Prop}
  433. \Declaration
  434. Procedure SetInt64Prop(Instance: TObject; PropInfo: PPropInfo; const Value: Int64);
  435. \Description
  436. \var{SetInt64Prop} assigns \var{Value} to the property of type
  437. \var{Int64} that is described by \var{PropInfo} for the object
  438. \var{Instance}.
  439. \Errors
  440. No checking is done whether \var{Instance} is non-nil, or whether
  441. \var{PropInfo} describes a valid \var{Int64} property of \var{Instance}.
  442. \SeeAlso
  443. \seef{GetInt64Prop}, \seef{GetMethodProp}, \seep{SetOrdProp}, \seep{SetStrProp},
  444. \seep{SetFloatProp}
  445. \end{procedure}
  446. \begin{function}{GetEnumName}
  447. \Declaration
  448. Function GetEnumName(TypeInfo : PTypeInfo;Value : Integer) : string;
  449. \Description
  450. \var{GetEnumName} scans the type information for the enumeration type
  451. desribed by \var{TypeInfo} and returns the name of the enumeration
  452. constant for the element with ordinal value equal to \var{Value}.
  453. If \var{Value} is out of range, the first element of the enumeration type
  454. is returned. The result is lowercased, but this may change in the future.
  455. This can be used in combination with \var{GetOrdProp} to stream a property
  456. of an enumerated type.
  457. \Errors
  458. No check is done whether \var{TypeInfo} points to the type information
  459. for an enumerated type.
  460. \SeeAlso
  461. \seef{GetOrdProp}, \seef{GetEnumValue}
  462. \end{function}
  463. \begin{function}{GetEnumValue}
  464. \Declaration
  465. Function GetEnumValue(TypeInfo : PTypeInfo;const Name : string) : Integer;
  466. \Description
  467. \var{GetEnumValue} scans the type information for the enumeration type
  468. described by \var{TypeInfor} and returns the ordinal value for the element
  469. in the enumerated type that has identifier \var{Name}. The identifier is
  470. searched in a case-insensitive manner.
  471. This can be used to set the value of enumerated properties from a stream.
  472. \Errors
  473. If \var{Name} is not found in the list of enumerated values, then -1 is
  474. returned. No check is done whether \var{TypeInfo} points to the type information
  475. for an enumerated type.
  476. \SeeAlso
  477. \seef{GetEnumName}, \seep{SetOrdProp}
  478. \end{function}