2009-04-07 16:01:30 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2009, Axel Dörfler, axeld@pinc-software.de.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
|
|
|
#ifndef _WEAK_REFERENCEABLE_H
|
|
|
|
#define _WEAK_REFERENCEABLE_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <Referenceable.h>
|
|
|
|
|
2011-09-23 06:03:10 +04:00
|
|
|
#include <new>
|
|
|
|
|
2009-04-07 16:01:30 +04:00
|
|
|
|
|
|
|
namespace BPrivate {
|
|
|
|
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
class BWeakReferenceable;
|
|
|
|
|
|
|
|
|
2010-12-16 19:35:42 +03:00
|
|
|
class WeakPointer : public BReferenceable {
|
2009-04-07 16:01:30 +04:00
|
|
|
public:
|
2011-09-23 02:43:16 +04:00
|
|
|
WeakPointer(BWeakReferenceable* object);
|
|
|
|
~WeakPointer();
|
|
|
|
|
|
|
|
BWeakReferenceable* Get();
|
2009-04-07 16:01:30 +04:00
|
|
|
bool Put();
|
|
|
|
|
|
|
|
int32 UseCount() const;
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
void GetUnchecked();
|
2009-04-07 16:01:30 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
vint32 fUseCount;
|
2011-09-23 02:43:16 +04:00
|
|
|
BWeakReferenceable* fObject;
|
2009-04-07 16:01:30 +04:00
|
|
|
};
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
|
|
|
|
class BWeakReferenceable {
|
2009-04-07 16:01:30 +04:00
|
|
|
public:
|
2011-09-23 02:43:16 +04:00
|
|
|
BWeakReferenceable();
|
|
|
|
virtual ~BWeakReferenceable();
|
2009-04-07 16:01:30 +04:00
|
|
|
|
2011-09-23 06:03:10 +04:00
|
|
|
status_t InitCheck();
|
|
|
|
|
2010-12-16 19:35:42 +03:00
|
|
|
void AcquireReference()
|
2011-09-23 02:43:16 +04:00
|
|
|
{ fPointer->GetUnchecked(); }
|
2009-04-07 16:01:30 +04:00
|
|
|
|
2010-12-16 19:35:42 +03:00
|
|
|
bool ReleaseReference()
|
2009-04-07 16:01:30 +04:00
|
|
|
{ return fPointer->Put(); }
|
|
|
|
|
|
|
|
int32 CountReferences() const
|
|
|
|
{ return fPointer->UseCount(); }
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
WeakPointer* GetWeakPointer();
|
|
|
|
private:
|
|
|
|
WeakPointer* fPointer;
|
2009-04-07 16:01:30 +04:00
|
|
|
};
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
|
2009-04-07 19:14:15 +04:00
|
|
|
template<typename Type>
|
2011-09-23 02:43:16 +04:00
|
|
|
class BWeakReference {
|
2009-04-07 16:01:30 +04:00
|
|
|
public:
|
2011-09-23 02:43:16 +04:00
|
|
|
BWeakReference()
|
2009-04-07 16:01:30 +04:00
|
|
|
:
|
2011-09-23 02:43:16 +04:00
|
|
|
fPointer(NULL)
|
2009-04-07 16:01:30 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
BWeakReference(Type* object)
|
2009-04-07 16:01:30 +04:00
|
|
|
:
|
2011-09-23 02:43:16 +04:00
|
|
|
fPointer(NULL)
|
2009-04-07 16:01:30 +04:00
|
|
|
{
|
|
|
|
SetTo(object);
|
|
|
|
}
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
BWeakReference(const BWeakReference<Type>& other)
|
2009-04-07 19:14:15 +04:00
|
|
|
:
|
2011-09-23 02:43:16 +04:00
|
|
|
fPointer(NULL)
|
2009-04-07 19:14:15 +04:00
|
|
|
{
|
|
|
|
SetTo(other);
|
|
|
|
}
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
BWeakReference(const BReference<Type>& other)
|
2009-04-07 16:01:30 +04:00
|
|
|
:
|
2011-09-23 02:43:16 +04:00
|
|
|
fPointer(NULL)
|
2009-04-07 16:01:30 +04:00
|
|
|
{
|
2011-09-23 02:43:16 +04:00
|
|
|
SetTo(other);
|
2009-04-07 16:01:30 +04:00
|
|
|
}
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
~BWeakReference()
|
2009-04-07 16:01:30 +04:00
|
|
|
{
|
|
|
|
Unset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetTo(Type* object)
|
|
|
|
{
|
|
|
|
Unset();
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
if (object != NULL)
|
2009-04-07 16:01:30 +04:00
|
|
|
fPointer = object->GetWeakPointer();
|
|
|
|
}
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
void SetTo(const BWeakReference<Type>& other)
|
2009-04-07 16:01:30 +04:00
|
|
|
{
|
|
|
|
Unset();
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
if (other.fPointer) {
|
|
|
|
fPointer = other.fPointer;
|
2010-12-16 19:35:42 +03:00
|
|
|
fPointer->AcquireReference();
|
2009-04-07 16:01:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
void SetTo(const BReference<Type>& other)
|
|
|
|
{
|
|
|
|
SetTo(other.Get());
|
|
|
|
}
|
|
|
|
|
2009-04-07 16:01:30 +04:00
|
|
|
void Unset()
|
|
|
|
{
|
|
|
|
if (fPointer != NULL) {
|
2010-12-16 19:35:42 +03:00
|
|
|
fPointer->ReleaseReference();
|
2009-04-07 16:01:30 +04:00
|
|
|
fPointer = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
bool IsAlive()
|
2009-04-07 16:01:30 +04:00
|
|
|
{
|
2011-09-23 02:43:16 +04:00
|
|
|
if (fPointer == NULL)
|
|
|
|
return false;
|
|
|
|
Type* object = static_cast<Type*>(fPointer->Get());
|
|
|
|
if (object == NULL)
|
|
|
|
return false;
|
|
|
|
fPointer->Put();
|
|
|
|
return true;
|
2009-04-07 19:14:15 +04:00
|
|
|
}
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
BReference<Type> GetReference()
|
2009-04-07 16:01:30 +04:00
|
|
|
{
|
2011-09-23 02:43:16 +04:00
|
|
|
Type* object = static_cast<Type*>(fPointer->Get());
|
|
|
|
return BReference<Type>(object, true);
|
2009-04-07 16:01:30 +04:00
|
|
|
}
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
BWeakReference& operator=(const BWeakReference<Type>& other)
|
2009-04-07 16:01:30 +04:00
|
|
|
{
|
2009-04-07 19:14:15 +04:00
|
|
|
if (this == &other)
|
|
|
|
return *this;
|
|
|
|
|
2009-04-07 16:01:30 +04:00
|
|
|
SetTo(other.fPointer);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
BWeakReference& operator=(const Type& other)
|
2009-04-07 16:01:30 +04:00
|
|
|
{
|
|
|
|
SetTo(&other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
BWeakReference& operator=(Type* other)
|
2009-04-07 19:14:15 +04:00
|
|
|
{
|
2011-09-23 02:43:16 +04:00
|
|
|
SetTo(other);
|
2009-04-07 19:14:15 +04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
BWeakReference& operator=(const BReference<Type>& other)
|
2009-04-07 19:14:15 +04:00
|
|
|
{
|
2011-09-23 02:43:16 +04:00
|
|
|
SetTo(other.Get());
|
2009-04-07 19:14:15 +04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
bool operator==(const BWeakReference<Type>& other) const
|
2009-04-07 16:01:30 +04:00
|
|
|
{
|
|
|
|
return fPointer == other.fPointer;
|
|
|
|
}
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
bool operator!=(const BWeakReference<Type>& other) const
|
2009-04-07 16:01:30 +04:00
|
|
|
{
|
|
|
|
return fPointer != other.fPointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-09-23 02:43:16 +04:00
|
|
|
WeakPointer* fPointer;
|
2009-04-07 16:01:30 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// #pragma mark -
|
|
|
|
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
inline
|
|
|
|
WeakPointer::WeakPointer(BWeakReferenceable* object)
|
|
|
|
:
|
|
|
|
fUseCount(1),
|
|
|
|
fObject(object)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline
|
|
|
|
WeakPointer::~WeakPointer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline BWeakReferenceable*
|
|
|
|
WeakPointer::Get()
|
2009-04-07 16:01:30 +04:00
|
|
|
{
|
2009-04-07 19:14:15 +04:00
|
|
|
int32 count = -11;
|
2009-04-07 16:01:30 +04:00
|
|
|
|
|
|
|
do {
|
2009-04-07 19:14:15 +04:00
|
|
|
count = atomic_get(&fUseCount);
|
2009-04-07 16:01:30 +04:00
|
|
|
if (count == 0)
|
|
|
|
return NULL;
|
2009-04-07 19:14:15 +04:00
|
|
|
} while (atomic_test_and_set(&fUseCount, count + 1, count) != count);
|
2009-04-07 16:01:30 +04:00
|
|
|
|
|
|
|
return fObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool
|
2011-09-23 02:43:16 +04:00
|
|
|
WeakPointer::Put()
|
2009-04-07 16:01:30 +04:00
|
|
|
{
|
|
|
|
if (atomic_add(&fUseCount, -1) == 1) {
|
|
|
|
delete fObject;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline int32
|
2011-09-23 02:43:16 +04:00
|
|
|
WeakPointer::UseCount() const
|
2009-04-07 16:01:30 +04:00
|
|
|
{
|
|
|
|
return fUseCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-24 13:58:54 +04:00
|
|
|
inline void
|
2011-09-23 02:43:16 +04:00
|
|
|
WeakPointer::GetUnchecked()
|
2009-04-07 16:01:30 +04:00
|
|
|
{
|
2009-04-24 13:58:54 +04:00
|
|
|
atomic_add(&fUseCount, 1);
|
2009-04-07 16:01:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// #pragma -
|
|
|
|
|
|
|
|
|
|
|
|
inline
|
2011-09-23 02:43:16 +04:00
|
|
|
BWeakReferenceable::BWeakReferenceable()
|
2009-04-07 16:01:30 +04:00
|
|
|
:
|
2011-09-23 06:03:10 +04:00
|
|
|
fPointer(new(std::nothrow) WeakPointer(this))
|
2009-04-07 19:14:15 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline
|
2011-09-23 02:43:16 +04:00
|
|
|
BWeakReferenceable::~BWeakReferenceable()
|
2009-04-07 16:01:30 +04:00
|
|
|
{
|
2010-12-16 19:35:42 +03:00
|
|
|
fPointer->ReleaseReference();
|
2009-04-07 16:01:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-23 06:03:10 +04:00
|
|
|
inline status_t
|
|
|
|
BWeakReferenceable::InitCheck()
|
|
|
|
{
|
|
|
|
if (fPointer == NULL)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
inline WeakPointer*
|
|
|
|
BWeakReferenceable::GetWeakPointer()
|
2009-04-07 16:01:30 +04:00
|
|
|
{
|
2010-12-16 19:35:42 +03:00
|
|
|
fPointer->AcquireReference();
|
2009-04-07 16:01:30 +04:00
|
|
|
return fPointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace BPrivate
|
|
|
|
|
2011-09-23 02:43:16 +04:00
|
|
|
using BPrivate::BWeakReferenceable;
|
|
|
|
using BPrivate::BWeakReference;
|
2009-04-07 16:01:30 +04:00
|
|
|
|
|
|
|
#endif // _WEAK_REFERENCEABLE_H
|