objpash.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 }
  49. TObject = class;
  50. TClass = class of tobject;
  51. PClass = ^tclass;
  52. { to access the message table from outside }
  53. TMsgStrTable = record
  54. name : pshortstring;
  55. method : pointer;
  56. end;
  57. PMsgStrTable = ^TMsgStrTable;
  58. TStringMessageTable = record
  59. count : dword;
  60. msgstrtable : array[0..0] of tmsgstrtable;
  61. end;
  62. pstringmessagetable = ^tstringmessagetable;
  63. PGuid = ^TGuid;
  64. TGuid = packed record
  65. case integer of
  66. 1 : (
  67. Data1 : DWord;
  68. Data2 : word;
  69. Data3 : word;
  70. Data4 : array[0..7] of byte;
  71. );
  72. 2 : (
  73. D1 : DWord;
  74. D2 : word;
  75. D3 : word;
  76. D4 : array[0..7] of byte;
  77. );
  78. end;
  79. pinterfaceentry = ^tinterfaceentry;
  80. tinterfaceentry = packed record
  81. IID: pguid; { if assigned(IID) then Com else Corba}
  82. VTable: Pointer;
  83. IOffset: DWord;
  84. IIDStr: pshortstring; { never nil. Com: upper(GuidToString(IID^)) }
  85. end;
  86. pinterfacetable = ^tinterfacetable;
  87. tinterfacetable = packed record
  88. EntryCount: Word;
  89. Entries: array[0..0] of tinterfaceentry;
  90. end;
  91. TMethod = record
  92. Code, Data : Pointer;
  93. end;
  94. TObject = class
  95. public
  96. { please don't change the order of virtual methods, because
  97. their vmt offsets are used by some assembler code which uses
  98. hard coded addresses (FK) }
  99. constructor Create;
  100. { the virtual procedures must be in THAT order }
  101. destructor Destroy;virtual;
  102. class function newinstance : tobject;virtual;
  103. procedure FreeInstance;virtual;
  104. function SafeCallException(exceptobject : tobject;
  105. exceptaddr : pointer) : longint;virtual;
  106. procedure DefaultHandler(var message);virtual;
  107. procedure Free;
  108. class function InitInstance(instance : pointer) : tobject;
  109. procedure CleanupInstance;
  110. class function ClassType : tclass;
  111. class function ClassInfo : pointer;
  112. class function ClassName : shortstring;
  113. class function ClassNameIs(const name : string) : boolean;
  114. class function ClassParent : tclass;
  115. class function InstanceSize : longint;
  116. class function InheritsFrom(aclass : tclass) : boolean;
  117. class function StringMessageTable : pstringmessagetable;
  118. { message handling routines }
  119. procedure Dispatch(var message);
  120. procedure DispatchStr(var message);
  121. class function MethodAddress(const name : shortstring) : pointer;
  122. class function MethodName(address : pointer) : shortstring;
  123. function FieldAddress(const name : shortstring) : pointer;
  124. { new since Delphi 4 }
  125. procedure AfterConstruction;virtual;
  126. procedure BeforeDestruction;virtual;
  127. { new for gtk, default handler for text based messages }
  128. procedure DefaultHandlerStr(var message);virtual;
  129. {$ifdef HASINTF}
  130. { interface functions }
  131. function GetInterface(const iid : tguid; out obj) : boolean;
  132. function GetInterfaceByStr(const iidstr : string; out obj) : boolean;
  133. class function GetInterfaceEntry(const iid : tguid) : pinterfaceentry;
  134. class function GetInterfaceEntryByStr(const iidstr : string) : pinterfaceentry;
  135. class function GetInterfaceTable : pinterfacetable;
  136. {$endif HASINTF}
  137. end;
  138. {$ifdef HASINTF}
  139. IUnknown = interface
  140. ['{00000000-0000-0000-C000-000000000046}']
  141. function QueryInterface(const iid : tguid;out obj) : longint;stdcall;
  142. function _AddRef : longint;stdcall;
  143. function _Release : longint;stdcall;
  144. end;
  145. IInterface = IUnknown;
  146. {$M+}
  147. IInvokable = interface(IInterface)
  148. end;
  149. {$M-}
  150. { for native dispinterface support }
  151. IDispatch = interface(IUnknown)
  152. ['{00020400-0000-0000-C000-000000000046}']
  153. function GetTypeInfoCount(out count : longint) : longint;stdcall;
  154. function GetTypeInfo(Index,LocaleID : longint;
  155. out TypeInfo): LongInt;stdcall;
  156. function GetIDsOfNames(const iid: TGUID; names: Pointer;
  157. NameCount, LocaleID: LongInt; DispIDs: Pointer) : longint;stdcall;
  158. function Invoke(DispID: LongInt;const iid : TGUID;
  159. LocaleID : longint; Flags: Word;var params;
  160. VarResult,ExcepInfo,ArgErr : pointer) : longint;stdcall;
  161. end;
  162. TInterfacedObject = class(TObject,IUnknown)
  163. protected
  164. frefcount : longint;
  165. { implement methods of IUnknown }
  166. function QueryInterface(const iid : tguid;out obj) : longint;stdcall;
  167. function _AddRef : longint;stdcall;
  168. function _Release : longint;stdcall;
  169. public
  170. procedure AfterConstruction;override;
  171. procedure BeforeDestruction;override;
  172. class function NewInstance : TObject;override;
  173. property RefCount : longint read frefcount;
  174. end;
  175. TInterfacedClass = class of TInterfacedObject;
  176. { some pointer definitions }
  177. PUnknown = ^IUnknown;
  178. PPUnknown = ^PUnknown;
  179. PDispatch = ^IDispatch;
  180. PPDispatch = ^PDispatch;
  181. {$endif HASINTF}
  182. TExceptProc = Procedure (Obj : TObject; Addr : Pointer; FrameCount:Longint; Frame: PPointer);
  183. { Exception object stack }
  184. PExceptObject = ^TExceptObject;
  185. TExceptObject = record
  186. FObject : TObject;
  187. Addr : pointer;
  188. Next : PExceptObject;
  189. refcount : Longint;
  190. Framecount : Longint;
  191. Frames : PPointer;
  192. end;
  193. Const
  194. ExceptProc : TExceptProc = Nil;
  195. RaiseProc : TExceptProc = Nil;
  196. RaiseMaxFrameCount : Longint = 16;
  197. Function RaiseList : PExceptObject;
  198. { @abstract(increase exception reference count)
  199. When leaving an except block, the exception object is normally
  200. freed automatically. To avoid this, call this function.
  201. If within the exception object you decide that you don't need
  202. the exception after all, call @link(ReleaseExceptionObject).
  203. Otherwise, if the reference count is > 0, the exception object
  204. goes into your "property" and you need to free it manually.
  205. The effect of this function is countered by re-raising an exception
  206. via "raise;", this zeroes the reference count again.
  207. Calling this method is only valid within an except block.
  208. @return(pointer to the exception object) }
  209. function AcquireExceptionObject: Pointer;
  210. { @abstract(decrease exception reference count)
  211. After calling @link(AcquireExceptionObject) you can call this method
  212. to decrease the exception reference count again.
  213. If the reference count is > 0, the exception object
  214. goes into your "property" and you need to free it manually.
  215. Calling this method is only valid within an except block. }
  216. procedure ReleaseExceptionObject;
  217. {*****************************************************************************
  218. Array of const support
  219. *****************************************************************************}
  220. const
  221. vtInteger = 0;
  222. vtBoolean = 1;
  223. vtChar = 2;
  224. vtExtended = 3;
  225. vtString = 4;
  226. vtPointer = 5;
  227. vtPChar = 6;
  228. vtObject = 7;
  229. vtClass = 8;
  230. vtWideChar = 9;
  231. vtPWideChar = 10;
  232. vtAnsiString = 11;
  233. vtCurrency = 12;
  234. vtVariant = 13;
  235. vtInterface = 14;
  236. vtWideString = 15;
  237. vtInt64 = 16;
  238. vtQWord = 17;
  239. type
  240. PVarRec = ^TVarRec;
  241. TVarRec = record
  242. case VType : Ptrint of
  243. vtInteger : (VInteger: Longint);
  244. {$ifdef ENDIAN_BIG}
  245. vtBoolean : (booldummy1,booldummy2,booldummy3: byte; VBoolean: Boolean);
  246. vtChar : (chardummy1,chardummy2,chardummy3: byte; VChar: Char);
  247. vtWideChar : (wchardummy1,VWideChar: WideChar);
  248. {$else ENDIAN_BIG}
  249. vtBoolean : (VBoolean: Boolean);
  250. vtChar : (VChar: Char);
  251. vtWideChar : (VWideChar: WideChar);
  252. {$endif ENDIAN_BIG}
  253. vtExtended : (VExtended: PExtended);
  254. vtString : (VString: PShortString);
  255. vtPointer : (VPointer: Pointer);
  256. vtPChar : (VPChar: PChar);
  257. vtObject : (VObject: TObject);
  258. vtClass : (VClass: TClass);
  259. vtPWideChar : (VPWideChar: PWideChar);
  260. vtAnsiString : (VAnsiString: Pointer);
  261. {$ifdef HASCURRENCY}
  262. vtCurrency : (VCurrency: PCurrency);
  263. {$endif HASCURRENCY}
  264. {$ifdef HASVARIANT}
  265. vtVariant : (VVariant: PVariant);
  266. {$endif HASVARIANT}
  267. vtInterface : (VInterface: Pointer);
  268. vtWideString : (VWideString: Pointer);
  269. vtInt64 : (VInt64: PInt64);
  270. vtQWord : (VQWord: PQWord);
  271. end;
  272. {
  273. $Log: objpash.inc,v $
  274. Revision 1.28 2005/02/14 17:13:26 peter
  275. * truncate log
  276. Revision 1.27 2005/02/01 19:32:14 florian
  277. + tmethod
  278. Revision 1.26 2005/01/31 19:41:39 peter
  279. * interface additions
  280. Revision 1.25 2005/01/26 17:07:10 peter
  281. * retrieve backtrace when exception is raised
  282. * RaiseMaxFrameCount added to limit the number of backtraces, setting
  283. it to 0 disables backtraces. Default is 16
  284. }