Leaked source code of windows server 2003
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.

160 lines
3.1 KiB

  1. // Copyright (c) 1997-1999 Microsoft Corporation
  2. //
  3. // Class Factory template
  4. //
  5. // 8-20-97 sburns
  6. #ifndef TEMPFACT_HPP_INCLUDED
  7. #define TEMPFACT_HPP_INCLUDED
  8. // COM class factory implementation
  9. template <class C>
  10. class ClassFactory : public IClassFactory
  11. {
  12. public:
  13. ClassFactory()
  14. :
  15. refcount(1) // implicit AddRef
  16. {
  17. LOG_CTOR(ClassFactory);
  18. }
  19. // IUnknown overrides
  20. virtual
  21. ULONG __stdcall
  22. AddRef()
  23. {
  24. LOG_ADDREF(ClassFactory::AddRef);
  25. return Win::InterlockedIncrement(refcount);
  26. }
  27. virtual
  28. ULONG __stdcall
  29. Release()
  30. {
  31. LOG_RELEASE(ClassFactory);
  32. if (Win::InterlockedDecrement(refcount) == 0)
  33. {
  34. delete this;
  35. return 0;
  36. }
  37. return refcount;
  38. }
  39. virtual
  40. HRESULT __stdcall
  41. QueryInterface(const IID& interfaceID, void** interfaceDesired)
  42. {
  43. LOG_FUNCTION(ClassFactory::QueryInterface);
  44. ASSERT(interfaceDesired);
  45. HRESULT hr = 0;
  46. if (!interfaceDesired)
  47. {
  48. hr = E_INVALIDARG;
  49. LOG_HRESULT(hr);
  50. return hr;
  51. }
  52. if (interfaceID == IID_IUnknown)
  53. {
  54. // LOG(L"Supplying IUnknown interface");
  55. *interfaceDesired =
  56. static_cast<IUnknown*>(static_cast<IClassFactory*>(this));
  57. }
  58. else if (interfaceID == IID_IClassFactory)
  59. {
  60. // LOG(L"Supplying IClassFactory interface");
  61. *interfaceDesired = static_cast<IClassFactory*>(this);
  62. }
  63. else
  64. {
  65. *interfaceDesired = 0;
  66. hr = E_NOINTERFACE;
  67. LOG(
  68. L"interface not supported: "
  69. + Win::StringFromGUID2(interfaceID));
  70. LOG_HRESULT(hr);
  71. return hr;
  72. }
  73. AddRef();
  74. return S_OK;
  75. }
  76. // IClassFactory overrides
  77. virtual
  78. HRESULT __stdcall
  79. CreateInstance(
  80. IUnknown* outerUnknown,
  81. const IID& interfaceID,
  82. void** interfaceDesired)
  83. {
  84. LOG_FUNCTION(ClassFactory::CreateInstance);
  85. HRESULT hr = 0;
  86. if (outerUnknown)
  87. {
  88. hr = CLASS_E_NOAGGREGATION;
  89. LOG_HRESULT(hr);
  90. return hr;
  91. }
  92. // the instance starts with a ref count of 1. If the QI fails, then it
  93. // will self-destruct upon release.
  94. C* c = new C;
  95. hr = c->QueryInterface(interfaceID, interfaceDesired);
  96. c->Release();
  97. return hr;
  98. }
  99. virtual
  100. HRESULT __stdcall
  101. LockServer(BOOL lock)
  102. {
  103. LOG_FUNCTION(ClassFactory::LockServer);
  104. ComServerLockState::LockServer(lock ? true : false);
  105. return S_OK;
  106. }
  107. private:
  108. // only Release can cause us to be deleted
  109. virtual
  110. ~ClassFactory()
  111. {
  112. LOG_DTOR(ClassFactory);
  113. ASSERT(refcount == 0);
  114. }
  115. // not implemented; no instance copying allowed.
  116. ClassFactory(const ClassFactory&);
  117. const ClassFactory& operator=(const ClassFactory&);
  118. ComServerReference dllref;
  119. long refcount;
  120. };
  121. #endif // TEMPFACT_HPP_INCLUDED