objpash.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. const
  24. vmtInstanceSize = 0;
  25. vmtParent = sizeof(ptruint)*2;
  26. { These were negative value's, but are now positive, else classes
  27. couldn't be used with shared linking which copies only all data from
  28. the .global directive and not the data before the directive (PFV) }
  29. vmtClassName = vmtParent+sizeof(pointer);
  30. vmtDynamicTable = vmtParent+sizeof(pointer)*2;
  31. vmtMethodTable = vmtParent+sizeof(pointer)*3;
  32. vmtFieldTable = vmtParent+sizeof(pointer)*4;
  33. vmtTypeInfo = vmtParent+sizeof(pointer)*5;
  34. vmtInitTable = vmtParent+sizeof(pointer)*6;
  35. vmtAutoTable = vmtParent+sizeof(pointer)*7;
  36. vmtIntfTable = vmtParent+sizeof(pointer)*8;
  37. vmtMsgStrPtr = vmtParent+sizeof(pointer)*9;
  38. { methods }
  39. vmtMethodStart = vmtParent+sizeof(pointer)*10;
  40. vmtDestroy = vmtMethodStart;
  41. vmtNewInstance = vmtMethodStart+sizeof(pointer);
  42. vmtFreeInstance = vmtMethodStart+sizeof(pointer)*2;
  43. vmtSafeCallException = vmtMethodStart+sizeof(pointer)*3;
  44. vmtDefaultHandler = vmtMethodStart+sizeof(pointer)*4;
  45. vmtAfterConstruction = vmtMethodStart+sizeof(pointer)*5;
  46. vmtBeforeDestruction = vmtMethodStart+sizeof(pointer)*6;
  47. vmtDefaultHandlerStr = vmtMethodStart+sizeof(pointer)*7;
  48. { IInterface }
  49. S_OK = 0;
  50. S_FALSE = 1;
  51. E_NOINTERFACE = hresult($80004002);
  52. E_UNEXPECTED = hresult($8000FFFF);
  53. E_NOTIMPL = hresult($80004001);
  54. type
  55. TextFile = Text;
  56. { now the let's declare the base classes for the class object
  57. model. The compiler expects TObject and IUnknown to be defined
  58. first as forward classes }
  59. TObject = class;
  60. IUnknown = interface;
  61. TClass = class of tobject;
  62. PClass = ^tclass;
  63. { to access the message table from outside }
  64. TMsgStrTable = record
  65. name : pshortstring;
  66. method : pointer;
  67. end;
  68. PMsgStrTable = ^TMsgStrTable;
  69. TStringMessageTable = record
  70. count : ptruint;
  71. msgstrtable : array[0..0] of tmsgstrtable;
  72. end;
  73. pstringmessagetable = ^tstringmessagetable;
  74. PGuid = ^TGuid;
  75. TGuid = packed record
  76. case integer of
  77. 1 : (
  78. Data1 : DWord;
  79. Data2 : word;
  80. Data3 : word;
  81. Data4 : array[0..7] of byte;
  82. );
  83. 2 : (
  84. D1 : DWord;
  85. D2 : word;
  86. D3 : word;
  87. D4 : array[0..7] of byte;
  88. );
  89. 3 : ( { uuid fields according to RFC4122 }
  90. time_low : dword; // The low field of the timestamp
  91. time_mid : word; // The middle field of the timestamp
  92. time_hi_and_version : word; // The high field of the timestamp multiplexed with the version number
  93. clock_seq_hi_and_reserved : byte; // The high field of the clock sequence multiplexed with the variant
  94. clock_seq_low : byte; // The low field of the clock sequence
  95. node : array[0..5] of byte; // The spatially unique node identifier
  96. );
  97. end;
  98. // This enumerate is found both in the rtl and compiler. Do not change the order of the fields.
  99. tinterfaceentrytype = (etStandard, etVirtualMethodResult, etStaticMethodResult, etFieldValue);
  100. pinterfaceentry = ^tinterfaceentry;
  101. tinterfaceentry = record
  102. IID : pguid; { if assigned(IID) then Com else Corba}
  103. VTable : Pointer;
  104. IOffset : ptruint;
  105. IIDStr : pshortstring; { never nil. Com: upper(GuidToString(IID^)) }
  106. case boolean of
  107. {$ifdef ENDIAN_BIG}
  108. true : ({$IFDEF CPU64}__pad: longint;{$ENDIF}IType : tinterfaceentrytype);
  109. {$else ENDIAN_BIG}
  110. true : (IType : tinterfaceentrytype);
  111. {$endif ENDIAN_BIG}
  112. false : (__pad_dummy : pointer);
  113. end;
  114. pinterfacetable = ^tinterfacetable;
  115. tinterfacetable = record
  116. EntryCount : ptruint;
  117. Entries : array[0..0] of tinterfaceentry;
  118. end;
  119. TMethod = record
  120. Code, Data : Pointer;
  121. end;
  122. TObject = class
  123. public
  124. { please don't change the order of virtual methods, because
  125. their vmt offsets are used by some assembler code which uses
  126. hard coded addresses (FK) }
  127. constructor Create;
  128. { the virtual procedures must be in THAT order }
  129. destructor Destroy;virtual;
  130. class function newinstance : tobject;virtual;
  131. procedure FreeInstance;virtual;
  132. function SafeCallException(exceptobject : tobject;
  133. exceptaddr : pointer) : longint;virtual;
  134. procedure DefaultHandler(var message);virtual;
  135. procedure Free;
  136. class function InitInstance(instance : pointer) : tobject; {$ifdef SYSTEMINLINE} inline; {$endif}
  137. procedure CleanupInstance;
  138. class function ClassType : tclass;{$ifdef SYSTEMINLINE}inline;{$endif}
  139. class function ClassInfo : pointer;
  140. class function ClassName : shortstring;
  141. class function ClassNameIs(const name : string) : boolean;
  142. class function ClassParent : tclass;{$ifdef SYSTEMINLINE}inline;{$endif}
  143. class function InstanceSize : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  144. class function InheritsFrom(aclass : tclass) : boolean;
  145. class function StringMessageTable : pstringmessagetable;
  146. { message handling routines }
  147. procedure Dispatch(var message);
  148. procedure DispatchStr(var message);
  149. class function MethodAddress(const name : shortstring) : pointer;
  150. class function MethodName(address : pointer) : shortstring;
  151. function FieldAddress(const name : shortstring) : pointer;
  152. { new since Delphi 4 }
  153. procedure AfterConstruction;virtual;
  154. procedure BeforeDestruction;virtual;
  155. { new for gtk, default handler for text based messages }
  156. procedure DefaultHandlerStr(var message);virtual;
  157. { interface functions }
  158. function GetInterface(const iid : tguid; out obj) : boolean;
  159. function GetInterfaceByStr(const iidstr : string; out obj) : boolean;
  160. class function GetInterfaceEntry(const iid : tguid) : pinterfaceentry;
  161. class function GetInterfaceEntryByStr(const iidstr : string) : pinterfaceentry;
  162. class function GetInterfaceTable : pinterfacetable;
  163. end;
  164. IUnknown = interface
  165. ['{00000000-0000-0000-C000-000000000046}']
  166. function QueryInterface(const iid : tguid;out obj) : longint;stdcall;
  167. function _AddRef : longint;stdcall;
  168. function _Release : longint;stdcall;
  169. end;
  170. IInterface = IUnknown;
  171. {$M+}
  172. IInvokable = interface(IInterface)
  173. end;
  174. {$M-}
  175. { for native dispinterface support }
  176. IDispatch = interface(IUnknown)
  177. ['{00020400-0000-0000-C000-000000000046}']
  178. function GetTypeInfoCount(out count : longint) : HResult;stdcall;
  179. function GetTypeInfo(Index,LocaleID : longint;
  180. out TypeInfo): HResult;stdcall;
  181. function GetIDsOfNames(const iid: TGUID; names: Pointer;
  182. NameCount, LocaleID: LongInt; DispIDs: Pointer) : HResult;stdcall;
  183. function Invoke(DispID: LongInt;const iid : TGUID;
  184. LocaleID : longint; Flags: Word;var params;
  185. VarResult,ExcepInfo,ArgErr : pointer) : HResult;stdcall;
  186. end;
  187. TInterfacedObject = class(TObject,IUnknown)
  188. protected
  189. frefcount : longint;
  190. { implement methods of IUnknown }
  191. function QueryInterface(const iid : tguid;out obj) : longint;stdcall;
  192. function _AddRef : longint;stdcall;
  193. function _Release : longint;stdcall;
  194. public
  195. procedure AfterConstruction;override;
  196. procedure BeforeDestruction;override;
  197. class function NewInstance : TObject;override;
  198. property RefCount : longint read frefcount;
  199. end;
  200. TInterfacedClass = class of TInterfacedObject;
  201. TAggregatedObject = class(TObject)
  202. private
  203. fcontroller: Pointer;
  204. function GetController: IUnknown;
  205. protected
  206. { implement methods of IUnknown }
  207. function QueryInterface(const iid : tguid;out obj) : longint;stdcall;
  208. function _AddRef : longint;stdcall;
  209. function _Release : longint;stdcall;
  210. public
  211. constructor Create(const aController: IUnknown);
  212. property Controller : IUnknown read GetController;
  213. end;
  214. TContainedObject = class(TAggregatedObject,IInterface)
  215. protected
  216. function QueryInterface(const iid : tguid;out obj) : longint;virtual; stdcall;
  217. end;
  218. { some pointer definitions }
  219. PUnknown = ^IUnknown;
  220. PPUnknown = ^PUnknown;
  221. PDispatch = ^IDispatch;
  222. PPDispatch = ^PDispatch;
  223. PInterface = PUnknown;
  224. TExceptProc = Procedure (Obj : TObject; Addr : Pointer; FrameCount:Longint; Frame: PPointer);
  225. { Exception object stack }
  226. PExceptObject = ^TExceptObject;
  227. TExceptObject = record
  228. FObject : TObject;
  229. Addr : pointer;
  230. Next : PExceptObject;
  231. refcount : Longint;
  232. Framecount : Longint;
  233. Frames : PPointer;
  234. end;
  235. Const
  236. ExceptProc : TExceptProc = Nil;
  237. RaiseProc : TExceptProc = Nil;
  238. RaiseMaxFrameCount : Longint = 16;
  239. Function RaiseList : PExceptObject;
  240. { @abstract(increase exception reference count)
  241. When leaving an except block, the exception object is normally
  242. freed automatically. To avoid this, call this function.
  243. If within the exception object you decide that you don't need
  244. the exception after all, call @link(ReleaseExceptionObject).
  245. Otherwise, if the reference count is > 0, the exception object
  246. goes into your "property" and you need to free it manually.
  247. The effect of this function is countered by re-raising an exception
  248. via "raise;", this zeroes the reference count again.
  249. Calling this method is only valid within an except block.
  250. @return(pointer to the exception object) }
  251. function AcquireExceptionObject: Pointer;
  252. { @abstract(decrease exception reference count)
  253. After calling @link(AcquireExceptionObject) you can call this method
  254. to decrease the exception reference count again.
  255. If the reference count is > 0, the exception object
  256. goes into your "property" and you need to free it manually.
  257. Calling this method is only valid within an except block. }
  258. procedure ReleaseExceptionObject;
  259. {*****************************************************************************
  260. Array of const support
  261. *****************************************************************************}
  262. const
  263. vtInteger = 0;
  264. vtBoolean = 1;
  265. vtChar = 2;
  266. {$ifndef FPUNONE}
  267. vtExtended = 3;
  268. {$endif}
  269. vtString = 4;
  270. vtPointer = 5;
  271. vtPChar = 6;
  272. vtObject = 7;
  273. vtClass = 8;
  274. vtWideChar = 9;
  275. vtPWideChar = 10;
  276. vtAnsiString = 11;
  277. vtCurrency = 12;
  278. vtVariant = 13;
  279. vtInterface = 14;
  280. vtWideString = 15;
  281. vtInt64 = 16;
  282. vtQWord = 17;
  283. type
  284. PVarRec = ^TVarRec;
  285. TVarRec = record
  286. case VType : sizeint of
  287. {$ifdef ENDIAN_BIG}
  288. vtInteger : ({$IFDEF CPU64}integerdummy1 : Longint;{$ENDIF CPU64}VInteger: Longint);
  289. vtBoolean : ({$IFDEF CPU64}booldummy : Longint;{$ENDIF CPU64}booldummy1,booldummy2,booldummy3: byte; VBoolean: Boolean);
  290. vtChar : ({$IFDEF CPU64}chardummy : Longint;{$ENDIF CPU64}chardummy1,chardummy2,chardummy3: byte; VChar: Char);
  291. vtWideChar : ({$IFDEF CPU64}widechardummy : Longint;{$ENDIF CPU64}wchardummy1,VWideChar: WideChar);
  292. {$else ENDIAN_BIG}
  293. vtInteger : (VInteger: Longint);
  294. vtBoolean : (VBoolean: Boolean);
  295. vtChar : (VChar: Char);
  296. vtWideChar : (VWideChar: WideChar);
  297. {$endif ENDIAN_BIG}
  298. {$ifndef FPUNONE}
  299. vtExtended : (VExtended: PExtended);
  300. {$endif}
  301. vtString : (VString: PShortString);
  302. vtPointer : (VPointer: Pointer);
  303. vtPChar : (VPChar: PChar);
  304. vtObject : (VObject: TObject);
  305. vtClass : (VClass: TClass);
  306. vtPWideChar : (VPWideChar: PWideChar);
  307. vtAnsiString : (VAnsiString: Pointer);
  308. vtCurrency : (VCurrency: PCurrency);
  309. vtVariant : (VVariant: PVariant);
  310. vtInterface : (VInterface: Pointer);
  311. vtWideString : (VWideString: Pointer);
  312. vtInt64 : (VInt64: PInt64);
  313. vtQWord : (VQWord: PQWord);
  314. end;
  315. var
  316. DispCallByIDProc : pointer;