objpas.inc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  12. procedure DoDispCallByIDError(res : Pointer; const disp : IDispatch;desc : PDispDesc; params : Pointer);
  13. begin
  14. handleerrorframe(RuntimeErrorExitCodes[reVarDispatch],get_frame);
  15. end;
  16. procedure fpc_dispatch_by_id(Result: Pointer; const Dispatch: pointer;
  17. DispDesc: Pointer; Params: Pointer); compilerproc;
  18. type
  19. TDispProc = procedure(res : Pointer; const disp : IDispatch;desc : PDispDesc; params : Pointer);
  20. begin
  21. TDispProc(DispCallByIDProc)(Result,IDispatch(Dispatch),DispDesc,Params);
  22. end;
  23. {$endif FPC_HAS_FEATURE_VARIANTS}
  24. {****************************************************************************
  25. Internal Routines called from the Compiler
  26. ****************************************************************************}
  27. { the reverse order of the parameters make code generation easier }
  28. function fpc_do_is(aclass : tclass;aobject : tobject) : boolean;[public,alias: 'FPC_DO_IS']; compilerproc;
  29. begin
  30. fpc_do_is:=assigned(aobject) and assigned(aclass) and
  31. aobject.inheritsfrom(aclass);
  32. end;
  33. { the reverse order of the parameters make code generation easier }
  34. function fpc_do_as(aclass : tclass;aobject : tobject): tobject;[public,alias: 'FPC_DO_AS']; compilerproc;
  35. begin
  36. if assigned(aobject) and not(aobject.inheritsfrom(aclass)) then
  37. handleerrorframe(219,get_frame);
  38. result := aobject;
  39. end;
  40. { interface helpers }
  41. procedure fpc_intf_decr_ref(var i: pointer);[public,alias: 'FPC_INTF_DECR_REF']; compilerproc;
  42. begin
  43. if assigned(i) then
  44. begin
  45. IUnknown(i)._Release;
  46. i:=nil;
  47. end;
  48. end;
  49. { local declaration for intf_decr_ref for local access }
  50. procedure intf_decr_ref(var i: pointer); [external name 'FPC_INTF_DECR_REF'];
  51. procedure fpc_intf_incr_ref(i: pointer);[public,alias: 'FPC_INTF_INCR_REF']; compilerproc;
  52. begin
  53. if assigned(i) then
  54. IUnknown(i)._AddRef;
  55. end;
  56. { local declaration of intf_incr_ref for local access }
  57. procedure intf_incr_ref(i: pointer); [external name 'FPC_INTF_INCR_REF'];
  58. procedure fpc_intf_assign(var D: pointer; const S: pointer);[public,alias: 'FPC_INTF_ASSIGN']; compilerproc;
  59. begin
  60. if assigned(S) then
  61. IUnknown(S)._AddRef;
  62. if assigned(D) then
  63. IUnknown(D)._Release;
  64. D:=S;
  65. end;
  66. procedure fpc_intf_assign_by_iid(var D: pointer; const S: pointer; const iid: TGUID);[public,alias: 'FPC_INTF_ASSIGN2']; compilerproc;
  67. var
  68. tmp : pointer;
  69. begin
  70. if assigned(S) then
  71. begin
  72. if IUnknown(S).QueryInterface(iid,tmp)<>S_OK then
  73. handleerror(219);
  74. if assigned(D) then
  75. IUnknown(D)._Release;
  76. D:=tmp;
  77. end
  78. else
  79. begin
  80. if assigned(D) then
  81. IUnknown(D)._Release;
  82. D:=nil;
  83. end;
  84. end;
  85. function fpc_intf_as(const S: pointer; const iid: TGUID): IInterface;[public,alias: 'FPC_INTF_AS']; compilerproc;
  86. var
  87. tmpi: pointer; // _AddRef before _Release
  88. begin
  89. if assigned(S) then
  90. begin
  91. if IUnknown(S).QueryInterface(iid,tmpi)<>S_OK then
  92. handleerror(219);
  93. pointer(fpc_intf_as):=tmpi;
  94. end
  95. else
  96. fpc_intf_as:=nil;
  97. end;
  98. function fpc_class_as_intf(const S: pointer; const iid: TGUID): IInterface;[public,alias: 'FPC_CLASS_AS_INTF']; compilerproc;
  99. var
  100. tmpi: pointer; // _AddRef before _Release
  101. begin
  102. if assigned(S) then
  103. begin
  104. if not TObject(S).GetInterface(iid,tmpi) then
  105. handleerror(219);
  106. pointer(fpc_class_as_intf):=tmpi;
  107. end
  108. else
  109. fpc_class_as_intf:=nil;
  110. end;
  111. function fpc_class_as_corbaintf(const S: pointer; const iid: Shortstring): Pointer;[public,alias: 'FPC_CLASS_AS_CORBAINTF']; compilerproc;
  112. var
  113. tmpi: pointer; // _AddRef before _Release
  114. begin
  115. if assigned(S) then
  116. begin
  117. if not TObject(S).GetInterface(iid,tmpi) then
  118. handleerror(219);
  119. fpc_class_as_corbaintf:=tmpi;
  120. end
  121. else
  122. fpc_class_as_corbaintf:=nil;
  123. end;
  124. {****************************************************************************
  125. TOBJECT
  126. ****************************************************************************}
  127. constructor TObject.Create;
  128. begin
  129. end;
  130. destructor TObject.Destroy;
  131. begin
  132. end;
  133. procedure TObject.Free;
  134. begin
  135. // the call via self avoids a warning
  136. if self<>nil then
  137. self.destroy;
  138. end;
  139. class function TObject.InstanceSize : SizeInt;
  140. begin
  141. InstanceSize := PVmt(Self)^.vInstanceSize;
  142. end;
  143. var
  144. emptyintf: ptruint; public name 'FPC_EMPTYINTF';
  145. procedure InitInterfacePointers(objclass: tclass;instance : pointer);
  146. var
  147. ovmt: PVmt;
  148. i: longint;
  149. intftable: pinterfacetable;
  150. Res: pinterfaceentry;
  151. begin
  152. ovmt := PVmt(objclass);
  153. while assigned(ovmt) and (ovmt^.vIntfTable <> @emptyintf) do
  154. begin
  155. intftable:=ovmt^.vIntfTable;
  156. if assigned(intftable) then
  157. begin
  158. i:=intftable^.EntryCount;
  159. Res:=@intftable^.Entries[0];
  160. while i>0 do begin
  161. if Res^.IType = etStandard then
  162. ppointer(@(pbyte(instance)[Res^.IOffset]))^:=
  163. pointer(Res^.VTable);
  164. inc(Res);
  165. dec(i);
  166. end;
  167. end;
  168. ovmt:=ovmt^.vParent;
  169. end;
  170. end;
  171. class function TObject.InitInstance(instance : pointer) : tobject; {$ifdef SYSTEMINLINE} inline; {$ENDIF}
  172. begin
  173. { the size is saved at offset 0 }
  174. fillchar(instance^, InstanceSize, 0);
  175. { insert VMT pointer into the new created memory area }
  176. { (in class methods self contains the VMT!) }
  177. ppointer(instance)^:=pointer(self);
  178. if PVmt(self)^.vIntfTable <> @emptyintf then
  179. InitInterfacePointers(self,instance);
  180. InitInstance:=TObject(Instance);
  181. end;
  182. class function TObject.ClassParent : tclass;
  183. begin
  184. { type of self is class of tobject => it points to the vmt }
  185. { the parent vmt is saved at offset vmtParent }
  186. classparent:=tclass(PVmt(Self)^.vParent);
  187. end;
  188. class function TObject.NewInstance : tobject;
  189. var
  190. p : pointer;
  191. begin
  192. getmem(p, InstanceSize);
  193. if p <> nil then
  194. InitInstance(p);
  195. NewInstance:=TObject(p);
  196. end;
  197. procedure TObject.FreeInstance;
  198. begin
  199. CleanupInstance;
  200. FreeMem(Pointer(Self));
  201. end;
  202. class function TObject.ClassType : TClass;
  203. begin
  204. ClassType:=TClass(Pointer(Self))
  205. end;
  206. type
  207. tmethodnamerec = packed record
  208. name : pshortstring;
  209. addr : pointer;
  210. end;
  211. tmethodnametable = packed record
  212. count : dword;
  213. entries : packed array[0..0] of tmethodnamerec;
  214. end;
  215. pmethodnametable = ^tmethodnametable;
  216. class function TObject.MethodAddress(const name : shortstring) : pointer;
  217. var
  218. methodtable : pmethodnametable;
  219. i : dword;
  220. ovmt : PVmt;
  221. begin
  222. ovmt:=PVmt(self);
  223. while assigned(ovmt) do
  224. begin
  225. methodtable:=pmethodnametable(ovmt^.vMethodTable);
  226. if assigned(methodtable) then
  227. begin
  228. for i:=0 to methodtable^.count-1 do
  229. if ShortCompareText(methodtable^.entries[i].name^, name)=0 then
  230. begin
  231. MethodAddress:=methodtable^.entries[i].addr;
  232. exit;
  233. end;
  234. end;
  235. ovmt := ovmt^.vParent;
  236. end;
  237. MethodAddress:=nil;
  238. end;
  239. class function TObject.MethodName(address : pointer) : shortstring;
  240. var
  241. methodtable : pmethodnametable;
  242. i : dword;
  243. ovmt : PVmt;
  244. begin
  245. ovmt:=PVmt(self);
  246. while assigned(ovmt) do
  247. begin
  248. methodtable:=pmethodnametable(ovmt^.vMethodTable);
  249. if assigned(methodtable) then
  250. begin
  251. for i:=0 to methodtable^.count-1 do
  252. if methodtable^.entries[i].addr=address then
  253. begin
  254. MethodName:=methodtable^.entries[i].name^;
  255. exit;
  256. end;
  257. end;
  258. ovmt := ovmt^.vParent;
  259. end;
  260. MethodName:='';
  261. end;
  262. function TObject.FieldAddress(const name : shortstring) : pointer;
  263. type
  264. PFieldInfo = ^TFieldInfo;
  265. TFieldInfo =
  266. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  267. packed
  268. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  269. record
  270. FieldOffset: PtrUInt;
  271. ClassTypeIndex: Word;
  272. Name: ShortString;
  273. end;
  274. PFieldTable = ^TFieldTable;
  275. TFieldTable =
  276. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  277. packed
  278. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  279. record
  280. FieldCount: Word;
  281. ClassTable: Pointer;
  282. { should be array[Word] of TFieldInfo; but
  283. Elements have variant size! force at least proper alignment }
  284. Fields: array[0..0] of TFieldInfo
  285. end;
  286. var
  287. ovmt: PVmt;
  288. FieldTable: PFieldTable;
  289. FieldInfo: PFieldInfo;
  290. i: longint;
  291. begin
  292. if Length(name) > 0 then
  293. begin
  294. ovmt := PVmt(ClassType);
  295. while ovmt <> nil do
  296. begin
  297. FieldTable := PFieldTable(ovmt^.vFieldTable);
  298. if FieldTable <> nil then
  299. begin
  300. FieldInfo := @FieldTable^.Fields[0];
  301. for i := 0 to FieldTable^.FieldCount - 1 do
  302. begin
  303. if ShortCompareText(FieldInfo^.Name, name) = 0 then
  304. begin
  305. fieldaddress := Pointer(Self) + FieldInfo^.FieldOffset;
  306. exit;
  307. end;
  308. FieldInfo := PFieldInfo(PByte(@FieldInfo^.Name) + 1 + Length(FieldInfo^.Name));
  309. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  310. { align to largest field of TFieldInfo }
  311. FieldInfo := Align(FieldInfo, SizeOf(PtrUInt));
  312. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  313. end;
  314. end;
  315. { Try again with the parent class type }
  316. ovmt:=ovmt^.vParent;
  317. end;
  318. end;
  319. fieldaddress:=nil;
  320. end;
  321. function TObject.SafeCallException(exceptobject : tobject;
  322. exceptaddr : pointer) : longint;
  323. begin
  324. safecallexception:=0;
  325. end;
  326. class function TObject.ClassInfo : pointer;
  327. begin
  328. ClassInfo := PVmt(Self)^.vTypeInfo;
  329. end;
  330. class function TObject.ClassName : ShortString;
  331. begin
  332. ClassName := PVmt(Self)^.vClassName^;
  333. end;
  334. class function TObject.ClassNameIs(const name : string) : boolean;
  335. begin
  336. // call to ClassName inlined here, this eliminates stack and string copying.
  337. ClassNameIs:=ShortCompareText(PVmt(Self)^.vClassName^, name) = 0;
  338. end;
  339. class function TObject.InheritsFrom(aclass : TClass) : Boolean;
  340. var
  341. vmt: PVmt;
  342. begin
  343. vmt:=PVmt(self);
  344. while assigned(vmt) and (vmt <> PVmt(aclass)) do
  345. vmt := vmt^.vParent;
  346. InheritsFrom := (vmt = PVmt(aclass));
  347. end;
  348. class function TObject.stringmessagetable : pstringmessagetable;
  349. begin
  350. stringmessagetable:=PVmt(Self)^.vMsgStrPtr;
  351. end;
  352. type
  353. tmessagehandler = procedure(var msg) of object;
  354. procedure TObject.Dispatch(var message);
  355. type
  356. tmsgtable = packed record
  357. index : dword;
  358. method : pointer;
  359. end;
  360. pmsgtable = ^tmsgtable;
  361. var
  362. index : dword;
  363. count,i : longint;
  364. msgtable : pmsgtable;
  365. p : pointer;
  366. ovmt : PVmt;
  367. msghandler : tmessagehandler;
  368. begin
  369. index:=dword(message);
  370. ovmt := PVmt(ClassType);
  371. while assigned(ovmt) do
  372. begin
  373. // See if we have messages at all in this class.
  374. p:=ovmt^.vDynamicTable;
  375. If Assigned(p) then
  376. begin
  377. msgtable:=pmsgtable(p+4);
  378. count:=pdword(p)^;
  379. end
  380. else
  381. Count:=0;
  382. { later, we can implement a binary search here }
  383. for i:=0 to count-1 do
  384. begin
  385. if index=msgtable[i].index then
  386. begin
  387. TMethod(msghandler).Code:=msgtable[i].method;
  388. TMethod(msghandler).Data:=self;
  389. msghandler(message);
  390. exit;
  391. end;
  392. end;
  393. ovmt:=ovmt^.vParent;
  394. end;
  395. DefaultHandler(message);
  396. end;
  397. procedure TObject.DispatchStr(var message);
  398. type
  399. PSizeUInt = ^SizeUInt;
  400. var
  401. name : shortstring;
  402. count,i : longint;
  403. msgstrtable : pmsgstrtable;
  404. p: pstringmessagetable;
  405. ovmt : PVmt;
  406. msghandler : tmessagehandler;
  407. begin
  408. name:=pshortstring(@message)^;
  409. ovmt:=PVmt(ClassType);
  410. while assigned(ovmt) do
  411. begin
  412. p := ovmt^.vMsgStrPtr;
  413. if (P<>Nil) and (p^.count<>0) then
  414. begin
  415. count:=p^.count;
  416. msgstrtable:=@p^.msgstrtable;
  417. end
  418. else
  419. Count:=0;
  420. { later, we can implement a binary search here }
  421. for i:=0 to count-1 do
  422. begin
  423. if name=msgstrtable[i].name^ then
  424. begin
  425. TMethod(msghandler).Code:=msgstrtable[i].method;
  426. TMethod(msghandler).Data:=self;
  427. msghandler(message);
  428. exit;
  429. end;
  430. end;
  431. ovmt:=ovmt^.vParent;
  432. end;
  433. DefaultHandlerStr(message);
  434. end;
  435. procedure TObject.DefaultHandler(var message);
  436. begin
  437. end;
  438. procedure TObject.DefaultHandlerStr(var message);
  439. begin
  440. end;
  441. procedure TObject.CleanupInstance;
  442. Type
  443. TRecElem = packed Record
  444. Info : Pointer;
  445. Offset : Longint;
  446. end;
  447. {$ifdef CPU16}
  448. TRecElemArray = packed array[1..Maxint div sizeof(TRecElem)-1] of TRecElem;
  449. {$else CPU16}
  450. TRecElemArray = packed array[1..Maxint] of TRecElem;
  451. {$endif CPU16}
  452. PRecRec = ^TRecRec;
  453. TRecRec = record
  454. Size,Count : Longint;
  455. Elements : TRecElemArray;
  456. end;
  457. var
  458. vmt : PVmt;
  459. temp : pbyte;
  460. count,
  461. i : longint;
  462. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  463. recelem : TRecElem;
  464. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  465. begin
  466. vmt := PVmt(ClassType);
  467. while vmt<>nil do
  468. begin
  469. { This need to be included here, because Finalize()
  470. has should support for tkClass }
  471. Temp:= vmt^.vInitTable;
  472. if Assigned(Temp) then
  473. begin
  474. inc(Temp);
  475. I:=Temp^;
  476. inc(temp,I+1); // skip name string;
  477. temp:=aligntoptr(temp);
  478. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  479. move(PRecRec(Temp)^.Count,Count,sizeof(Count));
  480. {$else FPC_REQUIRES_PROPER_ALIGNMENT}
  481. Count:=PRecRec(Temp)^.Count; // get element Count
  482. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  483. For I:=1 to count do
  484. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  485. begin
  486. move(PRecRec(Temp)^.elements[I],RecElem,sizeof(RecElem));
  487. With RecElem do
  488. int_Finalize (pointer(self)+Offset,Info);
  489. end;
  490. {$else FPC_REQUIRES_PROPER_ALIGNMENT}
  491. With PRecRec(Temp)^.elements[I] do
  492. int_Finalize (pointer(self)+Offset,Info);
  493. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  494. end;
  495. vmt:= vmt^.vParent;
  496. end;
  497. end;
  498. procedure TObject.AfterConstruction;
  499. begin
  500. end;
  501. procedure TObject.BeforeDestruction;
  502. begin
  503. end;
  504. function IsGUIDEqual(const guid1, guid2: tguid): boolean;
  505. begin
  506. IsGUIDEqual:=
  507. (guid1.D1=guid2.D1) and
  508. (PDWORD(@guid1.D2)^=PDWORD(@guid2.D2)^) and
  509. (PDWORD(@guid1.D4[0])^=PDWORD(@guid2.D4[0])^) and
  510. (PDWORD(@guid1.D4[4])^=PDWORD(@guid2.D4[4])^);
  511. end;
  512. function getinterfacebyentry(Instance: pointer; IEntry: pinterfaceentry; Corba: Boolean; out obj): boolean;
  513. var
  514. Getter: function: IInterface of object;
  515. begin
  516. Pointer(Obj) := nil;
  517. if Assigned(IEntry) and Assigned(Instance) then
  518. begin
  519. case IEntry^.IType of
  520. etStandard:
  521. begin
  522. //writeln('Doing etStandard cast of ', TObject(Instance).classname(), ' with self = ', ptruint(Instance), ' and offset = ', IEntry^.IOffset);
  523. Pbyte(Obj):=Pbyte(instance)+IEntry^.IOffset;
  524. end;
  525. etFieldValue:
  526. begin
  527. // writeln('Doing etFieldValue cast of ', TObject(Instance).classname(), ' with offset = ', IEntry^.IOffset);
  528. Pointer(obj) := PPointer(Pbyte(Instance)+IEntry^.IOffset)^;
  529. end;
  530. etVirtualMethodResult:
  531. begin
  532. //writeln('Doing etVirtualMethodResult cast of ', TObject(Instance).classname());
  533. TMethod(Getter).data := Instance;
  534. TMethod(Getter).code := ppointer(Pbyte(Instance) + IEntry^.IOffset)^;
  535. Pointer(obj) := Pointer(Getter());
  536. end;
  537. etStaticMethodResult:
  538. begin
  539. //writeln('Doing etStaticMethodResult cast of ', TObject(Instance).classname());
  540. TMethod(Getter).data := Instance;
  541. TMethod(Getter).code := pointer(IEntry^.IOffset);
  542. Pointer(obj) := Pointer(Getter());
  543. end;
  544. end;
  545. end;
  546. result := assigned(pointer(obj));
  547. if result and not Corba then
  548. IInterface(obj)._AddRef;
  549. end;
  550. function TObject.getinterface(const iid : tguid;out obj) : boolean;
  551. begin
  552. Result := getinterfacebyentry(self, getinterfaceentry(iid), false, obj);
  553. end;
  554. function TObject.getinterfacebystr(const iidstr : shortstring;out obj) : boolean;
  555. begin
  556. Result := getinterfacebyentry(self, getinterfaceentrybystr(iidstr), true, obj);
  557. end;
  558. function TObject.getinterface(const iidstr : shortstring;out obj) : boolean;
  559. begin
  560. Result := getinterfacebystr(iidstr,obj);
  561. end;
  562. class function TObject.getinterfaceentry(const iid : tguid) : pinterfaceentry;
  563. var
  564. i: longint;
  565. intftable: pinterfacetable;
  566. ovmt: PVmt;
  567. begin
  568. ovmt := PVmt(Self);
  569. while Assigned(ovmt) and (ovmt^.vIntfTable <> @emptyintf) do
  570. begin
  571. intftable:=ovmt^.vIntfTable;
  572. if assigned(intftable) then
  573. begin
  574. for i:=0 to intftable^.EntryCount-1 do
  575. begin
  576. result:=@intftable^.Entries[i];
  577. if assigned(Result^.iid) and IsGUIDEqual(Result^.iid^,iid) then
  578. Exit;
  579. end;
  580. end;
  581. ovmt := ovmt^.vParent;
  582. end;
  583. result := nil;
  584. end;
  585. class function TObject.getinterfaceentrybystr(const iidstr : shortstring) : pinterfaceentry;
  586. var
  587. i: longint;
  588. intftable: pinterfacetable;
  589. ovmt: PVmt;
  590. begin
  591. ovmt := PVmt(Self);
  592. while Assigned(ovmt) and (ovmt^.vIntfTable <> @emptyintf) do
  593. begin
  594. intftable:=ovmt^.vIntfTable;
  595. if assigned(intftable) then
  596. begin
  597. for i:=0 to intftable^.EntryCount-1 do
  598. begin
  599. result:=@intftable^.Entries[i];
  600. if assigned(result^.iidstr) and (result^.iidstr^ = iidstr) then
  601. Exit;
  602. end;
  603. end;
  604. ovmt := ovmt^.vParent;
  605. end;
  606. result:=nil;
  607. end;
  608. class function TObject.getinterfacetable : pinterfacetable;
  609. begin
  610. getinterfacetable:=PVmt(Self)^.vIntfTable;
  611. end;
  612. class function TObject.UnitName : string;
  613. type
  614. // from the typinfo unit
  615. TClassTypeInfo = {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  616. ClassType: TClass;
  617. ParentInfo: Pointer;
  618. PropCount: SmallInt;
  619. UnitName: ShortString;
  620. end;
  621. PClassTypeInfo = ^TClassTypeInfo;
  622. var
  623. classtypeinfo: PClassTypeInfo;
  624. begin
  625. classtypeinfo:=ClassInfo;
  626. if Assigned(classtypeinfo) then
  627. begin
  628. // offset PTypeInfo by Length(Name) + 2 (ShortString length byte + SizeOf(Kind))
  629. inc(Pointer(classtypeinfo), PByte(Pointer(classtypeinfo)+1)^ + 2);
  630. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  631. classtypeinfo:=align(classtypeinfo,sizeof(classtypeinfo));
  632. {$endif}
  633. result:=classtypeinfo^.UnitName;
  634. end
  635. else
  636. result:='';
  637. end;
  638. function TObject.Equals(Obj: TObject) : boolean;
  639. begin
  640. result:=Obj=Self;
  641. end;
  642. function TObject.GetHashCode: PtrInt;
  643. begin
  644. result:=PtrInt(Self);
  645. end;
  646. function TObject.ToString: string;
  647. begin
  648. result:=ClassName;
  649. end;
  650. {****************************************************************************
  651. TINTERFACEDOBJECT
  652. ****************************************************************************}
  653. function TInterfacedObject.QueryInterface(
  654. const iid : tguid;out obj) : longint;stdcall;
  655. begin
  656. if getinterface(iid,obj) then
  657. result:=0
  658. else
  659. result:=longint(E_NOINTERFACE);
  660. end;
  661. function TInterfacedObject._AddRef : longint;stdcall;
  662. begin
  663. _addref:=interlockedincrement(frefcount);
  664. end;
  665. function TInterfacedObject._Release : longint;stdcall;
  666. begin
  667. _Release:=interlockeddecrement(frefcount);
  668. if _Release=0 then
  669. self.destroy;
  670. end;
  671. procedure TInterfacedObject.AfterConstruction;
  672. begin
  673. { we need to fix the refcount we forced in newinstance }
  674. { further, it must be done in a thread safe way }
  675. declocked(frefcount);
  676. end;
  677. procedure TInterfacedObject.BeforeDestruction;
  678. begin
  679. if frefcount<>0 then
  680. HandleError(204);
  681. end;
  682. class function TInterfacedObject.NewInstance : TObject;
  683. begin
  684. NewInstance:=inherited NewInstance;
  685. if NewInstance<>nil then
  686. TInterfacedObject(NewInstance).frefcount:=1;
  687. end;
  688. {****************************************************************************
  689. TAGGREGATEDOBJECT
  690. ****************************************************************************}
  691. constructor TAggregatedObject.Create(const aController: IUnknown);
  692. begin
  693. inherited Create;
  694. { do not keep a counted reference to the controller! }
  695. fcontroller := Pointer(aController);
  696. end;
  697. function TAggregatedObject.QueryInterface(
  698. const iid : tguid;out obj) : longint;stdcall;
  699. begin
  700. Result := IUnknown(fcontroller).QueryInterface(iid, obj);
  701. end;
  702. function TAggregatedObject._AddRef : longint;stdcall;
  703. begin
  704. Result := IUnknown(fcontroller)._AddRef;
  705. end;
  706. function TAggregatedObject._Release : longint;stdcall;
  707. begin
  708. Result := IUnknown(fcontroller)._Release;
  709. end;
  710. function TAggregatedObject.GetController : IUnknown;
  711. begin
  712. Result := IUnknown(fcontroller);
  713. end;
  714. {****************************************************************************
  715. TContainedOBJECT
  716. ****************************************************************************}
  717. function TContainedObject.QueryInterface(
  718. const iid : tguid;out obj) : longint; stdcall;
  719. begin
  720. if getinterface(iid,obj) then
  721. result:=0
  722. else
  723. result:=longint(E_NOINTERFACE);
  724. end;
  725. {****************************************************************************
  726. Exception Support
  727. ****************************************************************************}
  728. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  729. {$i except.inc}
  730. {$endif FPC_HAS_FEATURE_EXCEPTIONS}