ECUnknown.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <kopano/platform.h>
  18. #include <kopano/lockhelper.hpp>
  19. #include <mapidefs.h>
  20. #include <mapicode.h>
  21. #include <mapiguid.h>
  22. #include <kopano/ECUnknown.h>
  23. #include <kopano/ECGuid.h>
  24. #include <kopano/ECInterfaceDefs.h>
  25. namespace KC {
  26. ECUnknown::ECUnknown(const char *szClassName)
  27. {
  28. this->szClassName = szClassName;
  29. }
  30. ECUnknown::~ECUnknown()
  31. {
  32. if (this->lpParent != nullptr)
  33. assert(false); // apparently, we're being destructed with delete() while
  34. // a parent was set up, so we should be deleted via Suicide() !
  35. }
  36. ULONG ECUnknown::AddRef() {
  37. scoped_lock lock(mutex);
  38. return ++this->m_cRef;
  39. }
  40. ULONG ECUnknown::Release() {
  41. bool bLastRef = false;
  42. ulock_normal locker(mutex);
  43. ULONG nRef = --this->m_cRef;
  44. if((int)m_cRef == -1)
  45. assert(false);
  46. bLastRef = this->lstChildren.empty() && this->m_cRef == 0;
  47. locker.unlock();
  48. if(bLastRef)
  49. this->Suicide();
  50. // The object may be deleted now
  51. return nRef;
  52. }
  53. HRESULT ECUnknown::QueryInterface(REFIID refiid, void **lppInterface) {
  54. REGISTER_INTERFACE2(ECUnknown, this);
  55. REGISTER_INTERFACE2(IUnknown, &this->m_xUnknown);
  56. return MAPI_E_INTERFACE_NOT_SUPPORTED;
  57. }
  58. HRESULT ECUnknown::AddChild(ECUnknown *lpChild) {
  59. scoped_lock locker(mutex);
  60. if(lpChild) {
  61. this->lstChildren.push_back(lpChild);
  62. lpChild->SetParent(this);
  63. }
  64. return hrSuccess;
  65. }
  66. HRESULT ECUnknown::RemoveChild(ECUnknown *lpChild) {
  67. std::list<ECUnknown *>::iterator iterChild;
  68. bool bLastRef;
  69. ulock_normal locker(mutex);
  70. if (lpChild != NULL)
  71. for (iterChild = lstChildren.begin(); iterChild != lstChildren.end(); ++iterChild)
  72. if(*iterChild == lpChild)
  73. break;
  74. if (iterChild == lstChildren.end())
  75. return MAPI_E_NOT_FOUND;
  76. lstChildren.erase(iterChild);
  77. bLastRef = this->lstChildren.empty() && this->m_cRef == 0;
  78. locker.unlock();
  79. if(bLastRef)
  80. this->Suicide();
  81. // The object may be deleted now
  82. return hrSuccess;
  83. }
  84. HRESULT ECUnknown::SetParent(ECUnknown *lpParent) {
  85. // Parent object may only be set once
  86. assert(this->lpParent == NULL);
  87. this->lpParent = lpParent;
  88. return hrSuccess;
  89. }
  90. /**
  91. * Returns whether this object is the parent of passed object
  92. *
  93. * @param lpObject Possible child object
  94. *
  95. * @return this is a parent of lpObject, or not
  96. */
  97. BOOL ECUnknown::IsParentOf(const ECUnknown *lpObject) {
  98. while (lpObject && lpObject->lpParent) {
  99. if (lpObject->lpParent == this)
  100. return TRUE;
  101. lpObject = lpObject->lpParent;
  102. }
  103. return FALSE;
  104. }
  105. /**
  106. * Returns whether this object is a child of passed object
  107. *
  108. * @param lpObject IUnknown object which may be a child of this
  109. *
  110. * @return lpObject is a parent of this, or not
  111. */
  112. BOOL ECUnknown::IsChildOf(const ECUnknown *lpObject) {
  113. if (lpObject) {
  114. for (auto p : lpObject->lstChildren) {
  115. if (this == p)
  116. return TRUE;
  117. if (this->IsChildOf(p))
  118. return TRUE;
  119. }
  120. }
  121. return FALSE;
  122. }
  123. // We kill the local object if there are no external (AddRef()) and no internal
  124. // (AddChild) objects depending on us.
  125. HRESULT ECUnknown::Suicide() {
  126. ECUnknown *lpParent = this->lpParent;
  127. auto self = this;
  128. // First, destroy the current object
  129. this->lpParent = NULL;
  130. delete this;
  131. // WARNING: The child list of our parent now contains a pointer to this
  132. // DELETED object. We must make sure that nobody ever follows pointer references
  133. // in this list during this interval. The list is, therefore PRIVATE to this object,
  134. // and may only be access through functions in ECUnknown.
  135. // Now, tell our parent to delete this object
  136. if (lpParent != nullptr)
  137. lpParent->RemoveChild(self);
  138. return hrSuccess;
  139. }
  140. DEF_HRMETHOD0(ECUnknown, Unknown, QueryInterface, (REFIID, refiid), (void **, lppInterface))
  141. DEF_ULONGMETHOD0(ECUnknown, Unknown, AddRef, (void))
  142. DEF_ULONGMETHOD0(ECUnknown, Unknown, Release, (void))
  143. } /* namespace */