typinfo.pp 59 KB

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