Clean up observer_ptr<> and removed the eronous casting of ptr in objectDeleted.

This commit is contained in:
Robert Osfield 2010-02-19 19:50:55 +00:00
parent 657c5c4e71
commit eca4361c4a

View File

@ -40,7 +40,11 @@ class observer_ptr : public Observer
observer_ptr(): _ptr(0L) {}
observer_ptr(T* t): _ptr(t) { if (_ptr) _ptr->addObserver(this); }
observer_ptr(const observer_ptr& rp):Observer(), _ptr(rp._ptr) { if (_ptr) _ptr->addObserver(this); }
observer_ptr(const observer_ptr& rp): _ptr(0L)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*getObserverMutex());
if (_ptr) _ptr->addObserver(this);
}
~observer_ptr()
{
@ -79,7 +83,8 @@ class observer_ptr : public Observer
ref_ptr<T> lock() const
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*getObserverMutex());
return ref_ptr<T>(_ptr);
if (_ptr && _ptr->referenceCount()>0) return ref_ptr<T>(_ptr);
else return ref_ptr<T>(_ptr);
}
// get the raw C pointer
@ -109,12 +114,10 @@ class observer_ptr : public Observer
protected:
virtual void objectDeleted(void* obj)
virtual void objectDeleted(void*)
{
T* ref_obj = static_cast<T*>(obj);
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*getObserverMutex());
if (ref_obj->referenceCount() == 0) _ptr = 0;
_ptr = 0;
}
T* _ptr;