Skip to content

Commit

Permalink
Merge pull request qdtroy#155 from nakkler/master
Browse files Browse the repository at this point in the history
成员函数相等判断错误,主要出现在删除委托的时候
  • Loading branch information
qdtroy authored Jul 27, 2022
2 parents 7c6a36e + 5c025e1 commit 99e2f20
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 96 deletions.
190 changes: 95 additions & 95 deletions DuiLib/Utils/UIDelegate.cpp
Original file line number Diff line number Diff line change
@@ -1,102 +1,102 @@
#include "StdAfx.h"

namespace DuiLib {

CDelegateBase::CDelegateBase(void* pObject, void* pFn)
{
m_pObject = pObject;
m_pFn = pFn;
}

CDelegateBase::CDelegateBase(const CDelegateBase& rhs)
{
m_pObject = rhs.m_pObject;
m_pFn = rhs.m_pFn;
}

CDelegateBase::~CDelegateBase()
{

}

bool CDelegateBase::Equals(const CDelegateBase& rhs) const
{
return m_pObject == rhs.m_pObject && m_pFn == rhs.m_pFn;
}

bool CDelegateBase::operator() (void* param)
{
return Invoke(param);
}

void* CDelegateBase::GetFn()
{
return m_pFn;
}

void* CDelegateBase::GetObject()
{
return m_pObject;
}

CEventSource::~CEventSource()
{
for( int i = 0; i < m_aDelegates.GetSize(); i++ ) {
CDelegateBase* pObject = static_cast<CDelegateBase*>(m_aDelegates[i]);
if( pObject) delete pObject;
}
}

CEventSource::operator bool()
{
return m_aDelegates.GetSize() > 0;
}

void CEventSource::operator+= (const CDelegateBase& d)
{
for( int i = 0; i < m_aDelegates.GetSize(); i++ ) {
CDelegateBase* pObject = static_cast<CDelegateBase*>(m_aDelegates[i]);
if( pObject && pObject->Equals(d) ) return;
}

m_aDelegates.Add(d.Copy());
}

void CEventSource::operator+= (FnType pFn)
{
(*this) += MakeDelegate(pFn);
}

void CEventSource::operator-= (const CDelegateBase& d)
{
for( int i = 0; i < m_aDelegates.GetSize(); i++ ) {
CDelegateBase* pObject = static_cast<CDelegateBase*>(m_aDelegates[i]);
if( pObject && pObject->Equals(d) ) {
delete pObject;
m_aDelegates.Remove(i);
return;
}
}
}
void CEventSource::operator-= (FnType pFn)
{
(*this) -= MakeDelegate(pFn);
}

bool CEventSource::operator() (void* param)
{
for( int i = 0; i < m_aDelegates.GetSize(); i++ ) {
CDelegateBase* pObject = static_cast<CDelegateBase*>(m_aDelegates[i]);
if( pObject && !(*pObject)(param) ) return false;
}
return true;
}
#include "StdAfx.h"

namespace DuiLib {

CDelegateBase::CDelegateBase(void* pObject, void* pFn)
{
m_pObject = pObject;
m_pFn = pFn;
}

CDelegateBase::CDelegateBase(const CDelegateBase& rhs)
{
m_pObject = rhs.m_pObject;
m_pFn = rhs.m_pFn;
}

CDelegateBase::~CDelegateBase()
{

}

bool CDelegateBase::Equals(const CDelegateBase& rhs) const
{
return m_pObject == rhs.m_pObject && GetFn() == rhs.GetFn();
}

bool CDelegateBase::operator() (void* param)
{
return Invoke(param);
}

void* CDelegateBase::GetFn() const
{
return m_pFn;
}

void* CDelegateBase::GetObject()
{
return m_pObject;
}

CEventSource::~CEventSource()
{
for( int i = 0; i < m_aDelegates.GetSize(); i++ ) {
CDelegateBase* pObject = static_cast<CDelegateBase*>(m_aDelegates[i]);
if( pObject) delete pObject;
}
}

CEventSource::operator bool()
{
return m_aDelegates.GetSize() > 0;
}

void CEventSource::operator+= (const CDelegateBase& d)
{
for( int i = 0; i < m_aDelegates.GetSize(); i++ ) {
CDelegateBase* pObject = static_cast<CDelegateBase*>(m_aDelegates[i]);
if( pObject && pObject->Equals(d) ) return;
}

m_aDelegates.Add(d.Copy());
}

void CEventSource::operator+= (FnType pFn)
{
(*this) += MakeDelegate(pFn);
}

void CEventSource::operator-= (const CDelegateBase& d)
{
for( int i = 0; i < m_aDelegates.GetSize(); i++ ) {
CDelegateBase* pObject = static_cast<CDelegateBase*>(m_aDelegates[i]);
if( pObject && pObject->Equals(d) ) {
delete pObject;
m_aDelegates.Remove(i);
return;
}
}
}
void CEventSource::operator-= (FnType pFn)
{
(*this) -= MakeDelegate(pFn);
}

bool CEventSource::operator() (void* param)
{
for( int i = 0; i < m_aDelegates.GetSize(); i++ ) {
CDelegateBase* pObject = static_cast<CDelegateBase*>(m_aDelegates[i]);
if( pObject && !(*pObject)(param) ) return false;
}
return true;
}
void CEventSource::Clear()
{
for( int i = 0; i < m_aDelegates.GetSize(); i++ ) {
CDelegateBase* pObject = static_cast<CDelegateBase*>(m_aDelegates[i]);
if( pObject) delete pObject;
}
m_aDelegates.Empty();
}
} // namespace DuiLib
}
} // namespace DuiLib
4 changes: 3 additions & 1 deletion DuiLib/Utils/UIDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class UILIB_API CDelegateBase
virtual CDelegateBase* Copy() const = 0; // add const for gcc

protected:
void* GetFn();
virtual void* GetFn() const;
void* GetObject();
virtual bool Invoke(void* param) = 0;

Expand Down Expand Up @@ -49,6 +49,7 @@ class CDelegate : public CDelegateBase
CDelegate(O* pObj, Fn pFn) : CDelegateBase(pObj, &pFn), m_pFn(pFn) { }
CDelegate(const CDelegate& rhs) : CDelegateBase(rhs) { m_pFn = rhs.m_pFn; }
virtual CDelegateBase* Copy() const { return new CDelegate(*this); }
virtual void* GetFn() const { return *(void**)&m_pFn; }

protected:
virtual bool Invoke(void* param)
Expand All @@ -61,6 +62,7 @@ class CDelegate : public CDelegateBase
Fn m_pFn;
};


template <class O, class T>
CDelegate<O, T> MakeDelegate(O* pObject, bool (T::* pFn)(void*))
{
Expand Down

0 comments on commit 99e2f20

Please sign in to comment.