New BWeakReferenceable API.
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@42763 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -11,38 +11,35 @@
|
|||||||
|
|
||||||
namespace BPrivate {
|
namespace BPrivate {
|
||||||
|
|
||||||
template<typename Type> class WeakReferenceable;
|
|
||||||
|
|
||||||
template<typename Type>
|
class BWeakReferenceable;
|
||||||
|
|
||||||
|
|
||||||
class WeakPointer : public BReferenceable {
|
class WeakPointer : public BReferenceable {
|
||||||
public:
|
public:
|
||||||
Type* Get();
|
WeakPointer(BWeakReferenceable* object);
|
||||||
|
~WeakPointer();
|
||||||
|
|
||||||
|
BWeakReferenceable* Get();
|
||||||
bool Put();
|
bool Put();
|
||||||
|
|
||||||
int32 UseCount() const;
|
int32 UseCount() const;
|
||||||
|
|
||||||
private:
|
void GetUnchecked();
|
||||||
friend class WeakReferenceable<Type>;
|
|
||||||
|
|
||||||
WeakPointer(Type* object);
|
|
||||||
~WeakPointer();
|
|
||||||
|
|
||||||
private:
|
|
||||||
void _GetUnchecked();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
vint32 fUseCount;
|
vint32 fUseCount;
|
||||||
Type* fObject;
|
BWeakReferenceable* fObject;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename Type>
|
|
||||||
class WeakReferenceable {
|
class BWeakReferenceable {
|
||||||
public:
|
public:
|
||||||
WeakReferenceable(Type* object);
|
BWeakReferenceable();
|
||||||
~WeakReferenceable();
|
virtual ~BWeakReferenceable();
|
||||||
|
|
||||||
void AcquireReference()
|
void AcquireReference()
|
||||||
{ fPointer->_GetUnchecked(); }
|
{ fPointer->GetUnchecked(); }
|
||||||
|
|
||||||
bool ReleaseReference()
|
bool ReleaseReference()
|
||||||
{ return fPointer->Put(); }
|
{ return fPointer->Put(); }
|
||||||
@@ -50,55 +47,43 @@ public:
|
|||||||
int32 CountReferences() const
|
int32 CountReferences() const
|
||||||
{ return fPointer->UseCount(); }
|
{ return fPointer->UseCount(); }
|
||||||
|
|
||||||
WeakPointer<Type>* GetWeakPointer();
|
WeakPointer* GetWeakPointer();
|
||||||
|
private:
|
||||||
protected:
|
WeakPointer* fPointer;
|
||||||
WeakPointer<Type>* fPointer;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
class WeakReference {
|
class BWeakReference {
|
||||||
public:
|
public:
|
||||||
WeakReference()
|
BWeakReference()
|
||||||
:
|
:
|
||||||
fPointer(NULL),
|
fPointer(NULL)
|
||||||
fObject(NULL)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
WeakReference(Type* object)
|
BWeakReference(Type* object)
|
||||||
:
|
:
|
||||||
fPointer(NULL),
|
fPointer(NULL)
|
||||||
fObject(NULL)
|
|
||||||
{
|
{
|
||||||
SetTo(object);
|
SetTo(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
WeakReference(WeakPointer<Type>& other)
|
BWeakReference(const BWeakReference<Type>& other)
|
||||||
:
|
:
|
||||||
fPointer(NULL),
|
fPointer(NULL)
|
||||||
fObject(NULL)
|
|
||||||
{
|
|
||||||
SetTo(&other);
|
|
||||||
}
|
|
||||||
|
|
||||||
WeakReference(WeakPointer<Type>* other)
|
|
||||||
:
|
|
||||||
fPointer(NULL),
|
|
||||||
fObject(NULL)
|
|
||||||
{
|
{
|
||||||
SetTo(other);
|
SetTo(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
WeakReference(const WeakReference<Type>& other)
|
BWeakReference(const BReference<Type>& other)
|
||||||
:
|
:
|
||||||
fPointer(NULL),
|
fPointer(NULL)
|
||||||
fObject(NULL)
|
|
||||||
{
|
{
|
||||||
SetTo(other.fPointer);
|
SetTo(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
~WeakReference()
|
~BWeakReference()
|
||||||
{
|
{
|
||||||
Unset();
|
Unset();
|
||||||
}
|
}
|
||||||
@@ -107,63 +92,51 @@ public:
|
|||||||
{
|
{
|
||||||
Unset();
|
Unset();
|
||||||
|
|
||||||
if (object != NULL) {
|
if (object != NULL)
|
||||||
fPointer = object->GetWeakPointer();
|
fPointer = object->GetWeakPointer();
|
||||||
fObject = fPointer->Get();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetTo(WeakPointer<Type>* pointer)
|
void SetTo(const BWeakReference<Type>& other)
|
||||||
{
|
{
|
||||||
Unset();
|
Unset();
|
||||||
|
|
||||||
if (pointer != NULL) {
|
if (other.fPointer) {
|
||||||
fPointer = pointer;
|
fPointer = other.fPointer;
|
||||||
fPointer->AcquireReference();
|
fPointer->AcquireReference();
|
||||||
fObject = pointer->Get();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SetTo(const BReference<Type>& other)
|
||||||
|
{
|
||||||
|
SetTo(other.Get());
|
||||||
|
}
|
||||||
|
|
||||||
void Unset()
|
void Unset()
|
||||||
{
|
{
|
||||||
if (fPointer != NULL) {
|
if (fPointer != NULL) {
|
||||||
if (fObject != NULL) {
|
|
||||||
fPointer->Put();
|
|
||||||
fObject = NULL;
|
|
||||||
}
|
|
||||||
fPointer->ReleaseReference();
|
fPointer->ReleaseReference();
|
||||||
fPointer = NULL;
|
fPointer = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Type* Get() const
|
bool IsAlive()
|
||||||
{
|
{
|
||||||
return fObject;
|
if (fPointer == NULL)
|
||||||
|
return false;
|
||||||
|
Type* object = static_cast<Type*>(fPointer->Get());
|
||||||
|
if (object == NULL)
|
||||||
|
return false;
|
||||||
|
fPointer->Put();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Type* Detach()
|
BReference<Type> GetReference()
|
||||||
{
|
{
|
||||||
Type* object = fObject;
|
Type* object = static_cast<Type*>(fPointer->Get());
|
||||||
Unset();
|
return BReference<Type>(object, true);
|
||||||
return object;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Type& operator*() const
|
BWeakReference& operator=(const BWeakReference<Type>& other)
|
||||||
{
|
|
||||||
return *fObject;
|
|
||||||
}
|
|
||||||
|
|
||||||
operator Type*() const
|
|
||||||
{
|
|
||||||
return fObject;
|
|
||||||
}
|
|
||||||
|
|
||||||
Type* operator->() const
|
|
||||||
{
|
|
||||||
return fObject;
|
|
||||||
}
|
|
||||||
|
|
||||||
WeakReference& operator=(const WeakReference<Type>& other)
|
|
||||||
{
|
{
|
||||||
if (this == &other)
|
if (this == &other)
|
||||||
return *this;
|
return *this;
|
||||||
@@ -172,46 +145,59 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
WeakReference& operator=(const Type& other)
|
BWeakReference& operator=(const Type& other)
|
||||||
{
|
{
|
||||||
SetTo(&other);
|
SetTo(&other);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
WeakReference& operator=(WeakPointer<Type>& other)
|
BWeakReference& operator=(Type* other)
|
||||||
{
|
|
||||||
SetTo(&other);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
WeakReference& operator=(WeakPointer<Type>* other)
|
|
||||||
{
|
{
|
||||||
SetTo(other);
|
SetTo(other);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const WeakReference<Type>& other) const
|
BWeakReference& operator=(const BReference<Type>& other)
|
||||||
|
{
|
||||||
|
SetTo(other.Get());
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(const BWeakReference<Type>& other) const
|
||||||
{
|
{
|
||||||
return fPointer == other.fPointer;
|
return fPointer == other.fPointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=(const WeakReference<Type>& other) const
|
bool operator!=(const BWeakReference<Type>& other) const
|
||||||
{
|
{
|
||||||
return fPointer != other.fPointer;
|
return fPointer != other.fPointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
WeakPointer<Type>* fPointer;
|
WeakPointer* fPointer;
|
||||||
Type* fObject;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// #pragma mark -
|
// #pragma mark -
|
||||||
|
|
||||||
|
|
||||||
template<typename Type>
|
inline
|
||||||
inline Type*
|
WeakPointer::WeakPointer(BWeakReferenceable* object)
|
||||||
WeakPointer<Type>::Get()
|
:
|
||||||
|
fUseCount(1),
|
||||||
|
fObject(object)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline
|
||||||
|
WeakPointer::~WeakPointer()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline BWeakReferenceable*
|
||||||
|
WeakPointer::Get()
|
||||||
{
|
{
|
||||||
int32 count = -11;
|
int32 count = -11;
|
||||||
|
|
||||||
@@ -225,9 +211,8 @@ WeakPointer<Type>::Get()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename Type>
|
|
||||||
inline bool
|
inline bool
|
||||||
WeakPointer<Type>::Put()
|
WeakPointer::Put()
|
||||||
{
|
{
|
||||||
if (atomic_add(&fUseCount, -1) == 1) {
|
if (atomic_add(&fUseCount, -1) == 1) {
|
||||||
delete fObject;
|
delete fObject;
|
||||||
@@ -238,34 +223,15 @@ WeakPointer<Type>::Put()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename Type>
|
|
||||||
inline int32
|
inline int32
|
||||||
WeakPointer<Type>::UseCount() const
|
WeakPointer::UseCount() const
|
||||||
{
|
{
|
||||||
return fUseCount;
|
return fUseCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename Type>
|
|
||||||
inline
|
|
||||||
WeakPointer<Type>::WeakPointer(Type* object)
|
|
||||||
:
|
|
||||||
fUseCount(1),
|
|
||||||
fObject(object)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template<typename Type>
|
|
||||||
inline
|
|
||||||
WeakPointer<Type>::~WeakPointer()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template<typename Type>
|
|
||||||
inline void
|
inline void
|
||||||
WeakPointer<Type>::_GetUnchecked()
|
WeakPointer::GetUnchecked()
|
||||||
{
|
{
|
||||||
atomic_add(&fUseCount, 1);
|
atomic_add(&fUseCount, 1);
|
||||||
}
|
}
|
||||||
@@ -274,26 +240,23 @@ WeakPointer<Type>::_GetUnchecked()
|
|||||||
// #pragma -
|
// #pragma -
|
||||||
|
|
||||||
|
|
||||||
template<typename Type>
|
|
||||||
inline
|
inline
|
||||||
WeakReferenceable<Type>::WeakReferenceable(Type* object)
|
BWeakReferenceable::BWeakReferenceable()
|
||||||
:
|
:
|
||||||
fPointer(new WeakPointer<Type>(object))
|
fPointer(new WeakPointer(this))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename Type>
|
|
||||||
inline
|
inline
|
||||||
WeakReferenceable<Type>::~WeakReferenceable()
|
BWeakReferenceable::~BWeakReferenceable()
|
||||||
{
|
{
|
||||||
fPointer->ReleaseReference();
|
fPointer->ReleaseReference();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename Type>
|
inline WeakPointer*
|
||||||
inline WeakPointer<Type>*
|
BWeakReferenceable::GetWeakPointer()
|
||||||
WeakReferenceable<Type>::GetWeakPointer()
|
|
||||||
{
|
{
|
||||||
fPointer->AcquireReference();
|
fPointer->AcquireReference();
|
||||||
return fPointer;
|
return fPointer;
|
||||||
@@ -301,8 +264,7 @@ WeakReferenceable<Type>::GetWeakPointer()
|
|||||||
|
|
||||||
} // namespace BPrivate
|
} // namespace BPrivate
|
||||||
|
|
||||||
using BPrivate::WeakReferenceable;
|
using BPrivate::BWeakReferenceable;
|
||||||
using BPrivate::WeakPointer;
|
using BPrivate::BWeakReference;
|
||||||
using BPrivate::WeakReference;
|
|
||||||
|
|
||||||
#endif // _WEAK_REFERENCEABLE_H
|
#endif // _WEAK_REFERENCEABLE_H
|
||||||
|
|||||||
Reference in New Issue
Block a user