You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
3.6 KiB

  1. unit uTestHelper;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, TestFramework;
  6. type
  7. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  8. TIntfObjOwner = class(TTestCase)
  9. private
  10. fIntfObjCounter: Integer;
  11. protected
  12. procedure SetUp; override;
  13. public
  14. property IntfObjCounter: Integer read fIntfObjCounter;
  15. end;
  16. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. TIntfObj = class(TObject, IUnknown)
  18. private
  19. fOwner: TIntfObjOwner;
  20. private { IUnknown }
  21. fRefCount : longint;
  22. function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  23. function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  24. function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  25. public
  26. constructor Create(const aOwner: TIntfObjOwner);
  27. destructor Destroy; override;
  28. end;
  29. implementation
  30. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  31. //TIntfObjOwner/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  32. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  33. procedure TIntfObjOwner.SetUp;
  34. begin
  35. inherited SetUp;
  36. fIntfObjCounter := 0;
  37. end;
  38. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  39. //TIntfObj//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  40. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  41. function TIntfObj.QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  42. begin
  43. if getinterface(iid,obj)
  44. then result := S_OK
  45. else result := longint(E_NOINTERFACE);
  46. end;
  47. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  48. function TIntfObj._AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  49. begin
  50. _addref := InterLockedIncrement(fRefCount);
  51. end;
  52. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  53. function TIntfObj._Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  54. begin
  55. _Release := InterLockedDecrement(fRefCount);
  56. if (_Release = 0) then
  57. Destroy;
  58. end;
  59. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  60. constructor TIntfObj.Create(const aOwner: TIntfObjOwner);
  61. begin
  62. inherited Create;
  63. fOwner := aOwner;
  64. inc(fOwner.fIntfObjCounter);
  65. end;
  66. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  67. destructor TIntfObj.Destroy;
  68. begin
  69. dec(fOwner.fIntfObjCounter);
  70. inherited Destroy;
  71. end;
  72. end.