2007-07-31 20:20:37 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2004-2007, Ingo Weinhold, bonefish@users.sf.net. All rights reserved.
|
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
|
|
|
|
|
|
|
namespace BPrivate {
|
|
|
|
|
2005-01-25 19:10:03 +03:00
|
|
|
class Referenceable {
|
2005-01-25 18:00:12 +03:00
|
|
|
public:
|
2005-01-25 19:10:03 +03:00
|
|
|
Referenceable(
|
2007-07-31 20:20:37 +04:00
|
|
|
bool deleteWhenUnreferenced = true);
|
2005-01-25 19:10:03 +03:00
|
|
|
virtual ~Referenceable();
|
2005-01-25 18:00:12 +03:00
|
|
|
|
2007-07-31 20:20:37 +04:00
|
|
|
void AddReference()
|
|
|
|
{ atomic_add(&fReferenceCount, 1); }
|
|
|
|
|
2005-01-25 18:00:12 +03:00
|
|
|
bool RemoveReference(); // returns true after last
|
|
|
|
|
2007-07-31 20:20:37 +04:00
|
|
|
int32 CountReferences() const
|
|
|
|
{ return fReferenceCount; }
|
2005-01-25 18:00:12 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
vint32 fReferenceCount;
|
|
|
|
bool fDeleteWhenUnreferenced;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Reference
|
2007-07-31 20:20:37 +04:00
|
|
|
template<typename Type = BPrivate::Referenceable>
|
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)
|
|
|
|
{
|
|
|
|
Unset();
|
|
|
|
fObject = object;
|
|
|
|
if (fObject && !alreadyHasReference)
|
|
|
|
fObject->AddReference();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2007-07-31 20:20:37 +04:00
|
|
|
} // namespace BPrivate
|
|
|
|
|
|
|
|
using BPrivate::Referenceable;
|
|
|
|
using BPrivate::Reference;
|
|
|
|
|
|
|
|
#endif // _REFERENCEABLE_H
|