objpash.inc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2005 by the Free Pascal development team
  4. This unit makes Free Pascal as much as possible Delphi compatible,
  5. defining several internal structures for classes, interfaces, and
  6. resource strings.
  7. Additionally this file defines the interface of TObject, providing
  8. their basic implementation in the corresponding objpas.inc file.
  9. WARNING: IF YOU CHANGE SOME OF THESE INTERNAL RECORDS, MAKE SURE
  10. TO MODIFY THE COMPILER AND OBJPAS.INC ACCORDINGLY, OTHERWISE
  11. THIS WILL LEAD TO CRASHES IN THE RESULTING COMPILER AND/OR RTL.
  12. IN PARTICULAR, THE IMPLEMENTATION PART OF THIS INCLUDE FILE,
  13. OBJPAS.INC, USES SOME HARDCODED RECORD MEMBER OFFSETS.
  14. See the file COPYING.FPC, included in this distribution,
  15. for details about the copyright.
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  19. **********************************************************************}
  20. {*****************************************************************************
  21. Basic Types/constants
  22. *****************************************************************************}
  23. type
  24. TextFile = Text;
  25. PGuid = ^TGuid;
  26. TGuid = packed record
  27. case integer of
  28. 1 : (
  29. Data1 : DWord;
  30. Data2 : word;
  31. Data3 : word;
  32. Data4 : array[0..7] of byte;
  33. );
  34. 2 : (
  35. D1 : DWord;
  36. D2 : word;
  37. D3 : word;
  38. D4 : array[0..7] of byte;
  39. );
  40. 3 : ( { uuid fields according to RFC4122 }
  41. time_low : dword; // The low field of the timestamp
  42. time_mid : word; // The middle field of the timestamp
  43. time_hi_and_version : word; // The high field of the timestamp multiplexed with the version number
  44. clock_seq_hi_and_reserved : byte; // The high field of the clock sequence multiplexed with the variant
  45. clock_seq_low : byte; // The low field of the clock sequence
  46. node : array[0..5] of byte; // The spatially unique node identifier
  47. );
  48. end;
  49. {$ifdef FPC_HAS_FEATURE_CLASSES}
  50. const
  51. vmtInstanceSize = 0;
  52. vmtParent = sizeof(SizeInt)*2;
  53. { These were negative value's, but are now positive, else classes
  54. couldn't be used with shared linking which copies only all data from
  55. the .global directive and not the data before the directive (PFV) }
  56. vmtClassName = vmtParent+sizeof(pointer);
  57. vmtDynamicTable = vmtParent+sizeof(pointer)*2;
  58. vmtMethodTable = vmtParent+sizeof(pointer)*3;
  59. vmtFieldTable = vmtParent+sizeof(pointer)*4;
  60. vmtTypeInfo = vmtParent+sizeof(pointer)*5;
  61. vmtInitTable = vmtParent+sizeof(pointer)*6;
  62. vmtAutoTable = vmtParent+sizeof(pointer)*7;
  63. vmtIntfTable = vmtParent+sizeof(pointer)*8;
  64. vmtMsgStrPtr = vmtParent+sizeof(pointer)*9;
  65. { methods }
  66. vmtMethodStart = vmtParent+sizeof(pointer)*10;
  67. vmtDestroy = vmtMethodStart;
  68. vmtNewInstance = vmtMethodStart+sizeof(codepointer);
  69. vmtFreeInstance = vmtMethodStart+sizeof(codepointer)*2;
  70. vmtSafeCallException = vmtMethodStart+sizeof(codepointer)*3;
  71. vmtDefaultHandler = vmtMethodStart+sizeof(codepointer)*4;
  72. vmtAfterConstruction = vmtMethodStart+sizeof(codepointer)*5;
  73. vmtBeforeDestruction = vmtMethodStart+sizeof(codepointer)*6;
  74. vmtDefaultHandlerStr = vmtMethodStart+sizeof(codepointer)*7;
  75. vmtDispatch = vmtMethodStart+sizeof(codepointer)*8;
  76. vmtDispatchStr = vmtMethodStart+sizeof(codepointer)*9;
  77. vmtEquals = vmtMethodStart+sizeof(codepointer)*10;
  78. vmtGetHashCode = vmtMethodStart+sizeof(codepointer)*11;
  79. vmtToString = vmtMethodStart+sizeof(codepointer)*12;
  80. { IInterface }
  81. S_OK = 0;
  82. S_FALSE = 1;
  83. E_NOINTERFACE = hresult($80004002);
  84. E_UNEXPECTED = hresult($8000FFFF);
  85. E_NOTIMPL = hresult($80004001);
  86. type
  87. { now the let's declare the base classes for the class object
  88. model. The compiler expects TObject and IUnknown to be defined
  89. first as forward classes }
  90. TObject = class;
  91. IUnknown = interface;
  92. TClass = class of tobject;
  93. PClass = ^tclass;
  94. { to access the message table from outside }
  95. TMsgStrTable = record
  96. name : pshortstring;
  97. method : codepointer;
  98. end;
  99. PMsgStrTable = ^TMsgStrTable;
  100. TStringMessageTable = record
  101. count : longint;
  102. msgstrtable : array[0..0] of tmsgstrtable;
  103. end;
  104. pstringmessagetable = ^tstringmessagetable;
  105. pinterfacetable = ^tinterfacetable;
  106. PVmt = ^TVmt;
  107. PPVmt = ^PVmt;
  108. TVmt = record
  109. vInstanceSize: SizeInt;
  110. vInstanceSize2: SizeInt;
  111. vParentRef: {$ifdef VER3_0}PVmt{$else}PPVmt{$endif};
  112. vClassName: PShortString;
  113. vDynamicTable: Pointer;
  114. vMethodTable: Pointer;
  115. vFieldTable: Pointer;
  116. vTypeInfo: Pointer;
  117. vInitTable: Pointer;
  118. vAutoTable: Pointer;
  119. vIntfTable: PInterfaceTable;
  120. vMsgStrPtr: pstringmessagetable;
  121. vDestroy: CodePointer;
  122. vNewInstance: CodePointer;
  123. vFreeInstance: CodePointer;
  124. vSafeCallException: CodePointer;
  125. vDefaultHandler: CodePointer;
  126. vAfterConstruction: CodePointer;
  127. vBeforeDestruction: CodePointer;
  128. vDefaultHandlerStr: CodePointer;
  129. vDispatch: CodePointer;
  130. vDispatchStr: CodePointer;
  131. vEquals: CodePointer;
  132. vGetHashCode: CodePointer;
  133. vToString: CodePointer;
  134. private
  135. function GetvParent: PVmt; inline;
  136. public
  137. property vParent: PVmt read GetvParent;
  138. end;
  139. // This enumerate is found both in the rtl and compiler. Do not change the order of the fields.
  140. tinterfaceentrytype = (etStandard,
  141. etVirtualMethodResult,
  142. etStaticMethodResult,
  143. etFieldValue,
  144. etVirtualMethodClass,
  145. etStaticMethodClass,
  146. etFieldValueClass
  147. );
  148. pinterfaceentry = ^tinterfaceentry;
  149. tinterfaceentry = record
  150. private
  151. function GetIID: pguid; inline;
  152. function GetIIDStr: pshortstring; inline;
  153. public
  154. property IID: pguid read GetIID;
  155. property IIDStr: pshortstring read GetIIDStr;
  156. public
  157. IIDRef : {$IFNDEF VER3_0}^{$ENDIF}pguid; { if assigned(IID) then Com else Corba}
  158. VTable : Pointer;
  159. case integer of
  160. 1 : (
  161. IOffset: sizeuint;
  162. );
  163. 2 : (
  164. IOffsetAsCodePtr: CodePointer;
  165. IIDStrRef : {$IFNDEF VER3_0}^{$ENDIF}pshortstring; { never nil. Com: upper(GuidToString(IID^)) }
  166. IType : tinterfaceentrytype;
  167. );
  168. end;
  169. tinterfacetable = record
  170. EntryCount : sizeuint;
  171. Entries : array[0..0] of tinterfaceentry;
  172. end;
  173. PMethod = ^TMethod;
  174. TMethod = record
  175. Code : CodePointer;
  176. Data : Pointer;
  177. end;
  178. // "Maximum" available stringtype : Shortstring, AnsiString or WideString
  179. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  180. {$IFNDEF UNICODERTL}
  181. RTLString = ansistring;
  182. {$ELSE UNICODERTL}
  183. RTLString = unicodestring;
  184. {$ENDIF UNICODERTL}
  185. {$else FPC_HAS_FEATURE_ANSISTRINGS}
  186. RTLString = shortstring;
  187. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  188. TObject = class
  189. public
  190. { please don't change the order of virtual methods, because
  191. their vmt offsets are used by some assembler code which uses
  192. hard coded addresses (FK) }
  193. constructor Create;
  194. { the virtual procedures must be in THAT order }
  195. destructor Destroy;virtual;
  196. class function newinstance : tobject;virtual;
  197. procedure FreeInstance;virtual;
  198. function SafeCallException(exceptobject : tobject;
  199. exceptaddr : codepointer) : HResult;virtual;
  200. procedure DefaultHandler(var message);virtual;
  201. procedure Free;
  202. class function InitInstance(instance : pointer) : tobject;
  203. procedure CleanupInstance;
  204. class function ClassType : tclass;{$ifdef SYSTEMINLINE}inline;{$endif}
  205. class function ClassInfo : pointer;
  206. class function ClassName : shortstring;
  207. class function ClassNameIs(const name : RTLString) : boolean;
  208. class function ClassParent : tclass;{$ifdef SYSTEMINLINE}inline;{$endif}
  209. class function InstanceSize : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  210. class function InheritsFrom(aclass : tclass) : boolean;
  211. class function StringMessageTable : pstringmessagetable;
  212. class function MethodAddress(const name : shortstring) : codepointer;
  213. class function MethodName(address : codepointer) : shortstring;
  214. function FieldAddress(const name : shortstring) : pointer;
  215. { new since Delphi 4 }
  216. procedure AfterConstruction;virtual;
  217. procedure BeforeDestruction;virtual;
  218. { new for gtk, default handler for text based messages }
  219. procedure DefaultHandlerStr(var message);virtual;
  220. { message handling routines }
  221. procedure Dispatch(var message);virtual;
  222. procedure DispatchStr(var message);virtual;
  223. { interface functions }
  224. function GetInterface(const iid : tguid; out obj) : boolean;
  225. function GetInterface(const iidstr : shortstring;out obj) : boolean;
  226. function GetInterfaceByStr(const iidstr : shortstring; out obj) : boolean;
  227. function GetInterfaceWeak(const iid : tguid; out obj) : boolean; // equal to GetInterface but the interface returned is not referenced
  228. class function GetInterfaceEntry(const iid : tguid) : pinterfaceentry;
  229. class function GetInterfaceEntryByStr(const iidstr : shortstring) : pinterfaceentry;
  230. class function GetInterfaceTable : pinterfacetable;
  231. { new since Delphi 2009 }
  232. class function UnitName : RTLString;
  233. class function QualifiedClassName: RTLString;
  234. function Equals(Obj: TObject) : boolean;virtual;
  235. function GetHashCode: PtrInt;virtual;
  236. function ToString: RTLString; virtual;
  237. end;
  238. IUnknown = interface
  239. ['{00000000-0000-0000-C000-000000000046}']
  240. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  241. function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  242. function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  243. end;
  244. IInterface = IUnknown;
  245. {$M+}
  246. IInvokable = interface(IInterface)
  247. end;
  248. {$M-}
  249. { enumerator support }
  250. IEnumerator = interface(IInterface)
  251. function GetCurrent: TObject;
  252. function MoveNext: Boolean;
  253. procedure Reset;
  254. property Current: TObject read GetCurrent;
  255. end;
  256. IEnumerable = interface(IInterface)
  257. function GetEnumerator: IEnumerator;
  258. end;
  259. { for native dispinterface support }
  260. IDispatch = interface(IUnknown)
  261. ['{00020400-0000-0000-C000-000000000046}']
  262. function GetTypeInfoCount(out count : longint) : HResult;stdcall;
  263. function GetTypeInfo(Index,LocaleID : longint;
  264. out TypeInfo): HResult;stdcall;
  265. function GetIDsOfNames(const iid: TGUID; names: Pointer;
  266. NameCount, LocaleID: LongInt; DispIDs: Pointer) : HResult;stdcall;
  267. function Invoke(DispID: LongInt;const iid : TGUID;
  268. LocaleID : longint; Flags: Word;var params;
  269. VarResult,ExcepInfo,ArgErr : pointer) : HResult;stdcall;
  270. end;
  271. { TInterfacedObject }
  272. TInterfacedObject = class(TObject,IUnknown)
  273. protected
  274. FRefCount : longint;
  275. FDestroyCount : longint;
  276. { implement methods of IUnknown }
  277. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  278. function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  279. function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  280. public
  281. destructor Destroy; override;
  282. procedure AfterConstruction;override;
  283. procedure BeforeDestruction;override;
  284. class function NewInstance : TObject;override;
  285. property RefCount : longint read FRefCount;
  286. end;
  287. TInterfacedClass = class of TInterfacedObject;
  288. TAggregatedObject = class(TObject)
  289. private
  290. fcontroller: Pointer;
  291. function GetController: IUnknown;
  292. protected
  293. { implement methods of IUnknown }
  294. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  295. function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  296. function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  297. public
  298. constructor Create(const aController: IUnknown);
  299. property Controller : IUnknown read GetController;
  300. end;
  301. TContainedObject = class(TAggregatedObject,IInterface)
  302. protected
  303. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;virtual; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  304. end;
  305. TNoRefCountObject = class(TObject, IInterface)
  306. protected
  307. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  308. function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  309. function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  310. end;
  311. TInterfaceThunk = Class(TInterfacedObject)
  312. Public
  313. Type
  314. TArgData = record
  315. addr : pointer; // Location
  316. info : pointer; // type info (if available: nil for untyped args)
  317. idx : smallint; // param index in rtti
  318. ahigh : sizeint; // For open arrays, high()
  319. end;
  320. PArgData = ^TargData;
  321. TThunkCallBack = Procedure(aInstance: Pointer; aMethod,aCount : Longint; aData : PArgData) of object;
  322. Private
  323. FCallback : TThunkCallback;
  324. Protected
  325. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  326. Procedure Thunk(aMethod: Longint; aCount : Longint; aData : PArgData); virtual;
  327. Public
  328. constructor create(aCallBack : TThunkCallback);
  329. function InterfaceVMTOffset : word; virtual;
  330. end;
  331. TInterfaceThunkClass = class of TInterfaceThunk;
  332. { some pointer definitions }
  333. PUnknown = ^IUnknown;
  334. PPUnknown = ^PUnknown;
  335. PDispatch = ^IDispatch;
  336. PPDispatch = ^PDispatch;
  337. PInterface = PUnknown;
  338. {$ifdef FPC_USE_PSABIEH}
  339. {$if (defined(CPUARMEL) or defined(CPUARMHF)) and not defined(darwin)}
  340. {$define __ARM_EABI_UNWINDER__}
  341. {$endif}
  342. { needed here for TExceptObject (rest is in psabiehh.inc) }
  343. FPC_Unwind_Reason_Code = longint; {cint}
  344. FPC_Unwind_Action = longint; {cint}
  345. {$ifdef __ARM_EABI_UNWINDER__}
  346. FPC_Unwind_State = longint; {cint}
  347. {$endif}
  348. PFPC_Unwind_Exception = ^FPC_Unwind_Exception;
  349. FPC_Unwind_Exception_Cleanup_Fn =
  350. procedure(reason: FPC_Unwind_Reason_Code; exc: PFPC_Unwind_Exception); cdecl;
  351. FPC_Unwind_Exception = record
  352. { qword instead of array of AnsiChar to ensure proper alignment and
  353. padding, and also easier to compare }
  354. exception_class: qword;
  355. exception_cleanup: FPC_Unwind_Exception_Cleanup_Fn;
  356. {$ifdef __ARM_EABI_UNWINDER__}
  357. { rest of UCB }
  358. // Unwinder cache, private fields for the unwinder's use
  359. unwinder_cache: record
  360. reserved1, // init reserved1 to 0, then don't touch
  361. reserved2,
  362. reserved3,
  363. reserved4,
  364. reserved5: UInt32;
  365. end;
  366. // Propagation barrier cache (valid after phase 1):
  367. barrier_cache: record
  368. sp: PtrUInt;
  369. bitpattern: array[0..4] of UInt32;
  370. end;
  371. // Cleanup cache (preserved over cleanup):
  372. cleanup_cache: record
  373. bitpattern: array[0..3] of UInt32;
  374. end;
  375. // Pr cache (for pr's benefit):
  376. pr_cache: record
  377. fnstart: UInt32; // function start address
  378. ehtp: pointer; // pointer to EHT entry header word
  379. additional: UInt32; // additional data
  380. reserved1: UInt32;
  381. end;
  382. {$else}
  383. private_1: ptruint;
  384. private_2: ptruint;
  385. private_3: ptruint;
  386. private_4: ptruint;
  387. private_5: ptruint;
  388. private_6: ptruint;
  389. {$endif}
  390. end;
  391. {$endif FPC_USE_PSABIEH}
  392. TExceptProc = Procedure (Obj : TObject; Addr : CodePointer; FrameCount:Longint; Frame: PCodePointer);
  393. { Exception object stack }
  394. PExceptObject = ^TExceptObject;
  395. TExceptObject = record
  396. FObject : TObject;
  397. Addr : codepointer;
  398. Next : PExceptObject;
  399. refcount : Longint;
  400. Framecount : Longint;
  401. Frames : PCodePointer;
  402. {$ifdef FPC_USE_WIN32_SEH}
  403. SEHFrame : Pointer;
  404. ExceptRec : Pointer;
  405. ReraiseBuf : jmp_buf;
  406. {$endif FPC_USE_WIN32_SEH}
  407. {$ifdef FPC_USE_PSABIEH}
  408. {$ifndef __ARM_EABI_UNWINDER__}
  409. { cached info from unwind phase for action phase }
  410. handler_switch_value: longint;
  411. language_specific_data: PByte;
  412. landing_pad: PtrUInt;
  413. {$endif __ARM_EABI_UNWINDER__}
  414. { libunwind exception handling data (must be last!) }
  415. unwind_exception: FPC_Unwind_Exception;
  416. {$endif FPC_USE_PSABIEH}
  417. end;
  418. {$PUSH}
  419. { disable the warning that the constructor should be public }
  420. {$WARN 3018 OFF}
  421. TCustomAttribute = class(TObject)
  422. private
  423. { if the user wants to use a parameterless constructor they need to
  424. explicitely declare it in their type }
  425. constructor Create;
  426. end;
  427. {$POP}
  428. WeakAttribute = class(TCustomAttribute);
  429. UnsafeAttribute = class(TCustomAttribute);
  430. RefAttribute = class(TCustomAttribute);
  431. VolatileAttribute = class(TCustomAttribute);
  432. StoredAttribute = Class(TCustomAttribute)
  433. Private
  434. FFlag : Boolean;
  435. FName : ShortString;
  436. Public
  437. Constructor Create;
  438. Constructor Create(Const aFlag : Boolean);
  439. Constructor Create(Const aName : ShortString);
  440. Property Flag : Boolean Read FFlag;
  441. Property Name : ShortString Read FName;
  442. end;
  443. Const
  444. ExceptProc : TExceptProc = Nil;
  445. RaiseProc : TExceptProc = Nil;
  446. RaiseMaxFrameCount : Longint = 16;
  447. Function RaiseList : PExceptObject;
  448. { @abstract(increase exception reference count)
  449. When leaving an except block, the exception object is normally
  450. freed automatically. To avoid this, call this function.
  451. If within the exception object you decide that you don't need
  452. the exception after all, call @link(ReleaseExceptionObject).
  453. Otherwise, if the reference count is > 0, the exception object
  454. goes into your "property" and you need to free it manually.
  455. The effect of this function is countered by re-raising an exception
  456. via "raise;", this zeroes the reference count again.
  457. Calling this method is only valid within an except block.
  458. @return(pointer to the exception object) }
  459. function AcquireExceptionObject: Pointer;
  460. { @abstract(decrease exception reference count)
  461. After calling @link(AcquireExceptionObject) you can call this method
  462. to decrease the exception reference count again.
  463. If the reference count is > 0, the exception object
  464. goes into your "property" and you need to free it manually.
  465. Calling this method is only valid within an except block. }
  466. procedure ReleaseExceptionObject;
  467. const
  468. { for safe as operator support }
  469. IObjectInstance: TGuid = '{D91C9AF4-3C93-420F-A303-BF5BA82BFD23}';
  470. {$endif FPC_HAS_FEATURE_CLASSES}
  471. {*****************************************************************************
  472. Array of const support
  473. *****************************************************************************}
  474. const
  475. vtInteger = 0;
  476. vtBoolean = 1;
  477. vtChar = 2;
  478. {$ifndef FPUNONE}
  479. vtExtended = 3;
  480. {$endif}
  481. vtString = 4;
  482. vtPointer = 5;
  483. vtPChar = 6;
  484. vtObject = 7;
  485. vtClass = 8;
  486. vtWideChar = 9;
  487. vtPWideChar = 10;
  488. vtAnsiString = 11;
  489. vtCurrency = 12;
  490. vtVariant = 13;
  491. vtInterface = 14;
  492. vtWideString = 15;
  493. vtInt64 = 16;
  494. vtQWord = 17;
  495. vtUnicodeString = 18;
  496. type
  497. PVarRec = ^TVarRec;
  498. TVarRec = record
  499. case VType : sizeint of
  500. {$ifdef ENDIAN_BIG}
  501. vtInteger : ({$IFDEF CPU64}integerdummy1 : Longint;{$ENDIF CPU64}VInteger: Longint);
  502. vtBoolean : ({$IFDEF CPU64}booldummy : Longint;{$ENDIF CPU64}booldummy1,booldummy2,booldummy3: byte; VBoolean: Boolean);
  503. vtChar : ({$IFDEF CPU64}chardummy : Longint;{$ENDIF CPU64}chardummy1,chardummy2,chardummy3: byte; VChar: AnsiChar);
  504. vtWideChar : ({$IFDEF CPU64}widechardummy : Longint;{$ENDIF CPU64}wchardummy1,VWideChar: WideChar);
  505. {$else ENDIAN_BIG}
  506. vtInteger : (VInteger: Longint);
  507. vtBoolean : (VBoolean: Boolean);
  508. vtChar : (VChar: AnsiChar);
  509. vtWideChar : (VWideChar: WideChar);
  510. {$endif ENDIAN_BIG}
  511. {$ifndef FPUNONE}
  512. vtExtended : (VExtended: PExtended);
  513. {$endif}
  514. vtString : (VString: PShortString);
  515. vtPointer : (VPointer: Pointer);
  516. vtPChar : (VPChar: PAnsiChar);
  517. {$ifdef FPC_HAS_FEATURE_CLASSES}
  518. vtObject : (VObject: TObject);
  519. vtClass : (VClass: TClass);
  520. {$endif FPC_HAS_FEATURE_CLASSES}
  521. vtPWideChar : (VPWideChar: PWideChar);
  522. vtAnsiString : (VAnsiString: Pointer);
  523. vtCurrency : (VCurrency: PCurrency);
  524. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  525. vtVariant : (VVariant: PVariant);
  526. {$endif FPC_HAS_FEATURE_VARIANTS}
  527. vtInterface : (VInterface: Pointer);
  528. vtWideString : (VWideString: Pointer);
  529. vtInt64 : (VInt64: PInt64);
  530. vtUnicodeString : (VUnicodeString: Pointer);
  531. vtQWord : (VQWord: PQWord);
  532. end;
  533. var
  534. DispCallByIDProc : codepointer;
  535. {*****************************************************************************
  536. Resourcestring support
  537. *****************************************************************************}
  538. {$ifdef FPC_HAS_FEATURE_RESOURCES}
  539. type
  540. PResourceStringRecord = ^TResourceStringRecord;
  541. TResourceStringRecord = Record
  542. Name : AnsiString;
  543. CurrentValue,
  544. DefaultValue : RTLString;
  545. HashValue : LongWord;
  546. end;
  547. {$endif FPC_HAS_FEATURE_RESOURCES}
  548. {*****************************************************************************
  549. Various Delphi elements
  550. *****************************************************************************}
  551. Type
  552. TPtrWrapper = record
  553. private
  554. FValue: Pointer;
  555. class function GetNilValue: TPtrWrapper; inline; static;
  556. public
  557. constructor Create(AValue: PtrInt); overload;
  558. constructor Create(AValue: Pointer); overload;
  559. function ToPointer: Pointer; inline;
  560. function ToInteger: PtrInt; inline;
  561. class property NilValue: TPtrWrapper read GetNilValue;
  562. class operator =(Left, Right: TPtrWrapper): Boolean; inline;
  563. { ...to allow convenient and direct reading without relying on inline... and convenient writing from SysUtils until TMarshal is moved here... }
  564. property Value: Pointer read FValue write FValue;
  565. end;
  566. TPtrWrapperArray = Array of TPtrWrapper;
  567. { Generic array type.
  568. Slightly Less useful in FPC, since dyn array compatibility is at the element level.
  569. But still useful for generic methods and of course Delphi compatibility}
  570. generic TArray<T> = array of T;
  571. TMarshal = class sealed
  572. public
  573. Type
  574. TUnicodeCharArray = Array of UnicodeChar;
  575. Tint8Array = Array of int8;
  576. Tint16Array = Array of int16;
  577. Tint32Array = Array of int32;
  578. Tint64Array = Array of int64;
  579. TUint8Array = Array of Uint8;
  580. TUint16Array = Array of Uint16;
  581. TUint32Array = Array of Uint32;
  582. TUint64Array = Array of Uint64;
  583. Public
  584. constructor Create;
  585. class function AllocMem(Size: SizeInt): TPtrWrapper; static; inline;
  586. class function ReallocMem(OldPtr: TPtrWrapper; NewSize: SizeInt): TPtrWrapper; static; inline;
  587. class procedure FreeMem(Ptr: TPtrWrapper); static; inline;
  588. class procedure Move(Src, Dest: TPtrWrapper; Count: SizeInt); static; inline;
  589. class function UnsafeAddrOf(var Value): TPtrWrapper; static; inline;
  590. {$IFDEF FPC_HAS_FEATURE_UNICODESTRINGS}
  591. class function AsAnsi(const S: UnicodeString): AnsiString; static; inline;
  592. class function AsAnsi(S: PUnicodeChar): AnsiString; static; inline;
  593. class function InOutString(const S: UnicodeString): PUnicodeChar; static; inline;
  594. class function InString(const S: UnicodeString): PUnicodeChar; static; inline;
  595. class function OutString(const S: UnicodeString): PUnicodeChar; static; inline;
  596. class function AllocStringAsAnsi(const Str: UnicodeString): TPtrWrapper; static; inline;
  597. class function AllocStringAsAnsi(const Str: UnicodeString; CodePage: Word): TPtrWrapper; static; inline;
  598. class function AllocStringAsAnsi(S: PUnicodeChar): TPtrWrapper; static; inline;
  599. class function AllocStringAsAnsi(S: PUnicodeChar; CodePage: Word): TPtrWrapper; static; inline;
  600. class function AllocStringAsUnicode(const Str: UnicodeString): TPtrWrapper; static;
  601. class function AllocStringAsUtf8(const Str: UnicodeString): TPtrWrapper; static; inline;
  602. class function AllocStringAsUtf8(S: PUnicodeChar): TPtrWrapper; static; inline;
  603. { Generalization of all AllocStringAsAnsi* above, public because used in TMarshaller. }
  604. class function AllocStringAsAnsi(S: PUnicodeChar; Len: SizeInt; CodePage: Word): TPtrWrapper; static;
  605. class procedure Copy(const Src: TUnicodeCharArray; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  606. class function FixString(var Str: UnicodeString): TPtrWrapper; static;
  607. class function UnsafeFixString(const Str: UnicodeString): TPtrWrapper; static;
  608. class procedure UnfixString(Ptr: TPtrWrapper); static;
  609. class function ReadStringAsAnsi(Ptr: TPtrWrapper; Len: SizeInt = -1): UnicodeString; static; inline;
  610. class function ReadStringAsAnsi(CodePage: Word; Ptr: TPtrWrapper; Len: SizeInt = -1): UnicodeString; static;
  611. class function ReadStringAsAnsiUpTo(CodePage: Word; Ptr: TPtrWrapper; MaxLen: SizeInt): UnicodeString; static;
  612. class procedure WriteStringAsAnsi(Ptr: TPtrWrapper; const Value: UnicodeString; MaxCharsIncNull: SizeInt); static; inline;
  613. class procedure WriteStringAsAnsi(Ptr: TPtrWrapper; const Value: UnicodeString; MaxCharsIncNull: SizeInt; CodePage: Word); static; inline;
  614. class procedure WriteStringAsAnsi(Ptr: TPtrWrapper; Ofs: SizeInt; const Value: UnicodeString; MaxCharsIncNull: SizeInt); static; inline;
  615. class procedure WriteStringAsAnsi(Ptr: TPtrWrapper; Ofs: SizeInt; const Value: UnicodeString; MaxCharsIncNull: SizeInt; CodePage: Word); static;
  616. class function ReadStringAsUnicode(Ptr: TPtrWrapper; Len: SizeInt = -1): UnicodeString; static;
  617. class function ReadStringAsUnicodeUpTo(Ptr: TPtrWrapper; MaxLen: SizeInt): UnicodeString; static;
  618. class procedure WriteStringAsUnicode(Ptr: TPtrWrapper; const Value: UnicodeString; MaxCharsIncNull: SizeInt); static;
  619. class procedure WriteStringAsUnicode(Ptr: TPtrWrapper; Ofs: SizeInt; const Value: UnicodeString; MaxCharsIncNull: SizeInt); static;
  620. class function ReadStringAsUtf8(Ptr: TPtrWrapper; Len: SizeInt = -1): UnicodeString; static; inline;
  621. class function ReadStringAsUtf8UpTo(Ptr: TPtrWrapper; MaxLen: SizeInt): UnicodeString; static; inline;
  622. class procedure WriteStringAsUtf8(Ptr: TPtrWrapper; const Value: UnicodeString; MaxCharsIncNull: SizeInt); static; inline;
  623. class procedure WriteStringAsUtf8(Ptr: TPtrWrapper; Ofs: SizeInt; const Value: UnicodeString; MaxCharsIncNull: SizeInt); static; inline;
  624. class procedure Copy(Src: TPtrWrapper; var Dest: TUnicodeCharArray; StartIndex: SizeInt; Count: SizeInt); static; inline;
  625. {$ENDIF}
  626. class procedure Copy(const Src: TUint8Array; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  627. class procedure Copy(Src: TPtrWrapper; var Dest: TUint8Array; StartIndex: SizeInt; Count: SizeInt); static; inline;
  628. class procedure Copy(const Src: TInt8Array; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  629. class procedure Copy(Src: TPtrWrapper; var Dest: TInt8Array; StartIndex: SizeInt; Count: SizeInt); static; inline;
  630. class procedure Copy(const Src: TUInt16Array; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  631. class procedure Copy(Src: TPtrWrapper; var Dest: TUInt16Array; StartIndex: SizeInt; Count: SizeInt); static; inline;
  632. class procedure Copy(const Src: TInt16Array; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  633. class procedure Copy(Src: TPtrWrapper; var Dest: TInt16Array; StartIndex: SizeInt; Count: SizeInt); static; inline;
  634. class procedure Copy(const Src: TInt32Array; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  635. class procedure Copy(Src: TPtrWrapper; var Dest: TInt32Array; StartIndex: SizeInt; Count: SizeInt); static; inline;
  636. class procedure Copy(const Src: TInt64Array; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  637. class procedure Copy(Src: TPtrWrapper; var Dest: TInt64Array; StartIndex: SizeInt; Count: SizeInt); static; inline;
  638. class procedure Copy(const Src: TPtrWrapperArray; StartIndex: SizeInt; Dest: TPtrWrapper; Count: SizeInt); static; inline;
  639. class procedure Copy(Src: TPtrWrapper; var Dest: TPtrWrapperArray; StartIndex: SizeInt; Count: SizeInt); static; inline;
  640. generic class function FixArray<T>(const Arr: specialize TArray<T>): TPtrWrapper; static;
  641. generic class procedure UnfixArray<T>(ArrPtr: TPtrWrapper); static;
  642. class function ReadByte(Ptr: TPtrWrapper; Ofs: SizeInt = 0): Byte; static; inline;
  643. class procedure WriteByte(Ptr: TPtrWrapper; Ofs: SizeInt; Value: Byte); static; inline;
  644. class procedure WriteByte(Ptr: TPtrWrapper; Value: Byte); static; inline;
  645. class function ReadInt16(Ptr: TPtrWrapper; Ofs: SizeInt = 0): Int16; static; inline;
  646. class procedure WriteInt16(Ptr: TPtrWrapper; Ofs: SizeInt; Value: Int16); static; inline;
  647. class procedure WriteInt16(Ptr: TPtrWrapper; Value: Int16); static; inline;
  648. class function ReadInt32(Ptr: TPtrWrapper; Ofs: SizeInt = 0): Int32; static; inline;
  649. class procedure WriteInt32(Ptr: TPtrWrapper; Ofs: SizeInt; Value: Int32); static; inline;
  650. class procedure WriteInt32(Ptr: TPtrWrapper; Value: Int32); static; inline;
  651. class function ReadInt64(Ptr: TPtrWrapper; Ofs: SizeInt = 0): Int64; static; inline;
  652. class procedure WriteInt64(Ptr: TPtrWrapper; Ofs: SizeInt; Value: Int64); static; inline;
  653. class procedure WriteInt64(Ptr: TPtrWrapper; Value: Int64); static; inline;
  654. class function ReadPtr(Ptr: TPtrWrapper; Ofs: SizeInt = 0): TPtrWrapper; static; inline;
  655. class procedure WritePtr(Ptr: TPtrWrapper; Ofs: SizeInt; Value: TPtrWrapper); static; inline;
  656. class procedure WritePtr(Ptr, Value: TPtrWrapper); static; inline;
  657. end;