* Added a WeakReferenceable implementation, completely untested yet.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@29990 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2009-04-07 12:01:30 +00:00
parent b936fc6770
commit e1fed132d2
4 changed files with 276 additions and 0 deletions

View File

@ -0,0 +1,261 @@
/*
* 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>
namespace BPrivate {
class WeakReferenceable;
class WeakPointer : public Referenceable {
public:
WeakReferenceable* Get();
bool Put();
int32 UseCount() const;
private:
friend class WeakReferenceable;
WeakPointer(WeakReferenceable* object);
~WeakPointer();
private:
bool _GetUnchecked();
private:
vint32 fUseCount;
WeakReferenceable* fObject;
};
class WeakReferenceable {
public:
WeakReferenceable();
virtual ~WeakReferenceable();
void AddReference()
{ fPointer->_GetUnchecked(); }
bool RemoveReference()
{ return fPointer->Put(); }
int32 CountReferences() const
{ return fPointer->UseCount(); }
WeakPointer* GetWeakPointer();
protected:
WeakPointer* fPointer;
};
template<typename Type = BPrivate::Referenceable>
class WeakReference {
public:
WeakReference()
:
fPointer(NULL),
fObject(NULL)
{
}
WeakReference(Type* object)
:
fPointer(NULL),
fObject(NULL)
{
SetTo(object);
}
WeakReference(const WeakPointer& other)
:
fPointer(NULL),
fObject(NULL)
{
SetTo(&other);
}
WeakReference(const WeakReference<Type>& other)
:
fPointer(NULL),
fObject(NULL)
{
SetTo(other.fPointer);
}
~WeakReference()
{
Unset();
}
void SetTo(Type* object)
{
Unset();
if (object != NULL) {
fPointer = object->GetWeakPointer();
fObject = fPointer->Get();
}
}
void SetTo(WeakPointer* pointer)
{
Unset();
if (pointer != NULL) {
fPointer = pointer->AddReference();
fObject = pointer->Get();
}
}
void Unset()
{
if (fPointer != NULL) {
if (fObject != NULL) {
fPointer->Put();
fObject = NULL;
}
fPointer->RemoveReference();
fPointer = NULL;
}
}
Type* Get() const
{
return fObject;
}
Type* Detach()
{
Type* object = fObject;
Unset();
return object;
}
Type& operator*() const
{
return *fObject;
}
Type* operator->() const
{
return fObject;
}
WeakReference& operator=(const WeakReference<Type>& other)
{
SetTo(other.fPointer);
return *this;
}
WeakReference& operator=(const Type& other)
{
SetTo(&other);
return *this;
}
bool operator==(const WeakReference<Type>& other) const
{
return fPointer == other.fPointer;
}
bool operator!=(const WeakReference<Type>& other) const
{
return fPointer != other.fPointer;
}
private:
WeakPointer* fPointer;
Type* fObject;
};
// #pragma mark -
inline WeakReferenceable*
WeakPointer::Get()
{
int32 count;
do {
count = fUseCount;
if (count == 0)
return NULL;
} while (atomic_test_and_set(&fUseCount, count, count + 1) != count);
return fObject;
}
inline bool
WeakPointer::Put()
{
if (atomic_add(&fUseCount, -1) == 1) {
delete fObject;
return true;
}
return false;
}
inline int32
WeakPointer::UseCount() const
{
return fUseCount;
}
inline
WeakPointer::WeakPointer(WeakReferenceable* object)
:
fUseCount(1),
fObject(object)
{
}
inline
WeakPointer::~WeakPointer()
{
}
inline bool
WeakPointer::_GetUnchecked()
{
return atomic_add(&fUseCount, 1) == 1;
}
// #pragma -
inline
WeakReferenceable::WeakReferenceable()
:
fPointer(new WeakPointer(this))
{
}
inline WeakPointer*
WeakReferenceable::GetWeakPointer()
{
fPointer->AddReference();
return fPointer;
}
} // namespace BPrivate
using BPrivate::WeakReferenceable;
using BPrivate::WeakPointer;
using BPrivate::WeakReference;
#endif // _WEAK_REFERENCEABLE_H

View File

@ -18,4 +18,5 @@ MergeObject <libbe>support_kit.o :
Referenceable.cpp
StopWatch.cpp
String.cpp
WeakReferenceable.cpp
;

View File

@ -0,0 +1,13 @@
/*
* Copyright 2009, Axel Dörfler, axeld@pinc-software.de.
* Distributed under the terms of the MIT License.
*/
#include <WeakReferenceable.h>
WeakReferenceable::~WeakReferenceable()
{
}

View File

@ -120,6 +120,7 @@ SEARCH_SOURCE = [ FDirName $(HAIKU_TOP) src kits support ] ;
KernelMergeObject kernel_misc.o :
Referenceable.cpp
WeakReferenceable.cpp
: $(TARGET_KERNEL_PIC_CCFLAGS)
;