123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- #include <kopano/platform.h>
- #include <kopano/lockhelper.hpp>
- #include <mapidefs.h>
- #include <mapicode.h>
- #include <mapiguid.h>
- #include <kopano/ECUnknown.h>
- #include <kopano/ECGuid.h>
- #include <kopano/ECInterfaceDefs.h>
- namespace KC {
- ECUnknown::ECUnknown(const char *szClassName)
- {
- this->szClassName = szClassName;
- }
- ECUnknown::~ECUnknown()
- {
- if (this->lpParent != nullptr)
- assert(false);
-
- }
- ULONG ECUnknown::AddRef() {
- scoped_lock lock(mutex);
- return ++this->m_cRef;
- }
- ULONG ECUnknown::Release() {
- bool bLastRef = false;
-
- ulock_normal locker(mutex);
- ULONG nRef = --this->m_cRef;
- if((int)m_cRef == -1)
- assert(false);
-
- bLastRef = this->lstChildren.empty() && this->m_cRef == 0;
- locker.unlock();
- if(bLastRef)
- this->Suicide();
-
- return nRef;
- }
- HRESULT ECUnknown::QueryInterface(REFIID refiid, void **lppInterface) {
- REGISTER_INTERFACE2(ECUnknown, this);
- REGISTER_INTERFACE2(IUnknown, &this->m_xUnknown);
- return MAPI_E_INTERFACE_NOT_SUPPORTED;
- }
- HRESULT ECUnknown::AddChild(ECUnknown *lpChild) {
-
- scoped_lock locker(mutex);
- if(lpChild) {
- this->lstChildren.push_back(lpChild);
- lpChild->SetParent(this);
- }
- return hrSuccess;
- }
- HRESULT ECUnknown::RemoveChild(ECUnknown *lpChild) {
- std::list<ECUnknown *>::iterator iterChild;
- bool bLastRef;
- ulock_normal locker(mutex);
- if (lpChild != NULL)
- for (iterChild = lstChildren.begin(); iterChild != lstChildren.end(); ++iterChild)
- if(*iterChild == lpChild)
- break;
- if (iterChild == lstChildren.end())
- return MAPI_E_NOT_FOUND;
- lstChildren.erase(iterChild);
- bLastRef = this->lstChildren.empty() && this->m_cRef == 0;
- locker.unlock();
- if(bLastRef)
- this->Suicide();
-
- return hrSuccess;
- }
- HRESULT ECUnknown::SetParent(ECUnknown *lpParent) {
-
- assert(this->lpParent == NULL);
- this->lpParent = lpParent;
- return hrSuccess;
- }
- BOOL ECUnknown::IsParentOf(const ECUnknown *lpObject) {
- while (lpObject && lpObject->lpParent) {
- if (lpObject->lpParent == this)
- return TRUE;
- lpObject = lpObject->lpParent;
- }
- return FALSE;
- }
- BOOL ECUnknown::IsChildOf(const ECUnknown *lpObject) {
- if (lpObject) {
- for (auto p : lpObject->lstChildren) {
- if (this == p)
- return TRUE;
- if (this->IsChildOf(p))
- return TRUE;
- }
- }
- return FALSE;
- }
- HRESULT ECUnknown::Suicide() {
- ECUnknown *lpParent = this->lpParent;
- auto self = this;
-
- this->lpParent = NULL;
- delete this;
-
-
-
-
-
- if (lpParent != nullptr)
- lpParent->RemoveChild(self);
- return hrSuccess;
- }
- DEF_HRMETHOD0(ECUnknown, Unknown, QueryInterface, (REFIID, refiid), (void **, lppInterface))
- DEF_ULONGMETHOD0(ECUnknown, Unknown, AddRef, (void))
- DEF_ULONGMETHOD0(ECUnknown, Unknown, Release, (void))
- }
|