typinfo.tex 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. % The TYPINFO unit
  3. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4. \chapter{The TYPINFO unit}
  5. \FPCexampledir{typinfex}
  6. The \file{TypeInfo} unit contains many routines which can be used for
  7. the querying of the Run-Time Type Information (RTTI) which is generated
  8. by the compiler for classes that are compiled under the \var{\{\*M+\}}
  9. switch. This information can be used to retrieve or set property values
  10. for published properties for totally unknown classes. In particular, it
  11. can be used to stream classes. The \var{TPersistent} class in the
  12. \file{Classes} unit is compiled in the \var{\{\*M+\}} state and serves
  13. as the base class for all classes that need to be streamed.
  14. The unit should be compatible to the Delphi 5 unit with the same name.
  15. The only calls that are still missing are the Variant calls, since \fpc
  16. does not support the variant type yet.
  17. The examples in this chapter use a \file{rttiobj} file, which contains
  18. an object that has a published property of all supported types. It also
  19. contains some auxiliary routines and definitions.
  20. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  21. % Constants, Types and variables
  22. \section{Constants, Types and variables}
  23. \subsection{Constants}
  24. The following constants are used in the implementation section of the unit.
  25. \begin{verbatim}
  26. BooleanIdents: array[Boolean] of String = ('False', 'True');
  27. DotSep: String = '.';
  28. \end{verbatim}
  29. The following constants determine the access method for the \var{Stored}
  30. identifier of a property as used in the \var{PropProcs} field of the
  31. \var{TPropInfo} record:
  32. \begin{verbatim}
  33. ptField = 0;
  34. ptStatic = 1;
  35. ptVirtual = 2;
  36. ptConst = 3;
  37. \end{verbatim}
  38. The following typed constants are used for easy selection of property types.
  39. \begin{verbatim}
  40. tkAny = [Low(TTypeKind)..High(TTypeKind)];
  41. tkMethods = [tkMethod];
  42. tkProperties = tkAny-tkMethods-[tkUnknown];
  43. \end{verbatim}
  44. \subsection{types}
  45. The following pointer types are defined:
  46. \begin{verbatim}
  47. PShortString =^ShortString;
  48. PByte =^Byte;
  49. PWord =^Word;
  50. PLongint =^Longint;
  51. PBoolean =^Boolean;
  52. PSingle =^Single;
  53. PDouble =^Double;
  54. PExtended =^Extended;
  55. PComp =^Comp;
  56. PFixed16 =^Fixed16;
  57. Variant = Pointer;
  58. \end{verbatim}
  59. The \var{TTypeKind} determines the type of a property:
  60. \begin{verbatim}
  61. TTypeKind = (tkUnknown,tkInteger,tkChar,tkEnumeration,
  62. tkFloat,tkSet,tkMethod,tkSString,tkLString,tkAString,
  63. tkWString,tkVariant,tkArray,tkRecord,tkInterface,
  64. tkClass,tkObject,tkWChar,tkBool,tkInt64,tkQWord,
  65. tkDynArray,tkInterfaceRaw);
  66. tkString = tkSString;
  67. \end{verbatim}
  68. \var{tkString} is an alias that is introduced for Delphi compatibility.
  69. If the property is and ordinal type, then \var{TTOrdType} determines the
  70. size and sign of the ordinal type:
  71. \begin{verbatim}
  72. TTOrdType = (otSByte,otUByte,otSWord,otUWord,otSLong,otULong);
  73. \end{verbatim}
  74. The size of a float type is determined by \var{TFloatType}:
  75. \begin{verbatim}
  76. TFloatType = (ftSingle,ftDouble,ftExtended,ftComp,ftCurr,
  77. ftFixed16,ftFixed32);
  78. \end{verbatim}
  79. A method property (e.g. an event) can have one of several types:
  80. \begin{verbatim}
  81. TMethodKind = (mkProcedure,mkFunction,mkConstructor,mkDestructor,
  82. mkClassProcedure, mkClassFunction);
  83. \end{verbatim}
  84. The kind of parameter to a method is determined by \var{TParamFlags}:
  85. \begin{verbatim}
  86. TParamFlags = set of (pfVar,pfConst,pfArray,pfAddress,pfReference,pfOut);
  87. \end{verbatim}
  88. Interfaces are described further with \var{TntfFlags}:
  89. \begin{verbatim}
  90. TIntfFlags = set of (ifHasGuid,ifDispInterface,ifDispatch);
  91. \end{verbatim}
  92. The following defines a set of \var{TTypeKind}:
  93. \begin{verbatim}
  94. TTypeKinds = set of TTypeKind;
  95. \end{verbatim}
  96. The \var{TypeInfo} function returns a pointer to a \var{TTypeInfo} record:
  97. \begin{verbatim}
  98. TTypeInfo = record
  99. Kind : TTypeKind;
  100. Name : ShortString;
  101. end;
  102. PTypeInfo = ^TTypeInfo;
  103. PPTypeInfo = ^PTypeInfo;
  104. \end{verbatim}
  105. Note that the Name is stored with as much bytes as needed to store the name,
  106. it is not padded to 255 characters.
  107. The type data immediatly follows the \var{TTypeInfo} record as a \var{TTypeData} record:
  108. \begin{verbatim}
  109. PTypeData = ^TTypeData;
  110. TTypeData = packed record
  111. case TTypeKind of
  112. tkUnKnown,tkLString,tkWString,tkAString,tkVariant:
  113. ();
  114. tkInteger,tkChar,tkEnumeration,tkWChar:
  115. (OrdType : TTOrdType;
  116. case TTypeKind of
  117. tkInteger,tkChar,tkEnumeration,tkBool,tkWChar : (
  118. MinValue,MaxValue : Longint;
  119. case TTypeKind of
  120. tkEnumeration: (
  121. BaseType : PTypeInfo;
  122. NameList : ShortString
  123. )
  124. );
  125. tkSet: (
  126. CompType : PTypeInfo
  127. )
  128. );
  129. tkFloat: (
  130. FloatType : TFloatType
  131. );
  132. tkSString:
  133. (MaxLength : Byte);
  134. tkClass:
  135. (ClassType : TClass;
  136. ParentInfo : PTypeInfo;
  137. PropCount : SmallInt;
  138. UnitName : ShortString
  139. );
  140. tkMethod:
  141. (MethodKind : TMethodKind;
  142. ParamCount : Byte;
  143. ParamList : array[0..1023] of Char
  144. {in reality ParamList is a array[1..ParamCount] of:
  145. record
  146. Flags : TParamFlags;
  147. ParamName : ShortString;
  148. TypeName : ShortString;
  149. end;
  150. followed by
  151. ResultType : ShortString}
  152. );
  153. tkInt64:
  154. (MinInt64Value, MaxInt64Value: Int64);
  155. tkQWord:
  156. (MinQWordValue, MaxQWordValue: QWord);
  157. tkInterface:
  158. ();
  159. end;
  160. \end{verbatim}
  161. If the typeinfo kind is \var{tkClass}, then the property
  162. information follows the \var{UnitName} string, as an array of \var{TPropInfo} records.
  163. The \var{TPropData} record is not used, but is provided for completeness and
  164. compatibility with Delphi.
  165. \begin{verbatim}
  166. TPropData = packed record
  167. PropCount : Word;
  168. PropList : record end;
  169. end;
  170. \end{verbatim}
  171. The \var{TPropInfo} record describes one published property of a class:
  172. \begin{verbatim}
  173. PPropInfo = ^TPropInfo;
  174. TPropInfo = packed record
  175. PropType : PTypeInfo;
  176. GetProc : Pointer;
  177. SetProc : Pointer;
  178. StoredProc : Pointer;
  179. Index : Integer;
  180. Default : Longint;
  181. NameIndex : SmallInt;
  182. PropProcs : Byte;
  183. Name : ShortString;
  184. end;
  185. \end{verbatim}
  186. The \var{Name} field is stored not with 255 characters, but with just as many characters
  187. as required to store the name.
  188. \begin{verbatim}
  189. TProcInfoProc = procedure(PropInfo : PPropInfo) of object;
  190. \end{verbatim}
  191. The following pointer and array types are used for typecasts:
  192. \begin{verbatim}
  193. PPropList = ^TPropList;
  194. TPropList = array[0..65535] of PPropInfo;
  195. \end{verbatim}
  196. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  197. % Functions and procedures by category
  198. \section{Function list by category}
  199. What follows is a listing of the available functions, grouped by category.
  200. For each function there is a reference to the page where the function
  201. can be found.
  202. \subsection{Examining published property information}
  203. Functions for retrieving or examining property information
  204. \begin{funclist}
  205. \funcref{FindPropInfo}{Getting property type information, With error checking.}
  206. \funcref{GetPropInfo}{Getting property type information, No error checking.}
  207. \funcref{GetPropInfos}{Find property information of a certain kind}
  208. \funcref{GetObjectPropClass}{Return the declared class of an object property }
  209. \funcref{GetPropList}{Get a list of all published properties}
  210. \funcref{IsPublishedProp}{Is a property published}
  211. \funcref{IsStoredProp}{Is a property stored}
  212. \funcref{PropIsType}{Is a property of a certain kind}
  213. \funcref{PropType}{Return the type of a property}
  214. \end{funclist}
  215. \subsection{Getting or setting property values}
  216. Functions to set or set a property's value.
  217. \begin{funclist}
  218. \funcref{GetEnumProp}{Return the value of an enumerated type property}
  219. \funcref{GetFloatProp}{Return the value of a float property}
  220. \funcref{GetInt64Prop}{Return the value of an Int64 property}
  221. \funcref{GetMethodProp}{Return the value of a procedural type property}
  222. \funcref{GetObjectProp}{Return the value of an object property}
  223. \funcref{GetOrdProp}{Return the value of an ordinal type property}
  224. \funcref{GetPropValue}{Return the value of a property as a variant}
  225. \funcref{GetSetProp}{Return the value of a set property}
  226. \funcref{GetStrProp}{Return the value of a string property}
  227. \funcref{GetVariantProp}{Return the value of a variant property}
  228. \funcref{SetEnumProp}{Set the value of an enumerated type property}
  229. \funcref{SetFloatProp}{Set the value of a float property}
  230. \funcref{SetInt64Prop}{Set the value of an Int64 property}
  231. \funcref{SetMethodProp}{Set the value of a procedural type property}
  232. \funcref{SetObjectProp}{Set the value of an object property}
  233. \funcref{SetOrdProp}{Set the value of an ordinal type property}
  234. \funcref{SetPropValue}{Set the value of a property trhough a variant}
  235. \funcref{SetSetProp}{Set the value of a set property}
  236. \funcref{SetStrProp}{Set the value of a string property}
  237. \funcref{SetVariantProp}{Set the value of a variant property}
  238. \end{funclist}
  239. \subsection{Auxiliary functions}
  240. \begin{funclist}
  241. \funcref{GetEnumName}{Get an enumerated type element name}
  242. \funcref{GetEnumValue}{Get ordinal number of an enumerated tye, based on the
  243. name.}
  244. \funcref{GetTypeData}{Skip type name and return a pointer to the type data}
  245. \funcref{SetToString}{Convert a set to its string representation}
  246. \funcref{StringToSet}{Convert a string representation of a set to a set}
  247. \end{funclist}
  248. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  249. % Functions and procedures
  250. \section{Functions and Procedures}
  251. \begin{function}{FindPropInfo}
  252. \Declaration
  253. Function FindPropInfo(AClass:TClass;const PropName: string): PPropInfo;\\
  254. Function FindPropInfo(Instance: TObject; const PropName: string): PPropInfo;
  255. \Description
  256. \var{FindPropInfo} examines the published property information of a class and
  257. returns a pointer to the property information for property \var{PropName}.
  258. The class to be examined can be specified in one of two ways:
  259. \begin{description}
  260. \item[AClass] a class pointer.
  261. \item[Instance] an instance of the class to be investigated.
  262. \end{description}
  263. If the property does not exist, a \var{EPropertyError} exception will be
  264. raised. The \seef{GetPropInfo} function has the same function as the
  265. \var{FindPropInfo} function, but returns \var{Nil} if the property does not
  266. exist.
  267. \Errors
  268. Specifying an invalid property name in \var{PropName} will result in an
  269. \var{EPropertyError} exception.
  270. \SeeAlso
  271. \seef{GetPropInfo}, \seef{GetPropList}, \seep{GetPropInfos}
  272. \end{function}
  273. \FPCexample{ex14}
  274. \begin{function}{GetEnumName}
  275. \Declaration
  276. Function GetEnumName(TypeInfo : PTypeInfo;Value : Integer) : string;
  277. \Description
  278. \var{GetEnumName} scans the type information for the enumeration type
  279. described by \var{TypeInfo} and returns the name of the enumeration
  280. constant for the element with ordinal value equal to \var{Value}.
  281. If \var{Value} is out of range, the first element of the enumeration type
  282. is returned. The result is lowercased, but this may change in the future.
  283. This can be used in combination with \var{GetOrdProp} to stream a property
  284. of an enumerated type.
  285. \Errors
  286. No check is done to determine whether \var{TypeInfo} really points to the
  287. type information for an enumerated type.
  288. \SeeAlso
  289. \seef{GetOrdProp}, \seef{GetEnumValue}
  290. \end{function}
  291. \FPCexample{ex9}
  292. \begin{function}{GetEnumProp}
  293. \Declaration
  294. Function GetEnumProp(Instance: TObject;const PropInfo: PPropInfo): string;\\
  295. Function GetEnumProp(Instance: TObject;const PropName: string): string;
  296. \Description
  297. \var{GetEnumProp} returns the value of an property of an enumerated type
  298. and returns the name of the enumerated value for the objetc \var{Instance}.
  299. The property whose value must be returned can be specified by its property
  300. info in \var{PropInfo} or by its name in \var{PropName}
  301. \Errors
  302. No check is done to determine whether \var{PropInfo} really points to the
  303. property information for an enumerated type.
  304. Specifying an invalid property name in \var{PropName} will result in an
  305. \var{EPropertyError} exception.
  306. \SeeAlso
  307. \seep{SetEnumProp} \seef{GetOrdProp}, \seef{GetStrProp},
  308. \seef{GetInt64Prop},\seef{GetMethodProp}, \seef{GetSetProp},
  309. \seef{GetObjectProp}, \seef{GetEnumProp}
  310. \end{function}
  311. \FPCexample{ex2}
  312. \begin{function}{GetEnumValue}
  313. \Declaration
  314. Function GetEnumValue(TypeInfo : PTypeInfo;const Name : string) : Integer;
  315. \Description
  316. \var{GetEnumValue} scans the type information for the enumeration type
  317. described by \var{TypeInfor} and returns the ordinal value for the element
  318. in the enumerated type that has identifier \var{Name}. The identifier is
  319. searched in a case-insensitive manner.
  320. This can be used to set the value of enumerated properties from a stream.
  321. \Errors
  322. If \var{Name} is not found in the list of enumerated values, then -1 is
  323. returned. No check is done whether \var{TypeInfo} points to the type information
  324. for an enumerated type.
  325. \SeeAlso
  326. \seef{GetEnumName}, \seep{SetOrdProp}
  327. \end{function}
  328. For an example, see \seef{GetEnumName}.
  329. \begin{function}{GetFloatProp}
  330. \Declaration
  331. Function GetFloatProp(Instance : TObject;PropInfo : PPropInfo) : Extended;\\
  332. Procedure SetFloatProp(Instance: TObject; const PropName: string; Value: Extended);
  333. \Description
  334. \var{GetFloatProp} returns the value of the float property described by
  335. \var{PropInfo} or with name \var{Propname} for the object \var{Instance}.
  336. All float types are converted
  337. to extended.
  338. \Errors
  339. No checking is done whether \var{Instance} is non-nil, or whether
  340. \var{PropInfo} describes a valid float property of \var{Instance}.
  341. Specifying an invalid property name in \var{PropName} will result in an
  342. \var{EPropertyError} exception.
  343. \SeeAlso
  344. \seep{SetFloatProp}, \seef{GetOrdProp}, \seef{GetStrProp},
  345. \seef{GetInt64Prop},\seef{GetMethodProp}, \seef{GetSetProp},
  346. \seef{GetObjectProp}, \seef{GetEnumProp}
  347. \end{function}
  348. \FPCexample{ex4}
  349. \begin{function}{GetInt64Prop}
  350. \Declaration
  351. Function GetInt64Prop(Instance: TObject; PropInfo: PPropInfo): Int64;\\
  352. Function GetInt64Prop(Instance: TObject; const PropName: string): Int64;
  353. \Description
  354. {\em Publishing of Int64 properties is not yet supported by \fpc. This
  355. function is provided for Delphi compatibility only at the moment.}
  356. \var{GetInt64Prop} returns the value of the property of type
  357. \var{Int64} that is described by \var{PropInfo} or with name \var{Propname}
  358. for the object \var{Instance}.
  359. \Errors
  360. No checking is done whether \var{Instance} is non-nil, or whether
  361. \var{PropInfo} describes a valid \var{Int64} property of \var{Instance}.
  362. Specifying an invalid property name in \var{PropName} will result in an
  363. \var{EPropertyError} exception
  364. \SeeAlso
  365. \seep{SetInt64Prop}, \seef{GetOrdProp}, \seef{GetStrProp},
  366. \seef{GetFloatProp}, \seef{GetMethodProp}, \seef{GetSetProp},
  367. \seef{GetObjectProp}, \seef{GetEnumProp}
  368. \end{function}
  369. \FPCexample{ex15}
  370. \begin{function}{GetMethodProp}
  371. \Declaration
  372. Function GetMethodProp(Instance : TObject;PropInfo : PPropInfo) : TMethod;\\
  373. Function GetMethodProp(Instance: TObject; const PropName: string): TMethod;
  374. \Description
  375. \var{GetMethodProp} returns the method the property described by
  376. \var{PropInfo} or with name \var{Propname} for object \var{Instance}.
  377. The return type \var{TMethod} is defined in the \file{SysUtils} unit as:
  378. \begin{verbatim}
  379. TMethod = packed record
  380. Code, Data: Pointer;
  381. end;
  382. \end{verbatim}
  383. \var{Data} points to the instance of the class with the method \var{Code}.
  384. \Errors
  385. No checking is done whether \var{Instance} is non-nil, or whether
  386. \var{PropInfo} describes a valid method property of \var{Instance}.
  387. Specifying an invalid property name in \var{PropName} will result in an
  388. \var{EPropertyError} exception
  389. \SeeAlso
  390. \seep{SetMethodProp}, \seef{GetOrdProp}, \seef{GetStrProp},
  391. \seef{GetFloatProp}, \seef{GetInt64Prop}, \seef{GetSetProp},
  392. \seef{GetObjectProp}, \seef{GetEnumProp}
  393. \end{function}
  394. \FPCexample{ex6}
  395. \begin{function}{GetObjectProp}
  396. \Declaration
  397. Function GetObjectProp(Instance: TObject; const PropName: string): TObject;\\
  398. Function GetObjectProp(Instance: TObject; const PropName: string; MinClass:TClass): TObject; \\
  399. Function GetObjectProp(Instance: TObject; PropInfo: PPropInfo; MinClass: TClass):
  400. TObject;\\
  401. \Description
  402. \var{GetObjectProp} returns the object which the property descroibed by
  403. \var{PropInfo} with name \var{Propname} points to for object \var{Instance}.
  404. If \var{MinClass} is specified, then if the object is not descendent of
  405. class \var{MinClass}, then \var{Nil} is returned.
  406. \Errors
  407. No checking is done whether \var{Instance} is non-nil, or whether
  408. \var{PropInfo} describes a valid method property of \var{Instance}.
  409. Specifying an invalid property name in \var{PropName} will result in an
  410. \var{EPropertyError} exception.
  411. \SeeAlso
  412. \seep{SetMethodProp}, \seef{GetOrdProp}, \seef{GetStrProp},
  413. \seef{GetFloatProp}, \seef{GetInt64Prop}, \seef{GetSetProp},
  414. \seef{GetObjectProp}, \seef{GetEnumProp}
  415. \end{function}
  416. \FPCexample{ex5}
  417. \begin{function}{GetObjectPropClass}
  418. \Declaration
  419. Function GetObjectPropClass(Instance: TObject; const PropName: string): TClass;
  420. \Description
  421. \var{GetObjectPropClass} returns the declared class of the property with name
  422. \var{PropName}. This may not be the actual class of the property value.
  423. \Errors
  424. No checking is done whether \var{Instance} is non-nil.
  425. Specifying an invalid property name in \var{PropName} will result in an
  426. \var{EPropertyError} exception.
  427. \SeeAlso
  428. \seep{SetMethodProp}, \seef{GetOrdProp}, \seef{GetStrProp},
  429. \seef{GetFloatProp}, \seef{GetInt64Prop}
  430. \end{function}
  431. For an example, see \seef{GetObjectProp}.
  432. \begin{function}{GetOrdProp}
  433. \Declaration
  434. Function GetOrdProp(Instance : TObject;PropInfo : PPropInfo) : Longint;\\
  435. Function GetOrdProp(Instance: TObject;const PropName: string): Longint;
  436. \Description
  437. \var{GetOrdProp} returns the value of the ordinal property described by
  438. \var{PropInfo} or with name \var{PropName} for the object \var{Instance}.
  439. The value is returned as a longint, which should be typecasted to the
  440. needed type.
  441. Ordinal properties that can be retrieved include:
  442. \begin{description}
  443. \item[Integers and subranges of integers] The value of the integer will be
  444. returned.
  445. \item[Enumerated types and subranges of enumerated types] The ordinal value
  446. of the enumerated type will be returned.
  447. \item[Sets] If the base type of the set has less than 31 possible values.
  448. If a bit is set in the return value, then the corresponding element of the
  449. base ordinal class of the set type must be included in the set.
  450. \end{description}
  451. \Errors
  452. No checking is done whether \var{Instance} is non-nil, or whether
  453. \var{PropInfo} describes a valid ordinal property of \var{Instance}
  454. Specifying an invalid property name in \var{PropName} will result in an
  455. \var{EPropertyError} exception.
  456. \SeeAlso
  457. \seep{SetOrdProp}, \seef{GetStrProp}, \seef{GetFloatProp},
  458. \seef{GetInt64Prop},\seef{GetMethodProp}, \seef{GetSetProp},
  459. \seef{GetObjectProp}, \seef{GetEnumProp}
  460. \end{function}
  461. \FPCexample{ex1}
  462. \begin{function}{GetPropInfo}
  463. \Declaration
  464. Function GetPropInfo(AClass: TClass; const PropName: string; AKinds: TTypeKinds) : PPropInfo;\\
  465. Function GetPropInfo(AClass: TClass; const PropName: string): PPropInfo;\\
  466. Function GetPropInfo(Instance: TObject; const PropName: string): PPropInfo;\\
  467. Function GetPropInfo(Instance: TObject; const PropName: string; AKinds: TTypeKinds) : PPropInfo;\\
  468. Function GetPropInfo(TypeInfo: PTypeInfo;const PropName: string) : PPropInfo;\\
  469. Function GetPropInfo(TypeInfo: PTypeInfo;const PropName: string; AKinds : TTypeKinds) : PPropInfo;
  470. \Description
  471. \var{GetPropInfo} returns a pointer to the \var{TPropInfo} record for a the
  472. \var{PropName} property of a class. The class to examine can be specified
  473. in one of three ways:
  474. \begin{description}
  475. \item[Instance] An instance of the class.
  476. \item[AClass] A class pointer to the class.
  477. \item[TypeInfo] A pointer to the type information of the class.
  478. \end{description}
  479. In each of these three ways, if \var{AKinds} is specified, if the property
  480. has \var{TypeKind} which is not included in \var{Akinds}, \var{Nil} will be
  481. returned.
  482. \Errors
  483. If the property \var{PropName} does not exist, \var{Nil} is returned.
  484. \SeeAlso
  485. \seep{GetPropInfos},\seef{GetPropList}
  486. \end{function}
  487. For an example, see most of the other functions.
  488. \begin{procedure}{GetPropInfos}
  489. \Declaration
  490. Procedure GetPropInfos(TypeInfo: PTypeInfo;PropList: PPropList);
  491. \Description
  492. \var{GetPropInfos} stores pointers to the property information of all published
  493. properties of a class with class info \var{TypeInfo} in the list pointed to by
  494. \var{Proplist}. The \var{PropList} pointer must point to a memory location that
  495. contains enough space to hold all properties of the class and its parent classes.
  496. \Errors
  497. No checks are done to see whether \var{PropList} points to a memory area that
  498. is big enough to hold all pointers.
  499. \SeeAlso
  500. \seef{GetPropInfo},\seef{GetPropList}
  501. \end{procedure}
  502. \FPCexample{ex12}
  503. \begin{function}{GetPropList}
  504. \Declaration
  505. Function GetPropList(TypeInfo : PTypeInfo;
  506. TypeKinds : TTypeKinds;
  507. PropList : PPropList) : Integer;
  508. \Description
  509. \var{GetPropList} stores pointers to property information of the class with class
  510. info \var{TypeInfo} for properties of kind \var{TypeKinds} in the list pointed to
  511. by \var{Proplist}. \var{PropList} must contain enough space to hold all properties.
  512. The function returns the number of pointers that matched the criteria and were stored
  513. in \var{PropList}.
  514. \Errors
  515. No checks are done to see whether \var{PropList} points to a memory area that is big enough
  516. to hold all pointers.
  517. \SeeAlso
  518. \seep{GetPropInfos}, \seef{GetPropInfo}
  519. \end{function}
  520. \FPCexample{ex13}
  521. \begin{function}{GetPropValue}
  522. \Declaration
  523. Function GetPropValue(Instance: TObject; const PropName: string): Variant;\\
  524. Function GetPropValue(Instance: TObject; const PropName: string; PreferStrings: Boolean): Variant;
  525. \Description
  526. Due to missing \var{Variant} support, \var{GetPropValue} is not yet implemented.
  527. The declaration is provided for compatibility with Delphi.
  528. \Errors
  529. \SeeAlso
  530. \end{function}
  531. \begin{function}{GetSetProp}
  532. \Declaration
  533. Function GetSetProp(Instance: TObject; const PropInfo: PPropInfo; Brackets: Boolean):
  534. string;\\
  535. Function GetSetProp(Instance: TObject; const PropName: string): string;\\
  536. Function GetSetProp(Instance: TObject; const PropName: string; Brackets: Boolean): string;
  537. \Description
  538. \var{GetSetProp} returns the contents of a set property as a string.
  539. The property to be returned can be specified by it's name in \var{PropName}
  540. or by its property information in \var{PropInfo}.
  541. The returned set is a string representation of the elements in the set as
  542. returned by \seef{SetToString}. The \var{Brackets} option can be used to
  543. enclose the string representation in square brackets.
  544. \Errors
  545. No checking is done whether \var{Instance} is non-nil, or whether
  546. \var{PropInfo} describes a valid ordinal property of \var{Instance}
  547. Specifying an invalid property name in \var{PropName} will result in an
  548. \var{EPropertyError} exception.
  549. \SeeAlso
  550. \seep{SetSetProp}, \seef{GetStrProp}, \seef{GetFloatProp},
  551. \seef{GetInt64Prop},\seef{GetMethodProp}
  552. \end{function}
  553. \FPCexample{ex7}
  554. \begin{function}{GetStrProp}
  555. \Declaration
  556. Function GetStrProp(Instance : TObject;
  557. PropInfo : PPropInfo) : Ansistring;\\
  558. Function GetStrProp(Instance: TObject;
  559. const PropName: string): string;
  560. \Description
  561. \var{GetStrProp} returns the value of the string property described by
  562. \var{PropInfo} or with name \var{PropName} for object \var{Instance}.
  563. \Errors
  564. No checking is done whether \var{Instance} is non-nil, or whether
  565. \var{PropInfo} describes a valid string property of \var{Instance}.
  566. Specifying an invalid property name in \var{PropName} will result in an
  567. \var{EPropertyError} exception.
  568. \SeeAlso
  569. \seep{SetStrProp}, \seef{GetOrdProp}, \seef{GetFloatProp},
  570. \seef{GetInt64Prop},\seef{GetMethodProp}
  571. \end{function}
  572. \FPCexample{ex3}
  573. \begin{function}{GetTypeData}
  574. \Declaration
  575. Function GetTypeData(TypeInfo : PTypeInfo) : PTypeData;
  576. \Description
  577. \var{GetTypeData} returns a pointer to the \var{TTypeData} record that
  578. follows after the \var{TTypeInfo} record pointed to by \var{TypeInfo}.
  579. It essentially skips the \var{Kind} and \var{Name} fields in the
  580. \var{TTypeInfo} record.
  581. \Errors
  582. None.
  583. \SeeAlso
  584. \end{function}
  585. \begin{function}{GetVariantProp}
  586. \Declaration
  587. Function GetVariantProp(Instance : TObject;PropInfo : PPropInfo): Variant;
  588. \Description
  589. Due to mising Variant support, the \var{GetVariantProp} function is not
  590. yet implemented. Provided for Delphi compatibility only.
  591. \Errors
  592. \SeeAlso
  593. \seep{SetVariantProp}
  594. \end{function}
  595. \begin{function}{IsPublishedProp}
  596. \Declaration
  597. Function IsPublishedProp(AClass: TClass; const PropName: string): Boolean;\\
  598. Function IsPublishedProp(Instance: TObject; const PropName: string): Boolean;
  599. \Description
  600. \var{IsPublishedProp} returns true if a class has a published property with
  601. name \var{PropName}. The class can be specfied in one of two ways:
  602. \begin{description}
  603. \item[AClass] A class pointer to the class.
  604. \item[Instance] An instance of the class.
  605. \end{description}
  606. \Errors
  607. No checks are done to ensure \var{Instance} or \var{AClass} are valid
  608. pointers. Specifying an invalid property name in \var{PropName} will result
  609. in an \var{EPropertyError} exception.
  610. \SeeAlso
  611. \seef{IsStoredProp}, \seef{PropIsType}
  612. \end{function}
  613. \FPCexample{ex10}
  614. \begin{function}{IsStoredProp}
  615. \Declaration
  616. Function IsStoredProp(Instance : TObject;PropInfo : PPropInfo) : Boolean;\\
  617. Function IsStoredProp(Instance: TObject; const PropName: string): Boolean;
  618. \Description
  619. \var{IsStoredProp} returns \var{True} if the \var{Stored} modifier evaluates
  620. to \var{True} for the property described by \var{PropInfo} or with name
  621. \var{PropName} for object \var{Instance}.
  622. It returns \var{False} otherwise. If the function returns
  623. \var{True}, this indicates that the property should be written when
  624. streaming the object \var{Instance}.
  625. If there was no \var{stored} modifier in the declaration of the property,
  626. \var{True} will be returned.
  627. \Errors
  628. No checking is done whether \var{Instance} is non-nil, or whether
  629. \var{PropInfo} describes a valid property of \var{Instance}.
  630. Specifying an invalid property name in \var{PropName} will result in an
  631. \var{EPropertyError} exception.
  632. \SeeAlso
  633. \seef{IsPublishedProp}, \seef{PropIsType}
  634. \end{function}
  635. \FPCexample{ex11}
  636. \begin{function}{PropIsType}
  637. \Declaration
  638. Function PropIsType(AClass: TClass;
  639. const PropName: string; TypeKind: TTypeKind): Boolean;\\
  640. Function PropIsType(Instance: TObject;
  641. const PropName: string; TypeKind: TTypeKind): Boolean;
  642. \Description
  643. \var{PropIsType} returns \var{True} if the property with name \var{PropName}
  644. has type \var{TypeKind}. It returns \var{False} otherwise. The class to be
  645. examined can be specified in one of two ways:
  646. \begin{description}
  647. \item[AClass] A class pointer.
  648. \item[Instance] An instance of the class.
  649. \end{description}
  650. \Errors
  651. No checks are done to ensure \var{Instance} or \var{AClass} are valid
  652. pointers.Specifying an invalid property name in \var{PropName} will result
  653. in an \var{EPropertyError} exception.
  654. \SeeAlso
  655. \seef{IsPublishedProp}, \seef{IsStoredProp}, \seef{PropType}
  656. \end{function}
  657. \FPCexample{ex16}
  658. \begin{function}{PropType}
  659. \Declaration
  660. Function PropType(AClass: TClass; const PropName: string): TTypeKind;\\
  661. Function PropType(Instance: TObject; const PropName: string): TTypeKind;
  662. \Description
  663. \var{Proptype} returns the type of the property \var{PropName} for a class.
  664. The class to be examined can be specified in one of 2 ways:
  665. \begin{description}
  666. \item[AClass] A class pointer.
  667. \item[Instance] An instance of the class.
  668. \end{description}
  669. \Errors
  670. No checks are done to ensure \var{Instance} or \var{AClass} are valid
  671. pointers. Specifying an invalid property name in \var{PropName} will result
  672. in an \var{EPropertyError} exception.
  673. \SeeAlso
  674. \seef{IsPublishedProp}, \seef{IsStoredProp}, \seef{PropIsType}
  675. \end{function}
  676. \FPCexample{ex17}
  677. \begin{procedure}{SetEnumProp}
  678. \Declaration
  679. Procedure SetEnumProp(Instance: TObject; const PropInfo: PPropInfo;
  680. const Value: string);\\
  681. Procedure SetEnumProp(Instance: TObject; const PropName: string;
  682. const Value: string);
  683. \Description
  684. \var{SetEnumProp} sets the property described by \var{PropInfo} or with name
  685. \var{PropName} to \var{Value}. \var{Value} must be a string with the name
  686. of the enumerate value, i.e. it can be used as an argument to
  687. \seef{GetEnumValue}.
  688. \Errors
  689. No checks are done to ensure \var{Instance} or \var{PropInfo} are valid
  690. pointers. Specifying an invalid property name in \var{PropName} will result
  691. in an \var{EPropertyError} exception.
  692. \SeeAlso
  693. \seef{GetEnumProp}, \seep{SetStrProp}, \seep{SetFloatProp},
  694. \seep{SetInt64Prop},\seep{SetMethodProp}.
  695. \end{procedure}
  696. For an example, see \seef{GetEnumProp}.
  697. \begin{procedure}{SetFloatProp}
  698. \Declaration
  699. Procedure SetFloatProp(Instance : TObject;
  700. PropInfo : PPropInfo;
  701. Value : Extended);\\
  702. Procedure SetFloatProp(Instance: TObject;
  703. const PropName: string;
  704. Value: Extended);
  705. \Description
  706. \var{SetFloatProp} assigns \var{Value} to the property described by
  707. \var{PropInfo} or with name \var{Propname} for the object \var{Instance}.
  708. \Errors
  709. No checking is done whether \var{Instance} is non-nil, or whether
  710. \var{PropInfo} describes a valid float property of \var{Instance}.
  711. Specifying an invalid property name in \var{PropName} will result in an
  712. \var{EPropertyError} exception.
  713. \SeeAlso
  714. \seef{GetFloatProp}, \seep{SetOrdProp}, \seep{SetStrProp},
  715. \seep{SetInt64Prop},\seep{SetMethodProp}
  716. \end{procedure}
  717. For an example, see \seef{GetFloatProp}.
  718. \begin{procedure}{SetInt64Prop}
  719. \Declaration
  720. Procedure SetInt64Prop(Instance: TObject; PropInfo: PPropInfo; const Value: Int64);\\
  721. Procedure SetInt64Prop(Instance: TObject; const PropName: string; const Value: Int64);
  722. \Description
  723. \var{SetInt64Prop} assigns \var{Value} to the property of type
  724. \var{Int64} that is described by \var{PropInfo} or with name \var{Propname}
  725. for the object \var{Instance}.
  726. \Errors
  727. No checking is done whether \var{Instance} is non-nil, or whether
  728. \var{PropInfo} describes a valid \var{Int64} property of \var{Instance}.
  729. Specifying an invalid property name in \var{PropName} will result in an
  730. \var{EPropertyError} exception.
  731. \SeeAlso
  732. \seef{GetInt64Prop}, \seef{GetMethodProp}, \seep{SetOrdProp}, \seep{SetStrProp},
  733. \seep{SetFloatProp}
  734. \end{procedure}
  735. For an example, see \seef{GetInt64Prop}.
  736. \begin{procedure}{SetMethodProp}
  737. \Declaration
  738. Procedure SetMethodProp(Instance : TObject;PropInfo : PPropInfo; const Value :
  739. TMethod);\\
  740. Procedure SetMethodProp(Instance: TObject; const PropName: string; const Value: TMethod);
  741. \Description
  742. \var{SetMethodProp} assigns \var{Value} to the method the property described
  743. by \var{PropInfo} or with name \var{Propname} for object \var{Instance}.
  744. The type \var{TMethod} of the \var{Value} parameter is defined in the
  745. \file{SysUtils} unit as:
  746. \begin{verbatim}
  747. TMethod = packed record
  748. Code, Data: Pointer;
  749. end;
  750. \end{verbatim}
  751. \var{Data} should point to the instance of the class with the method \var{Code}.
  752. \Errors
  753. No checking is done whether \var{Instance} is non-nil, or whether
  754. \var{PropInfo} describes a valid method property of \var{Instance}.
  755. Specifying an invalid property name in \var{PropName} will result in an
  756. \var{EPropertyError} exception.
  757. \SeeAlso
  758. \seef{GetMethodProp}, \seep{SetOrdProp}, \seep{SetStrProp},
  759. \seep{SetFloatProp}, \seep{SetInt64Prop}
  760. \end{procedure}
  761. For an example, see \seef{GetMethodProp}.
  762. \begin{procedure}{SetObjectProp}
  763. \Declaration
  764. Procedure SetObjectProp(Instance: TObject;
  765. PropInfo: PPropInfo; Value: TObject);\\
  766. Procedure SetObjectProp(Instance: TObject;
  767. const PropName: string; Value: TObject);
  768. \Description
  769. \var{SetObjectProp} assigns \var{Value} to the the object property described by
  770. \var{PropInfo} or with name \var{Propname} for the object \var{Instance}.
  771. \Errors
  772. No checking is done whether \var{Instance} is non-nil, or whether
  773. \var{PropInfo} describes a valid method property of \var{Instance}.
  774. Specifying an invalid property name in \var{PropName} will result in an
  775. \var{EPropertyError} exception.
  776. \SeeAlso
  777. \seef{GetObjectProp}, \seep{SetOrdProp}, \seep{SetStrProp},
  778. \seep{SetFloatProp}, \seep{SetInt64Prop}, \seep{SetMethodProp}
  779. \end{procedure}
  780. For an example, see \seef{GetObjectProp}.
  781. \begin{procedure}{SetOrdProp}
  782. \Declaration
  783. Procedure SetOrdProp(Instance : TObject; PropInfo : PPropInfo;
  784. Value : Longint);\\
  785. Procedure SetOrdProp(Instance: TObject; const PropName: string;
  786. Value: Longint);
  787. \Description
  788. \var{SetOrdProp} assigns \var{Value} to the the ordinal property described by
  789. \var{PropInfo} or with name \var{Propname} for the object \var{Instance}.
  790. Ordinal properties that can be set include:
  791. \begin{description}
  792. \item[Integers and subranges of integers] The actual value of the integer must be
  793. passed.
  794. \item[Enumerated types and subranges of enumerated types] The ordinal value
  795. of the enumerated type must be passed.
  796. \item[Subrange types] of integers or enumerated types. Here the ordinal
  797. value must be passed.
  798. \item[Sets] If the base type of the set has less than 31 possible values.
  799. For each possible value; the corresponding bit of \var{Value} must be set.
  800. \end{description}
  801. \Errors
  802. No checking is done whether \var{Instance} is non-nil, or whether
  803. \var{PropInfo} describes a valid ordinal property of \var{Instance}.
  804. No range checking is performed.
  805. Specifying an invalid property name in \var{PropName} will result in an
  806. \var{EPropertyError} exception.
  807. \SeeAlso
  808. \seef{GetOrdProp}, \seep{SetStrProp}, \seep{SetFloatProp},
  809. \seep{SetInt64Prop},\seep{SetMethodProp}
  810. \end{procedure}
  811. For an example, see \seef{GetOrdProp}.
  812. \begin{procedure}{SetPropValue}
  813. \Declaration
  814. Procedure SetPropValue(Instance: TObject;
  815. const PropName: string; const Value: Variant);
  816. \Description
  817. Due to missing Variant support, this function is not yet implemented;
  818. it is provided for Delphi compatibility only.
  819. \Errors
  820. \SeeAlso
  821. \end{procedure}
  822. \begin{procedure}{SetSetProp}
  823. \Declaration
  824. Procedure SetSetProp(Instance: TObject;
  825. const PropInfo: PPropInfo; const Value: string);\\
  826. Procedure SetSetProp(Instance: TObject;
  827. const PropName: string; const Value: string);
  828. \Description
  829. \var{SetSetProp} sets the property specified by \var{PropInfo} or
  830. \var{PropName} for object \var{Instance} to \var{Value}. \var{Value} is a
  831. string which contains a comma-separated list of values, each value being a
  832. string-representation of the enumerated value that should be included in
  833. the set. The value should be accepted by the \seef{StringToSet} function.
  834. The value can be formed using the \seef{SetToString} function.
  835. \Errors
  836. No checking is done whether \var{Instance} is non-nil, or whether
  837. \var{PropInfo} describes a valid ordinal property of \var{Instance}.
  838. No range checking is performed.
  839. Specifying an invalid property name in \var{PropName} will result in an
  840. \var{EPropertyError} exception.
  841. \SeeAlso
  842. \seef{GetSetProp}, \seep{SetOrdProp}, \seep{SetStrProp}, \seep{SetFloatProp},
  843. \seep{SetInt64Prop},\seep{SetMethodProp}, \seef{SetToString},
  844. \seef{StringToSet}
  845. \end{procedure}
  846. For an example, see \seef{GetSetProp}.
  847. \begin{procedure}{SetStrProp}
  848. \Declaration
  849. procedure SetStrProp(Instance : TObject; PropInfo : PPropInfo;
  850. const Value : Ansistring);\\
  851. Procedure SetStrProp(Instance: TObject; const PropName: string;
  852. const Value: AnsiString);
  853. \Description
  854. \var{SetStrProp} assigns \var{Value} to the string property described by
  855. \var{PropInfo} or with name \var{Propname} for object \var{Instance}.
  856. \Errors
  857. No checking is done whether \var{Instance} is non-nil, or whether
  858. \var{PropInfo} describes a valid string property of \var{Instance}.
  859. Specifying an invalid property name in \var{PropName} will result in an
  860. \var{EPropertyError} exception.
  861. \SeeAlso
  862. \seef{GetStrProp}, \seep{SetOrdProp}, \seep{SetFloatProp},
  863. \seep{SetInt64Prop},\seep{SetMethodProp}
  864. \end{procedure}
  865. For an example, see \seef{GetStrProp}
  866. \begin{function}{SetToString}
  867. \Declaration
  868. function SetToString(PropInfo: PPropInfo;
  869. Value: Integer) : String;\\
  870. function SetToString(PropInfo: PPropInfo;
  871. Value: Integer; Brackets: Boolean) : String;
  872. \Description
  873. \var{SetToString} takes an integer representation of a set (as received e.g.
  874. by \var{GetOrdProp}) and turns it into a string representing the elements in
  875. the set, based on the type information found in the \var{PropInfo} property
  876. information. By default, the string representation is not surrounded by
  877. square brackets. Setting the \var{Brackets} parameter to \var{True} will
  878. surround the string representation with brackets.
  879. The function returns the string representation of the set.
  880. \Errors
  881. No checking is done to see whether \var{PropInfo} points to valid property
  882. information.
  883. \SeeAlso
  884. \seef{GetEnumName}, \seef{GetEnumValue}, \seef{StringToSet}
  885. \end{function}
  886. \FPCexample{ex18}
  887. \begin{procedure}{SetVariantProp}
  888. \Declaration
  889. Procedure SetVariantProp(Instance : TObject;
  890. PropInfo : PPropInfo;
  891. Const Value: Variant);\\
  892. Procedure SetVariantProp(Instance: TObject;
  893. const PropName: string;
  894. const Value: Variant);
  895. \Description
  896. Due to missing Variant support, this function is not yet implemented.
  897. Provided for Delphi compatibility only.
  898. \Errors
  899. \SeeAlso
  900. \end{procedure}
  901. \begin{function}{StringToSet}
  902. \Declaration
  903. function StringToSet(PropInfo: PPropInfo; const Value: string): Integer;
  904. \Description
  905. \var{StringToSet} converts the string representation of a set in \var{Value}
  906. to a integer representation of the set, using the property information found
  907. in \var{PropInfo}. This property information should point to the property
  908. information of a set property. The function returns the integer
  909. representation of the set. (i.e, the set value, typecast to an integer)
  910. The string representation can be surrounded with square brackets, and must
  911. consist of the names of the elements of the base type of the set. The base
  912. type of the set should be an enumerated type. The elements should be
  913. separated by commas, and may be surrounded by spaces.
  914. each of the names will be fed to the \seef{GetEnumValue} function.
  915. \Errors
  916. No checking is done to see whether \var{PropInfo} points to valid property
  917. information. If a wrong name is given for an enumerated value, then an
  918. \var{EPropertyError} will be raised.
  919. \SeeAlso
  920. \seef{GetEnumName}, \seef{GetEnumValue}, \seef{SetToString}
  921. \end{function}
  922. For an example, see \seef{SetToString}.