objpas.inc 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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. function TVmt.GetvParent: PVmt;
  229. begin
  230. {$ifdef ver2_6}
  231. Result:=vParentRef;
  232. {$else}
  233. if Assigned(vParentRef) then
  234. Result:=vParentRef^
  235. else
  236. Result:=Nil;
  237. {$endif}
  238. end;
  239. {****************************************************************************
  240. TOBJECT
  241. ****************************************************************************}
  242. constructor TObject.Create;
  243. begin
  244. end;
  245. destructor TObject.Destroy;
  246. begin
  247. end;
  248. procedure TObject.Free;
  249. begin
  250. // the call via self avoids a warning
  251. if self<>nil then
  252. self.destroy;
  253. end;
  254. class function TObject.InstanceSize : SizeInt;
  255. begin
  256. InstanceSize := PVmt(Self)^.vInstanceSize;
  257. end;
  258. var
  259. emptyintf: ptruint; public name 'FPC_EMPTYINTF';
  260. procedure InitInterfacePointers(objclass: tclass;instance : pointer);
  261. var
  262. ovmt: PVmt;
  263. i: longint;
  264. intftable: pinterfacetable;
  265. Res: pinterfaceentry;
  266. begin
  267. ovmt := PVmt(objclass);
  268. while assigned(ovmt) and (ovmt^.vIntfTable <> @emptyintf) do
  269. begin
  270. intftable:=ovmt^.vIntfTable;
  271. if assigned(intftable) then
  272. begin
  273. i:=intftable^.EntryCount;
  274. Res:=@intftable^.Entries[0];
  275. while i>0 do begin
  276. if Res^.IType = etStandard then
  277. ppointer(@(pbyte(instance)[Res^.IOffset]))^:=
  278. pointer(Res^.VTable);
  279. inc(Res);
  280. dec(i);
  281. end;
  282. end;
  283. ovmt:=ovmt^.vParent;
  284. end;
  285. end;
  286. class function TObject.InitInstance(instance : pointer) : tobject; {$ifdef SYSTEMINLINE} inline; {$ENDIF}
  287. begin
  288. { the size is saved at offset 0 }
  289. fillchar(instance^, InstanceSize, 0);
  290. { insert VMT pointer into the new created memory area }
  291. { (in class methods self contains the VMT!) }
  292. ppointer(instance)^:=pointer(self);
  293. if PVmt(self)^.vIntfTable <> @emptyintf then
  294. InitInterfacePointers(self,instance);
  295. InitInstance:=TObject(Instance);
  296. end;
  297. class function TObject.ClassParent : tclass;
  298. begin
  299. { type of self is class of tobject => it points to the vmt }
  300. { the parent vmt is saved at offset vmtParent }
  301. classparent:=tclass(PVmt(Self)^.vParent);
  302. end;
  303. class function TObject.NewInstance : tobject;
  304. var
  305. p : pointer;
  306. begin
  307. getmem(p, InstanceSize);
  308. if p <> nil then
  309. InitInstance(p);
  310. NewInstance:=TObject(p);
  311. end;
  312. procedure TObject.FreeInstance;
  313. begin
  314. CleanupInstance;
  315. FreeMem(Pointer(Self));
  316. end;
  317. class function TObject.ClassType : TClass;
  318. begin
  319. ClassType:=TClass(Pointer(Self))
  320. end;
  321. type
  322. tmethodnamerec = packed record
  323. name : pshortstring;
  324. addr : codepointer;
  325. end;
  326. tmethodnametable = packed record
  327. count : dword;
  328. entries : packed array[0..0] of tmethodnamerec;
  329. end;
  330. pmethodnametable = ^tmethodnametable;
  331. class function TObject.MethodAddress(const name : shortstring) : codepointer;
  332. var
  333. methodtable : pmethodnametable;
  334. i : dword;
  335. ovmt : PVmt;
  336. begin
  337. ovmt:=PVmt(self);
  338. while assigned(ovmt) do
  339. begin
  340. methodtable:=pmethodnametable(ovmt^.vMethodTable);
  341. if assigned(methodtable) then
  342. begin
  343. for i:=0 to methodtable^.count-1 do
  344. if ShortCompareText(methodtable^.entries[i].name^, name)=0 then
  345. begin
  346. MethodAddress:=methodtable^.entries[i].addr;
  347. exit;
  348. end;
  349. end;
  350. ovmt := ovmt^.vParent;
  351. end;
  352. MethodAddress:=nil;
  353. end;
  354. class function TObject.MethodName(address : codepointer) : shortstring;
  355. var
  356. methodtable : pmethodnametable;
  357. i : dword;
  358. ovmt : PVmt;
  359. begin
  360. ovmt:=PVmt(self);
  361. while assigned(ovmt) do
  362. begin
  363. methodtable:=pmethodnametable(ovmt^.vMethodTable);
  364. if assigned(methodtable) then
  365. begin
  366. for i:=0 to methodtable^.count-1 do
  367. if methodtable^.entries[i].addr=address then
  368. begin
  369. MethodName:=methodtable^.entries[i].name^;
  370. exit;
  371. end;
  372. end;
  373. ovmt := ovmt^.vParent;
  374. end;
  375. MethodName:='';
  376. end;
  377. function TObject.FieldAddress(const name : shortstring) : pointer;
  378. type
  379. PFieldInfo = ^TFieldInfo;
  380. TFieldInfo =
  381. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  382. packed
  383. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  384. record
  385. FieldOffset: PtrUInt;
  386. ClassTypeIndex: Word;
  387. Name: ShortString;
  388. end;
  389. PFieldTable = ^TFieldTable;
  390. TFieldTable =
  391. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  392. packed
  393. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  394. record
  395. FieldCount: Word;
  396. ClassTable: Pointer;
  397. { should be array[Word] of TFieldInfo; but
  398. Elements have variant size! force at least proper alignment }
  399. Fields: array[0..0] of TFieldInfo
  400. end;
  401. var
  402. ovmt: PVmt;
  403. FieldTable: PFieldTable;
  404. FieldInfo: PFieldInfo;
  405. i: longint;
  406. begin
  407. if Length(name) > 0 then
  408. begin
  409. ovmt := PVmt(ClassType);
  410. while ovmt <> nil do
  411. begin
  412. FieldTable := PFieldTable(ovmt^.vFieldTable);
  413. if FieldTable <> nil then
  414. begin
  415. FieldInfo := @FieldTable^.Fields[0];
  416. for i := 0 to FieldTable^.FieldCount - 1 do
  417. begin
  418. if ShortCompareText(FieldInfo^.Name, name) = 0 then
  419. begin
  420. fieldaddress := Pointer(Self) + FieldInfo^.FieldOffset;
  421. exit;
  422. end;
  423. FieldInfo := PFieldInfo(PByte(@FieldInfo^.Name) + 1 + Length(FieldInfo^.Name));
  424. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  425. { align to largest field of TFieldInfo }
  426. FieldInfo := Align(FieldInfo, SizeOf(PtrUInt));
  427. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  428. end;
  429. end;
  430. { Try again with the parent class type }
  431. ovmt:=ovmt^.vParent;
  432. end;
  433. end;
  434. fieldaddress:=nil;
  435. end;
  436. function TObject.SafeCallException(exceptobject : tobject;
  437. exceptaddr : codepointer) : HResult;
  438. begin
  439. safecallexception:=E_UNEXPECTED;
  440. end;
  441. class function TObject.ClassInfo : pointer;
  442. begin
  443. ClassInfo := PVmt(Self)^.vTypeInfo;
  444. end;
  445. class function TObject.ClassName : ShortString;
  446. begin
  447. ClassName := PVmt(Self)^.vClassName^;
  448. end;
  449. class function TObject.ClassNameIs(const name : string) : boolean;
  450. begin
  451. // call to ClassName inlined here, this eliminates stack and string copying.
  452. ClassNameIs:=ShortCompareText(PVmt(Self)^.vClassName^, name) = 0;
  453. end;
  454. class function TObject.InheritsFrom(aclass : TClass) : Boolean;
  455. var
  456. vmt: PVmt;
  457. begin
  458. if assigned(aclass) then
  459. begin
  460. vmt:=PVmt(self);
  461. while assigned(vmt) and (vmt <> PVmt(aclass)) do
  462. vmt := vmt^.vParent;
  463. InheritsFrom := (vmt = PVmt(aclass));
  464. end
  465. else
  466. inheritsFrom := False;
  467. end;
  468. class function TObject.stringmessagetable : pstringmessagetable;
  469. begin
  470. stringmessagetable:=PVmt(Self)^.vMsgStrPtr;
  471. end;
  472. type
  473. tmessagehandler = procedure(var msg) of object;
  474. procedure TObject.Dispatch(var message);
  475. type
  476. {$PUSH}
  477. {$PACKRECORDS NORMAL}
  478. PMsgIntTable = ^TMsgIntTable;
  479. TMsgIntTable = record
  480. index : dword;
  481. method : codepointer;
  482. end;
  483. PMsgInt = ^TMsgInt;
  484. TMsgInt = record
  485. count : longint;
  486. msgs : array[0..0] of TMsgIntTable;
  487. end;
  488. {$POP}
  489. var
  490. index : dword;
  491. count,i : longint;
  492. msgtable : PMsgIntTable;
  493. p : PMsgInt;
  494. ovmt : PVmt;
  495. msghandler : tmessagehandler;
  496. begin
  497. index:=dword(message);
  498. ovmt := PVmt(ClassType);
  499. while assigned(ovmt) do
  500. begin
  501. // See if we have messages at all in this class.
  502. p:=PMsgInt(ovmt^.vDynamicTable);
  503. If Assigned(p) then
  504. begin
  505. msgtable:=@p^.msgs;
  506. count:=p^.count;
  507. end
  508. else
  509. Count:=0;
  510. { later, we can implement a binary search here }
  511. for i:=0 to count-1 do
  512. begin
  513. if index=msgtable[i].index then
  514. begin
  515. TMethod(msghandler).Code:=msgtable[i].method;
  516. TMethod(msghandler).Data:=self;
  517. msghandler(message);
  518. exit;
  519. end;
  520. end;
  521. ovmt:=ovmt^.vParent;
  522. end;
  523. DefaultHandler(message);
  524. end;
  525. procedure TObject.DispatchStr(var message);
  526. var
  527. name : shortstring;
  528. count,i : longint;
  529. msgstrtable : pmsgstrtable;
  530. p: pstringmessagetable;
  531. ovmt : PVmt;
  532. msghandler : tmessagehandler;
  533. begin
  534. name:=pshortstring(@message)^;
  535. ovmt:=PVmt(ClassType);
  536. while assigned(ovmt) do
  537. begin
  538. p := ovmt^.vMsgStrPtr;
  539. if (P<>Nil) and (p^.count<>0) then
  540. begin
  541. count:=p^.count;
  542. msgstrtable:=@p^.msgstrtable;
  543. end
  544. else
  545. Count:=0;
  546. { later, we can implement a binary search here }
  547. for i:=0 to count-1 do
  548. begin
  549. if name=msgstrtable[i].name^ then
  550. begin
  551. TMethod(msghandler).Code:=msgstrtable[i].method;
  552. TMethod(msghandler).Data:=self;
  553. msghandler(message);
  554. exit;
  555. end;
  556. end;
  557. ovmt:=ovmt^.vParent;
  558. end;
  559. DefaultHandlerStr(message);
  560. end;
  561. procedure TObject.DefaultHandler(var message);
  562. begin
  563. end;
  564. procedure TObject.DefaultHandlerStr(var message);
  565. begin
  566. end;
  567. procedure TObject.CleanupInstance;
  568. var
  569. vmt : PVmt;
  570. temp : pointer;
  571. begin
  572. vmt := PVmt(ClassType);
  573. while vmt<>nil do
  574. begin
  575. Temp:= vmt^.vInitTable;
  576. { The RTTI format matches one for records, except the type is tkClass.
  577. Since RecordRTTI does not check the type, calling it yields the desired result. }
  578. if Assigned(Temp) then
  579. RecordRTTI(Self,Temp,@int_finalize);
  580. vmt:= vmt^.vParent;
  581. end;
  582. end;
  583. procedure TObject.AfterConstruction;
  584. begin
  585. end;
  586. procedure TObject.BeforeDestruction;
  587. begin
  588. end;
  589. function IsGUIDEqual(const guid1, guid2: tguid): boolean;
  590. begin
  591. IsGUIDEqual:=
  592. (guid1.D1=guid2.D1) and
  593. (PDWORD(@guid1.D2)^=PDWORD(@guid2.D2)^) and
  594. (PDWORD(@guid1.D4[0])^=PDWORD(@guid2.D4[0])^) and
  595. (PDWORD(@guid1.D4[4])^=PDWORD(@guid2.D4[4])^);
  596. end;
  597. // Use of managed types should be avoided here; implicit _Addref/_Release
  598. // will end up in unpredictable behaviour if called on CORBA interfaces.
  599. type
  600. TInterfaceGetter = procedure(out Obj) of object;
  601. TClassGetter = function: TObject of object;
  602. function GetInterfaceByEntry(Instance: pointer; IEntry: pinterfaceentry; out obj): boolean;
  603. var
  604. Getter: TMethod;
  605. begin
  606. Pointer(Obj) := nil;
  607. Getter.Data := Instance;
  608. if Assigned(IEntry) and Assigned(Instance) then
  609. begin
  610. case IEntry^.IType of
  611. etStandard:
  612. Pointer(Obj) := PByte(instance)+IEntry^.IOffset;
  613. etFieldValue, etFieldValueClass:
  614. Pointer(obj) := PPointer(PByte(Instance)+IEntry^.IOffset)^;
  615. etVirtualMethodResult:
  616. begin
  617. // IOffset is relative to the VMT, not to instance.
  618. Getter.code := PCodePointer(PByte(PPointer(Instance)^) + IEntry^.IOffset)^;
  619. TInterfaceGetter(Getter)(obj);
  620. end;
  621. etVirtualMethodClass:
  622. begin
  623. // IOffset is relative to the VMT, not to instance.
  624. Getter.code := PCodePointer(PByte(PPointer(Instance)^) + IEntry^.IOffset)^;
  625. TObject(obj) := TClassGetter(Getter)();
  626. end;
  627. etStaticMethodResult:
  628. begin
  629. Getter.code := CodePointer(IEntry^.IOffset);
  630. TInterfaceGetter(Getter)(obj);
  631. end;
  632. etStaticMethodClass:
  633. begin
  634. Getter.code := CodePointer(IEntry^.IOffset);
  635. TObject(obj) := TClassGetter(Getter)();
  636. end;
  637. end;
  638. end;
  639. result := assigned(pointer(obj));
  640. end;
  641. function TObject.GetInterface(const iid : tguid;out obj) : boolean;
  642. var
  643. IEntry: PInterfaceEntry;
  644. Instance: TObject;
  645. begin
  646. if IsGUIDEqual(IObjectInstance,iid) then
  647. begin
  648. TObject(Obj) := Self;
  649. Result := True;
  650. Exit;
  651. end;
  652. Instance := self;
  653. repeat
  654. IEntry := Instance.GetInterfaceEntry(iid);
  655. result := GetInterfaceByEntry(Instance, IEntry, obj);
  656. if (not result) or
  657. (IEntry^.IType in [etStandard, etFieldValue,
  658. etStaticMethodResult, etVirtualMethodResult]) then
  659. Break;
  660. { if interface is implemented by a class-type property or field,
  661. continue search }
  662. Instance := TObject(obj);
  663. until False;
  664. { Getter function will normally AddRef, so adding another reference here
  665. will cause memleak. }
  666. if result and (IEntry^.IType in [etStandard, etFieldValue]) then
  667. IInterface(obj)._AddRef;
  668. end;
  669. function TObject.GetInterfaceWeak(const iid : tguid; out obj) : boolean;
  670. var
  671. IEntry: PInterfaceEntry;
  672. Instance: TObject;
  673. begin
  674. if IsGUIDEqual(IObjectInstance,iid) then
  675. begin
  676. TObject(Obj) := Self;
  677. Result := True;
  678. Exit;
  679. end;
  680. Instance := self;
  681. repeat
  682. IEntry := Instance.GetInterfaceEntry(iid);
  683. result := GetInterfaceByEntry(Instance, IEntry, obj);
  684. if (not result) or
  685. (IEntry^.IType in [etStandard, etFieldValue,
  686. etStaticMethodResult, etVirtualMethodResult]) then
  687. Break;
  688. { if interface is implemented by a class-type property or field,
  689. continue search }
  690. Instance := TObject(obj);
  691. until False;
  692. { Getter function will normally AddRef, so we have to release it,
  693. else the ref is not weak. }
  694. if result and not (IEntry^.IType in [etStandard, etFieldValue]) then
  695. IInterface(obj)._Release;
  696. end;
  697. function TObject.GetInterfaceByStr(const iidstr : shortstring;out obj) : boolean;
  698. var
  699. IEntry: PInterfaceEntry;
  700. Instance: TObject;
  701. begin
  702. Instance := self;
  703. repeat
  704. IEntry := Instance.GetInterfaceEntryByStr(iidstr);
  705. result := GetInterfaceByEntry(Instance, IEntry, obj);
  706. if (not result) or
  707. (IEntry^.IType in [etStandard, etFieldValue,
  708. etStaticMethodResult, etVirtualMethodResult]) then
  709. Break;
  710. { if interface is implemented by a class-type property or field,
  711. continue search }
  712. Instance := TObject(obj);
  713. until False;
  714. { Getter function will normally AddRef, so adding another reference here
  715. will cause memleak. (com interfaces only!) }
  716. if result and Assigned(IEntry^.IID) and (IEntry^.IType in [etStandard, etFieldValue]) then
  717. IInterface(obj)._AddRef;
  718. end;
  719. function TObject.GetInterface(const iidstr : shortstring;out obj) : boolean;
  720. begin
  721. Result := GetInterfaceByStr(iidstr,obj);
  722. end;
  723. class function TObject.GetInterfaceEntry(const iid : tguid) : pinterfaceentry;
  724. var
  725. i: longint;
  726. intftable: pinterfacetable;
  727. ovmt: PVmt;
  728. begin
  729. ovmt := PVmt(Self);
  730. while Assigned(ovmt) and (ovmt^.vIntfTable <> @emptyintf) do
  731. begin
  732. intftable:=ovmt^.vIntfTable;
  733. if assigned(intftable) then
  734. begin
  735. for i:=0 to intftable^.EntryCount-1 do
  736. begin
  737. result:=@intftable^.Entries[i];
  738. if assigned(Result^.iid) and IsGUIDEqual(Result^.iid^,iid) then
  739. Exit;
  740. end;
  741. end;
  742. ovmt := ovmt^.vParent;
  743. end;
  744. result := nil;
  745. end;
  746. class function TObject.GetInterfaceEntryByStr(const iidstr : shortstring) : pinterfaceentry;
  747. var
  748. i: longint;
  749. intftable: pinterfacetable;
  750. ovmt: PVmt;
  751. begin
  752. ovmt := PVmt(Self);
  753. while Assigned(ovmt) and (ovmt^.vIntfTable <> @emptyintf) do
  754. begin
  755. intftable:=ovmt^.vIntfTable;
  756. if assigned(intftable) then
  757. begin
  758. for i:=0 to intftable^.EntryCount-1 do
  759. begin
  760. result:=@intftable^.Entries[i];
  761. if assigned(result^.iidstr) and (result^.iidstr^ = iidstr) then
  762. Exit;
  763. end;
  764. end;
  765. ovmt := ovmt^.vParent;
  766. end;
  767. result:=nil;
  768. end;
  769. class function TObject.GetInterfaceTable : pinterfacetable;
  770. begin
  771. getinterfacetable:=PVmt(Self)^.vIntfTable;
  772. end;
  773. class function TObject.UnitName : ansistring;
  774. type
  775. // from the typinfo unit
  776. TClassTypeInfo = {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}packed{$endif}record
  777. ClassType: TClass;
  778. ParentInfo: Pointer;
  779. PropCount: SmallInt;
  780. UnitName: ShortString;
  781. end;
  782. PClassTypeInfo = ^TClassTypeInfo;
  783. var
  784. classtypeinfo: PClassTypeInfo;
  785. begin
  786. classtypeinfo:=ClassInfo;
  787. if Assigned(classtypeinfo) then
  788. begin
  789. // offset PTypeInfo by Length(Name) + 2 (ShortString length byte + SizeOf(Kind))
  790. inc(Pointer(classtypeinfo), PByte(Pointer(classtypeinfo)+1)^ + 2);
  791. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  792. classtypeinfo:=align(classtypeinfo,sizeof(classtypeinfo));
  793. {$endif}
  794. result:=classtypeinfo^.UnitName;
  795. end
  796. else
  797. result:='';
  798. end;
  799. function TObject.Equals(Obj: TObject) : boolean;
  800. begin
  801. result:=Obj=Self;
  802. end;
  803. function TObject.GetHashCode: PtrInt;
  804. begin
  805. result:=PtrInt(Self);
  806. end;
  807. function TObject.ToString: ansistring;
  808. begin
  809. result:=ClassName;
  810. end;
  811. {****************************************************************************
  812. TINTERFACEDOBJECT
  813. ****************************************************************************}
  814. function TInterfacedObject.QueryInterface(
  815. {$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  816. begin
  817. if getinterface(iid,obj) then
  818. result:=S_OK
  819. else
  820. result:=longint(E_NOINTERFACE);
  821. end;
  822. function TInterfacedObject._AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  823. begin
  824. _addref:=interlockedincrement(frefcount);
  825. end;
  826. function TInterfacedObject._Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  827. begin
  828. _Release:=interlockeddecrement(frefcount);
  829. if _Release=0 then
  830. self.destroy;
  831. end;
  832. procedure TInterfacedObject.AfterConstruction;
  833. begin
  834. { we need to fix the refcount we forced in newinstance }
  835. { further, it must be done in a thread safe way }
  836. declocked(frefcount);
  837. end;
  838. procedure TInterfacedObject.BeforeDestruction;
  839. begin
  840. if frefcount<>0 then
  841. HandleError(204);
  842. end;
  843. class function TInterfacedObject.NewInstance : TObject;
  844. begin
  845. NewInstance:=inherited NewInstance;
  846. if NewInstance<>nil then
  847. TInterfacedObject(NewInstance).frefcount:=1;
  848. end;
  849. {****************************************************************************
  850. TAGGREGATEDOBJECT
  851. ****************************************************************************}
  852. constructor TAggregatedObject.Create(const aController: IUnknown);
  853. begin
  854. inherited Create;
  855. { do not keep a counted reference to the controller! }
  856. fcontroller := Pointer(aController);
  857. end;
  858. function TAggregatedObject.QueryInterface(
  859. {$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  860. begin
  861. Result := IUnknown(fcontroller).QueryInterface(iid, obj);
  862. end;
  863. function TAggregatedObject._AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  864. begin
  865. Result := IUnknown(fcontroller)._AddRef;
  866. end;
  867. function TAggregatedObject._Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  868. begin
  869. Result := IUnknown(fcontroller)._Release;
  870. end;
  871. function TAggregatedObject.GetController : IUnknown;
  872. begin
  873. Result := IUnknown(fcontroller);
  874. end;
  875. {****************************************************************************
  876. TContainedOBJECT
  877. ****************************************************************************}
  878. function TContainedObject.QueryInterface(
  879. {$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  880. begin
  881. if getinterface(iid,obj) then
  882. result:=S_OK
  883. else
  884. result:=longint(E_NOINTERFACE);
  885. end;
  886. {****************************************************************************
  887. Exception Support
  888. ****************************************************************************}
  889. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  890. {$i except.inc}
  891. {$endif FPC_HAS_FEATURE_EXCEPTIONS}