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