Files
haiku-beta6/headers/private/shared/WeakReferenceable.h
T

331 lines
4.9 KiB
C++
Raw Normal View History

/*
* Copyright 2009, Axel Dörfler, [email protected].
* Distributed under the terms of the MIT License.
*/
#ifndef _WEAK_REFERENCEABLE_H
#define _WEAK_REFERENCEABLE_H
#include <Referenceable.h>
#include <new>
namespace BPrivate {
2011-09-22 22:43:16 +00:00
class BWeakReferenceable;
class WeakPointer : public BReferenceable {
public:
2011-09-22 22:43:16 +00:00
WeakPointer(BWeakReferenceable* object);
~WeakPointer();
BWeakReferenceable* Get();
bool Put();
int32 UseCount() const;
2011-09-22 22:43:16 +00:00
void GetUnchecked();
private:
vint32 fUseCount;
2011-09-22 22:43:16 +00:00
BWeakReferenceable* fObject;
};
2011-09-22 22:43:16 +00:00
class BWeakReferenceable {
public:
2011-09-22 22:43:16 +00:00
BWeakReferenceable();
virtual ~BWeakReferenceable();
status_t InitCheck();
void AcquireReference()
2011-09-22 22:43:16 +00:00
{ fPointer->GetUnchecked(); }
bool ReleaseReference()
{ return fPointer->Put(); }
int32 CountReferences() const
{ return fPointer->UseCount(); }
2011-09-22 22:43:16 +00:00
WeakPointer* GetWeakPointer();
private:
WeakPointer* fPointer;
};
2011-09-22 22:43:16 +00:00
template<typename Type>
2011-09-22 22:43:16 +00:00
class BWeakReference {
public:
2011-09-22 22:43:16 +00:00
BWeakReference()
:
2011-09-22 22:43:16 +00:00
fPointer(NULL)
{
}
2011-09-22 22:43:16 +00:00
BWeakReference(Type* object)
:
2011-09-22 22:43:16 +00:00
fPointer(NULL)
{
SetTo(object);
}
2011-09-22 22:43:16 +00:00
BWeakReference(const BWeakReference<Type>& other)
:
2011-09-22 22:43:16 +00:00
fPointer(NULL)
{
SetTo(other);
}
2011-09-22 22:43:16 +00:00
BWeakReference(const BReference<Type>& other)
:
2011-09-22 22:43:16 +00:00
fPointer(NULL)
{
2011-09-22 22:43:16 +00:00
SetTo(other);
}
template <typename OtherType>
BWeakReference(const BReference<OtherType>& other)
:
fPointer(NULL)
{
SetTo(other.Get());
}
template <typename OtherType>
BWeakReference(const BWeakReference<OtherType>& other)
:
fPointer(NULL)
{
SetTo(other);
}
2011-09-22 22:43:16 +00:00
~BWeakReference()
{
Unset();
}
void SetTo(Type* object)
{
Unset();
2011-09-22 22:43:16 +00:00
if (object != NULL)
fPointer = object->GetWeakPointer();
}
2011-09-22 22:43:16 +00:00
void SetTo(const BWeakReference<Type>& other)
{
Unset();
2011-09-22 22:43:16 +00:00
if (other.fPointer) {
fPointer = other.fPointer;
fPointer->AcquireReference();
}
}
template <typename OtherType>
void SetTo(const BWeakReference<OtherType>& other)
{
// Just a compiler check if the types are compatible.
OtherType* otherDummy = NULL;
Type* dummy = otherDummy;
dummy = NULL;
Unset();
if (other.Get()) {
fPointer = const_cast<WeakPointer*>(other.Get());
fPointer->AcquireReference();
}
}
2011-09-22 22:43:16 +00:00
void SetTo(const BReference<Type>& other)
{
SetTo(other.Get());
}
void Unset()
{
if (fPointer != NULL) {
fPointer->ReleaseReference();
fPointer = NULL;
}
}
2011-09-22 22:43:16 +00:00
bool IsAlive()
{
2011-09-22 22:43:16 +00:00
if (fPointer == NULL)
return false;
Type* object = static_cast<Type*>(fPointer->Get());
if (object == NULL)
return false;
fPointer->Put();
return true;
}
2011-09-22 22:43:16 +00:00
BReference<Type> GetReference()
{
2011-09-22 22:43:16 +00:00
Type* object = static_cast<Type*>(fPointer->Get());
return BReference<Type>(object, true);
}
/*! Do not use this if you do not know what you are doing. The WeakPointer
is for internal use only. */
const WeakPointer* Get() const
{
return fPointer;
}
2011-09-22 22:43:16 +00:00
BWeakReference& operator=(const BWeakReference<Type>& other)
{
if (this == &other)
return *this;
SetTo(other);
return *this;
}
BWeakReference& operator=(Type* other)
{
SetTo(other);
return *this;
}
BWeakReference& operator=(const BReference<Type>& other)
{
SetTo(other.Get());
return *this;
}
template <typename OtherType>
BWeakReference& operator=(const BReference<OtherType>& other)
{
2011-09-22 22:43:16 +00:00
SetTo(other.Get());
return *this;
}
template <typename OtherType>
BWeakReference& operator=(const BWeakReference<OtherType>& other)
{
SetTo(other);
return *this;
}
2011-09-22 22:43:16 +00:00
bool operator==(const BWeakReference<Type>& other) const
{
return fPointer == other.fPointer;
}
2011-09-22 22:43:16 +00:00
bool operator!=(const BWeakReference<Type>& other) const
{
return fPointer != other.fPointer;
}
private:
2011-09-22 22:43:16 +00:00
WeakPointer* fPointer;
};
// #pragma mark -
2011-09-22 22:43:16 +00:00
inline
WeakPointer::WeakPointer(BWeakReferenceable* object)
:
fUseCount(1),
fObject(object)
{
}
inline
WeakPointer::~WeakPointer()
{
}
inline BWeakReferenceable*
WeakPointer::Get()
{
int32 count = -11;
do {
count = atomic_get(&fUseCount);
if (count == 0)
return NULL;
} while (atomic_test_and_set(&fUseCount, count + 1, count) != count);
return fObject;
}
inline bool
2011-09-22 22:43:16 +00:00
WeakPointer::Put()
{
if (atomic_add(&fUseCount, -1) == 1) {
delete fObject;
return true;
}
return false;
}
inline int32
2011-09-22 22:43:16 +00:00
WeakPointer::UseCount() const
{
return fUseCount;
}
inline void
2011-09-22 22:43:16 +00:00
WeakPointer::GetUnchecked()
{
atomic_add(&fUseCount, 1);
}
// #pragma -
inline
2011-09-22 22:43:16 +00:00
BWeakReferenceable::BWeakReferenceable()
:
fPointer(new(std::nothrow) WeakPointer(this))
{
}
inline
2011-09-22 22:43:16 +00:00
BWeakReferenceable::~BWeakReferenceable()
{
fPointer->ReleaseReference();
}
inline status_t
BWeakReferenceable::InitCheck()
{
if (fPointer == NULL)
return B_NO_MEMORY;
return B_OK;
}
2011-09-22 22:43:16 +00:00
inline WeakPointer*
BWeakReferenceable::GetWeakPointer()
{
fPointer->AcquireReference();
return fPointer;
}
} // namespace BPrivate
2011-09-22 22:43:16 +00:00
using BPrivate::BWeakReferenceable;
using BPrivate::BWeakReference;
#endif // _WEAK_REFERENCEABLE_H