objpash.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by the Free Pascal development team
  5. This unit makes Free Pascal as much as possible Delphi compatible
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {*****************************************************************************
  13. Basic Types/constants
  14. *****************************************************************************}
  15. const
  16. vmtInstanceSize = 0;
  17. vmtParent = sizeof(ptrint)*2;
  18. { These were negative value's, but are now positive, else classes
  19. couldn't be used with shared linking which copies only all data from
  20. the .global directive and not the data before the directive (PFV) }
  21. vmtClassName = vmtParent+sizeof(pointer);
  22. vmtDynamicTable = vmtParent+sizeof(pointer)*2;
  23. vmtMethodTable = vmtParent+sizeof(pointer)*3;
  24. vmtFieldTable = vmtParent+sizeof(pointer)*4;
  25. vmtTypeInfo = vmtParent+sizeof(pointer)*5;
  26. vmtInitTable = vmtParent+sizeof(pointer)*6;
  27. vmtAutoTable = vmtParent+sizeof(pointer)*7;
  28. vmtIntfTable = vmtParent+sizeof(pointer)*8;
  29. vmtMsgStrPtr = vmtParent+sizeof(pointer)*9;
  30. { methods }
  31. vmtMethodStart = vmtParent+sizeof(pointer)*10;
  32. vmtDestroy = vmtMethodStart;
  33. vmtNewInstance = vmtMethodStart+sizeof(pointer);
  34. vmtFreeInstance = vmtMethodStart+sizeof(pointer)*2;
  35. vmtSafeCallException = vmtMethodStart+sizeof(pointer)*3;
  36. vmtDefaultHandler = vmtMethodStart+sizeof(pointer)*4;
  37. vmtAfterConstruction = vmtMethodStart+sizeof(pointer)*5;
  38. vmtBeforeDestruction = vmtMethodStart+sizeof(pointer)*6;
  39. vmtDefaultHandlerStr = vmtMethodStart+sizeof(pointer)*7;
  40. { IInterface }
  41. S_OK = 0;
  42. S_FALSE = 1;
  43. E_NOINTERFACE = hresult($80004002);
  44. E_UNEXPECTED = hresult($8000FFFF);
  45. E_NOTIMPL = hresult($80004001);
  46. type
  47. TextFile = Text;
  48. { now the let's declare the base classes for the class object }
  49. { model }
  50. TObject = class;
  51. TClass = class of tobject;
  52. PClass = ^tclass;
  53. { to access the message table from outside }
  54. TMsgStrTable = record
  55. name : pshortstring;
  56. method : pointer;
  57. end;
  58. PMsgStrTable = ^TMsgStrTable;
  59. TStringMessageTable = record
  60. count : dword;
  61. msgstrtable : array[0..0] of tmsgstrtable;
  62. end;
  63. pstringmessagetable = ^tstringmessagetable;
  64. PGuid = ^TGuid;
  65. TGuid = packed record
  66. case integer of
  67. 1 : (
  68. Data1 : DWord;
  69. Data2 : word;
  70. Data3 : word;
  71. Data4 : array[0..7] of byte;
  72. );
  73. 2 : (
  74. D1 : DWord;
  75. D2 : word;
  76. D3 : word;
  77. D4 : array[0..7] of byte;
  78. );
  79. end;
  80. pinterfaceentry = ^tinterfaceentry;
  81. tinterfaceentry = packed record
  82. IID: pguid; { if assigned(IID) then Com else Corba}
  83. VTable: Pointer;
  84. IOffset: DWord;
  85. IIDStr: pshortstring; { never nil. Com: upper(GuidToString(IID^)) }
  86. end;
  87. pinterfacetable = ^tinterfacetable;
  88. tinterfacetable = packed record
  89. EntryCount: Word;
  90. Entries: array[0..0] of tinterfaceentry;
  91. end;
  92. TMethod = record
  93. Code, Data : Pointer;
  94. end;
  95. TObject = class
  96. public
  97. { please don't change the order of virtual methods, because
  98. their vmt offsets are used by some assembler code which uses
  99. hard coded addresses (FK) }
  100. constructor Create;
  101. { the virtual procedures must be in THAT order }
  102. destructor Destroy;virtual;
  103. class function newinstance : tobject;virtual;
  104. procedure FreeInstance;virtual;
  105. function SafeCallException(exceptobject : tobject;
  106. exceptaddr : pointer) : longint;virtual;
  107. procedure DefaultHandler(var message);virtual;
  108. procedure Free;
  109. class function InitInstance(instance : pointer) : tobject;
  110. procedure CleanupInstance;
  111. class function ClassType : tclass;
  112. class function ClassInfo : pointer;
  113. class function ClassName : shortstring;
  114. class function ClassNameIs(const name : string) : boolean;
  115. class function ClassParent : tclass;
  116. class function InstanceSize : longint;
  117. class function InheritsFrom(aclass : tclass) : boolean;
  118. class function StringMessageTable : pstringmessagetable;
  119. { message handling routines }
  120. procedure Dispatch(var message);
  121. procedure DispatchStr(var message);
  122. class function MethodAddress(const name : shortstring) : pointer;
  123. class function MethodName(address : pointer) : shortstring;
  124. function FieldAddress(const name : shortstring) : pointer;
  125. { new since Delphi 4 }
  126. procedure AfterConstruction;virtual;
  127. procedure BeforeDestruction;virtual;
  128. { new for gtk, default handler for text based messages }
  129. procedure DefaultHandlerStr(var message);virtual;
  130. {$ifdef HASINTF}
  131. { interface functions }
  132. function GetInterface(const iid : tguid; out obj) : boolean;
  133. function GetInterfaceByStr(const iidstr : string; out obj) : boolean;
  134. class function GetInterfaceEntry(const iid : tguid) : pinterfaceentry;
  135. class function GetInterfaceEntryByStr(const iidstr : string) : pinterfaceentry;
  136. class function GetInterfaceTable : pinterfacetable;
  137. {$endif HASINTF}
  138. end;
  139. {$ifdef HASINTF}
  140. IUnknown = interface
  141. ['{00000000-0000-0000-C000-000000000046}']
  142. function QueryInterface(const iid : tguid;out obj) : longint;stdcall;
  143. function _AddRef : longint;stdcall;
  144. function _Release : longint;stdcall;
  145. end;
  146. IInterface = IUnknown;
  147. {$M+}
  148. IInvokable = interface(IInterface)
  149. end;
  150. {$M-}
  151. { for native dispinterface support }
  152. IDispatch = interface(IUnknown)
  153. ['{00020400-0000-0000-C000-000000000046}']
  154. function GetTypeInfoCount(out count : longint) : longint;stdcall;
  155. function GetTypeInfo(Index,LocaleID : longint;
  156. out TypeInfo): LongInt;stdcall;
  157. function GetIDsOfNames(const iid: TGUID; names: Pointer;
  158. NameCount, LocaleID: LongInt; DispIDs: Pointer) : longint;stdcall;
  159. function Invoke(DispID: LongInt;const iid : TGUID;
  160. LocaleID : longint; Flags: Word;var params;
  161. VarResult,ExcepInfo,ArgErr : pointer) : longint;stdcall;
  162. end;
  163. TInterfacedObject = class(TObject,IUnknown)
  164. protected
  165. frefcount : longint;
  166. { implement methods of IUnknown }
  167. function QueryInterface(const iid : tguid;out obj) : longint;stdcall;
  168. function _AddRef : longint;stdcall;
  169. function _Release : longint;stdcall;
  170. public
  171. procedure AfterConstruction;override;
  172. procedure BeforeDestruction;override;
  173. class function NewInstance : TObject;override;
  174. property RefCount : longint read frefcount;
  175. end;
  176. TInterfacedClass = class of TInterfacedObject;
  177. { some pointer definitions }
  178. PUnknown = ^IUnknown;
  179. PPUnknown = ^PUnknown;
  180. PDispatch = ^IDispatch;
  181. PPDispatch = ^PDispatch;
  182. {$endif HASINTF}
  183. TExceptProc = Procedure (Obj : TObject; Addr : Pointer; FrameCount:Longint; Frame: PPointer);
  184. { Exception object stack }
  185. PExceptObject = ^TExceptObject;
  186. TExceptObject = record
  187. FObject : TObject;
  188. Addr : pointer;
  189. Next : PExceptObject;
  190. refcount : Longint;
  191. Framecount : Longint;
  192. Frames : PPointer;
  193. end;
  194. Const
  195. ExceptProc : TExceptProc = Nil;
  196. RaiseProc : TExceptProc = Nil;
  197. RaiseMaxFrameCount : Longint = 16;
  198. Function RaiseList : PExceptObject;
  199. { @abstract(increase exception reference count)
  200. When leaving an except block, the exception object is normally
  201. freed automatically. To avoid this, call this function.
  202. If within the exception object you decide that you don't need
  203. the exception after all, call @link(ReleaseExceptionObject).
  204. Otherwise, if the reference count is > 0, the exception object
  205. goes into your "property" and you need to free it manually.
  206. The effect of this function is countered by re-raising an exception
  207. via "raise;", this zeroes the reference count again.
  208. Calling this method is only valid within an except block.
  209. @return(pointer to the exception object) }
  210. function AcquireExceptionObject: Pointer;
  211. { @abstract(decrease exception reference count)
  212. After calling @link(AcquireExceptionObject) you can call this method
  213. to decrease the exception reference count again.
  214. If the reference count is > 0, the exception object
  215. goes into your "property" and you need to free it manually.
  216. Calling this method is only valid within an except block. }
  217. procedure ReleaseExceptionObject;
  218. {*****************************************************************************
  219. Array of const support
  220. *****************************************************************************}
  221. const
  222. vtInteger = 0;
  223. vtBoolean = 1;
  224. vtChar = 2;
  225. vtExtended = 3;
  226. vtString = 4;
  227. vtPointer = 5;
  228. vtPChar = 6;
  229. vtObject = 7;
  230. vtClass = 8;
  231. vtWideChar = 9;
  232. vtPWideChar = 10;
  233. vtAnsiString = 11;
  234. vtCurrency = 12;
  235. vtVariant = 13;
  236. vtInterface = 14;
  237. vtWideString = 15;
  238. vtInt64 = 16;
  239. vtQWord = 17;
  240. type
  241. PVarRec = ^TVarRec;
  242. TVarRec = record
  243. case VType : Ptrint of
  244. vtInteger : (VInteger: Longint);
  245. {$ifdef ENDIAN_BIG}
  246. vtBoolean : (booldummy1,booldummy2,booldummy3: byte; VBoolean: Boolean);
  247. vtChar : (chardummy1,chardummy2,chardummy3: byte; VChar: Char);
  248. vtWideChar : (wchardummy1,VWideChar: WideChar);
  249. {$else ENDIAN_BIG}
  250. vtBoolean : (VBoolean: Boolean);
  251. vtChar : (VChar: Char);
  252. vtWideChar : (VWideChar: WideChar);
  253. {$endif ENDIAN_BIG}
  254. vtExtended : (VExtended: PExtended);
  255. vtString : (VString: PShortString);
  256. vtPointer : (VPointer: Pointer);
  257. vtPChar : (VPChar: PChar);
  258. vtObject : (VObject: TObject);
  259. vtClass : (VClass: TClass);
  260. vtPWideChar : (VPWideChar: PWideChar);
  261. vtAnsiString : (VAnsiString: Pointer);
  262. {$ifdef HASCURRENCY}
  263. vtCurrency : (VCurrency: PCurrency);
  264. {$endif HASCURRENCY}
  265. {$ifdef HASVARIANT}
  266. vtVariant : (VVariant: PVariant);
  267. {$endif HASVARIANT}
  268. vtInterface : (VInterface: Pointer);
  269. vtWideString : (VWideString: Pointer);
  270. vtInt64 : (VInt64: PInt64);
  271. vtQWord : (VQWord: PQWord);
  272. end;
  273. {
  274. $Log$
  275. Revision 1.27 2005-02-01 19:32:14 florian
  276. + tmethod
  277. Revision 1.26 2005/01/31 19:41:39 peter
  278. * interface additions
  279. Revision 1.25 2005/01/26 17:07:10 peter
  280. * retrieve backtrace when exception is raised
  281. * RaiseMaxFrameCount added to limit the number of backtraces, setting
  282. it to 0 disables backtraces. Default is 16
  283. Revision 1.24 2004/04/28 19:52:41 peter
  284. * vtype changed to ptrint
  285. Revision 1.23 2004/04/26 21:06:00 peter
  286. * class size changed to aint
  287. Revision 1.22 2004/04/25 16:32:31 florian
  288. * fixed vmt issues on 64 bit systems
  289. Revision 1.21 2003/12/06 00:02:34 florian
  290. * widechars in tvarrec fixed for big endian machines as well
  291. Revision 1.20 2003/12/05 23:54:04 jonas
  292. * changed tvarrec so chars and booleans work on big endian machines
  293. Revision 1.19 2003/11/30 19:48:20 florian
  294. * fixed some arm stuff
  295. Revision 1.18 2003/11/03 09:42:28 marco
  296. * Peter's Cardinal<->Longint fixes patch
  297. Revision 1.17 2003/10/06 15:59:20 florian
  298. + applied patch for ref. counted exceptions by Johannes Berg
  299. Revision 1.16 2003/03/17 20:55:58 peter
  300. * ClassType changed to class method
  301. Revision 1.15 2002/09/26 14:43:24 florian
  302. + tvarrec field vcurrency for compilers with hascurrency released
  303. Revision 1.14 2002/09/07 15:07:46 peter
  304. * old logs removed and tabs fixed
  305. }