2007-07-31 20:20:37 +04:00
|
|
|
/*
|
2009-07-02 01:59:27 +04:00
|
|
|
* Copyright 2004-2009, Ingo Weinhold, ingo_weinhold@gmx.de.
|
2005-01-25 19:10:03 +03:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
2007-07-31 20:20:37 +04:00
|
|
|
#ifndef _REFERENCEABLE_H
|
|
|
|
#define _REFERENCEABLE_H
|
2005-01-25 18:00:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
#include <SupportDefs.h>
|
|
|
|
|
2007-07-31 20:20:37 +04:00
|
|
|
|
2009-11-04 18:08:53 +03:00
|
|
|
class BReferenceable {
|
2005-01-25 18:00:12 +03:00
|
|
|
public:
|
2009-11-04 18:08:53 +03:00
|
|
|
BReferenceable(
|
2007-07-31 20:20:37 +04:00
|
|
|
bool deleteWhenUnreferenced = true);
|
2009-07-02 01:59:27 +04:00
|
|
|
// TODO: The parameter is deprecated.
|
|
|
|
// Override LastReferenceReleased()
|
|
|
|
// instead!
|
2009-11-04 18:08:53 +03:00
|
|
|
virtual ~BReferenceable();
|
2005-01-25 18:00:12 +03:00
|
|
|
|
2009-07-02 01:59:27 +04:00
|
|
|
void AcquireReference();
|
|
|
|
bool ReleaseReference();
|
|
|
|
// returns true after last
|
2005-01-25 18:00:12 +03:00
|
|
|
|
2007-07-31 20:20:37 +04:00
|
|
|
int32 CountReferences() const
|
|
|
|
{ return fReferenceCount; }
|
2005-01-25 18:00:12 +03:00
|
|
|
|
2009-07-02 01:59:27 +04:00
|
|
|
// deprecate aliases
|
|
|
|
inline void AddReference();
|
|
|
|
inline bool RemoveReference();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void FirstReferenceAcquired();
|
|
|
|
virtual void LastReferenceReleased();
|
|
|
|
|
2005-01-25 18:00:12 +03:00
|
|
|
protected:
|
|
|
|
vint32 fReferenceCount;
|
|
|
|
bool fDeleteWhenUnreferenced;
|
|
|
|
};
|
|
|
|
|
2009-07-02 01:59:27 +04:00
|
|
|
|
|
|
|
void
|
2009-11-04 18:08:53 +03:00
|
|
|
BReferenceable::AddReference()
|
2009-07-02 01:59:27 +04:00
|
|
|
{
|
|
|
|
AcquireReference();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
2009-11-04 18:08:53 +03:00
|
|
|
BReferenceable::RemoveReference()
|
2009-07-02 01:59:27 +04:00
|
|
|
{
|
|
|
|
return ReleaseReference();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-04 18:08:53 +03:00
|
|
|
// 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)
|
2010-02-15 01:22:17 +03:00
|
|
|
object->AcquireReference();
|
2009-11-04 18:08:53 +03:00
|
|
|
|
|
|
|
Unset();
|
|
|
|
|
|
|
|
fObject = object;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unset()
|
|
|
|
{
|
|
|
|
if (fObject) {
|
2010-02-15 01:22:17 +03:00
|
|
|
fObject->ReleaseReference();
|
2009-11-04 18:08:53 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-07-26 23:18:56 +04:00
|
|
|
operator Type*() const
|
|
|
|
{
|
|
|
|
return fObject;
|
|
|
|
}
|
|
|
|
|
2009-11-04 18:08:53 +03:00
|
|
|
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 {
|
|
|
|
|
|
|
|
|
2005-01-25 18:00:12 +03:00
|
|
|
// Reference
|
2009-11-04 18:08:53 +03:00
|
|
|
template<typename Type = BReferenceable>
|
2005-01-25 18:00:12 +03:00
|
|
|
class Reference {
|
|
|
|
public:
|
|
|
|
Reference()
|
|
|
|
: fObject(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Reference(Type* object, bool alreadyHasReference = false)
|
|
|
|
: fObject(NULL)
|
|
|
|
{
|
|
|
|
SetTo(object, alreadyHasReference);
|
|
|
|
}
|
|
|
|
|
|
|
|
Reference(const Reference<Type>& other)
|
|
|
|
: fObject(NULL)
|
|
|
|
{
|
|
|
|
SetTo(other.fObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Reference()
|
|
|
|
{
|
|
|
|
Unset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetTo(Type* object, bool alreadyHasReference = false)
|
|
|
|
{
|
2009-09-21 08:29:27 +04:00
|
|
|
if (object != NULL && !alreadyHasReference)
|
|
|
|
object->AddReference();
|
|
|
|
|
2005-01-25 18:00:12 +03:00
|
|
|
Unset();
|
2009-09-21 08:29:27 +04:00
|
|
|
|
2005-01-25 18:00:12 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Reference& operator=(const Reference<Type>& other)
|
|
|
|
{
|
|
|
|
SetTo(other.fObject);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const Reference<Type>& other) const
|
|
|
|
{
|
|
|
|
return (fObject == other.fObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Reference<Type>& other) const
|
|
|
|
{
|
|
|
|
return (fObject != other.fObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Type* fObject;
|
|
|
|
};
|
|
|
|
|
2009-11-04 18:08:53 +03:00
|
|
|
|
|
|
|
typedef BReferenceable Referenceable;
|
|
|
|
|
2007-07-31 20:20:37 +04:00
|
|
|
} // namespace BPrivate
|
|
|
|
|
|
|
|
using BPrivate::Referenceable;
|
|
|
|
using BPrivate::Reference;
|
|
|
|
|
2009-07-25 04:20:06 +04:00
|
|
|
|
2007-07-31 20:20:37 +04:00
|
|
|
#endif // _REFERENCEABLE_H
|