objpas.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. Internal Routines called from the Compiler
  14. ****************************************************************************}
  15. { the reverse order of the parameters make code generation easier }
  16. function fpc_do_is(aclass : tclass;aobject : tobject) : boolean;[public,alias: 'FPC_DO_IS']; {$ifdef hascompilerproc} compilerproc; {$endif}
  17. begin
  18. fpc_do_is:=assigned(aobject) and assigned(aclass) and
  19. aobject.inheritsfrom(aclass);
  20. end;
  21. { the reverse order of the parameters make code generation easier }
  22. function fpc_do_as(aclass : tclass;aobject : tobject): tobject;[public,alias: 'FPC_DO_AS']; {$ifdef hascompilerproc} compilerproc; {$endif}
  23. begin
  24. if assigned(aobject) and not(aobject.inheritsfrom(aclass)) then
  25. handleerrorframe(219,get_frame);
  26. result := aobject;
  27. end;
  28. {$ifndef HASINTF}
  29. { dummies for make cycle with 1.0.x }
  30. procedure fpc_intf_decr_ref(var i: pointer);saveregisters;[public,alias: 'FPC_INTF_DECR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  31. begin
  32. end;
  33. procedure fpc_intf_incr_ref(i: pointer);saveregisters;[public,alias: 'FPC_INTF_INCR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  34. begin
  35. end;
  36. procedure fpc_intf_assign(var D: pointer; const S: pointer);[public,alias: 'FPC_INTF_ASSIGN']; {$ifdef hascompilerproc} compilerproc; {$endif}
  37. begin
  38. end;
  39. function fpc_intf_as(const S: pointer; const iid: TGUID): pointer;[public,alias: 'FPC_INTF_AS']; {$ifdef hascompilerproc} compilerproc; {$endif}
  40. begin
  41. end;
  42. function fpc_class_as_intf(const S: pointer; const iid: TGUID): pointer;[public,alias: 'FPC_CLASS_AS_INTF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  43. begin
  44. end;
  45. {$else HASINTF}
  46. { interface helpers }
  47. procedure fpc_intf_decr_ref(var i: pointer);saveregisters;[public,alias: 'FPC_INTF_DECR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  48. begin
  49. if assigned(i) then
  50. IUnknown(i)._Release;
  51. i:=nil;
  52. end;
  53. {$ifdef hascompilerproc}
  54. { local declaration for intf_decr_ref for local access }
  55. procedure intf_decr_ref(var i: pointer);saveregisters; [external name 'FPC_INTF_DECR_REF'];
  56. {$endif hascompilerproc}
  57. procedure fpc_intf_incr_ref(i: pointer);saveregisters;[public,alias: 'FPC_INTF_INCR_REF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  58. begin
  59. if assigned(i) then
  60. IUnknown(i)._AddRef;
  61. end;
  62. {$ifdef hascompilerproc}
  63. { local declaration of intf_incr_ref for local access }
  64. procedure intf_incr_ref(i: pointer);saveregisters; [external name 'FPC_INTF_INCR_REF'];
  65. {$endif hascompilerproc}
  66. procedure fpc_intf_assign(var D: pointer; const S: pointer);[public,alias: 'FPC_INTF_ASSIGN']; {$ifdef hascompilerproc} compilerproc; {$endif}
  67. begin
  68. if assigned(S) then
  69. IUnknown(S)._AddRef;
  70. if assigned(D) then
  71. IUnknown(D)._Release;
  72. D:=S;
  73. end;
  74. function fpc_intf_as(const S: pointer; const iid: TGUID): pointer;[public,alias: 'FPC_INTF_AS']; {$ifdef hascompilerproc} compilerproc; {$endif}
  75. const
  76. S_OK = 0;
  77. var
  78. tmpi: pointer; // _AddRef before _Release
  79. begin
  80. if assigned(S) then
  81. begin
  82. if IUnknown(S).QueryInterface(iid,tmpi)<>S_OK then
  83. handleerror(219);
  84. fpc_intf_as:=tmpi;
  85. end
  86. else
  87. fpc_intf_as:=nil;
  88. end;
  89. function fpc_class_as_intf(const S: pointer; const iid: TGUID): pointer;[public,alias: 'FPC_CLASS_AS_INTF']; {$ifdef hascompilerproc} compilerproc; {$endif}
  90. const
  91. S_OK = 0;
  92. var
  93. tmpi: pointer; // _AddRef before _Release
  94. begin
  95. if assigned(S) then
  96. begin
  97. if not TObject(S).GetInterface(iid,tmpi) then
  98. handleerror(219);
  99. fpc_class_as_intf:=tmpi;
  100. end
  101. else
  102. fpc_class_as_intf:=nil;
  103. end;
  104. {$endif HASINTF}
  105. {****************************************************************************
  106. TOBJECT
  107. ****************************************************************************}
  108. constructor TObject.Create;
  109. begin
  110. end;
  111. destructor TObject.Destroy;
  112. begin
  113. end;
  114. procedure TObject.Free;
  115. begin
  116. // the call via self avoids a warning
  117. if self<>nil then
  118. self.destroy;
  119. end;
  120. class function TObject.InstanceSize : LongInt;
  121. begin
  122. InstanceSize:=plongint(pointer(self)+vmtInstanceSize)^;
  123. end;
  124. procedure InitInterfacePointers(objclass: tclass;instance : pointer);
  125. {$ifdef HASINTF}
  126. var
  127. intftable : pinterfacetable;
  128. i : longint;
  129. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  130. IOffset : longint;
  131. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  132. begin
  133. while assigned(objclass) do
  134. begin
  135. intftable:=pinterfacetable((pointer(objclass)+vmtIntfTable)^);
  136. if assigned(intftable) then
  137. for i:=0 to intftable^.EntryCount-1 do
  138. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  139. begin
  140. move(intftable^.Entries[i].IOffset,IOffset,sizeof(longint));
  141. move(pointer(intftable^.Entries[i].VTable),ppointer(@(PChar(instance)[IOffset]))^,sizeof(pointer));
  142. end;
  143. {$else FPC_REQUIRES_PROPER_ALIGNMENT}
  144. ppointer(@(PChar(instance)[intftable^.Entries[i].IOffset]))^:=
  145. pointer(intftable^.Entries[i].VTable);
  146. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  147. objclass:=pclass(pointer(objclass)+vmtParent)^;
  148. end;
  149. end;
  150. {$else HASINTF}
  151. begin
  152. end;
  153. {$endif HASINTF}
  154. class function TObject.InitInstance(instance : pointer) : tobject;
  155. begin
  156. { the size is saved at offset 0 }
  157. fillchar(instance^,plongint(pointer(self)+vmtInstanceSize)^,0);
  158. { insert VMT pointer into the new created memory area }
  159. { (in class methods self contains the VMT!) }
  160. ppointer(instance)^:=pointer(self);
  161. {$ifdef HASINTF}
  162. InitInterfacePointers(self,instance);
  163. {$endif HASINTF}
  164. InitInstance:=TObject(Instance);
  165. end;
  166. class function TObject.ClassParent : tclass;
  167. begin
  168. { type of self is class of tobject => it points to the vmt }
  169. { the parent vmt is saved at offset vmtParent }
  170. classparent:=pclass(pointer(self)+vmtParent)^;
  171. end;
  172. class function TObject.NewInstance : tobject;
  173. var
  174. p : pointer;
  175. begin
  176. getmem(p,plongint(pointer(self)+vmtInstanceSize)^);
  177. if p <> nil then
  178. InitInstance(p);
  179. NewInstance:=TObject(p);
  180. end;
  181. procedure TObject.FreeInstance;
  182. begin
  183. CleanupInstance;
  184. FreeMem(Pointer(Self));
  185. end;
  186. class function TObject.ClassType : TClass;
  187. begin
  188. ClassType:=TClass(Pointer(Self))
  189. end;
  190. type
  191. tmethodnamerec = packed record
  192. name : pshortstring;
  193. addr : pointer;
  194. end;
  195. tmethodnametable = packed record
  196. count : dword;
  197. entries : packed array[0..0] of tmethodnamerec;
  198. end;
  199. pmethodnametable = ^tmethodnametable;
  200. class function TObject.MethodAddress(const name : shortstring) : pointer;
  201. var
  202. UName : ShortString;
  203. methodtable : pmethodnametable;
  204. i : dword;
  205. vmt : tclass;
  206. begin
  207. UName := UpCase(name);
  208. vmt:=self;
  209. while assigned(vmt) do
  210. begin
  211. methodtable:=pmethodnametable((Pointer(vmt)+vmtMethodTable)^);
  212. if assigned(methodtable) then
  213. begin
  214. for i:=0 to methodtable^.count-1 do
  215. if UpCase(methodtable^.entries[i].name^)=UName then
  216. begin
  217. MethodAddress:=methodtable^.entries[i].addr;
  218. exit;
  219. end;
  220. end;
  221. vmt:=pclass(pointer(vmt)+vmtParent)^;
  222. end;
  223. MethodAddress:=nil;
  224. end;
  225. class function TObject.MethodName(address : pointer) : shortstring;
  226. var
  227. methodtable : pmethodnametable;
  228. i : dword;
  229. vmt : tclass;
  230. begin
  231. vmt:=self;
  232. while assigned(vmt) do
  233. begin
  234. methodtable:=pmethodnametable((Pointer(vmt)+vmtMethodTable)^);
  235. if assigned(methodtable) then
  236. begin
  237. for i:=0 to methodtable^.count-1 do
  238. if methodtable^.entries[i].addr=address then
  239. begin
  240. MethodName:=methodtable^.entries[i].name^;
  241. exit;
  242. end;
  243. end;
  244. vmt:=pclass(pointer(vmt)+vmtParent)^;
  245. end;
  246. MethodName:='';
  247. end;
  248. function TObject.FieldAddress(const name : shortstring) : pointer;
  249. type
  250. PFieldInfo = ^TFieldInfo;
  251. TFieldInfo = packed record
  252. FieldOffset: LongWord;
  253. ClassTypeIndex: Word;
  254. Name: ShortString;
  255. end;
  256. PFieldTable = ^TFieldTable;
  257. TFieldTable = packed record
  258. FieldCount: Word;
  259. ClassTable: Pointer;
  260. { Fields: array[Word] of TFieldInfo; Elements have variant size! }
  261. end;
  262. var
  263. UName: ShortString;
  264. CurClassType: TClass;
  265. FieldTable: PFieldTable;
  266. FieldInfo: PFieldInfo;
  267. i: Integer;
  268. begin
  269. if Length(name) > 0 then
  270. begin
  271. UName := UpCase(name);
  272. CurClassType := ClassType;
  273. while CurClassType <> nil do
  274. begin
  275. FieldTable := PFieldTable((Pointer(CurClassType) + vmtFieldTable)^);
  276. if FieldTable <> nil then
  277. begin
  278. FieldInfo := PFieldInfo(Pointer(FieldTable) + 6);
  279. for i := 0 to FieldTable^.FieldCount - 1 do
  280. begin
  281. if UpCase(FieldInfo^.Name) = UName then
  282. begin
  283. fieldaddress := Pointer(Self) + FieldInfo^.FieldOffset;
  284. exit;
  285. end;
  286. Inc(Pointer(FieldInfo), 7 + Length(FieldInfo^.Name));
  287. end;
  288. end;
  289. { Try again with the parent class type }
  290. CurClassType:=pclass(pointer(CurClassType)+vmtParent)^;
  291. end;
  292. end;
  293. fieldaddress:=nil;
  294. end;
  295. function TObject.SafeCallException(exceptobject : tobject;
  296. exceptaddr : pointer) : longint;
  297. begin
  298. safecallexception:=0;
  299. end;
  300. class function TObject.ClassInfo : pointer;
  301. begin
  302. ClassInfo:=ppointer(Pointer(self)+vmtTypeInfo)^;
  303. end;
  304. class function TObject.ClassName : ShortString;
  305. begin
  306. ClassName:=PShortString((Pointer(Self)+vmtClassName)^)^;
  307. end;
  308. class function TObject.ClassNameIs(const name : string) : boolean;
  309. begin
  310. ClassNameIs:=Upcase(ClassName)=Upcase(name);
  311. end;
  312. class function TObject.InheritsFrom(aclass : TClass) : Boolean;
  313. var
  314. vmt : tclass;
  315. begin
  316. vmt:=self;
  317. while assigned(vmt) do
  318. begin
  319. if vmt=aclass then
  320. begin
  321. InheritsFrom:=true;
  322. exit;
  323. end;
  324. vmt:=pclass(pointer(vmt)+vmtParent)^;
  325. end;
  326. InheritsFrom:=false;
  327. end;
  328. class function TObject.stringmessagetable : pstringmessagetable;
  329. type
  330. pdword = ^dword;
  331. begin
  332. stringmessagetable:=pstringmessagetable((pointer(Self)+vmtMsgStrPtr)^);
  333. end;
  334. type
  335. tmessagehandler = procedure(var msg) of object;
  336. tmessagehandlerrec = packed record
  337. proc : pointer;
  338. obj : pointer;
  339. end;
  340. procedure TObject.Dispatch(var message);
  341. type
  342. tmsgtable = record
  343. index : dword;
  344. method : pointer;
  345. end;
  346. pmsgtable = ^tmsgtable;
  347. PSizeUInt = ^SizeUInt;
  348. var
  349. index : dword;
  350. count,i : longint;
  351. msgtable : pmsgtable;
  352. p : pointer;
  353. vmt : tclass;
  354. msghandler : tmessagehandler;
  355. begin
  356. index:=dword(message);
  357. vmt:=ClassType;
  358. while assigned(vmt) do
  359. begin
  360. // See if we have messages at all in this class.
  361. p:=pointer(vmt)+vmtDynamicTable;
  362. If Assigned(p) and (Pdword(p)^<>0) then
  363. begin
  364. msgtable:=pmsgtable(PSizeUInt(P)^+4);
  365. count:=pdword(PSizeUInt(P)^)^;
  366. end
  367. else
  368. Count:=0;
  369. { later, we can implement a binary search here }
  370. for i:=0 to count-1 do
  371. begin
  372. if index=msgtable[i].index then
  373. begin
  374. p:=msgtable[i].method;
  375. tmessagehandlerrec(msghandler).proc:=p;
  376. tmessagehandlerrec(msghandler).obj:=self;
  377. msghandler(message);
  378. exit;
  379. end;
  380. end;
  381. vmt:=pclass(pointer(vmt)+vmtParent)^;
  382. end;
  383. DefaultHandler(message);
  384. end;
  385. procedure TObject.DispatchStr(var message);
  386. type
  387. PSizeUInt = ^SizeUInt;
  388. var
  389. name : shortstring;
  390. count,i : longint;
  391. msgstrtable : pmsgstrtable;
  392. p : pointer;
  393. vmt : tclass;
  394. msghandler : tmessagehandler;
  395. begin
  396. name:=pshortstring(@message)^;
  397. vmt:=ClassType;
  398. while assigned(vmt) do
  399. begin
  400. p:=(pointer(vmt)+vmtMsgStrPtr);
  401. If (P<>Nil) and (PDWord(P)^<>0) then
  402. begin
  403. count:=pdword(PSizeUInt(p)^)^;
  404. msgstrtable:=pmsgstrtable(PSizeUInt(P)^+4);
  405. end
  406. else
  407. Count:=0;
  408. { later, we can implement a binary search here }
  409. for i:=0 to count-1 do
  410. begin
  411. if name=msgstrtable[i].name^ then
  412. begin
  413. p:=msgstrtable[i].method;
  414. tmessagehandlerrec(msghandler).proc:=p;
  415. tmessagehandlerrec(msghandler).obj:=self;
  416. msghandler(message);
  417. exit;
  418. end;
  419. end;
  420. vmt:=pclass(pointer(vmt)+vmtParent)^;
  421. end;
  422. DefaultHandlerStr(message);
  423. end;
  424. procedure TObject.DefaultHandler(var message);
  425. begin
  426. end;
  427. procedure TObject.DefaultHandlerStr(var message);
  428. begin
  429. end;
  430. procedure TObject.CleanupInstance;
  431. Type
  432. TRecElem = Record
  433. Info : Pointer;
  434. Offset : Longint;
  435. end;
  436. TRecElemArray = Array[1..Maxint] of TRecElem;
  437. PRecRec = ^TRecRec;
  438. TRecRec = record
  439. Size,Count : Longint;
  440. Elements : TRecElemArray;
  441. end;
  442. var
  443. vmt : tclass;
  444. temp : pbyte;
  445. count,
  446. i : longint;
  447. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  448. recelem : TRecElem;
  449. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  450. begin
  451. vmt:=ClassType;
  452. while vmt<>nil do
  453. begin
  454. { This need to be included here, because Finalize()
  455. has should support for tkClass }
  456. Temp:=Pointer((Pointer(vmt)+vmtInitTable)^);
  457. if Assigned(Temp) then
  458. begin
  459. inc(Temp);
  460. I:=Temp^;
  461. inc(temp,I+1); // skip name string;
  462. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  463. move(PRecRec(Temp)^.Count,Count,sizeof(Count));
  464. {$else FPC_REQUIRES_PROPER_ALIGNMENT}
  465. Count:=PRecRec(Temp)^.Count; // get element Count
  466. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  467. For I:=1 to count do
  468. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  469. begin
  470. move(PRecRec(Temp)^.elements[I],RecElem,sizeof(RecElem));
  471. With RecElem do
  472. int_Finalize (pointer(self)+Offset,Info);
  473. end;
  474. {$else FPC_REQUIRES_PROPER_ALIGNMENT}
  475. With PRecRec(Temp)^.elements[I] do
  476. int_Finalize (pointer(self)+Offset,Info);
  477. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  478. end;
  479. vmt:=pclass(pointer(vmt)+vmtParent)^;
  480. end;
  481. end;
  482. procedure TObject.AfterConstruction;
  483. begin
  484. end;
  485. procedure TObject.BeforeDestruction;
  486. begin
  487. end;
  488. {$ifdef HASINTF}
  489. function IsGUIDEqual(const guid1, guid2: tguid): boolean;
  490. begin
  491. IsGUIDEqual:=
  492. (guid1.D1=guid2.D1) and
  493. (PDWORD(@guid1.D2)^=PDWORD(@guid2.D2)^) and
  494. (PDWORD(@guid1.D4[0])^=PDWORD(@guid2.D4[0])^) and
  495. (PDWORD(@guid1.D4[4])^=PDWORD(@guid2.D4[4])^);
  496. end;
  497. function TObject.getinterface(const iid : tguid;out obj) : boolean;
  498. var
  499. IEntry: pinterfaceentry;
  500. begin
  501. IEntry:=getinterfaceentry(iid);
  502. if Assigned(IEntry) then begin
  503. PPointer(@obj)^:=Pointer(Self)+IEntry^.IOffset;
  504. intf_incr_ref(pointer(obj)); { it must be an com interface }
  505. getinterface:=True;
  506. end
  507. else begin
  508. PPointer(@Obj)^:=nil;
  509. getinterface:=False;
  510. end;
  511. end;
  512. function TObject.getinterfacebystr(const iidstr : string;out obj) : boolean;
  513. var
  514. IEntry: pinterfaceentry;
  515. begin
  516. IEntry:=getinterfaceentrybystr(iidstr);
  517. if Assigned(IEntry) then begin
  518. PPointer(@obj)^:=Pointer(Self)+IEntry^.IOffset;
  519. if Assigned(IEntry^.iid) then { for Com interfaces }
  520. intf_incr_ref(pointer(obj));
  521. getinterfacebystr:=True;
  522. end
  523. else begin
  524. PPointer(@Obj)^:=nil;
  525. getinterfacebystr:=False;
  526. end;
  527. end;
  528. class function TObject.getinterfaceentry(const iid : tguid) : pinterfaceentry;
  529. var
  530. i: integer;
  531. intftable: pinterfacetable;
  532. Res: pinterfaceentry;
  533. begin
  534. getinterfaceentry:=nil;
  535. intftable:=pinterfacetable((pointer(Self)+vmtIntfTable)^);
  536. if assigned(intftable) then begin
  537. i:=intftable^.EntryCount;
  538. Res:=@intftable^.Entries[0];
  539. while (i>0) and
  540. not (assigned(Res^.iid) and IsGUIDEqual(Res^.iid^,iid)) do begin
  541. inc(Res);
  542. dec(i);
  543. end;
  544. if (i>0) then
  545. getinterfaceentry:=Res;
  546. end;
  547. if (getinterfaceentry=nil)and not(classparent=nil) then
  548. getinterfaceentry:=classparent.getinterfaceentry(iid)
  549. end;
  550. class function TObject.getinterfaceentrybystr(const iidstr : string) : pinterfaceentry;
  551. var
  552. i: integer;
  553. intftable: pinterfacetable;
  554. Res: pinterfaceentry;
  555. begin
  556. getinterfaceentrybystr:=nil;
  557. intftable:=getinterfacetable;
  558. if assigned(intftable) then begin
  559. i:=intftable^.EntryCount;
  560. Res:=@intftable^.Entries[0];
  561. while (i>0) and (Res^.iidstr^<>iidstr) do begin
  562. inc(Res);
  563. dec(i);
  564. end;
  565. if (i>0) then
  566. getinterfaceentrybystr:=Res;
  567. end;
  568. if (getinterfaceentrybystr=nil)and not(classparent=nil) then
  569. getinterfaceentrybystr:=classparent.getinterfaceentrybystr(iidstr)
  570. end;
  571. class function TObject.getinterfacetable : pinterfacetable;
  572. begin
  573. getinterfacetable:=pinterfacetable((pointer(Self)+vmtIntfTable)^);
  574. end;
  575. {****************************************************************************
  576. TINTERFACEDOBJECT
  577. ****************************************************************************}
  578. function TInterfacedObject.QueryInterface(
  579. const iid : tguid;out obj) : longint;stdcall;
  580. begin
  581. if getinterface(iid,obj) then
  582. result:=0
  583. else
  584. result:=longint($80004002);
  585. end;
  586. function TInterfacedObject._AddRef : longint;stdcall;
  587. begin
  588. inclocked(frefcount);
  589. _addref:=frefcount;
  590. end;
  591. function TInterfacedObject._Release : longint;stdcall;
  592. begin
  593. if declocked(frefcount) then
  594. begin
  595. destroy;
  596. _Release:=0;
  597. end
  598. else
  599. _Release:=frefcount;
  600. end;
  601. procedure TInterfacedObject.AfterConstruction;
  602. begin
  603. { we need to fix the refcount we forced in newinstance }
  604. { further, it must be done in a thread safe way }
  605. declocked(frefcount);
  606. end;
  607. procedure TInterfacedObject.BeforeDestruction;
  608. begin
  609. if frefcount<>0 then
  610. HandleError(204);
  611. end;
  612. class function TInterfacedObject.NewInstance : TObject;
  613. begin
  614. NewInstance:=inherited NewInstance;
  615. TInterfacedObject(NewInstance).frefcount:=1;
  616. end;
  617. {$endif HASINTF}
  618. {****************************************************************************
  619. Exception Support
  620. ****************************************************************************}
  621. {$i except.inc}
  622. {****************************************************************************
  623. Initialize
  624. ****************************************************************************}
  625. {
  626. $Log$
  627. Revision 1.37 2004-04-28 20:48:20 peter
  628. * ordinal-pointer conversions fixed
  629. Revision 1.36 2004/03/22 22:19:36 florian
  630. * more alignment fixes
  631. Revision 1.35 2004/03/21 22:41:29 florian
  632. * CleanupInstance takes now care of FPC_REQUIRES_PROPER_ALIGNMENT
  633. Revision 1.34 2004/02/26 16:19:01 peter
  634. * tkclass removed from finalize()
  635. * cleanupinstance now parses the tkclass rtti entry itself and
  636. calls finalize() for the rtti members
  637. Revision 1.33 2003/07/19 11:19:07 michael
  638. + fix from Ivan Shikhalev for QueryInterface to return ancestor methods
  639. Revision 1.32 2003/05/01 08:05:23 florian
  640. * started to make the rtl 64 bit save by introducing SizeInt and SizeUInt (similar to size_t of C)
  641. Revision 1.31 2003/03/17 20:55:58 peter
  642. * ClassType changed to class method
  643. Revision 1.30 2002/10/19 15:53:20 peter
  644. * 'inlined' some more calls
  645. Revision 1.29 2002/10/15 19:29:49 peter
  646. * manual inline classparent calls in the loops
  647. Revision 1.28 2002/10/11 14:05:21 florian
  648. * initinterfacepointers improved
  649. Revision 1.27 2002/09/07 15:07:46 peter
  650. * old logs removed and tabs fixed
  651. Revision 1.26 2002/09/07 11:08:58 carl
  652. - remove logs
  653. Revision 1.25 2002/08/31 13:11:11 florian
  654. * several fixes for Linux/PPC compilation
  655. }