objpas.inc 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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. handleerroraddrframeind(RuntimeErrorExitCodes[reVarDispatch],
  15. get_pc_addr,get_frame);
  16. end;
  17. procedure fpc_dispatch_by_id(Result: Pointer; const Dispatch: pointer;
  18. DispDesc: Pointer; Params: Pointer); compilerproc;
  19. type
  20. TDispProc = procedure(res : Pointer; const disp : IDispatch;desc : PDispDesc; params : Pointer);
  21. begin
  22. TDispProc(DispCallByIDProc)(Result,IDispatch(Dispatch),DispDesc,Params);
  23. end;
  24. {$endif FPC_HAS_FEATURE_VARIANTS}
  25. {****************************************************************************
  26. Internal Routines called from the Compiler
  27. ****************************************************************************}
  28. { the reverse order of the parameters make code generation easier }
  29. function fpc_do_is(aclass : tclass;aobject : tobject) : boolean;[public,alias: 'FPC_DO_IS']; compilerproc;
  30. begin
  31. fpc_do_is:=assigned(aobject) and assigned(aclass) and
  32. aobject.inheritsfrom(aclass);
  33. end;
  34. { the reverse order of the parameters make code generation easier }
  35. function fpc_do_as(aclass : tclass;aobject : tobject): tobject;[public,alias: 'FPC_DO_AS']; compilerproc;
  36. begin
  37. if assigned(aobject) and not(aobject.inheritsfrom(aclass)) then
  38. handleerroraddrframeInd(219,get_pc_addr,get_frame);
  39. result := aobject;
  40. end;
  41. { interface helpers }
  42. procedure fpc_intf_decr_ref(var i: pointer);[public,alias: 'FPC_INTF_DECR_REF']; compilerproc;
  43. begin
  44. if assigned(i) then
  45. begin
  46. IUnknown(i)._Release;
  47. i:=nil;
  48. end;
  49. end;
  50. { local declaration for intf_decr_ref for local access }
  51. procedure intf_decr_ref(var i: pointer); [external name 'FPC_INTF_DECR_REF'];
  52. procedure fpc_intf_incr_ref(i: pointer);[public,alias: 'FPC_INTF_INCR_REF']; compilerproc;
  53. begin
  54. if assigned(i) then
  55. IUnknown(i)._AddRef;
  56. end;
  57. { local declaration of intf_incr_ref for local access }
  58. procedure intf_incr_ref(i: pointer); [external name 'FPC_INTF_INCR_REF'];
  59. procedure fpc_intf_assign(var D: pointer; const S: pointer);[public,alias: 'FPC_INTF_ASSIGN']; compilerproc;
  60. begin
  61. if assigned(S) then
  62. IUnknown(S)._AddRef;
  63. if assigned(D) then
  64. IUnknown(D)._Release;
  65. D:=S;
  66. end;
  67. procedure fpc_intf_assign(var D: pointer; const s: pointer); [external name 'FPC_INTF_ASSIGN'];
  68. {procedure fpc_intf_assign_by_iid(var D: pointer; const S: pointer; const iid: TGUID);[public,alias: 'FPC_INTF_ASSIGN2']; compilerproc;
  69. var
  70. tmp : pointer;
  71. begin
  72. if assigned(S) then
  73. begin
  74. tmp:=nil;
  75. if IUnknown(S).QueryInterface(iid,tmp)<>S_OK then
  76. handleerror(219);
  77. if assigned(D) then
  78. IUnknown(D)._Release;
  79. D:=tmp;
  80. end
  81. else
  82. begin
  83. if assigned(D) then
  84. IUnknown(D)._Release;
  85. D:=nil;
  86. end;
  87. end;}
  88. function fpc_intf_is(const S: pointer; const iid: TGUID): Boolean;[public,alias: 'FPC_INTF_IS']; compilerproc;
  89. var
  90. tmpi: pointer;
  91. begin
  92. tmpi:=nil;
  93. fpc_intf_is:=Assigned(S) and (IUnknown(S).QueryInterface(iid,tmpi)=S_OK);
  94. if Assigned(tmpi) then
  95. IUnknown(tmpi)._Release;
  96. end;
  97. function fpc_intf_is_class(const S: pointer; const aclass: tclass): Boolean;[public,alias: 'FPC_INTF_IS_CLASS']; compilerproc;
  98. var
  99. tmpo: tobject;
  100. begin
  101. fpc_intf_is_class:=Assigned(S) and (IUnknown(S).QueryInterface(IObjectInstance,tmpo)=S_OK) and tmpo.InheritsFrom(aclass);
  102. end;
  103. function fpc_class_is_intf(const S: pointer; const iid: TGUID): Boolean;[public,alias: 'FPC_CLASS_IS_INTF']; compilerproc;
  104. var
  105. tmpi: pointer;
  106. tmpi2: pointer; // weak!
  107. begin
  108. tmpi:=nil;
  109. tmpi2:=nil;
  110. fpc_class_is_intf:=Assigned(S) and ((TObject(S).GetInterfaceWeak(IUnknown,tmpi2) and (IUnknown(tmpi2).QueryInterface(IID,tmpi)=S_OK)) or
  111. TObject(S).GetInterface(IID,tmpi));
  112. if Assigned(tmpi) then
  113. IUnknown(tmpi)._Release;
  114. end;
  115. function fpc_class_is_corbaintf(const S: pointer; const iid: Shortstring): Boolean;[public,alias: 'FPC_CLASS_IS_CORBAINTF']; compilerproc;
  116. begin
  117. fpc_class_is_corbaintf:=Assigned(S) and Assigned(TObject(S).GetInterfaceEntryByStr(iid));
  118. end;
  119. function fpc_intf_cast(const S: pointer; const iid: TGUID): IInterface;[public,alias: 'FPC_INTF_CAST']; compilerproc;
  120. var
  121. tmpi: pointer;
  122. begin
  123. tmpi:=nil;
  124. if Assigned(S) and (IUnknown(S).QueryInterface(iid,tmpi)=S_OK) then
  125. pointer(fpc_intf_cast):=tmpi
  126. else
  127. fpc_intf_cast:= nil;
  128. end;
  129. function fpc_intf_cast_class(const S: pointer; const aclass: tclass): pointer;[public,alias: 'FPC_INTF_CAST_CLASS']; compilerproc;
  130. var
  131. tmpo: tobject;
  132. begin
  133. if Assigned(S) and (IUnknown(S).QueryInterface(IObjectInstance,tmpo)=S_OK) and tmpo.InheritsFrom(aclass) then
  134. fpc_intf_cast_class:=tmpo
  135. else
  136. fpc_intf_cast_class:=nil;
  137. end;
  138. function fpc_class_cast_intf(const S: pointer; const iid: TGUID): IInterface;[public,alias: 'FPC_CLASS_CAST_INTF']; compilerproc;
  139. var
  140. tmpi: pointer;
  141. tmpi2: pointer; // weak!
  142. begin
  143. tmpi:=nil;
  144. tmpi2:=nil;
  145. if Assigned(S) and ((TObject(S).GetInterfaceWeak(IUnknown,tmpi2) and (IUnknown(tmpi2).QueryInterface(IID,tmpi)=S_OK)) or
  146. TObject(S).GetInterface(IID,tmpi)) then
  147. begin
  148. // decrease reference count
  149. fpc_class_cast_intf:=nil;
  150. pointer(fpc_class_cast_intf):=tmpi
  151. end
  152. else
  153. fpc_class_cast_intf:=nil;
  154. end;
  155. function fpc_class_cast_corbaintf(const S: pointer; const iid: Shortstring): Pointer;[public,alias: 'FPC_CLASS_CAST_CORBAINTF']; compilerproc;
  156. var
  157. tmpi: pointer;
  158. begin
  159. if Assigned(S) and TObject(S).GetInterface(iid,tmpi) then
  160. fpc_class_cast_corbaintf:=tmpi
  161. else
  162. fpc_class_cast_corbaintf:=nil;
  163. end;
  164. function fpc_intf_as(const S: pointer; const iid: TGUID): IInterface;[public,alias: 'FPC_INTF_AS']; compilerproc;
  165. var
  166. tmpi: pointer; // _AddRef before _Release
  167. begin
  168. if assigned(S) then
  169. begin
  170. tmpi:=nil;
  171. if IUnknown(S).QueryInterface(iid,tmpi)<>S_OK then
  172. handleerror(219);
  173. // decrease reference count
  174. fpc_intf_as:=nil;
  175. pointer(fpc_intf_as):=tmpi;
  176. end
  177. else
  178. fpc_intf_as:=nil;
  179. end;
  180. function fpc_intf_as_class(const S: pointer; const aclass: tclass): pointer;[public,alias: 'FPC_INTF_AS_CLASS']; compilerproc;
  181. var
  182. tmpo: tobject;
  183. begin
  184. if assigned(S) then
  185. begin
  186. if not ((IUnknown(S).QueryInterface(IObjectInstance,tmpo)=S_OK) and tmpo.inheritsfrom(aclass)) then
  187. handleerror(219);
  188. fpc_intf_as_class:=tmpo;
  189. end
  190. else
  191. fpc_intf_as_class:=nil;
  192. end;
  193. function fpc_class_as_intf(const S: pointer; const iid: TGUID): IInterface;[public,alias: 'FPC_CLASS_AS_INTF']; compilerproc;
  194. var
  195. tmpi: pointer; // _AddRef before _Release
  196. tmpi2: pointer; // weak!
  197. begin
  198. if assigned(S) then
  199. begin
  200. tmpi:=nil;
  201. tmpi2:=nil;
  202. if not ((TObject(S).GetInterfaceWeak(IUnknown,tmpi2) and (IUnknown(tmpi2).QueryInterface(IID,tmpi)=S_OK)) or TObject(S).GetInterface(IID,tmpi)) then
  203. handleerror(219);
  204. // decrease reference count
  205. fpc_class_as_intf:=nil;
  206. pointer(fpc_class_as_intf):=tmpi;
  207. end
  208. else
  209. fpc_class_as_intf:=nil;
  210. end;
  211. function fpc_class_as_corbaintf(const S: pointer; const iid: Shortstring): Pointer;[public,alias: 'FPC_CLASS_AS_CORBAINTF']; compilerproc;
  212. var
  213. tmpi: pointer; // _AddRef before _Release
  214. begin
  215. if assigned(S) then
  216. begin
  217. tmpi:=nil;
  218. if not TObject(S).GetInterface(iid,tmpi) then
  219. handleerror(219);
  220. fpc_class_as_corbaintf:=tmpi;
  221. end
  222. else
  223. fpc_class_as_corbaintf:=nil;
  224. end;
  225. {****************************************************************************
  226. TOBJECT
  227. ****************************************************************************}
  228. constructor TObject.Create;
  229. begin
  230. end;
  231. destructor TObject.Destroy;
  232. begin
  233. end;
  234. procedure TObject.Free;
  235. begin
  236. // the call via self avoids a warning
  237. if self<>nil then
  238. self.destroy;
  239. end;
  240. class function TObject.InstanceSize : SizeInt;
  241. begin
  242. InstanceSize := PVmt(Self)^.vInstanceSize;
  243. end;
  244. var
  245. emptyintf: ptruint; public name 'FPC_EMPTYINTF';
  246. procedure InitInterfacePointers(objclass: tclass;instance : pointer);
  247. var
  248. ovmt: PVmt;
  249. i: longint;
  250. intftable: pinterfacetable;
  251. Res: pinterfaceentry;
  252. begin
  253. ovmt := PVmt(objclass);
  254. while assigned(ovmt) and (ovmt^.vIntfTable <> @emptyintf) do
  255. begin
  256. intftable:=ovmt^.vIntfTable;
  257. if assigned(intftable) then
  258. begin
  259. i:=intftable^.EntryCount;
  260. Res:=@intftable^.Entries[0];
  261. while i>0 do begin
  262. if Res^.IType = etStandard then
  263. ppointer(@(pbyte(instance)[Res^.IOffset]))^:=
  264. pointer(Res^.VTable);
  265. inc(Res);
  266. dec(i);
  267. end;
  268. end;
  269. ovmt:=ovmt^.vParent;
  270. end;
  271. end;
  272. class function TObject.InitInstance(instance : pointer) : tobject; {$ifdef SYSTEMINLINE} inline; {$ENDIF}
  273. begin
  274. { the size is saved at offset 0 }
  275. fillchar(instance^, InstanceSize, 0);
  276. { insert VMT pointer into the new created memory area }
  277. { (in class methods self contains the VMT!) }
  278. ppointer(instance)^:=pointer(self);
  279. if PVmt(self)^.vIntfTable <> @emptyintf then
  280. InitInterfacePointers(self,instance);
  281. InitInstance:=TObject(Instance);
  282. end;
  283. class function TObject.ClassParent : tclass;
  284. begin
  285. { type of self is class of tobject => it points to the vmt }
  286. { the parent vmt is saved at offset vmtParent }
  287. classparent:=tclass(PVmt(Self)^.vParent);
  288. end;
  289. class function TObject.NewInstance : tobject;
  290. var
  291. p : pointer;
  292. begin
  293. getmem(p, InstanceSize);
  294. if p <> nil then
  295. InitInstance(p);
  296. NewInstance:=TObject(p);
  297. end;
  298. procedure TObject.FreeInstance;
  299. begin
  300. CleanupInstance;
  301. FreeMem(Pointer(Self));
  302. end;
  303. class function TObject.ClassType : TClass;
  304. begin
  305. ClassType:=TClass(Pointer(Self))
  306. end;
  307. type
  308. tmethodnamerec = packed record
  309. name : pshortstring;
  310. addr : codepointer;
  311. end;
  312. tmethodnametable = packed record
  313. count : dword;
  314. entries : packed array[0..0] of tmethodnamerec;
  315. end;
  316. pmethodnametable = ^tmethodnametable;
  317. class function TObject.MethodAddress(const name : shortstring) : codepointer;
  318. var
  319. methodtable : pmethodnametable;
  320. i : dword;
  321. ovmt : PVmt;
  322. begin
  323. ovmt:=PVmt(self);
  324. while assigned(ovmt) do
  325. begin
  326. methodtable:=pmethodnametable(ovmt^.vMethodTable);
  327. if assigned(methodtable) then
  328. begin
  329. for i:=0 to methodtable^.count-1 do
  330. if ShortCompareText(methodtable^.entries[i].name^, name)=0 then
  331. begin
  332. MethodAddress:=methodtable^.entries[i].addr;
  333. exit;
  334. end;
  335. end;
  336. ovmt := ovmt^.vParent;
  337. end;
  338. MethodAddress:=nil;
  339. end;
  340. class function TObject.MethodName(address : codepointer) : shortstring;
  341. var
  342. methodtable : pmethodnametable;
  343. i : dword;
  344. ovmt : PVmt;
  345. begin
  346. ovmt:=PVmt(self);
  347. while assigned(ovmt) do
  348. begin
  349. methodtable:=pmethodnametable(ovmt^.vMethodTable);
  350. if assigned(methodtable) then
  351. begin
  352. for i:=0 to methodtable^.count-1 do
  353. if methodtable^.entries[i].addr=address then
  354. begin
  355. MethodName:=methodtable^.entries[i].name^;
  356. exit;
  357. end;
  358. end;
  359. ovmt := ovmt^.vParent;
  360. end;
  361. MethodName:='';
  362. end;
  363. function TObject.FieldAddress(const name : shortstring) : pointer;
  364. type
  365. PFieldInfo = ^TFieldInfo;
  366. TFieldInfo =
  367. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  368. packed
  369. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  370. record
  371. FieldOffset: PtrUInt;
  372. ClassTypeIndex: Word;
  373. Name: ShortString;
  374. end;
  375. PFieldTable = ^TFieldTable;
  376. TFieldTable =
  377. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  378. packed
  379. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  380. record
  381. FieldCount: Word;
  382. ClassTable: Pointer;
  383. { should be array[Word] of TFieldInfo; but
  384. Elements have variant size! force at least proper alignment }
  385. Fields: array[0..0] of TFieldInfo
  386. end;
  387. var
  388. ovmt: PVmt;
  389. FieldTable: PFieldTable;
  390. FieldInfo: PFieldInfo;
  391. i: longint;
  392. begin
  393. if Length(name) > 0 then
  394. begin
  395. ovmt := PVmt(ClassType);
  396. while ovmt <> nil do
  397. begin
  398. FieldTable := PFieldTable(ovmt^.vFieldTable);
  399. if FieldTable <> nil then
  400. begin
  401. FieldInfo := @FieldTable^.Fields[0];
  402. for i := 0 to FieldTable^.FieldCount - 1 do
  403. begin
  404. if ShortCompareText(FieldInfo^.Name, name) = 0 then
  405. begin
  406. fieldaddress := Pointer(Self) + FieldInfo^.FieldOffset;
  407. exit;
  408. end;
  409. FieldInfo := PFieldInfo(PByte(@FieldInfo^.Name) + 1 + Length(FieldInfo^.Name));
  410. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  411. { align to largest field of TFieldInfo }
  412. FieldInfo := Align(FieldInfo, SizeOf(PtrUInt));
  413. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  414. end;
  415. end;
  416. { Try again with the parent class type }
  417. ovmt:=ovmt^.vParent;
  418. end;
  419. end;
  420. fieldaddress:=nil;
  421. end;
  422. function TObject.SafeCallException(exceptobject : tobject;
  423. exceptaddr : codepointer) : HResult;
  424. begin
  425. safecallexception:=E_UNEXPECTED;
  426. end;
  427. class function TObject.ClassInfo : pointer;
  428. begin
  429. ClassInfo := PVmt(Self)^.vTypeInfo;
  430. end;
  431. class function TObject.ClassName : ShortString;
  432. begin
  433. ClassName := PVmt(Self)^.vClassName^;
  434. end;
  435. class function TObject.ClassNameIs(const name : string) : boolean;
  436. begin
  437. // call to ClassName inlined here, this eliminates stack and string copying.
  438. ClassNameIs:=ShortCompareText(PVmt(Self)^.vClassName^, name) = 0;
  439. end;
  440. class function TObject.InheritsFrom(aclass : TClass) : Boolean;
  441. var
  442. vmt: PVmt;
  443. begin
  444. if assigned(aclass) then
  445. begin
  446. vmt:=PVmt(self);
  447. while assigned(vmt) and (vmt <> PVmt(aclass)) do
  448. vmt := vmt^.vParent;
  449. InheritsFrom := (vmt = PVmt(aclass));
  450. end
  451. else
  452. inheritsFrom := False;
  453. end;
  454. class function TObject.stringmessagetable : pstringmessagetable;
  455. begin
  456. stringmessagetable:=PVmt(Self)^.vMsgStrPtr;
  457. end;
  458. type
  459. tmessagehandler = procedure(var msg) of object;
  460. procedure TObject.Dispatch(var message);
  461. type
  462. {$PUSH}
  463. {$PACKRECORDS NORMAL}
  464. PMsgIntTable = ^TMsgIntTable;
  465. TMsgIntTable = record
  466. index : dword;
  467. method : codepointer;
  468. end;
  469. PMsgInt = ^TMsgInt;
  470. TMsgInt = record
  471. count : longint;
  472. msgs : array[0..0] of TMsgIntTable;
  473. end;
  474. {$POP}
  475. var
  476. index : dword;
  477. count,i : longint;
  478. msgtable : PMsgIntTable;
  479. p : PMsgInt;
  480. ovmt : PVmt;
  481. msghandler : tmessagehandler;
  482. begin
  483. index:=dword(message);
  484. ovmt := PVmt(ClassType);
  485. while assigned(ovmt) do
  486. begin
  487. // See if we have messages at all in this class.
  488. p:=PMsgInt(ovmt^.vDynamicTable);
  489. If Assigned(p) then
  490. begin
  491. msgtable:=@p^.msgs;
  492. count:=p^.count;
  493. end
  494. else
  495. Count:=0;
  496. { later, we can implement a binary search here }
  497. for i:=0 to count-1 do
  498. begin
  499. if index=msgtable[i].index then
  500. begin
  501. TMethod(msghandler).Code:=msgtable[i].method;
  502. TMethod(msghandler).Data:=self;
  503. msghandler(message);
  504. exit;
  505. end;
  506. end;
  507. ovmt:=ovmt^.vParent;
  508. end;
  509. DefaultHandler(message);
  510. end;
  511. procedure TObject.DispatchStr(var message);
  512. var
  513. name : shortstring;
  514. count,i : longint;
  515. msgstrtable : pmsgstrtable;
  516. p: pstringmessagetable;
  517. ovmt : PVmt;
  518. msghandler : tmessagehandler;
  519. begin
  520. name:=pshortstring(@message)^;
  521. ovmt:=PVmt(ClassType);
  522. while assigned(ovmt) do
  523. begin
  524. p := ovmt^.vMsgStrPtr;
  525. if (P<>Nil) and (p^.count<>0) then
  526. begin
  527. count:=p^.count;
  528. msgstrtable:=@p^.msgstrtable;
  529. end
  530. else
  531. Count:=0;
  532. { later, we can implement a binary search here }
  533. for i:=0 to count-1 do
  534. begin
  535. if name=msgstrtable[i].name^ then
  536. begin
  537. TMethod(msghandler).Code:=msgstrtable[i].method;
  538. TMethod(msghandler).Data:=self;
  539. msghandler(message);
  540. exit;
  541. end;
  542. end;
  543. ovmt:=ovmt^.vParent;
  544. end;
  545. DefaultHandlerStr(message);
  546. end;
  547. procedure TObject.DefaultHandler(var message);
  548. begin
  549. end;
  550. procedure TObject.DefaultHandlerStr(var message);
  551. begin
  552. end;
  553. procedure TObject.CleanupInstance;
  554. var
  555. vmt : PVmt;
  556. temp : pointer;
  557. begin
  558. vmt := PVmt(ClassType);
  559. while vmt<>nil do
  560. begin
  561. Temp:= vmt^.vInitTable;
  562. { The RTTI format matches one for records, except the type is tkClass.
  563. Since RecordRTTI does not check the type, calling it yields the desired result. }
  564. if Assigned(Temp) then
  565. RecordRTTI(Self,Temp,@int_finalize);
  566. vmt:= vmt^.vParent;
  567. end;
  568. end;
  569. procedure TObject.AfterConstruction;
  570. begin
  571. end;
  572. procedure TObject.BeforeDestruction;
  573. begin
  574. end;
  575. function IsGUIDEqual(const guid1, guid2: tguid): boolean;
  576. begin
  577. IsGUIDEqual:=
  578. (guid1.D1=guid2.D1) and
  579. (PDWORD(@guid1.D2)^=PDWORD(@guid2.D2)^) and
  580. (PDWORD(@guid1.D4[0])^=PDWORD(@guid2.D4[0])^) and
  581. (PDWORD(@guid1.D4[4])^=PDWORD(@guid2.D4[4])^);
  582. end;
  583. // Use of managed types should be avoided here; implicit _Addref/_Release
  584. // will end up in unpredictable behaviour if called on CORBA interfaces.
  585. type
  586. TInterfaceGetter = procedure(out Obj) of object;
  587. TClassGetter = function: TObject of object;
  588. function GetInterfaceByEntry(Instance: pointer; IEntry: pinterfaceentry; out obj): boolean;
  589. var
  590. Getter: TMethod;
  591. begin
  592. Pointer(Obj) := nil;
  593. Getter.Data := Instance;
  594. if Assigned(IEntry) and Assigned(Instance) then
  595. begin
  596. case IEntry^.IType of
  597. etStandard:
  598. Pointer(Obj) := PByte(instance)+IEntry^.IOffset;
  599. etFieldValue, etFieldValueClass:
  600. Pointer(obj) := PPointer(PByte(Instance)+IEntry^.IOffset)^;
  601. etVirtualMethodResult:
  602. begin
  603. // IOffset is relative to the VMT, not to instance.
  604. Getter.code := PCodePointer(PByte(PPointer(Instance)^) + IEntry^.IOffset)^;
  605. TInterfaceGetter(Getter)(obj);
  606. end;
  607. etVirtualMethodClass:
  608. begin
  609. // IOffset is relative to the VMT, not to instance.
  610. Getter.code := PCodePointer(PByte(PPointer(Instance)^) + IEntry^.IOffset)^;
  611. TObject(obj) := TClassGetter(Getter)();
  612. end;
  613. etStaticMethodResult:
  614. begin
  615. Getter.code := CodePointer(IEntry^.IOffset);
  616. TInterfaceGetter(Getter)(obj);
  617. end;
  618. etStaticMethodClass:
  619. begin
  620. Getter.code := CodePointer(IEntry^.IOffset);
  621. TObject(obj) := TClassGetter(Getter)();
  622. end;
  623. end;
  624. end;
  625. result := assigned(pointer(obj));
  626. end;
  627. function TObject.GetInterface(const iid : tguid;out obj) : boolean;
  628. var
  629. IEntry: PInterfaceEntry;
  630. Instance: TObject;
  631. begin
  632. if IsGUIDEqual(IObjectInstance,iid) then
  633. begin
  634. TObject(Obj) := Self;
  635. Result := True;
  636. Exit;
  637. end;
  638. Instance := self;
  639. repeat
  640. IEntry := Instance.GetInterfaceEntry(iid);
  641. result := GetInterfaceByEntry(Instance, IEntry, obj);
  642. if (not result) or
  643. (IEntry^.IType in [etStandard, etFieldValue,
  644. etStaticMethodResult, etVirtualMethodResult]) then
  645. Break;
  646. { if interface is implemented by a class-type property or field,
  647. continue search }
  648. Instance := TObject(obj);
  649. until False;
  650. { Getter function will normally AddRef, so adding another reference here
  651. will cause memleak. }
  652. if result and (IEntry^.IType in [etStandard, etFieldValue]) then
  653. IInterface(obj)._AddRef;
  654. end;
  655. function TObject.GetInterfaceWeak(const iid : tguid; out obj) : boolean;
  656. var
  657. IEntry: PInterfaceEntry;
  658. Instance: TObject;
  659. begin
  660. if IsGUIDEqual(IObjectInstance,iid) then
  661. begin
  662. TObject(Obj) := Self;
  663. Result := True;
  664. Exit;
  665. end;
  666. Instance := self;
  667. repeat
  668. IEntry := Instance.GetInterfaceEntry(iid);
  669. result := GetInterfaceByEntry(Instance, IEntry, obj);
  670. if (not result) or
  671. (IEntry^.IType in [etStandard, etFieldValue,
  672. etStaticMethodResult, etVirtualMethodResult]) then
  673. Break;
  674. { if interface is implemented by a class-type property or field,
  675. continue search }
  676. Instance := TObject(obj);
  677. until False;
  678. { Getter function will normally AddRef, so we have to release it,
  679. else the ref is not weak. }
  680. if result and not (IEntry^.IType in [etStandard, etFieldValue]) then
  681. IInterface(obj)._Release;
  682. end;
  683. function TObject.GetInterfaceByStr(const iidstr : shortstring;out obj) : boolean;
  684. var
  685. IEntry: PInterfaceEntry;
  686. Instance: TObject;
  687. begin
  688. Instance := self;
  689. repeat
  690. IEntry := Instance.GetInterfaceEntryByStr(iidstr);
  691. result := GetInterfaceByEntry(Instance, IEntry, obj);
  692. if (not result) or
  693. (IEntry^.IType in [etStandard, etFieldValue,
  694. etStaticMethodResult, etVirtualMethodResult]) then
  695. Break;
  696. { if interface is implemented by a class-type property or field,
  697. continue search }
  698. Instance := TObject(obj);
  699. until False;
  700. { Getter function will normally AddRef, so adding another reference here
  701. will cause memleak. (com interfaces only!) }
  702. if result and Assigned(IEntry^.IID) and (IEntry^.IType in [etStandard, etFieldValue]) then
  703. IInterface(obj)._AddRef;
  704. end;
  705. function TObject.GetInterface(const iidstr : shortstring;out obj) : boolean;
  706. begin
  707. Result := GetInterfaceByStr(iidstr,obj);
  708. end;
  709. class function TObject.GetInterfaceEntry(const iid : tguid) : pinterfaceentry;
  710. var
  711. i: longint;
  712. intftable: pinterfacetable;
  713. ovmt: PVmt;
  714. begin
  715. ovmt := PVmt(Self);
  716. while Assigned(ovmt) and (ovmt^.vIntfTable <> @emptyintf) do
  717. begin
  718. intftable:=ovmt^.vIntfTable;
  719. if assigned(intftable) then
  720. begin
  721. for i:=0 to intftable^.EntryCount-1 do
  722. begin
  723. result:=@intftable^.Entries[i];
  724. if assigned(Result^.iid) and IsGUIDEqual(Result^.iid^,iid) then
  725. Exit;
  726. end;
  727. end;
  728. ovmt := ovmt^.vParent;
  729. end;
  730. result := nil;
  731. end;
  732. class function TObject.GetInterfaceEntryByStr(const iidstr : shortstring) : pinterfaceentry;
  733. var
  734. i: longint;
  735. intftable: pinterfacetable;
  736. ovmt: PVmt;
  737. begin
  738. ovmt := PVmt(Self);
  739. while Assigned(ovmt) and (ovmt^.vIntfTable <> @emptyintf) do
  740. begin
  741. intftable:=ovmt^.vIntfTable;
  742. if assigned(intftable) then
  743. begin
  744. for i:=0 to intftable^.EntryCount-1 do
  745. begin
  746. result:=@intftable^.Entries[i];
  747. if assigned(result^.iidstr) and (result^.iidstr^ = iidstr) then
  748. Exit;
  749. end;
  750. end;
  751. ovmt := ovmt^.vParent;
  752. end;
  753. result:=nil;
  754. end;
  755. class function TObject.GetInterfaceTable : pinterfacetable;
  756. begin
  757. getinterfacetable:=PVmt(Self)^.vIntfTable;
  758. end;
  759. class function TObject.UnitName : ansistring;
  760. type
  761. // from the typinfo unit
  762. TClassTypeInfo = {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  763. ClassType: TClass;
  764. ParentInfo: Pointer;
  765. PropCount: SmallInt;
  766. UnitName: ShortString;
  767. end;
  768. PClassTypeInfo = ^TClassTypeInfo;
  769. var
  770. classtypeinfo: PClassTypeInfo;
  771. begin
  772. classtypeinfo:=ClassInfo;
  773. if Assigned(classtypeinfo) then
  774. begin
  775. // offset PTypeInfo by Length(Name) + 2 (ShortString length byte + SizeOf(Kind))
  776. inc(Pointer(classtypeinfo), PByte(Pointer(classtypeinfo)+1)^ + 2);
  777. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  778. classtypeinfo:=align(classtypeinfo,sizeof(classtypeinfo));
  779. {$endif}
  780. result:=classtypeinfo^.UnitName;
  781. end
  782. else
  783. result:='';
  784. end;
  785. function TObject.Equals(Obj: TObject) : boolean;
  786. begin
  787. result:=Obj=Self;
  788. end;
  789. function TObject.GetHashCode: PtrInt;
  790. begin
  791. result:=PtrInt(Self);
  792. end;
  793. function TObject.ToString: ansistring;
  794. begin
  795. result:=ClassName;
  796. end;
  797. {****************************************************************************
  798. TINTERFACEDOBJECT
  799. ****************************************************************************}
  800. function TInterfacedObject.QueryInterface(
  801. {$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  802. begin
  803. if getinterface(iid,obj) then
  804. result:=S_OK
  805. else
  806. result:=longint(E_NOINTERFACE);
  807. end;
  808. function TInterfacedObject._AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  809. begin
  810. _addref:=interlockedincrement(frefcount);
  811. end;
  812. function TInterfacedObject._Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  813. begin
  814. _Release:=interlockeddecrement(frefcount);
  815. if _Release=0 then
  816. self.destroy;
  817. end;
  818. procedure TInterfacedObject.AfterConstruction;
  819. begin
  820. { we need to fix the refcount we forced in newinstance }
  821. { further, it must be done in a thread safe way }
  822. declocked(frefcount);
  823. end;
  824. procedure TInterfacedObject.BeforeDestruction;
  825. begin
  826. if frefcount<>0 then
  827. HandleError(204);
  828. end;
  829. class function TInterfacedObject.NewInstance : TObject;
  830. begin
  831. NewInstance:=inherited NewInstance;
  832. if NewInstance<>nil then
  833. TInterfacedObject(NewInstance).frefcount:=1;
  834. end;
  835. {****************************************************************************
  836. TAGGREGATEDOBJECT
  837. ****************************************************************************}
  838. constructor TAggregatedObject.Create(const aController: IUnknown);
  839. begin
  840. inherited Create;
  841. { do not keep a counted reference to the controller! }
  842. fcontroller := Pointer(aController);
  843. end;
  844. function TAggregatedObject.QueryInterface(
  845. {$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  846. begin
  847. Result := IUnknown(fcontroller).QueryInterface(iid, obj);
  848. end;
  849. function TAggregatedObject._AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  850. begin
  851. Result := IUnknown(fcontroller)._AddRef;
  852. end;
  853. function TAggregatedObject._Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  854. begin
  855. Result := IUnknown(fcontroller)._Release;
  856. end;
  857. function TAggregatedObject.GetController : IUnknown;
  858. begin
  859. Result := IUnknown(fcontroller);
  860. end;
  861. {****************************************************************************
  862. TContainedOBJECT
  863. ****************************************************************************}
  864. function TContainedObject.QueryInterface(
  865. {$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  866. begin
  867. if getinterface(iid,obj) then
  868. result:=S_OK
  869. else
  870. result:=longint(E_NOINTERFACE);
  871. end;
  872. {****************************************************************************
  873. Exception Support
  874. ****************************************************************************}
  875. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  876. {$i except.inc}
  877. {$endif FPC_HAS_FEATURE_EXCEPTIONS}