typinfo.pp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Florian Klaempfl
  4. member of the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. { This unit provides the same Functionality as the TypInfo Unit }
  12. { of Delphi }
  13. unit typinfo;
  14. interface
  15. {$MODE objfpc}
  16. {$inline on}
  17. {$h+}
  18. uses SysUtils;
  19. // temporary types:
  20. type
  21. {$MINENUMSIZE 1 this saves a lot of memory }
  22. {$ifdef FPC_RTTI_PACKSET1}
  23. { for Delphi compatibility }
  24. {$packset 1}
  25. {$endif}
  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,tkFloat,
  29. tkSet,tkMethod,tkSString,tkLString,tkAString,
  30. tkWString,tkVariant,tkArray,tkRecord,tkInterface,
  31. tkClass,tkObject,tkWChar,tkBool,tkInt64,tkQWord,
  32. tkDynArray,tkInterfaceRaw,tkProcVar,tkUString,tkUChar,
  33. tkHelper);
  34. TOrdType = (otSByte,otUByte,otSWord,otUWord,otSLong,otULong);
  35. {$ifndef FPUNONE}
  36. TFloatType = (ftSingle,ftDouble,ftExtended,ftComp,ftCurr);
  37. {$endif}
  38. TMethodKind = (mkProcedure,mkFunction,mkConstructor,mkDestructor,
  39. mkClassProcedure,mkClassFunction,mkClassConstructor,
  40. mkClassDestructor,mkOperatorOverload);
  41. TParamFlag = (pfVar,pfConst,pfArray,pfAddress,pfReference,pfOut);
  42. TParamFlags = set of TParamFlag;
  43. TIntfFlag = (ifHasGuid,ifDispInterface,ifDispatch,ifHasStrGUID);
  44. TIntfFlags = set of TIntfFlag;
  45. TIntfFlagsBase = set of TIntfFlag;
  46. // don't rely on integer values of TCallConv since it includes all conventions
  47. // which both delphi and fpc support. In the future delphi can support more and
  48. // fpc own conventions will be shifted/reordered accordinly
  49. TCallConv = (ccReg, ccCdecl, ccPascal, ccStdCall, ccSafeCall,
  50. ccCppdecl, ccFar16, ccOldFPCCall, ccInternProc,
  51. ccSysCall, ccSoftFloat, ccMWPascal);
  52. {$MINENUMSIZE DEFAULT}
  53. const
  54. ptField = 0;
  55. ptStatic = 1;
  56. ptVirtual = 2;
  57. ptConst = 3;
  58. tkString = tkSString;
  59. type
  60. TTypeKinds = set of TTypeKind;
  61. ShortStringBase = string[255];
  62. PVmtFieldEntry = ^TVmtFieldEntry;
  63. TVmtFieldEntry =
  64. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  65. packed
  66. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  67. record
  68. FieldOffset: PtrUInt;
  69. TypeIndex: Word;
  70. Name: ShortString;
  71. end;
  72. PVmtFieldTable = ^TVmtFieldTable;
  73. TVmtFieldTable =
  74. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  75. packed
  76. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  77. record
  78. Count: Word;
  79. ClassTab: Pointer;
  80. { should be array[Word] of TFieldInfo; but
  81. Elements have variant size! force at least proper alignment }
  82. Fields: array[0..0] of TVmtFieldEntry
  83. end;
  84. {$PACKRECORDS 1}
  85. TTypeInfo = record
  86. Kind : TTypeKind;
  87. Name : ShortString;
  88. // here the type data follows as TTypeData record
  89. end;
  90. PTypeInfo = ^TTypeInfo;
  91. PPTypeInfo = ^PTypeInfo;
  92. {$PACKRECORDS C}
  93. PTypeData = ^TTypeData;
  94. TTypeData =
  95. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  96. packed
  97. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  98. record
  99. case TTypeKind of
  100. tkUnKnown,tkLString,tkWString,tkAString,tkVariant,tkUString:
  101. ();
  102. tkInteger,tkChar,tkEnumeration,tkWChar,tkSet:
  103. (OrdType : TOrdType;
  104. case TTypeKind of
  105. tkInteger,tkChar,tkEnumeration,tkBool,tkWChar : (
  106. MinValue,MaxValue : Longint;
  107. case TTypeKind of
  108. tkEnumeration:
  109. (
  110. BaseType : PTypeInfo;
  111. NameList : ShortString;
  112. {EnumUnitName: ShortString;})
  113. );
  114. tkSet:
  115. (CompType : PTypeInfo)
  116. );
  117. {$ifndef FPUNONE}
  118. tkFloat:
  119. (FloatType : TFloatType);
  120. {$endif}
  121. tkSString:
  122. (MaxLength : Byte);
  123. tkClass:
  124. (ClassType : TClass;
  125. ParentInfo : PTypeInfo;
  126. PropCount : SmallInt;
  127. UnitName : ShortString
  128. // here the properties follow as array of TPropInfo
  129. );
  130. tkHelper:
  131. (HelperParent : PTypeInfo;
  132. ExtendedInfo : PTypeInfo;
  133. HelperProps : SmallInt;
  134. HelperUnit : ShortString
  135. // here the properties follow as array of TPropInfo
  136. );
  137. tkMethod:
  138. (MethodKind : TMethodKind;
  139. ParamCount : Byte;
  140. ParamList : array[0..1023] of Char
  141. {in reality ParamList is a array[1..ParamCount] of:
  142. record
  143. Flags : TParamFlags;
  144. ParamName : ShortString;
  145. TypeName : ShortString;
  146. end;
  147. followed by
  148. ResultType : ShortString // for mkFunction, mkClassFunction only
  149. ResultTypeRef : PPTypeInfo; // for mkFunction, mkClassFunction only
  150. CC : TCallConv;
  151. ParamTypeRefs : array[1..ParamCount] of PPTypeInfo;}
  152. );
  153. tkInt64:
  154. (MinInt64Value, MaxInt64Value: Int64);
  155. tkQWord:
  156. (MinQWordValue, MaxQWordValue: QWord);
  157. tkInterface:
  158. (
  159. IntfParent: PTypeInfo;
  160. IntfFlags : TIntfFlagsBase;
  161. GUID: TGUID;
  162. IntfUnit: ShortString;
  163. );
  164. tkInterfaceRaw:
  165. (
  166. RawIntfParent: PTypeInfo;
  167. RawIntfFlags : TIntfFlagsBase;
  168. IID: TGUID;
  169. RawIntfUnit: ShortString;
  170. IIDStr: ShortString;
  171. );
  172. tkDynArray:
  173. (
  174. elSize : PtrUInt;
  175. elType2 : PPTypeInfo;
  176. varType : Longint;
  177. elType : PPTypeInfo;
  178. DynUnitName: ShortStringBase
  179. );
  180. end;
  181. // unsed, just for completeness
  182. TPropData =
  183. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  184. packed
  185. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  186. record
  187. PropCount : Word;
  188. PropList : record _alignmentdummy : ptrint; end;
  189. end;
  190. {$PACKRECORDS 1}
  191. PPropInfo = ^TPropInfo;
  192. TPropInfo = packed record
  193. PropType : PTypeInfo;
  194. GetProc : Pointer;
  195. SetProc : Pointer;
  196. StoredProc : Pointer;
  197. Index : Integer;
  198. Default : Longint;
  199. NameIndex : SmallInt;
  200. // contains the type of the Get/Set/Storedproc, see also ptxxx
  201. // bit 0..1 GetProc
  202. // 2..3 SetProc
  203. // 4..5 StoredProc
  204. // 6 : true, constant index property
  205. PropProcs : Byte;
  206. Name : ShortString;
  207. end;
  208. TProcInfoProc = Procedure(PropInfo : PPropInfo) of object;
  209. PPropList = ^TPropList;
  210. TPropList = array[0..65535] of PPropInfo;
  211. const
  212. tkAny = [Low(TTypeKind)..High(TTypeKind)];
  213. tkMethods = [tkMethod];
  214. tkProperties = tkAny-tkMethods-[tkUnknown];
  215. // general property handling
  216. Function GetTypeData(TypeInfo : PTypeInfo) : PTypeData;
  217. Function GetPropInfo(TypeInfo: PTypeInfo;const PropName: string): PPropInfo;
  218. Function GetPropInfo(TypeInfo: PTypeInfo;const PropName: string; AKinds: TTypeKinds): PPropInfo;
  219. Function GetPropInfo(Instance: TObject; const PropName: string): PPropInfo;
  220. Function GetPropInfo(Instance: TObject; const PropName: string; AKinds: TTypeKinds): PPropInfo;
  221. Function GetPropInfo(AClass: TClass; const PropName: string): PPropInfo;
  222. Function GetPropInfo(AClass: TClass; const PropName: string; AKinds: TTypeKinds): PPropInfo;
  223. Function FindPropInfo(Instance: TObject; const PropName: string): PPropInfo;
  224. Function FindPropInfo(Instance: TObject; const PropName: string; AKinds: TTypeKinds): PPropInfo;
  225. Function FindPropInfo(AClass: TClass; const PropName: string): PPropInfo;
  226. Function FindPropInfo(AClass: TClass; const PropName: string; AKinds: TTypeKinds): PPropInfo;
  227. Procedure GetPropInfos(TypeInfo: PTypeInfo; PropList: PPropList);
  228. Function GetPropList(TypeInfo: PTypeInfo; TypeKinds: TTypeKinds; PropList: PPropList; Sorted: boolean = true): longint;
  229. Function GetPropList(TypeInfo: PTypeInfo; out PropList: PPropList): SizeInt;
  230. function GetPropList(AClass: TClass; out PropList: PPropList): Integer;
  231. function GetPropList(Instance: TObject; out PropList: PPropList): Integer;
  232. // Property information routines.
  233. Function IsStoredProp(Instance: TObject;PropInfo : PPropInfo) : Boolean;
  234. Function IsStoredProp(Instance: TObject; const PropName: string): Boolean;
  235. Function IsPublishedProp(Instance: TObject; const PropName: string): Boolean;
  236. Function IsPublishedProp(AClass: TClass; const PropName: string): Boolean;
  237. Function PropType(Instance: TObject; const PropName: string): TTypeKind;
  238. Function PropType(AClass: TClass; const PropName: string): TTypeKind;
  239. Function PropIsType(Instance: TObject; const PropName: string; TypeKind: TTypeKind): Boolean;
  240. Function PropIsType(AClass: TClass; const PropName: string; TypeKind: TTypeKind): Boolean;
  241. // subroutines to read/write properties
  242. Function GetOrdProp(Instance: TObject; PropInfo : PPropInfo) : Int64;
  243. Function GetOrdProp(Instance: TObject; const PropName: string): Int64;
  244. Procedure SetOrdProp(Instance: TObject; PropInfo : PPropInfo; Value : Int64);
  245. Procedure SetOrdProp(Instance: TObject; const PropName: string; Value: Int64);
  246. Function GetEnumProp(Instance: TObject; const PropName: string): string;
  247. Function GetEnumProp(Instance: TObject; const PropInfo: PPropInfo): string;
  248. Procedure SetEnumProp(Instance: TObject; const PropName: string;const Value: string);
  249. Procedure SetEnumProp(Instance: TObject; const PropInfo: PPropInfo;const Value: string);
  250. Function GetSetProp(Instance: TObject; const PropName: string): string;
  251. Function GetSetProp(Instance: TObject; const PropName: string; Brackets: Boolean): string;
  252. Function GetSetProp(Instance: TObject; const PropInfo: PPropInfo; Brackets: Boolean): string;
  253. Procedure SetSetProp(Instance: TObject; const PropName: string; const Value: string);
  254. Procedure SetSetProp(Instance: TObject; const PropInfo: PPropInfo; const Value: string);
  255. Function GetStrProp(Instance: TObject; PropInfo : PPropInfo) : Ansistring;
  256. Function GetStrProp(Instance: TObject; const PropName: string): string;
  257. Procedure SetStrProp(Instance: TObject; const PropName: string; const Value: AnsiString);
  258. Procedure SetStrProp(Instance: TObject; PropInfo : PPropInfo; const Value : Ansistring);
  259. Function GetWideStrProp(Instance: TObject; PropInfo: PPropInfo): WideString;
  260. Function GetWideStrProp(Instance: TObject; const PropName: string): WideString;
  261. Procedure SetWideStrProp(Instance: TObject; const PropName: string; const Value: WideString);
  262. Procedure SetWideStrProp(Instance: TObject; PropInfo: PPropInfo; const Value: WideString);
  263. Function GetUnicodeStrProp(Instance: TObject; PropInfo: PPropInfo): UnicodeString;
  264. Function GetUnicodeStrProp(Instance: TObject; const PropName: string): UnicodeString;
  265. Procedure SetUnicodeStrProp(Instance: TObject; const PropName: string; const Value: UnicodeString);
  266. Procedure SetUnicodeStrProp(Instance: TObject; PropInfo: PPropInfo; const Value: UnicodeString);
  267. {$ifndef FPUNONE}
  268. Function GetFloatProp(Instance: TObject; PropInfo : PPropInfo) : Extended;
  269. Function GetFloatProp(Instance: TObject; const PropName: string): Extended;
  270. Procedure SetFloatProp(Instance: TObject; const PropName: string; Value: Extended);
  271. Procedure SetFloatProp(Instance: TObject; PropInfo : PPropInfo; Value : Extended);
  272. {$endif}
  273. Function GetObjectProp(Instance: TObject; const PropName: string): TObject;
  274. Function GetObjectProp(Instance: TObject; const PropName: string; MinClass: TClass): TObject;
  275. Function GetObjectProp(Instance: TObject; PropInfo: PPropInfo): TObject;
  276. Function GetObjectProp(Instance: TObject; PropInfo: PPropInfo; MinClass: TClass): TObject;
  277. Procedure SetObjectProp(Instance: TObject; const PropName: string; Value: TObject);
  278. Procedure SetObjectProp(Instance: TObject; PropInfo: PPropInfo; Value: TObject);
  279. Function GetObjectPropClass(Instance: TObject; const PropName: string): TClass;
  280. Function GetObjectPropClass(AClass: TClass; const PropName: string): TClass;
  281. Function GetMethodProp(Instance: TObject; PropInfo: PPropInfo) : TMethod;
  282. Function GetMethodProp(Instance: TObject; const PropName: string): TMethod;
  283. Procedure SetMethodProp(Instance: TObject; PropInfo: PPropInfo; const Value : TMethod);
  284. Procedure SetMethodProp(Instance: TObject; const PropName: string; const Value: TMethod);
  285. Function GetInt64Prop(Instance: TObject; PropInfo: PPropInfo): Int64;
  286. Function GetInt64Prop(Instance: TObject; const PropName: string): Int64;
  287. Procedure SetInt64Prop(Instance: TObject; PropInfo: PPropInfo; const Value: Int64);
  288. Procedure SetInt64Prop(Instance: TObject; const PropName: string; const Value: Int64);
  289. Function GetPropValue(Instance: TObject; const PropName: string): Variant;
  290. Function GetPropValue(Instance: TObject; const PropName: string; PreferStrings: Boolean): Variant;
  291. Procedure SetPropValue(Instance: TObject; const PropName: string; const Value: Variant);
  292. Function GetVariantProp(Instance: TObject; PropInfo : PPropInfo): Variant;
  293. Function GetVariantProp(Instance: TObject; const PropName: string): Variant;
  294. Procedure SetVariantProp(Instance: TObject; const PropName: string; const Value: Variant);
  295. Procedure SetVariantProp(Instance: TObject; PropInfo : PPropInfo; const Value: Variant);
  296. function GetInterfaceProp(Instance: TObject; const PropName: string): IInterface;
  297. function GetInterfaceProp(Instance: TObject; PropInfo: PPropInfo): IInterface;
  298. procedure SetInterfaceProp(Instance: TObject; const PropName: string; const Value: IInterface);
  299. procedure SetInterfaceProp(Instance: TObject; PropInfo: PPropInfo; const Value: IInterface);
  300. function GetRawInterfaceProp(Instance: TObject; const PropName: string): Pointer;
  301. function GetRawInterfaceProp(Instance: TObject; PropInfo: PPropInfo): Pointer;
  302. procedure SetRawInterfaceProp(Instance: TObject; const PropName: string; const Value: Pointer);
  303. procedure SetRawInterfaceProp(Instance: TObject; PropInfo: PPropInfo; const Value: Pointer);
  304. // Auxiliary routines, which may be useful
  305. Function GetEnumName(TypeInfo : PTypeInfo;Value : Integer) : string;
  306. Function GetEnumValue(TypeInfo : PTypeInfo;const Name : string) : Integer;
  307. function GetEnumNameCount(enum1: PTypeInfo): SizeInt;
  308. function SetToString(TypeInfo: PTypeInfo; Value: Integer; Brackets: Boolean) : String;
  309. function SetToString(PropInfo: PPropInfo; Value: Integer; Brackets: Boolean) : String;
  310. function SetToString(PropInfo: PPropInfo; Value: Integer) : String;
  311. function StringToSet(PropInfo: PPropInfo; const Value: string): Integer;
  312. function StringToSet(TypeInfo: PTypeInfo; const Value: string): Integer;
  313. const
  314. BooleanIdents: array[Boolean] of String = ('False', 'True');
  315. DotSep: String = '.';
  316. Type
  317. EPropertyError = Class(Exception);
  318. TGetPropValue = Function (Instance: TObject; const PropName: string; PreferStrings: Boolean) : Variant;
  319. TSetPropValue = Procedure (Instance: TObject; const PropName: string; const Value: Variant);
  320. TGetVariantProp = Function (Instance: TObject; PropInfo : PPropInfo): Variant;
  321. TSetVariantProp = Procedure (Instance: TObject; PropInfo : PPropInfo; const Value: Variant);
  322. EPropertyConvertError = class(Exception); // Not used (yet), but defined for compatibility.
  323. Const
  324. OnGetPropValue : TGetPropValue = Nil;
  325. OnSetPropValue : TSetPropValue = Nil;
  326. OnGetVariantprop : TGetVariantProp = Nil;
  327. OnSetVariantprop : TSetVariantProp = Nil;
  328. Implementation
  329. uses rtlconsts;
  330. type
  331. PMethod = ^TMethod;
  332. { ---------------------------------------------------------------------
  333. Auxiliary methods
  334. ---------------------------------------------------------------------}
  335. function aligntoptr(p : pointer) : pointer;inline;
  336. begin
  337. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  338. result:=align(p,sizeof(p));
  339. {$else FPC_REQUIRES_PROPER_ALIGNMENT}
  340. result:=p;
  341. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  342. end;
  343. Function GetEnumName(TypeInfo : PTypeInfo;Value : Integer) : string;
  344. Var PS : PShortString;
  345. PT : PTypeData;
  346. begin
  347. PT:=GetTypeData(TypeInfo);
  348. if TypeInfo^.Kind=tkBool then
  349. begin
  350. case Value of
  351. 0,1:
  352. Result:=BooleanIdents[Boolean(Value)];
  353. else
  354. Result:='';
  355. end;
  356. end
  357. else
  358. begin
  359. PS:=@PT^.NameList;
  360. dec(Value,PT^.MinValue);
  361. While Value>0 Do
  362. begin
  363. PS:=PShortString(pointer(PS)+PByte(PS)^+1);
  364. Dec(Value);
  365. end;
  366. Result:=PS^;
  367. end;
  368. end;
  369. Function GetEnumValue(TypeInfo : PTypeInfo;const Name : string) : Integer;
  370. Var PS : PShortString;
  371. PT : PTypeData;
  372. Count : longint;
  373. sName: shortstring;
  374. begin
  375. If Length(Name)=0 then
  376. exit(-1);
  377. sName := Name;
  378. PT:=GetTypeData(TypeInfo);
  379. Count:=0;
  380. Result:=-1;
  381. if TypeInfo^.Kind=tkBool then
  382. begin
  383. If CompareText(BooleanIdents[false],Name)=0 then
  384. result:=0
  385. else if CompareText(BooleanIdents[true],Name)=0 then
  386. result:=1;
  387. end
  388. else
  389. begin
  390. PS:=@PT^.NameList;
  391. While (Result=-1) and (PByte(PS)^<>0) do
  392. begin
  393. If ShortCompareText(PS^, sName) = 0 then
  394. Result:=Count+PT^.MinValue;
  395. PS:=PShortString(pointer(PS)+PByte(PS)^+1);
  396. Inc(Count);
  397. end;
  398. end;
  399. end;
  400. function GetEnumNameCount(enum1: PTypeInfo): SizeInt;
  401. var
  402. PS: PShortString;
  403. PT: PTypeData;
  404. Count: SizeInt;
  405. begin
  406. PT:=GetTypeData(enum1);
  407. if enum1^.Kind=tkBool then
  408. Result:=2
  409. else
  410. begin
  411. Count:=0;
  412. Result:=0;
  413. PS:=@PT^.NameList;
  414. While (PByte(PS)^<>0) do
  415. begin
  416. PS:=PShortString(pointer(PS)+PByte(PS)^+1);
  417. Inc(Count);
  418. end;
  419. { the last string is the unit name }
  420. Result := Count - 1;
  421. end;
  422. end;
  423. Function SetToString(PropInfo: PPropInfo; Value: Integer; Brackets: Boolean) : String;
  424. begin
  425. Result:=SetToString(PropInfo^.PropType,Value,Brackets);
  426. end;
  427. Function SetToString(TypeInfo: PTypeInfo; Value: Integer; Brackets: Boolean) : String;
  428. type
  429. tsetarr = bitpacked array[0..31] of 0..1;
  430. Var
  431. I : Integer;
  432. PTI : PTypeInfo;
  433. begin
  434. {$if defined(FPC_BIG_ENDIAN)}
  435. { On big endian systems, set element 0 is in the most significant bit,
  436. and the same goes for the elements of bitpacked arrays there. }
  437. case GetTypeData(TypeInfo)^.OrdType of
  438. otSByte,otUByte: Value:=Value shl 24;
  439. otSWord,otUWord: Value:=Value shl 16;
  440. end;
  441. {$endif}
  442. PTI:=GetTypeData(TypeInfo)^.CompType;
  443. Result:='';
  444. For I:=0 to SizeOf(Integer)*8-1 do
  445. begin
  446. if (tsetarr(Value)[i]<>0) then
  447. begin
  448. If Result='' then
  449. Result:=GetEnumName(PTI,i)
  450. else
  451. Result:=Result+','+GetEnumName(PTI,I);
  452. end;
  453. end;
  454. if Brackets then
  455. Result:='['+Result+']';
  456. end;
  457. Function SetToString(PropInfo: PPropInfo; Value: Integer) : String;
  458. begin
  459. Result:=SetToString(PropInfo,Value,False);
  460. end;
  461. Const
  462. SetDelim = ['[',']',',',' '];
  463. Function GetNextElement(Var S : String) : String;
  464. Var
  465. J : Integer;
  466. begin
  467. J:=1;
  468. Result:='';
  469. If Length(S)>0 then
  470. begin
  471. While (J<=Length(S)) and Not (S[j] in SetDelim) do
  472. Inc(j);
  473. Result:=Copy(S,1,j-1);
  474. Delete(S,1,j);
  475. end;
  476. end;
  477. Function StringToSet(PropInfo: PPropInfo; const Value: string): Integer;
  478. begin
  479. Result:=StringToSet(PropInfo^.PropType,Value);
  480. end;
  481. Function StringToSet(TypeInfo: PTypeInfo; const Value: string): Integer;
  482. Var
  483. S,T : String;
  484. I : Integer;
  485. PTI : PTypeInfo;
  486. begin
  487. Result:=0;
  488. PTI:=GetTypeData(TypeInfo)^.Comptype;
  489. S:=Value;
  490. I:=1;
  491. If Length(S)>0 then
  492. begin
  493. While (I<=Length(S)) and (S[i] in SetDelim) do
  494. Inc(I);
  495. Delete(S,1,i-1);
  496. end;
  497. While (S<>'') do
  498. begin
  499. T:=GetNextElement(S);
  500. if T<>'' then
  501. begin
  502. I:=GetEnumValue(PTI,T);
  503. if (I<0) then
  504. raise EPropertyError.CreateFmt(SErrUnknownEnumValue, [T]);
  505. Result:=Result or (1 shl i);
  506. end;
  507. end;
  508. end;
  509. Function GetTypeData(TypeInfo : PTypeInfo) : PTypeData;
  510. begin
  511. GetTypeData:=PTypeData(aligntoptr(PTypeData(pointer(TypeInfo)+2+PByte(pointer(TypeInfo)+1)^)));
  512. end;
  513. { ---------------------------------------------------------------------
  514. Basic Type information functions.
  515. ---------------------------------------------------------------------}
  516. Function GetPropInfo(TypeInfo : PTypeInfo;const PropName : string) : PPropInfo;
  517. var
  518. hp : PTypeData;
  519. i : longint;
  520. p : shortstring;
  521. pd : ^TPropData;
  522. begin
  523. P:=PropName; // avoid Ansi<->short conversion in a loop
  524. while Assigned(TypeInfo) do
  525. begin
  526. // skip the name
  527. hp:=GetTypeData(Typeinfo);
  528. // the class info rtti the property rtti follows immediatly
  529. pd:=aligntoptr(pointer(pointer(@hp^.UnitName)+Length(hp^.UnitName)+1));
  530. Result:=PPropInfo(@pd^.PropList);
  531. for i:=1 to pd^.PropCount do
  532. begin
  533. // found a property of that name ?
  534. if ShortCompareText(Result^.Name, P) = 0 then
  535. exit;
  536. // skip to next property
  537. Result:=PPropInfo(aligntoptr(pointer(@Result^.Name)+byte(Result^.Name[0])+1));
  538. end;
  539. // parent class
  540. Typeinfo:=hp^.ParentInfo;
  541. end;
  542. Result:=Nil;
  543. end;
  544. Function GetPropInfo(TypeInfo : PTypeInfo;const PropName : string; Akinds : TTypeKinds) : PPropInfo;
  545. begin
  546. Result:=GetPropInfo(TypeInfo,PropName);
  547. If (Akinds<>[]) then
  548. If (Result<>Nil) then
  549. If Not (Result^.PropType^.Kind in AKinds) then
  550. Result:=Nil;
  551. end;
  552. Function GetPropInfo(AClass: TClass; const PropName: string; AKinds: TTypeKinds) : PPropInfo;
  553. begin
  554. Result:=GetPropInfo(PTypeInfo(AClass.ClassInfo),PropName,AKinds);
  555. end;
  556. Function GetPropInfo(Instance: TObject; const PropName: string; AKinds: TTypeKinds) : PPropInfo;
  557. begin
  558. Result:=GetPropInfo(Instance.ClassType,PropName,AKinds);
  559. end;
  560. Function GetPropInfo(Instance: TObject; const PropName: string): PPropInfo;
  561. begin
  562. Result:=GetPropInfo(Instance,PropName,[]);
  563. end;
  564. Function GetPropInfo(AClass: TClass; const PropName: string): PPropInfo;
  565. begin
  566. Result:=GetPropInfo(AClass,PropName,[]);
  567. end;
  568. Function FindPropInfo(Instance: TObject; const PropName: string): PPropInfo;
  569. begin
  570. result:=GetPropInfo(Instance, PropName);
  571. if Result=nil then
  572. Raise EPropertyError.CreateFmt(SErrPropertyNotFound, [PropName]);
  573. end;
  574. Function FindPropInfo(Instance: TObject; const PropName: string; AKinds: TTypeKinds): PPropInfo;
  575. begin
  576. result:=GetPropInfo(Instance, PropName, AKinds);
  577. if Result=nil then
  578. Raise EPropertyError.CreateFmt(SErrPropertyNotFound, [PropName]);
  579. end;
  580. Function FindPropInfo(AClass: TClass; const PropName: string): PPropInfo;
  581. begin
  582. result:=GetPropInfo(AClass, PropName);
  583. if result=nil then
  584. Raise EPropertyError.CreateFmt(SErrPropertyNotFound, [PropName]);
  585. end;
  586. Function FindPropInfo(AClass: TClass; const PropName: string; AKinds: TTypeKinds): PPropInfo;
  587. begin
  588. result:=GetPropInfo(AClass, PropName, AKinds);
  589. if result=nil then
  590. Raise EPropertyError.CreateFmt(SErrPropertyNotFound, [PropName]);
  591. end;
  592. Function IsStoredProp(Instance : TObject;PropInfo : PPropInfo) : Boolean;
  593. type
  594. TBooleanIndexFunc=function(Index:integer):boolean of object;
  595. TBooleanFunc=function:boolean of object;
  596. var
  597. AMethod : TMethod;
  598. begin
  599. case (PropInfo^.PropProcs shr 4) and 3 of
  600. ptfield:
  601. Result:=PBoolean(Pointer(Instance)+PtrUInt(PropInfo^.StoredProc))^;
  602. ptconst:
  603. Result:=LongBool(PropInfo^.StoredProc);
  604. ptstatic,
  605. ptvirtual:
  606. begin
  607. if (PropInfo^.PropProcs shr 4) and 3=ptstatic then
  608. AMethod.Code:=PropInfo^.StoredProc
  609. else
  610. AMethod.Code:=ppointer(Pointer(Instance.ClassType)+PtrUInt(PropInfo^.StoredProc))^;
  611. AMethod.Data:=Instance;
  612. if ((PropInfo^.PropProcs shr 6) and 1)<>0 then
  613. Result:=TBooleanIndexFunc(AMethod)(PropInfo^.Index)
  614. else
  615. Result:=TBooleanFunc(AMethod)();
  616. end;
  617. end;
  618. end;
  619. Procedure GetPropInfos(TypeInfo : PTypeInfo;PropList : PPropList);
  620. {
  621. Store Pointers to property information in the list pointed
  622. to by proplist. PRopList must contain enough space to hold ALL
  623. properties.
  624. }
  625. Var
  626. TD : PTypeData;
  627. TP : PPropInfo;
  628. Count : Longint;
  629. begin
  630. // Get this objects TOTAL published properties count
  631. TD:=GetTypeData(TypeInfo);
  632. // Clear list
  633. FillChar(PropList^,TD^.PropCount*sizeof(Pointer),0);
  634. repeat
  635. TD:=GetTypeData(TypeInfo);
  636. // published properties count for this object
  637. TP:=aligntoptr(PPropInfo(aligntoptr((Pointer(@TD^.UnitName)+Length(TD^.UnitName)+1))));
  638. Count:=PWord(TP)^;
  639. // Now point TP to first propinfo record.
  640. Inc(Pointer(TP),SizeOF(Word));
  641. tp:=aligntoptr(tp);
  642. While Count>0 do
  643. begin
  644. // Don't overwrite properties with the same name
  645. if PropList^[TP^.NameIndex]=nil then
  646. PropList^[TP^.NameIndex]:=TP;
  647. // Point to TP next propinfo record.
  648. // Located at Name[Length(Name)+1] !
  649. TP:=aligntoptr(PPropInfo(pointer(@TP^.Name)+PByte(@TP^.Name)^+1));
  650. Dec(Count);
  651. end;
  652. TypeInfo:=TD^.Parentinfo;
  653. until TypeInfo=nil;
  654. end;
  655. Procedure InsertProp (PL : PProplist;PI : PPropInfo; Count : longint);
  656. Var
  657. I : Longint;
  658. begin
  659. I:=0;
  660. While (I<Count) and (PI^.Name>PL^[I]^.Name) do
  661. Inc(I);
  662. If I<Count then
  663. Move(PL^[I], PL^[I+1], (Count - I) * SizeOf(Pointer));
  664. PL^[I]:=PI;
  665. end;
  666. Procedure InsertPropnosort (PL : PProplist;PI : PPropInfo; Count : longint);
  667. begin
  668. PL^[Count]:=PI;
  669. end;
  670. Type TInsertProp = Procedure (PL : PProplist;PI : PPropInfo; Count : longint);
  671. //Const InsertProps : array[false..boolean] of TInsertProp = (InsertPropNoSort,InsertProp);
  672. Function GetPropList(TypeInfo : PTypeInfo;TypeKinds : TTypeKinds; PropList : PPropList;Sorted : boolean = true):longint;
  673. {
  674. Store Pointers to property information OF A CERTAIN KIND in the list pointed
  675. to by proplist. PRopList must contain enough space to hold ALL
  676. properties.
  677. }
  678. Var
  679. TempList : PPropList;
  680. PropInfo : PPropinfo;
  681. I,Count : longint;
  682. DoInsertProp : TInsertProp;
  683. begin
  684. if sorted then
  685. DoInsertProp:=@InsertProp
  686. else
  687. DoInsertProp:=@InsertPropnosort;
  688. Result:=0;
  689. Count:=GetTypeData(TypeInfo)^.Propcount;
  690. If Count>0 then
  691. begin
  692. GetMem(TempList,Count*SizeOf(Pointer));
  693. Try
  694. GetPropInfos(TypeInfo,TempList);
  695. For I:=0 to Count-1 do
  696. begin
  697. PropInfo:=TempList^[i];
  698. If PropInfo^.PropType^.Kind in TypeKinds then
  699. begin
  700. If (PropList<>Nil) then
  701. DoInsertProp(PropList,PropInfo,Result);
  702. Inc(Result);
  703. end;
  704. end;
  705. finally
  706. FreeMem(TempList,Count*SizeOf(Pointer));
  707. end;
  708. end;
  709. end;
  710. Function GetPropList(TypeInfo: PTypeInfo; out PropList: PPropList): SizeInt;
  711. begin
  712. result:=GetTypeData(TypeInfo)^.Propcount;
  713. if result>0 then
  714. begin
  715. getmem(PropList,result*sizeof(pointer));
  716. GetPropInfos(TypeInfo,PropList);
  717. end
  718. else
  719. PropList:=Nil;
  720. end;
  721. function GetPropList(AClass: TClass; out PropList: PPropList): Integer;
  722. begin
  723. Result := GetPropList(PTypeInfo(AClass.ClassInfo), PropList);
  724. end;
  725. function GetPropList(Instance: TObject; out PropList: PPropList): Integer;
  726. begin
  727. Result := GetPropList(Instance.ClassType, PropList);
  728. end;
  729. { ---------------------------------------------------------------------
  730. Property access functions
  731. ---------------------------------------------------------------------}
  732. { ---------------------------------------------------------------------
  733. Ordinal properties
  734. ---------------------------------------------------------------------}
  735. Function GetOrdProp(Instance : TObject;PropInfo : PPropInfo) : Int64;
  736. type
  737. TGetInt64ProcIndex=function(index:longint):Int64 of object;
  738. TGetInt64Proc=function():Int64 of object;
  739. TGetIntegerProcIndex=function(index:longint):longint of object;
  740. TGetIntegerProc=function:longint of object;
  741. TGetWordProcIndex=function(index:longint):word of object;
  742. TGetWordProc=function:word of object;
  743. TGetByteProcIndex=function(index:longint):Byte of object;
  744. TGetByteProc=function:Byte of object;
  745. var
  746. TypeInfo: PTypeInfo;
  747. AMethod : TMethod;
  748. DataSize: Integer;
  749. OrdType: TOrdType;
  750. Signed: Boolean;
  751. begin
  752. Result:=0;
  753. TypeInfo := PropInfo^.PropType;
  754. Signed := false;
  755. DataSize := 4;
  756. case TypeInfo^.Kind of
  757. {$ifdef cpu64}
  758. tkInterface,
  759. tkInterfaceRaw,
  760. tkDynArray,
  761. tkClass:
  762. DataSize:=8;
  763. {$endif cpu64}
  764. tkChar, tkBool:
  765. DataSize:=1;
  766. tkWChar:
  767. DataSize:=2;
  768. tkSet,
  769. tkEnumeration,
  770. tkInteger:
  771. begin
  772. OrdType:=GetTypeData(TypeInfo)^.OrdType;
  773. case OrdType of
  774. otSByte,otUByte: DataSize := 1;
  775. otSWord,otUWord: DataSize := 2;
  776. end;
  777. Signed := OrdType in [otSByte,otSWord,otSLong];
  778. end;
  779. tkInt64 :
  780. begin
  781. DataSize:=8;
  782. Signed:=true;
  783. end;
  784. tkQword :
  785. begin
  786. DataSize:=8;
  787. Signed:=false;
  788. end;
  789. end;
  790. case (PropInfo^.PropProcs) and 3 of
  791. ptfield:
  792. if Signed then begin
  793. case DataSize of
  794. 1: Result:=PShortInt(Pointer(Instance)+PtrUInt(PropInfo^.GetProc))^;
  795. 2: Result:=PSmallInt(Pointer(Instance)+PtrUInt(PropInfo^.GetProc))^;
  796. 4: Result:=PLongint(Pointer(Instance)+PtrUInt(PropInfo^.GetProc))^;
  797. 8: Result:=PInt64(Pointer(Instance)+PtrUInt(PropInfo^.GetProc))^;
  798. end;
  799. end else begin
  800. case DataSize of
  801. 1: Result:=PByte(Pointer(Instance)+PtrUInt(PropInfo^.GetProc))^;
  802. 2: Result:=PWord(Pointer(Instance)+PtrUInt(PropInfo^.GetProc))^;
  803. 4: Result:=PLongint(Pointer(Instance)+PtrUInt(PropInfo^.GetProc))^;
  804. 8: Result:=PInt64(Pointer(Instance)+PtrUInt(PropInfo^.GetProc))^;
  805. end;
  806. end;
  807. ptstatic,
  808. ptvirtual :
  809. begin
  810. if (PropInfo^.PropProcs and 3)=ptStatic then
  811. AMethod.Code:=PropInfo^.GetProc
  812. else
  813. AMethod.Code:=PPointer(Pointer(Instance.ClassType)+PtrUInt(PropInfo^.GetProc))^;
  814. AMethod.Data:=Instance;
  815. if ((PropInfo^.PropProcs shr 6) and 1)<>0 then begin
  816. case DataSize of
  817. 1: Result:=TGetByteProcIndex(AMethod)(PropInfo^.Index);
  818. 2: Result:=TGetWordProcIndex(AMethod)(PropInfo^.Index);
  819. 4: Result:=TGetIntegerProcIndex(AMethod)(PropInfo^.Index);
  820. 8: result:=TGetInt64ProcIndex(AMethod)(PropInfo^.Index)
  821. end;
  822. end else begin
  823. case DataSize of
  824. 1: Result:=TGetByteProc(AMethod)();
  825. 2: Result:=TGetWordProc(AMethod)();
  826. 4: Result:=TGetIntegerProc(AMethod)();
  827. 8: result:=TGetInt64Proc(AMethod)();
  828. end;
  829. end;
  830. if Signed then begin
  831. case DataSize of
  832. 1: Result:=ShortInt(Result);
  833. 2: Result:=SmallInt(Result);
  834. end;
  835. end;
  836. end;
  837. end;
  838. end;
  839. Procedure SetOrdProp(Instance : TObject;PropInfo : PPropInfo;Value : Int64);
  840. type
  841. TSetInt64ProcIndex=procedure(index:longint;i:Int64) of object;
  842. TSetInt64Proc=procedure(i:Int64) of object;
  843. TSetIntegerProcIndex=procedure(index,i:longint) of object;
  844. TSetIntegerProc=procedure(i:longint) of object;
  845. var
  846. DataSize: Integer;
  847. AMethod : TMethod;
  848. begin
  849. if PropInfo^.PropType^.Kind in [tkInt64,tkQword
  850. { why do we have to handle classes here, see also below? (FK) }
  851. {$ifdef cpu64}
  852. ,tkInterface
  853. ,tkInterfaceRaw
  854. ,tkDynArray
  855. ,tkClass
  856. {$endif cpu64}
  857. ] then
  858. DataSize := 8
  859. else
  860. DataSize := 4;
  861. if not(PropInfo^.PropType^.Kind in [tkInt64,tkQword,tkClass]) then
  862. begin
  863. { cut off unnecessary stuff }
  864. case GetTypeData(PropInfo^.PropType)^.OrdType of
  865. otSWord,otUWord:
  866. begin
  867. Value:=Value and $ffff;
  868. DataSize := 2;
  869. end;
  870. otSByte,otUByte:
  871. begin
  872. Value:=Value and $ff;
  873. DataSize := 1;
  874. end;
  875. end;
  876. end;
  877. case (PropInfo^.PropProcs shr 2) and 3 of
  878. ptfield:
  879. case DataSize of
  880. 1: PByte(Pointer(Instance)+PtrUInt(PropInfo^.SetProc))^:=Byte(Value);
  881. 2: PWord(Pointer(Instance)+PtrUInt(PropInfo^.SetProc))^:=Word(Value);
  882. 4: PLongint(Pointer(Instance)+PtrUInt(PropInfo^.SetProc))^:=Longint(Value);
  883. 8: PInt64(Pointer(Instance)+PtrUInt(PropInfo^.SetProc))^:=Value;
  884. end;
  885. ptstatic,
  886. ptvirtual :
  887. begin
  888. if ((PropInfo^.PropProcs shr 2) and 3)=ptStatic then
  889. AMethod.Code:=PropInfo^.SetProc
  890. else
  891. AMethod.Code:=PPointer(Pointer(Instance.ClassType)+PtrUInt(PropInfo^.SetProc))^;
  892. AMethod.Data:=Instance;
  893. if datasize=8 then
  894. begin
  895. if ((PropInfo^.PropProcs shr 6) and 1)<>0 then
  896. TSetInt64ProcIndex(AMethod)(PropInfo^.Index,Value)
  897. else
  898. TSetInt64Proc(AMethod)(Value);
  899. end
  900. else
  901. begin
  902. if ((PropInfo^.PropProcs shr 6) and 1)<>0 then
  903. TSetIntegerProcIndex(AMethod)(PropInfo^.Index,Value)
  904. else
  905. TSetIntegerProc(AMethod)(Value);
  906. end;
  907. end;
  908. end;
  909. end;
  910. Function GetOrdProp(Instance: TObject; const PropName: string): Int64;
  911. begin
  912. Result:=GetOrdProp(Instance,FindPropInfo(Instance,PropName));
  913. end;
  914. Procedure SetOrdProp(Instance: TObject; const PropName: string; Value: Int64);
  915. begin
  916. SetOrdProp(Instance,FindPropInfo(Instance,PropName),Value);
  917. end;
  918. Function GetEnumProp(Instance: TObject; Const PropInfo: PPropInfo): string;
  919. begin
  920. Result:=GetEnumName(PropInfo^.PropType, GetOrdProp(Instance, PropInfo));
  921. end;
  922. Function GetEnumProp(Instance: TObject; const PropName: string): string;
  923. begin
  924. Result:=GetEnumProp(Instance,FindPropInfo(Instance,PropName));
  925. end;
  926. Procedure SetEnumProp(Instance: TObject; const PropName: string; const Value: string);
  927. begin
  928. SetEnumProp(Instance,FindPropInfo(Instance,PropName),Value);
  929. end;
  930. Procedure SetEnumProp(Instance: TObject; Const PropInfo : PPropInfo; const Value: string);
  931. Var
  932. PV : Longint;
  933. begin
  934. If PropInfo<>Nil then
  935. begin
  936. PV:=GetEnumValue(PropInfo^.PropType, Value);
  937. if (PV<0) then
  938. raise EPropertyError.CreateFmt(SErrUnknownEnumValue, [Value]);
  939. SetOrdProp(Instance, PropInfo,PV);
  940. end;
  941. end;
  942. { ---------------------------------------------------------------------
  943. Int64 wrappers
  944. ---------------------------------------------------------------------}
  945. Function GetInt64Prop(Instance: TObject; PropInfo: PPropInfo): Int64;
  946. begin
  947. Result:=GetOrdProp(Instance,PropInfo);
  948. end;
  949. procedure SetInt64Prop(Instance: TObject; PropInfo: PPropInfo; const Value: Int64);
  950. begin
  951. SetOrdProp(Instance,PropInfo,Value);
  952. end;
  953. Function GetInt64Prop(Instance: TObject; const PropName: string): Int64;
  954. begin
  955. Result:=GetInt64Prop(Instance,FindPropInfo(Instance,PropName));
  956. end;
  957. Procedure SetInt64Prop(Instance: TObject; const PropName: string; const Value: Int64);
  958. begin
  959. SetInt64Prop(Instance,FindPropInfo(Instance,PropName),Value);
  960. end;
  961. { ---------------------------------------------------------------------
  962. Set properties
  963. ---------------------------------------------------------------------}
  964. Function GetSetProp(Instance: TObject; const PropName: string): string;
  965. begin
  966. Result:=GetSetProp(Instance,PropName,False);
  967. end;
  968. Function GetSetProp(Instance: TObject; const PropName: string; Brackets: Boolean): string;
  969. begin
  970. Result:=GetSetProp(Instance,FindPropInfo(Instance,PropName),Brackets);
  971. end;
  972. Function GetSetProp(Instance: TObject; const PropInfo: PPropInfo; Brackets: Boolean): string;
  973. begin
  974. Result:=SetToString(PropInfo,GetOrdProp(Instance,PropInfo),Brackets);
  975. end;
  976. Procedure SetSetProp(Instance: TObject; const PropName: string; const Value: string);
  977. begin
  978. SetSetProp(Instance,FindPropInfo(Instance,PropName),Value);
  979. end;
  980. Procedure SetSetProp(Instance: TObject; const PropInfo: PPropInfo; const Value: string);
  981. begin
  982. SetOrdProp(Instance,PropInfo,StringToSet(PropInfo,Value));
  983. end;
  984. { ---------------------------------------------------------------------
  985. Object properties
  986. ---------------------------------------------------------------------}
  987. Function GetObjectProp(Instance: TObject; const PropName: string): TObject;
  988. begin
  989. Result:=GetObjectProp(Instance,PropName,Nil);
  990. end;
  991. Function GetObjectProp(Instance: TObject; const PropName: string; MinClass: TClass): TObject;
  992. begin
  993. Result:=GetObjectProp(Instance,FindPropInfo(Instance,PropName),MinClass);
  994. end;
  995. Function GetObjectProp(Instance: TObject; PropInfo : PPropInfo): TObject;
  996. begin
  997. Result:=GetObjectProp(Instance,PropInfo,Nil);
  998. end;
  999. Function GetObjectProp(Instance: TObject; PropInfo : PPropInfo; MinClass: TClass): TObject;
  1000. begin
  1001. {$ifdef cpu64}
  1002. Result:=TObject(GetInt64Prop(Instance,PropInfo));
  1003. {$else cpu64}
  1004. Result:=TObject(PtrInt(GetOrdProp(Instance,PropInfo)));
  1005. {$endif cpu64}
  1006. If (MinClass<>Nil) and (Result<>Nil) Then
  1007. If Not Result.InheritsFrom(MinClass) then
  1008. Result:=Nil;
  1009. end;
  1010. Procedure SetObjectProp(Instance: TObject; const PropName: string; Value: TObject);
  1011. begin
  1012. SetObjectProp(Instance,FindPropInfo(Instance,PropName),Value);
  1013. end;
  1014. Procedure SetObjectProp(Instance: TObject; PropInfo : PPropInfo; Value: TObject);
  1015. begin
  1016. {$ifdef cpu64}
  1017. SetInt64Prop(Instance,PropInfo,Int64(Value));
  1018. {$else cpu64}
  1019. SetOrdProp(Instance,PropInfo,Integer(Value));
  1020. {$endif cpu64}
  1021. end;
  1022. Function GetObjectPropClass(Instance: TObject; const PropName: string): TClass;
  1023. begin
  1024. Result:=GetTypeData(FindPropInfo(Instance,PropName,[tkClass])^.PropType)^.ClassType;
  1025. end;
  1026. Function GetObjectPropClass(AClass: TClass; const PropName: string): TClass;
  1027. begin
  1028. Result:=GetTypeData(FindPropInfo(AClass,PropName,[tkClass])^.PropType)^.ClassType;
  1029. end;
  1030. { ---------------------------------------------------------------------
  1031. Interface wrapprers
  1032. ---------------------------------------------------------------------}
  1033. function GetInterfaceProp(Instance: TObject; const PropName: string): IInterface;
  1034. begin
  1035. Result:=GetInterfaceProp(Instance,FindPropInfo(Instance,PropName));
  1036. end;
  1037. function GetInterfaceProp(Instance: TObject; PropInfo: PPropInfo): IInterface;
  1038. begin
  1039. {$ifdef cpu64}
  1040. Result:=IInterface(GetInt64Prop(Instance,PropInfo));
  1041. {$else cpu64}
  1042. Result:=IInterface(PtrInt(GetOrdProp(Instance,PropInfo)));
  1043. {$endif cpu64}
  1044. end;
  1045. procedure SetInterfaceProp(Instance: TObject; const PropName: string; const Value: IInterface);
  1046. begin
  1047. SetInterfaceProp(Instance,FindPropInfo(Instance,PropName),Value);
  1048. end;
  1049. procedure SetInterfaceProp(Instance: TObject; PropInfo: PPropInfo; const Value: IInterface);
  1050. type
  1051. TSetIntfStrProcIndex=procedure(index:longint;const i:IInterface) of object;
  1052. TSetIntfStrProc=procedure(i:IInterface) of object;
  1053. var
  1054. AMethod : TMethod;
  1055. begin
  1056. case Propinfo^.PropType^.Kind of
  1057. tkInterface:
  1058. begin
  1059. case (PropInfo^.PropProcs shr 2) and 3 of
  1060. ptField:
  1061. PInterface(Pointer(Instance)+PtrUInt(PropInfo^.SetProc))^:=Value;
  1062. ptstatic,
  1063. ptvirtual :
  1064. begin
  1065. if ((PropInfo^.PropProcs shr 2) and 3)=ptStatic then
  1066. AMethod.Code:=PropInfo^.SetProc
  1067. else
  1068. AMethod.Code:=PPointer(Pointer(Instance.ClassType)+PtrUInt(PropInfo^.SetProc))^;
  1069. AMethod.Data:=Instance;
  1070. if ((PropInfo^.PropProcs shr 6) and 1)<>0 then
  1071. TSetIntfStrProcIndex(AMethod)(PropInfo^.Index,Value)
  1072. else
  1073. TSetIntfStrProc(AMethod)(Value);
  1074. end;
  1075. end;
  1076. end;
  1077. tkInterfaceRaw:
  1078. Raise Exception.Create('Cannot set RAW interface from IUnknown interface');
  1079. end;
  1080. end;
  1081. { ---------------------------------------------------------------------
  1082. RAW (Corba) Interface wrapprers
  1083. ---------------------------------------------------------------------}
  1084. function GetRawInterfaceProp(Instance: TObject; const PropName: string): Pointer;
  1085. begin
  1086. Result:=GetRawInterfaceProp(Instance,FindPropInfo(Instance,PropName));
  1087. end;
  1088. function GetRawInterfaceProp(Instance: TObject; PropInfo: PPropInfo): Pointer;
  1089. begin
  1090. {$ifdef cpu64}
  1091. Result:=Pointer(GetInt64Prop(Instance,PropInfo));
  1092. {$else cpu64}
  1093. Result:=Pointer(PtrInt(GetOrdProp(Instance,PropInfo)));
  1094. {$endif cpu64}
  1095. end;
  1096. procedure SetRawInterfaceProp(Instance: TObject; const PropName: string; const Value: Pointer);
  1097. begin
  1098. SetRawInterfaceProp(Instance,FindPropInfo(Instance,PropName),Value);
  1099. end;
  1100. procedure SetRawInterfaceProp(Instance: TObject; PropInfo: PPropInfo; const Value: Pointer);
  1101. type
  1102. TSetPointerProcIndex=procedure(index:longint;const i:Pointer) of object;
  1103. TSetPointerProc=procedure(i:Pointer) of object;
  1104. var
  1105. AMethod : TMethod;
  1106. begin
  1107. case Propinfo^.PropType^.Kind of
  1108. tkInterfaceRaw:
  1109. begin
  1110. case (PropInfo^.PropProcs shr 2) and 3 of
  1111. ptField:
  1112. PPointer(Pointer(Instance)+PtrUInt(PropInfo^.SetProc))^:=Value;
  1113. ptstatic,
  1114. ptvirtual :
  1115. begin
  1116. if ((PropInfo^.PropProcs shr 2) and 3)=ptStatic then
  1117. AMethod.Code:=PropInfo^.SetProc
  1118. else
  1119. AMethod.Code:=PPointer(Pointer(Instance.ClassType)+PtrUInt(PropInfo^.SetProc))^;
  1120. AMethod.Data:=Instance;
  1121. if ((PropInfo^.PropProcs shr 6) and 1)<>0 then
  1122. TSetPointerProcIndex(AMethod)(PropInfo^.Index,Value)
  1123. else
  1124. TSetPointerProc(AMethod)(Value);
  1125. end;
  1126. end;
  1127. end;
  1128. tkInterface:
  1129. Raise Exception.Create('Cannot set interface from RAW interface');
  1130. end;
  1131. end;
  1132. { ---------------------------------------------------------------------
  1133. String properties
  1134. ---------------------------------------------------------------------}
  1135. Function GetStrProp(Instance: TObject; PropInfo: PPropInfo): AnsiString;
  1136. type
  1137. TGetShortStrProcIndex=function(index:longint):ShortString of object;
  1138. TGetShortStrProc=function():ShortString of object;
  1139. TGetAnsiStrProcIndex=function(index:longint):AnsiString of object;
  1140. TGetAnsiStrProc=function():AnsiString of object;
  1141. var
  1142. AMethod : TMethod;
  1143. begin
  1144. Result:='';
  1145. case Propinfo^.PropType^.Kind of
  1146. tkWString:
  1147. Result:=GetWideStrProp(Instance,PropInfo);
  1148. tkUString :
  1149. Result := GetUnicodeStrProp(Instance,PropInfo);
  1150. tkSString:
  1151. begin
  1152. case (PropInfo^.PropProcs) and 3 of
  1153. ptField:
  1154. Result := PShortString(Pointer(Instance) + LongWord(PropInfo^.GetProc))^;
  1155. ptstatic,
  1156. ptvirtual :
  1157. begin
  1158. if (PropInfo^.PropProcs and 3)=ptStatic then
  1159. AMethod.Code:=PropInfo^.GetProc
  1160. else
  1161. AMethod.Code:=PPointer(Pointer(Instance.ClassType)+PtrUInt(PropInfo^.GetProc))^;
  1162. AMethod.Data:=Instance;
  1163. if ((PropInfo^.PropProcs shr 6) and 1)<>0 then
  1164. Result:=TGetShortStrProcIndex(AMethod)(PropInfo^.Index)
  1165. else
  1166. Result:=TGetShortStrProc(AMethod)();
  1167. end;
  1168. end;
  1169. end;
  1170. tkAString:
  1171. begin
  1172. case (PropInfo^.PropProcs) and 3 of
  1173. ptField:
  1174. Result := PAnsiString(Pointer(Instance) + LongWord(PropInfo^.GetProc))^;
  1175. ptstatic,
  1176. ptvirtual :
  1177. begin
  1178. if (PropInfo^.PropProcs and 3)=ptStatic then
  1179. AMethod.Code:=PropInfo^.GetProc
  1180. else
  1181. AMethod.Code:=PPointer(Pointer(Instance.ClassType)+PtrUInt(PropInfo^.GetProc))^;
  1182. AMethod.Data:=Instance;
  1183. if ((PropInfo^.PropProcs shr 6) and 1)<>0 then
  1184. Result:=TGetAnsiStrProcIndex(AMethod)(PropInfo^.Index)
  1185. else
  1186. Result:=TGetAnsiStrProc(AMethod)();
  1187. end;
  1188. end;
  1189. end;
  1190. end;
  1191. end;
  1192. Procedure SetStrProp(Instance : TObject;PropInfo : PPropInfo; const Value : AnsiString);
  1193. type
  1194. TSetShortStrProcIndex=procedure(index:longint;const s:ShortString) of object;
  1195. TSetShortStrProc=procedure(const s:ShortString) of object;
  1196. TSetAnsiStrProcIndex=procedure(index:longint;s:AnsiString) of object;
  1197. TSetAnsiStrProc=procedure(s:AnsiString) of object;
  1198. var
  1199. AMethod : TMethod;
  1200. begin
  1201. case Propinfo^.PropType^.Kind of
  1202. tkWString:
  1203. SetWideStrProp(Instance,PropInfo,Value);
  1204. tkUString:
  1205. SetUnicodeStrProp(Instance,PropInfo,Value);
  1206. tkSString:
  1207. begin
  1208. case (PropInfo^.PropProcs shr 2) and 3 of
  1209. ptField:
  1210. PShortString(Pointer(Instance) + LongWord(PropInfo^.SetProc))^:=Value;
  1211. ptstatic,
  1212. ptvirtual :
  1213. begin
  1214. if ((PropInfo^.PropProcs shr 2) and 3)=ptStatic then
  1215. AMethod.Code:=PropInfo^.SetProc
  1216. else
  1217. AMethod.Code:=PPointer(Pointer(Instance.ClassType)+PtrUInt(PropInfo^.SetProc))^;
  1218. AMethod.Data:=Instance;
  1219. if ((PropInfo^.PropProcs shr 6) and 1)<>0 then
  1220. TSetShortStrProcIndex(AMethod)(PropInfo^.Index,Value)
  1221. else
  1222. TSetShortStrProc(AMethod)(Value);
  1223. end;
  1224. end;
  1225. end;
  1226. tkAString:
  1227. begin
  1228. case (PropInfo^.PropProcs shr 2) and 3 of
  1229. ptField:
  1230. PAnsiString(Pointer(Instance) + LongWord(PropInfo^.SetProc))^:=Value;
  1231. ptstatic,
  1232. ptvirtual :
  1233. begin
  1234. if ((PropInfo^.PropProcs shr 2) and 3)=ptStatic then
  1235. AMethod.Code:=PropInfo^.SetProc
  1236. else
  1237. AMethod.Code:=PPointer(Pointer(Instance.ClassType)+PtrUInt(PropInfo^.SetProc))^;
  1238. AMethod.Data:=Instance;
  1239. if ((PropInfo^.PropProcs shr 6) and 1)<>0 then
  1240. TSetAnsiStrProcIndex(AMethod)(PropInfo^.Index,Value)
  1241. else
  1242. TSetAnsiStrProc(AMethod)(Value);
  1243. end;
  1244. end;
  1245. end;
  1246. end;
  1247. end;
  1248. Function GetStrProp(Instance: TObject; const PropName: string): string;
  1249. begin
  1250. Result:=GetStrProp(Instance,FindPropInfo(Instance,PropName));
  1251. end;
  1252. Procedure SetStrProp(Instance: TObject; const PropName: string; const Value: AnsiString);
  1253. begin
  1254. SetStrProp(Instance,FindPropInfo(Instance,PropName),Value);
  1255. end;
  1256. Function GetWideStrProp(Instance: TObject; const PropName: string): WideString;
  1257. begin
  1258. Result:=GetWideStrProp(Instance, FindPropInfo(Instance, PropName));
  1259. end;
  1260. procedure SetWideStrProp(Instance: TObject; const PropName: string; const Value: WideString);
  1261. begin
  1262. SetWideStrProp(Instance,FindPropInfo(Instance,PropName),Value);
  1263. end;
  1264. Function GetWideStrProp(Instance: TObject; PropInfo: PPropInfo): WideString;
  1265. type
  1266. TGetWideStrProcIndex=function(index:longint):WideString of object;
  1267. TGetWideStrProc=function():WideString of object;
  1268. var
  1269. AMethod : TMethod;
  1270. begin
  1271. Result:='';
  1272. case Propinfo^.PropType^.Kind of
  1273. tkSString,tkAString:
  1274. Result:=GetStrProp(Instance,PropInfo);
  1275. tkUString :
  1276. Result := GetUnicodeStrProp(Instance,PropInfo);
  1277. tkWString:
  1278. begin
  1279. case (PropInfo^.PropProcs) and 3 of
  1280. ptField:
  1281. Result := PWideString(Pointer(Instance)+PtrUInt(PropInfo^.GetProc))^;
  1282. ptstatic,
  1283. ptvirtual :
  1284. begin
  1285. if (PropInfo^.PropProcs and 3)=ptStatic then
  1286. AMethod.Code:=PropInfo^.GetProc
  1287. else
  1288. AMethod.Code:=PPointer(Pointer(Instance.ClassType)+PtrUInt(PropInfo^.GetProc))^;
  1289. AMethod.Data:=Instance;
  1290. if ((PropInfo^.PropProcs shr 6) and 1)<>0 then
  1291. Result:=TGetWideStrProcIndex(AMethod)(PropInfo^.Index)
  1292. else
  1293. Result:=TGetWideStrProc(AMethod)();
  1294. end;
  1295. end;
  1296. end;
  1297. end;
  1298. end;
  1299. Procedure SetWideStrProp(Instance: TObject; PropInfo: PPropInfo; const Value: WideString);
  1300. type
  1301. TSetWideStrProcIndex=procedure(index:longint;s:WideString) of object;
  1302. TSetWideStrProc=procedure(s:WideString) of object;
  1303. var
  1304. AMethod : TMethod;
  1305. begin
  1306. case Propinfo^.PropType^.Kind of
  1307. tkSString,tkAString:
  1308. SetStrProp(Instance,PropInfo,Value);
  1309. tkUString:
  1310. SetUnicodeStrProp(Instance,PropInfo,Value);
  1311. tkWString:
  1312. begin
  1313. case (PropInfo^.PropProcs shr 2) and 3 of
  1314. ptField:
  1315. PWideString(Pointer(Instance)+PtrUInt(PropInfo^.SetProc))^:=Value;
  1316. ptstatic,
  1317. ptvirtual :
  1318. begin
  1319. if ((PropInfo^.PropProcs shr 2) and 3)=ptStatic then
  1320. AMethod.Code:=PropInfo^.SetProc
  1321. else
  1322. AMethod.Code:=PPointer(Pointer(Instance.ClassType)+PtrUInt(PropInfo^.SetProc))^;
  1323. AMethod.Data:=Instance;
  1324. if ((PropInfo^.PropProcs shr 6) and 1)<>0 then
  1325. TSetWideStrProcIndex(AMethod)(PropInfo^.Index,Value)
  1326. else
  1327. TSetWideStrProc(AMethod)(Value);
  1328. end;
  1329. end;
  1330. end;
  1331. end;
  1332. end;
  1333. Function GetUnicodeStrProp(Instance: TObject; const PropName: string): UnicodeString;
  1334. begin
  1335. Result:=GetUnicodeStrProp(Instance, FindPropInfo(Instance, PropName));
  1336. end;
  1337. procedure SetUnicodeStrProp(Instance: TObject; const PropName: string; const Value: UnicodeString);
  1338. begin
  1339. SetUnicodeStrProp(Instance,FindPropInfo(Instance,PropName),Value);
  1340. end;
  1341. Function GetUnicodeStrProp(Instance: TObject; PropInfo: PPropInfo): UnicodeString;
  1342. type
  1343. TGetUnicodeStrProcIndex=function(index:longint):UnicodeString of object;
  1344. TGetUnicodeStrProc=function():UnicodeString of object;
  1345. var
  1346. AMethod : TMethod;
  1347. begin
  1348. Result:='';
  1349. case Propinfo^.PropType^.Kind of
  1350. tkSString,tkAString:
  1351. Result:=GetStrProp(Instance,PropInfo);
  1352. tkWString:
  1353. Result:=GetWideStrProp(Instance,PropInfo);
  1354. tkUString:
  1355. begin
  1356. case (PropInfo^.PropProcs) and 3 of
  1357. ptField:
  1358. Result := PUnicodeString(Pointer(Instance)+PtrUInt(PropInfo^.GetProc))^;
  1359. ptstatic,
  1360. ptvirtual :
  1361. begin
  1362. if (PropInfo^.PropProcs and 3)=ptStatic then
  1363. AMethod.Code:=PropInfo^.GetProc
  1364. else
  1365. AMethod.Code:=PPointer(Pointer(Instance.ClassType)+PtrUInt(PropInfo^.GetProc))^;
  1366. AMethod.Data:=Instance;
  1367. if ((PropInfo^.PropProcs shr 6) and 1)<>0 then
  1368. Result:=TGetUnicodeStrProcIndex(AMethod)(PropInfo^.Index)
  1369. else
  1370. Result:=TGetUnicodeStrProc(AMethod)();
  1371. end;
  1372. end;
  1373. end;
  1374. end;
  1375. end;
  1376. Procedure SetUnicodeStrProp(Instance: TObject; PropInfo: PPropInfo; const Value: UnicodeString);
  1377. type
  1378. TSetUnicodeStrProcIndex=procedure(index:longint;s:UnicodeString) of object;
  1379. TSetUnicodeStrProc=procedure(s:UnicodeString) of object;
  1380. var
  1381. AMethod : TMethod;
  1382. begin
  1383. case Propinfo^.PropType^.Kind of
  1384. tkSString,tkAString:
  1385. SetStrProp(Instance,PropInfo,Value);
  1386. tkWString:
  1387. SetWideStrProp(Instance,PropInfo,Value);
  1388. tkUString:
  1389. begin
  1390. case (PropInfo^.PropProcs shr 2) and 3 of
  1391. ptField:
  1392. PUnicodeString(Pointer(Instance)+PtrUInt(PropInfo^.SetProc))^:=Value;
  1393. ptstatic,
  1394. ptvirtual :
  1395. begin
  1396. if ((PropInfo^.PropProcs shr 2) and 3)=ptStatic then
  1397. AMethod.Code:=PropInfo^.SetProc
  1398. else
  1399. AMethod.Code:=PPointer(Pointer(Instance.ClassType)+PtrUInt(PropInfo^.SetProc))^;
  1400. AMethod.Data:=Instance;
  1401. if ((PropInfo^.PropProcs shr 6) and 1)<>0 then
  1402. TSetUnicodeStrProcIndex(AMethod)(PropInfo^.Index,Value)
  1403. else
  1404. TSetUnicodeStrProc(AMethod)(Value);
  1405. end;
  1406. end;
  1407. end;
  1408. end;
  1409. end;
  1410. {$ifndef FPUNONE}
  1411. { ---------------------------------------------------------------------
  1412. Float properties
  1413. ---------------------------------------------------------------------}
  1414. function GetFloatProp(Instance : TObject;PropInfo : PPropInfo) : Extended;
  1415. type
  1416. TGetExtendedProc = function:Extended of object;
  1417. TGetExtendedProcIndex = function(Index: integer): Extended of object;
  1418. TGetDoubleProc = function:Double of object;
  1419. TGetDoubleProcIndex = function(Index: integer): Double of object;
  1420. TGetSingleProc = function:Single of object;
  1421. TGetSingleProcIndex = function(Index: integer):Single of object;
  1422. TGetCurrencyProc = function : Currency of object;
  1423. TGetCurrencyProcIndex = function(Index: integer) : Currency of object;
  1424. var
  1425. AMethod : TMethod;
  1426. begin
  1427. Result:=0.0;
  1428. case PropInfo^.PropProcs and 3 of
  1429. ptField:
  1430. Case GetTypeData(PropInfo^.PropType)^.FloatType of
  1431. ftSingle:
  1432. Result:=PSingle(Pointer(Instance)+PtrUInt(PropInfo^.GetProc))^;
  1433. ftDouble:
  1434. Result:=PDouble(Pointer(Instance)+PtrUInt(PropInfo^.GetProc))^;
  1435. ftExtended:
  1436. Result:=PExtended(Pointer(Instance)+PtrUInt(PropInfo^.GetProc))^;
  1437. ftcomp:
  1438. Result:=PComp(Pointer(Instance)+PtrUInt(PropInfo^.GetProc))^;
  1439. ftcurr:
  1440. Result:=PCurrency(Pointer(Instance)+PtrUInt(PropInfo^.GetProc))^;
  1441. end;
  1442. ptStatic,
  1443. ptVirtual:
  1444. begin
  1445. if (PropInfo^.PropProcs and 3)=ptStatic then
  1446. AMethod.Code:=PropInfo^.GetProc
  1447. else
  1448. AMethod.Code:=PPointer(Pointer(Instance.ClassType)+PtrUInt(PropInfo^.GetProc))^;
  1449. AMethod.Data:=Instance;
  1450. Case GetTypeData(PropInfo^.PropType)^.FloatType of
  1451. ftSingle:
  1452. if ((PropInfo^.PropProcs shr 6) and 1)=0 then
  1453. Result:=TGetSingleProc(AMethod)()
  1454. else
  1455. Result:=TGetSingleProcIndex(AMethod)(PropInfo^.Index);
  1456. ftDouble:
  1457. if ((PropInfo^.PropProcs shr 6) and 1)=0 then
  1458. Result:=TGetDoubleProc(AMethod)()
  1459. else
  1460. Result:=TGetDoubleProcIndex(AMethod)(PropInfo^.Index);
  1461. ftExtended:
  1462. if ((PropInfo^.PropProcs shr 6) and 1)=0 then
  1463. Result:=TGetExtendedProc(AMethod)()
  1464. else
  1465. Result:=TGetExtendedProcIndex(AMethod)(PropInfo^.Index);
  1466. ftCurr:
  1467. if ((PropInfo^.PropProcs shr 6) and 1)=0 then
  1468. Result:=TGetCurrencyProc(AMethod)()
  1469. else
  1470. Result:=TGetCurrencyProcIndex(AMethod)(PropInfo^.Index);
  1471. end;
  1472. end;
  1473. end;
  1474. end;
  1475. Procedure SetFloatProp(Instance : TObject;PropInfo : PPropInfo; Value : Extended);
  1476. type
  1477. TSetExtendedProc = procedure(const AValue: Extended) of object;
  1478. TSetExtendedProcIndex = procedure(Index: integer; AValue: Extended) of object;
  1479. TSetDoubleProc = procedure(const AValue: Double) of object;
  1480. TSetDoubleProcIndex = procedure(Index: integer; AValue: Double) of object;
  1481. TSetSingleProc = procedure(const AValue: Single) of object;
  1482. TSetSingleProcIndex = procedure(Index: integer; AValue: Single) of object;
  1483. TSetCurrencyProc = procedure(const AValue: Currency) of object;
  1484. TSetCurrencyProcIndex = procedure(Index: integer; AValue: Currency) of object;
  1485. Var
  1486. AMethod : TMethod;
  1487. begin
  1488. case (PropInfo^.PropProcs shr 2) and 3 of
  1489. ptfield:
  1490. Case GetTypeData(PropInfo^.PropType)^.FloatType of
  1491. ftSingle:
  1492. PSingle(Pointer(Instance)+PtrUInt(PropInfo^.SetProc))^:=Value;
  1493. ftDouble:
  1494. PDouble(Pointer(Instance)+PtrUInt(PropInfo^.SetProc))^:=Value;
  1495. ftExtended:
  1496. PExtended(Pointer(Instance)+PtrUInt(PropInfo^.SetProc))^:=Value;
  1497. {$ifdef FPC_COMP_IS_INT64}
  1498. ftComp:
  1499. PComp(Pointer(Instance)+PtrUInt(PropInfo^.SetProc))^:=trunc(Value);
  1500. {$else FPC_COMP_IS_INT64}
  1501. ftComp:
  1502. PComp(Pointer(Instance)+PtrUInt(PropInfo^.SetProc))^:=Comp(Value);
  1503. {$endif FPC_COMP_IS_INT64}
  1504. ftCurr:
  1505. PCurrency(Pointer(Instance)+PtrUInt(PropInfo^.SetProc))^:=Value;
  1506. end;
  1507. ptStatic,
  1508. ptVirtual:
  1509. begin
  1510. if ((PropInfo^.PropProcs shr 2) and 3)=ptStatic then
  1511. AMethod.Code:=PropInfo^.SetProc
  1512. else
  1513. AMethod.Code:=PPointer(Pointer(Instance.ClassType)+PtrUInt(PropInfo^.SetProc))^;
  1514. AMethod.Data:=Instance;
  1515. Case GetTypeData(PropInfo^.PropType)^.FloatType of
  1516. ftSingle:
  1517. if ((PropInfo^.PropProcs shr 6) and 1)=0 then
  1518. TSetSingleProc(AMethod)(Value)
  1519. else
  1520. TSetSingleProcIndex(AMethod)(PropInfo^.Index,Value);
  1521. ftDouble:
  1522. if ((PropInfo^.PropProcs shr 6) and 1)=0 then
  1523. TSetDoubleProc(AMethod)(Value)
  1524. else
  1525. TSetDoubleProcIndex(AMethod)(PropInfo^.Index,Value);
  1526. ftExtended:
  1527. if ((PropInfo^.PropProcs shr 6) and 1)=0 then
  1528. TSetExtendedProc(AMethod)(Value)
  1529. else
  1530. TSetExtendedProcIndex(AMethod)(PropInfo^.Index,Value);
  1531. ftCurr:
  1532. if ((PropInfo^.PropProcs shr 6) and 1)=0 then
  1533. TSetCurrencyProc(AMethod)(Value)
  1534. else
  1535. TSetCurrencyProcIndex(AMethod)(PropInfo^.Index,Value);
  1536. end;
  1537. end;
  1538. end;
  1539. end;
  1540. function GetFloatProp(Instance: TObject; const PropName: string): Extended;
  1541. begin
  1542. Result:=GetFloatProp(Instance,FindPropInfo(Instance,PropName))
  1543. end;
  1544. Procedure SetFloatProp(Instance: TObject; const PropName: string; Value: Extended);
  1545. begin
  1546. SetFloatProp(Instance,FindPropInfo(Instance,PropName),Value);
  1547. end;
  1548. {$endif}
  1549. { ---------------------------------------------------------------------
  1550. Method properties
  1551. ---------------------------------------------------------------------}
  1552. Function GetMethodProp(Instance : TObject;PropInfo : PPropInfo) : TMethod;
  1553. type
  1554. TGetMethodProcIndex=function(Index: Longint): TMethod of object;
  1555. TGetMethodProc=function(): TMethod of object;
  1556. var
  1557. value: PMethod;
  1558. AMethod : TMethod;
  1559. begin
  1560. Result.Code:=nil;
  1561. Result.Data:=nil;
  1562. case (PropInfo^.PropProcs) and 3 of
  1563. ptfield:
  1564. begin
  1565. Value:=PMethod(Pointer(Instance)+PtrUInt(PropInfo^.GetProc));
  1566. if Value<>nil then
  1567. Result:=Value^;
  1568. end;
  1569. ptstatic,
  1570. ptvirtual :
  1571. begin
  1572. if (PropInfo^.PropProcs and 3)=ptStatic then
  1573. AMethod.Code:=PropInfo^.GetProc
  1574. else
  1575. AMethod.Code:=PPointer(Pointer(Instance.ClassType)+PtrUInt(PropInfo^.GetProc))^;
  1576. AMethod.Data:=Instance;
  1577. if ((PropInfo^.PropProcs shr 6) and 1)<>0 then
  1578. Result:=TGetMethodProcIndex(AMethod)(PropInfo^.Index)
  1579. else
  1580. Result:=TGetMethodProc(AMethod)();
  1581. end;
  1582. end;
  1583. end;
  1584. Procedure SetMethodProp(Instance : TObject;PropInfo : PPropInfo; const Value : TMethod);
  1585. type
  1586. TSetMethodProcIndex=procedure(index:longint;p:TMethod) of object;
  1587. TSetMethodProc=procedure(p:TMethod) of object;
  1588. var
  1589. AMethod : TMethod;
  1590. begin
  1591. case (PropInfo^.PropProcs shr 2) and 3 of
  1592. ptfield:
  1593. PMethod(Pointer(Instance)+PtrUInt(PropInfo^.SetProc))^ := Value;
  1594. ptstatic,
  1595. ptvirtual :
  1596. begin
  1597. if ((PropInfo^.PropProcs shr 2) and 3)=ptStatic then
  1598. AMethod.Code:=PropInfo^.SetProc
  1599. else
  1600. AMethod.Code:=PPointer(Pointer(Instance.ClassType)+PtrUInt(PropInfo^.SetProc))^;
  1601. AMethod.Data:=Instance;
  1602. if ((PropInfo^.PropProcs shr 6) and 1)<>0 then
  1603. TSetMethodProcIndex(AMethod)(PropInfo^.Index,Value)
  1604. else
  1605. TSetMethodProc(AMethod)(Value);
  1606. end;
  1607. end;
  1608. end;
  1609. Function GetMethodProp(Instance: TObject; const PropName: string): TMethod;
  1610. begin
  1611. Result:=GetMethodProp(Instance,FindPropInfo(Instance,PropName));
  1612. end;
  1613. Procedure SetMethodProp(Instance: TObject; const PropName: string; const Value: TMethod);
  1614. begin
  1615. SetMethodProp(Instance,FindPropInfo(Instance,PropName),Value);
  1616. end;
  1617. { ---------------------------------------------------------------------
  1618. Variant properties
  1619. ---------------------------------------------------------------------}
  1620. Procedure CheckVariantEvent(P : Pointer);
  1621. begin
  1622. If (P=Nil) then
  1623. Raise Exception.Create(SErrNoVariantSupport);
  1624. end;
  1625. Function GetVariantProp(Instance : TObject;PropInfo : PPropInfo): Variant;
  1626. begin
  1627. CheckVariantEvent(Pointer(OnGetVariantProp));
  1628. Result:=OnGetVariantProp(Instance,PropInfo);
  1629. end;
  1630. Procedure SetVariantProp(Instance : TObject;PropInfo : PPropInfo; const Value: Variant);
  1631. begin
  1632. CheckVariantEvent(Pointer(OnSetVariantProp));
  1633. OnSetVariantProp(Instance,PropInfo,Value);
  1634. end;
  1635. Function GetVariantProp(Instance: TObject; const PropName: string): Variant;
  1636. begin
  1637. Result:=GetVariantProp(Instance,FindPropInfo(Instance,PropName));
  1638. end;
  1639. Procedure SetVariantProp(Instance: TObject; const PropName: string; const Value: Variant);
  1640. begin
  1641. SetVariantprop(instance,FindpropInfo(Instance,PropName),Value);
  1642. end;
  1643. { ---------------------------------------------------------------------
  1644. All properties through variant.
  1645. ---------------------------------------------------------------------}
  1646. Function GetPropValue(Instance: TObject; const PropName: string): Variant;
  1647. begin
  1648. Result:=GetPropValue(Instance,PropName,True);
  1649. end;
  1650. Function GetPropValue(Instance: TObject; const PropName: string; PreferStrings: Boolean): Variant;
  1651. begin
  1652. CheckVariantEvent(Pointer(OnGetPropValue));
  1653. Result:=OnGetPropValue(Instance,PropName,PreferStrings)
  1654. end;
  1655. Procedure SetPropValue(Instance: TObject; const PropName: string; const Value: Variant);
  1656. begin
  1657. CheckVariantEvent(Pointer(OnSetPropValue));
  1658. OnSetPropValue(Instance,PropName,Value);
  1659. end;
  1660. { ---------------------------------------------------------------------
  1661. Easy access methods that appeared in Delphi 5
  1662. ---------------------------------------------------------------------}
  1663. Function IsPublishedProp(Instance: TObject; const PropName: string): Boolean;
  1664. begin
  1665. Result:=GetPropInfo(Instance,PropName)<>Nil;
  1666. end;
  1667. Function IsPublishedProp(AClass: TClass; const PropName: string): Boolean;
  1668. begin
  1669. Result:=GetPropInfo(AClass,PropName)<>Nil;
  1670. end;
  1671. Function PropIsType(Instance: TObject; const PropName: string; TypeKind: TTypeKind): Boolean;
  1672. begin
  1673. Result:=PropType(Instance,PropName)=TypeKind
  1674. end;
  1675. Function PropIsType(AClass: TClass; const PropName: string; TypeKind: TTypeKind): Boolean;
  1676. begin
  1677. Result:=PropType(AClass,PropName)=TypeKind
  1678. end;
  1679. Function PropType(Instance: TObject; const PropName: string): TTypeKind;
  1680. begin
  1681. Result:=FindPropInfo(Instance,PropName)^.PropType^.Kind;
  1682. end;
  1683. Function PropType(AClass: TClass; const PropName: string): TTypeKind;
  1684. begin
  1685. Result:=FindPropInfo(AClass,PropName)^.PropType^.Kind;
  1686. end;
  1687. Function IsStoredProp(Instance: TObject; const PropName: string): Boolean;
  1688. begin
  1689. Result:=IsStoredProp(instance,FindPropInfo(Instance,PropName));
  1690. end;
  1691. end.