objpash.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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(ptrint)*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 : PtrInt;
  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 : PtrInt;
  105. IIDStr : pshortstring; { never nil. Com: upper(GuidToString(IID^)) }
  106. case boolean of
  107. true : (IType : tinterfaceentrytype);
  108. false : (__pad_dummy : pointer);
  109. end;
  110. pinterfacetable = ^tinterfacetable;
  111. tinterfacetable = record
  112. EntryCount : PtrInt;
  113. Entries : array[0..0] of tinterfaceentry;
  114. end;
  115. TMethod = record
  116. Code, Data : Pointer;
  117. end;
  118. TObject = class
  119. public
  120. { please don't change the order of virtual methods, because
  121. their vmt offsets are used by some assembler code which uses
  122. hard coded addresses (FK) }
  123. constructor Create;
  124. { the virtual procedures must be in THAT order }
  125. destructor Destroy;virtual;
  126. class function newinstance : tobject;virtual;
  127. procedure FreeInstance;virtual;
  128. function SafeCallException(exceptobject : tobject;
  129. exceptaddr : pointer) : longint;virtual;
  130. procedure DefaultHandler(var message);virtual;
  131. procedure Free;
  132. class function InitInstance(instance : pointer) : tobject;
  133. procedure CleanupInstance;
  134. class function ClassType : tclass;{$ifdef SYSTEMINLINE}inline;{$endif}
  135. class function ClassInfo : pointer;
  136. class function ClassName : shortstring;
  137. class function ClassNameIs(const name : string) : boolean;
  138. class function ClassParent : tclass;{$ifdef SYSTEMINLINE}inline;{$endif}
  139. class function InstanceSize : SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  140. class function InheritsFrom(aclass : tclass) : boolean;
  141. class function StringMessageTable : pstringmessagetable;
  142. { message handling routines }
  143. procedure Dispatch(var message);
  144. procedure DispatchStr(var message);
  145. class function MethodAddress(const name : shortstring) : pointer;
  146. class function MethodName(address : pointer) : shortstring;
  147. function FieldAddress(const name : shortstring) : pointer;
  148. { new since Delphi 4 }
  149. procedure AfterConstruction;virtual;
  150. procedure BeforeDestruction;virtual;
  151. { new for gtk, default handler for text based messages }
  152. procedure DefaultHandlerStr(var message);virtual;
  153. { interface functions }
  154. function GetInterface(const iid : tguid; out obj) : boolean;
  155. function GetInterfaceByStr(const iidstr : string; out obj) : boolean;
  156. class function GetInterfaceEntry(const iid : tguid) : pinterfaceentry;
  157. class function GetInterfaceEntryByStr(const iidstr : string) : pinterfaceentry;
  158. class function GetInterfaceTable : pinterfacetable;
  159. end;
  160. IUnknown = interface
  161. ['{00000000-0000-0000-C000-000000000046}']
  162. function QueryInterface(const iid : tguid;out obj) : longint;stdcall;
  163. function _AddRef : longint;stdcall;
  164. function _Release : longint;stdcall;
  165. end;
  166. IInterface = IUnknown;
  167. {$M+}
  168. IInvokable = interface(IInterface)
  169. end;
  170. {$M-}
  171. { for native dispinterface support }
  172. IDispatch = interface(IUnknown)
  173. ['{00020400-0000-0000-C000-000000000046}']
  174. function GetTypeInfoCount(out count : longint) : HResult;stdcall;
  175. function GetTypeInfo(Index,LocaleID : longint;
  176. out TypeInfo): HResult;stdcall;
  177. function GetIDsOfNames(const iid: TGUID; names: Pointer;
  178. NameCount, LocaleID: LongInt; DispIDs: Pointer) : HResult;stdcall;
  179. function Invoke(DispID: LongInt;const iid : TGUID;
  180. LocaleID : longint; Flags: Word;var params;
  181. VarResult,ExcepInfo,ArgErr : pointer) : HResult;stdcall;
  182. end;
  183. TInterfacedObject = class(TObject,IUnknown)
  184. protected
  185. frefcount : longint;
  186. { implement methods of IUnknown }
  187. function QueryInterface(const iid : tguid;out obj) : longint;stdcall;
  188. function _AddRef : longint;stdcall;
  189. function _Release : longint;stdcall;
  190. public
  191. procedure AfterConstruction;override;
  192. procedure BeforeDestruction;override;
  193. class function NewInstance : TObject;override;
  194. property RefCount : longint read frefcount;
  195. end;
  196. TInterfacedClass = class of TInterfacedObject;
  197. TAggregatedObject = class(TObject)
  198. private
  199. fcontroller: Pointer;
  200. function GetController: IUnknown;
  201. protected
  202. { implement methods of IUnknown }
  203. function QueryInterface(const iid : tguid;out obj) : longint;stdcall;
  204. function _AddRef : longint;stdcall;
  205. function _Release : longint;stdcall;
  206. public
  207. constructor Create(const aController: IUnknown);
  208. property Controller : IUnknown read GetController;
  209. end;
  210. { some pointer definitions }
  211. PUnknown = ^IUnknown;
  212. PPUnknown = ^PUnknown;
  213. PDispatch = ^IDispatch;
  214. PPDispatch = ^PDispatch;
  215. TExceptProc = Procedure (Obj : TObject; Addr : Pointer; FrameCount:Longint; Frame: PPointer);
  216. { Exception object stack }
  217. PExceptObject = ^TExceptObject;
  218. TExceptObject = record
  219. FObject : TObject;
  220. Addr : pointer;
  221. Next : PExceptObject;
  222. refcount : Longint;
  223. Framecount : Longint;
  224. Frames : PPointer;
  225. end;
  226. Const
  227. ExceptProc : TExceptProc = Nil;
  228. RaiseProc : TExceptProc = Nil;
  229. RaiseMaxFrameCount : Longint = 16;
  230. Function RaiseList : PExceptObject;
  231. { @abstract(increase exception reference count)
  232. When leaving an except block, the exception object is normally
  233. freed automatically. To avoid this, call this function.
  234. If within the exception object you decide that you don't need
  235. the exception after all, call @link(ReleaseExceptionObject).
  236. Otherwise, if the reference count is > 0, the exception object
  237. goes into your "property" and you need to free it manually.
  238. The effect of this function is countered by re-raising an exception
  239. via "raise;", this zeroes the reference count again.
  240. Calling this method is only valid within an except block.
  241. @return(pointer to the exception object) }
  242. function AcquireExceptionObject: Pointer;
  243. { @abstract(decrease exception reference count)
  244. After calling @link(AcquireExceptionObject) you can call this method
  245. to decrease the exception reference count again.
  246. If the reference count is > 0, the exception object
  247. goes into your "property" and you need to free it manually.
  248. Calling this method is only valid within an except block. }
  249. procedure ReleaseExceptionObject;
  250. {*****************************************************************************
  251. Array of const support
  252. *****************************************************************************}
  253. const
  254. vtInteger = 0;
  255. vtBoolean = 1;
  256. vtChar = 2;
  257. vtExtended = 3;
  258. vtString = 4;
  259. vtPointer = 5;
  260. vtPChar = 6;
  261. vtObject = 7;
  262. vtClass = 8;
  263. vtWideChar = 9;
  264. vtPWideChar = 10;
  265. vtAnsiString = 11;
  266. vtCurrency = 12;
  267. vtVariant = 13;
  268. vtInterface = 14;
  269. vtWideString = 15;
  270. vtInt64 = 16;
  271. vtQWord = 17;
  272. type
  273. PVarRec = ^TVarRec;
  274. TVarRec = record
  275. case VType : Ptrint of
  276. {$ifdef ENDIAN_BIG}
  277. vtInteger : ({$IFDEF CPU64}integerdummy1 : Longint;{$ENDIF CPU64}VInteger: Longint);
  278. vtBoolean : ({$IFDEF CPU64}booldummy : Longint;{$ENDIF CPU64}booldummy1,booldummy2,booldummy3: byte; VBoolean: Boolean);
  279. vtChar : ({$IFDEF CPU64}chardummy : Longint;{$ENDIF CPU64}chardummy1,chardummy2,chardummy3: byte; VChar: Char);
  280. vtWideChar : ({$IFDEF CPU64}widechardummy : Longint;{$ENDIF CPU64}wchardummy1,VWideChar: WideChar);
  281. {$else ENDIAN_BIG}
  282. vtInteger : (VInteger: Longint);
  283. vtBoolean : (VBoolean: Boolean);
  284. vtChar : (VChar: Char);
  285. vtWideChar : (VWideChar: WideChar);
  286. {$endif ENDIAN_BIG}
  287. vtExtended : (VExtended: PExtended);
  288. vtString : (VString: PShortString);
  289. vtPointer : (VPointer: Pointer);
  290. vtPChar : (VPChar: PChar);
  291. vtObject : (VObject: TObject);
  292. vtClass : (VClass: TClass);
  293. vtPWideChar : (VPWideChar: PWideChar);
  294. vtAnsiString : (VAnsiString: Pointer);
  295. vtCurrency : (VCurrency: PCurrency);
  296. vtVariant : (VVariant: PVariant);
  297. vtInterface : (VInterface: Pointer);
  298. vtWideString : (VWideString: Pointer);
  299. vtInt64 : (VInt64: PInt64);
  300. vtQWord : (VQWord: PQWord);
  301. end;
  302. var
  303. DispCallByIDProc : pointer;