* 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:
parent
4b0459b2ee
commit
4736b6b9e5
@ -9,16 +9,14 @@
|
||||
#include <SupportDefs.h>
|
||||
|
||||
|
||||
namespace BPrivate {
|
||||
|
||||
class Referenceable {
|
||||
class BReferenceable {
|
||||
public:
|
||||
Referenceable(
|
||||
BReferenceable(
|
||||
bool deleteWhenUnreferenced = true);
|
||||
// TODO: The parameter is deprecated.
|
||||
// Override LastReferenceReleased()
|
||||
// instead!
|
||||
virtual ~Referenceable();
|
||||
virtual ~BReferenceable();
|
||||
|
||||
void AcquireReference();
|
||||
bool ReleaseReference();
|
||||
@ -42,21 +40,116 @@ protected:
|
||||
|
||||
|
||||
void
|
||||
Referenceable::AddReference()
|
||||
BReferenceable::AddReference()
|
||||
{
|
||||
AcquireReference();
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
Referenceable::RemoveReference()
|
||||
BReferenceable::RemoveReference()
|
||||
{
|
||||
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
|
||||
template<typename Type = BPrivate::Referenceable>
|
||||
template<typename Type = BReferenceable>
|
||||
class Reference {
|
||||
public:
|
||||
Reference()
|
||||
@ -141,12 +234,13 @@ private:
|
||||
Type* fObject;
|
||||
};
|
||||
|
||||
|
||||
typedef BReferenceable Referenceable;
|
||||
|
||||
} // namespace BPrivate
|
||||
|
||||
using BPrivate::Referenceable;
|
||||
using BPrivate::Reference;
|
||||
|
||||
typedef BPrivate::Referenceable BReferenceable;
|
||||
|
||||
|
||||
#endif // _REFERENCEABLE_H
|
||||
|
@ -6,20 +6,20 @@
|
||||
#include <Referenceable.h>
|
||||
|
||||
|
||||
Referenceable::Referenceable(bool deleteWhenUnreferenced)
|
||||
BReferenceable::BReferenceable(bool deleteWhenUnreferenced)
|
||||
: fReferenceCount(1),
|
||||
fDeleteWhenUnreferenced(deleteWhenUnreferenced)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Referenceable::~Referenceable()
|
||||
BReferenceable::~BReferenceable()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Referenceable::AcquireReference()
|
||||
BReferenceable::AcquireReference()
|
||||
{
|
||||
if (atomic_add(&fReferenceCount, 1) == 0)
|
||||
FirstReferenceAcquired();
|
||||
@ -27,7 +27,7 @@ Referenceable::AcquireReference()
|
||||
|
||||
|
||||
bool
|
||||
Referenceable::ReleaseReference()
|
||||
BReferenceable::ReleaseReference()
|
||||
{
|
||||
bool unreferenced = (atomic_add(&fReferenceCount, -1) == 1);
|
||||
if (unreferenced)
|
||||
@ -37,13 +37,13 @@ Referenceable::ReleaseReference()
|
||||
|
||||
|
||||
void
|
||||
Referenceable::FirstReferenceAcquired()
|
||||
BReferenceable::FirstReferenceAcquired()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Referenceable::LastReferenceReleased()
|
||||
BReferenceable::LastReferenceReleased()
|
||||
{
|
||||
if (fDeleteWhenUnreferenced)
|
||||
delete this;
|
||||
|
Loading…
Reference in New Issue
Block a user