objpas.inc 34 KB

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