objpash.inc 12 KB

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