* Made BReferenceable the class implementation and BPrivate::Referenceable the
typedef, so it's clearer which one is the preferred one.
* Added BReference, a clone of BPrivate::Reference.
BPrivate::{Referenceable,Reference} are being phased out. Only the B* versions
should be used.
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33879 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -9,16 +9,14 @@
|
|||||||
#include <SupportDefs.h>
|
#include <SupportDefs.h>
|
||||||
|
|
||||||
|
|
||||||
namespace BPrivate {
|
class BReferenceable {
|
||||||
|
|
||||||
class Referenceable {
|
|
||||||
public:
|
public:
|
||||||
Referenceable(
|
BReferenceable(
|
||||||
bool deleteWhenUnreferenced = true);
|
bool deleteWhenUnreferenced = true);
|
||||||
// TODO: The parameter is deprecated.
|
// TODO: The parameter is deprecated.
|
||||||
// Override LastReferenceReleased()
|
// Override LastReferenceReleased()
|
||||||
// instead!
|
// instead!
|
||||||
virtual ~Referenceable();
|
virtual ~BReferenceable();
|
||||||
|
|
||||||
void AcquireReference();
|
void AcquireReference();
|
||||||
bool ReleaseReference();
|
bool ReleaseReference();
|
||||||
@@ -42,21 +40,116 @@ protected:
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
Referenceable::AddReference()
|
BReferenceable::AddReference()
|
||||||
{
|
{
|
||||||
AcquireReference();
|
AcquireReference();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool
|
bool
|
||||||
Referenceable::RemoveReference()
|
BReferenceable::RemoveReference()
|
||||||
{
|
{
|
||||||
return ReleaseReference();
|
return ReleaseReference();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// BReference
|
||||||
|
template<typename Type = BReferenceable>
|
||||||
|
class BReference {
|
||||||
|
public:
|
||||||
|
BReference()
|
||||||
|
: fObject(NULL)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
BReference(Type* object, bool alreadyHasReference = false)
|
||||||
|
: fObject(NULL)
|
||||||
|
{
|
||||||
|
SetTo(object, alreadyHasReference);
|
||||||
|
}
|
||||||
|
|
||||||
|
BReference(const BReference<Type>& other)
|
||||||
|
: fObject(NULL)
|
||||||
|
{
|
||||||
|
SetTo(other.fObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
~BReference()
|
||||||
|
{
|
||||||
|
Unset();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetTo(Type* object, bool alreadyHasReference = false)
|
||||||
|
{
|
||||||
|
if (object != NULL && !alreadyHasReference)
|
||||||
|
object->AddReference();
|
||||||
|
|
||||||
|
Unset();
|
||||||
|
|
||||||
|
fObject = object;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Unset()
|
||||||
|
{
|
||||||
|
if (fObject) {
|
||||||
|
fObject->RemoveReference();
|
||||||
|
fObject = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Type* Get() const
|
||||||
|
{
|
||||||
|
return fObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
Type* Detach()
|
||||||
|
{
|
||||||
|
Type* object = fObject;
|
||||||
|
fObject = NULL;
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
||||||
|
Type& operator*() const
|
||||||
|
{
|
||||||
|
return *fObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
Type* operator->() const
|
||||||
|
{
|
||||||
|
return fObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
BReference& operator=(const BReference<Type>& other)
|
||||||
|
{
|
||||||
|
SetTo(other.fObject);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(const BReference<Type>& other) const
|
||||||
|
{
|
||||||
|
return (fObject == other.fObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator!=(const BReference<Type>& other) const
|
||||||
|
{
|
||||||
|
return (fObject != other.fObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Type* fObject;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// #pragma mark Obsolete API
|
||||||
|
|
||||||
|
// TODO: To be phased out!
|
||||||
|
|
||||||
|
|
||||||
|
namespace BPrivate {
|
||||||
|
|
||||||
|
|
||||||
// Reference
|
// Reference
|
||||||
template<typename Type = BPrivate::Referenceable>
|
template<typename Type = BReferenceable>
|
||||||
class Reference {
|
class Reference {
|
||||||
public:
|
public:
|
||||||
Reference()
|
Reference()
|
||||||
@@ -141,12 +234,13 @@ private:
|
|||||||
Type* fObject;
|
Type* fObject;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
typedef BReferenceable Referenceable;
|
||||||
|
|
||||||
} // namespace BPrivate
|
} // namespace BPrivate
|
||||||
|
|
||||||
using BPrivate::Referenceable;
|
using BPrivate::Referenceable;
|
||||||
using BPrivate::Reference;
|
using BPrivate::Reference;
|
||||||
|
|
||||||
typedef BPrivate::Referenceable BReferenceable;
|
|
||||||
|
|
||||||
|
|
||||||
#endif // _REFERENCEABLE_H
|
#endif // _REFERENCEABLE_H
|
||||||
|
|||||||
@@ -6,20 +6,20 @@
|
|||||||
#include <Referenceable.h>
|
#include <Referenceable.h>
|
||||||
|
|
||||||
|
|
||||||
Referenceable::Referenceable(bool deleteWhenUnreferenced)
|
BReferenceable::BReferenceable(bool deleteWhenUnreferenced)
|
||||||
: fReferenceCount(1),
|
: fReferenceCount(1),
|
||||||
fDeleteWhenUnreferenced(deleteWhenUnreferenced)
|
fDeleteWhenUnreferenced(deleteWhenUnreferenced)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Referenceable::~Referenceable()
|
BReferenceable::~BReferenceable()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
Referenceable::AcquireReference()
|
BReferenceable::AcquireReference()
|
||||||
{
|
{
|
||||||
if (atomic_add(&fReferenceCount, 1) == 0)
|
if (atomic_add(&fReferenceCount, 1) == 0)
|
||||||
FirstReferenceAcquired();
|
FirstReferenceAcquired();
|
||||||
@@ -27,7 +27,7 @@ Referenceable::AcquireReference()
|
|||||||
|
|
||||||
|
|
||||||
bool
|
bool
|
||||||
Referenceable::ReleaseReference()
|
BReferenceable::ReleaseReference()
|
||||||
{
|
{
|
||||||
bool unreferenced = (atomic_add(&fReferenceCount, -1) == 1);
|
bool unreferenced = (atomic_add(&fReferenceCount, -1) == 1);
|
||||||
if (unreferenced)
|
if (unreferenced)
|
||||||
@@ -37,13 +37,13 @@ Referenceable::ReleaseReference()
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
Referenceable::FirstReferenceAcquired()
|
BReferenceable::FirstReferenceAcquired()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
Referenceable::LastReferenceReleased()
|
BReferenceable::LastReferenceReleased()
|
||||||
{
|
{
|
||||||
if (fDeleteWhenUnreferenced)
|
if (fDeleteWhenUnreferenced)
|
||||||
delete this;
|
delete this;
|
||||||
|
|||||||
Reference in New Issue
Block a user