* Removed the obsolescent [B]Reference[able] API and replaced the remaining

uses. Fixes the gcc 2 acpi build.
* Renamed WeakReferenceable::{Add,Remove}Reference() to
  {Acquire,Release}Reference() for consistency.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@39871 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold 2010-12-16 16:35:42 +00:00
parent 88e38c178a
commit 756b64fd83
17 changed files with 47 additions and 170 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2004-2009, Ingo Weinhold, ingo_weinhold@gmx.de. * Copyright 2004-2010, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#ifndef _REFERENCEABLE_H #ifndef _REFERENCEABLE_H
@ -9,13 +9,12 @@
#include <SupportDefs.h> #include <SupportDefs.h>
// #pragma mark - BReferenceable
class BReferenceable { class BReferenceable {
public: public:
BReferenceable( BReferenceable();
bool deleteWhenUnreferenced = true);
// TODO: The parameter is deprecated.
// Override LastReferenceReleased()
// instead!
virtual ~BReferenceable(); virtual ~BReferenceable();
void AcquireReference(); void AcquireReference();
@ -25,35 +24,18 @@ public:
int32 CountReferences() const int32 CountReferences() const
{ return fReferenceCount; } { return fReferenceCount; }
// deprecate aliases
inline void AddReference();
inline bool RemoveReference();
protected: protected:
virtual void FirstReferenceAcquired(); virtual void FirstReferenceAcquired();
virtual void LastReferenceReleased(); virtual void LastReferenceReleased();
protected: protected:
vint32 fReferenceCount; vint32 fReferenceCount;
bool fDeleteWhenUnreferenced;
}; };
void // #pragma mark - BReference
BReferenceable::AddReference()
{
AcquireReference();
}
bool
BReferenceable::RemoveReference()
{
return ReleaseReference();
}
// BReference
template<typename Type = BReferenceable> template<typename Type = BReferenceable>
class BReference { class BReference {
public: public:
@ -145,107 +127,4 @@ private:
}; };
// #pragma mark Obsolete API
// TODO: To be phased out!
namespace BPrivate {
// Reference
template<typename Type = BReferenceable>
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)
{
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;
}
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;
};
typedef BReferenceable Referenceable;
} // namespace BPrivate
using BPrivate::Referenceable;
using BPrivate::Reference;
#endif // _REFERENCEABLE_H #endif // _REFERENCEABLE_H

View File

@ -14,7 +14,7 @@ namespace BPrivate {
template<typename Type> class WeakReferenceable; template<typename Type> class WeakReferenceable;
template<typename Type> template<typename Type>
class WeakPointer : public Referenceable { class WeakPointer : public BReferenceable {
public: public:
Type* Get(); Type* Get();
bool Put(); bool Put();
@ -41,10 +41,10 @@ public:
WeakReferenceable(Type* object); WeakReferenceable(Type* object);
~WeakReferenceable(); ~WeakReferenceable();
void AddReference() void AcquireReference()
{ fPointer->_GetUnchecked(); } { fPointer->_GetUnchecked(); }
bool RemoveReference() bool ReleaseReference()
{ return fPointer->Put(); } { return fPointer->Put(); }
int32 CountReferences() const int32 CountReferences() const
@ -119,7 +119,7 @@ public:
if (pointer != NULL) { if (pointer != NULL) {
fPointer = pointer; fPointer = pointer;
fPointer->AddReference(); fPointer->AcquireReference();
fObject = pointer->Get(); fObject = pointer->Get();
} }
} }
@ -131,7 +131,7 @@ public:
fPointer->Put(); fPointer->Put();
fObject = NULL; fObject = NULL;
} }
fPointer->RemoveReference(); fPointer->ReleaseReference();
fPointer = NULL; fPointer = NULL;
} }
} }
@ -287,7 +287,7 @@ template<typename Type>
inline inline
WeakReferenceable<Type>::~WeakReferenceable() WeakReferenceable<Type>::~WeakReferenceable()
{ {
fPointer->RemoveReference(); fPointer->ReleaseReference();
} }
@ -295,7 +295,7 @@ template<typename Type>
inline WeakPointer<Type>* inline WeakPointer<Type>*
WeakReferenceable<Type>::GetWeakPointer() WeakReferenceable<Type>::GetWeakPointer()
{ {
fPointer->AddReference(); fPointer->AcquireReference();
return fPointer; return fPointer;
} }

View File

@ -148,7 +148,7 @@ net_socket_private::RemoveFromParent()
{ {
ASSERT(!is_in_socket_list && parent != NULL); ASSERT(!is_in_socket_list && parent != NULL);
parent->RemoveReference(); parent->ReleaseReference();
parent = NULL; parent = NULL;
mutex_lock(&sSocketLock); mutex_lock(&sSocketLock);
@ -157,7 +157,7 @@ net_socket_private::RemoveFromParent()
is_in_socket_list = true; is_in_socket_list = true;
RemoveReference(); ReleaseReference();
} }
@ -458,7 +458,7 @@ socket_free(net_socket* _socket)
{ {
net_socket_private* socket = (net_socket_private*)_socket; net_socket_private* socket = (net_socket_private*)_socket;
socket->first_info->free(socket->first_protocol); socket->first_info->free(socket->first_protocol);
socket->RemoveReference(); socket->ReleaseReference();
} }
@ -674,7 +674,7 @@ socket_acquire(net_socket* _socket)
if (socket->CountReferences() == 0) if (socket->CountReferences() == 0)
return false; return false;
socket->AddReference(); socket->AcquireReference();
return true; return true;
} }
@ -683,7 +683,7 @@ bool
socket_release(net_socket* _socket) socket_release(net_socket* _socket)
{ {
net_socket_private* socket = (net_socket_private*)_socket; net_socket_private* socket = (net_socket_private*)_socket;
return socket->RemoveReference(); return socket->ReleaseReference();
} }
@ -739,7 +739,7 @@ socket_dequeue_connected(net_socket* _parent, net_socket** _socket)
net_socket_private* socket = parent->connected_children.RemoveHead(); net_socket_private* socket = parent->connected_children.RemoveHead();
if (socket != NULL) { if (socket != NULL) {
socket->AddReference(); socket->AcquireReference();
socket->RemoveFromParent(); socket->RemoveFromParent();
parent->child_count--; parent->child_count--;
*_socket = socket; *_socket = socket;

View File

@ -35,7 +35,7 @@ PLItemsCommand::_CleanUp(PlaylistItem**& items, int32 count, bool deleteItems)
return; return;
if (deleteItems) { if (deleteItems) {
for (int32 i = 0; i < count; i++) for (int32 i = 0; i < count; i++)
items[i]->RemoveReference(); items[i]->ReleaseReference();
} }
delete[] items; delete[] items;
items = NULL; items = NULL;

View File

@ -235,7 +235,7 @@ Playlist::MakeEmpty(bool deleteItems)
PlaylistItem* item = RemoveItem(i, false); PlaylistItem* item = RemoveItem(i, false);
_NotifyItemRemoved(i); _NotifyItemRemoved(i);
if (deleteItems) if (deleteItems)
item->RemoveReference(); item->ReleaseReference();
} }
SetCurrentItemIndex(-1); SetCurrentItemIndex(-1);
} }

View File

@ -46,7 +46,7 @@ protected:
}; };
class PowerStatusDriverInterface : public Monitor, public Referenceable { class PowerStatusDriverInterface : public Monitor, public BReferenceable {
public: public:
PowerStatusDriverInterface(); PowerStatusDriverInterface();
~PowerStatusDriverInterface(); ~PowerStatusDriverInterface();

View File

@ -30,7 +30,7 @@ enum {
}; };
class BitmapOwner : public Referenceable { class BitmapOwner : public BReferenceable {
public: public:
BitmapOwner(BBitmap* bitmap); BitmapOwner(BBitmap* bitmap);
virtual ~BitmapOwner(); virtual ~BitmapOwner();

View File

@ -16,11 +16,11 @@ Image::Image(SharedImage* image, const image_info& info, team_id owner,
fCreationEvent(creationEvent), fCreationEvent(creationEvent),
fDeletionEvent(-1) fDeletionEvent(-1)
{ {
fImage->AddReference(); fImage->AcquireReference();
} }
Image::~Image() Image::~Image()
{ {
fImage->RemoveReference(); fImage->ReleaseReference();
} }

View File

@ -8,7 +8,7 @@
#include "SharedImage.h" #include "SharedImage.h"
class Image : public Referenceable { class Image : public BReferenceable {
public: public:
Image(SharedImage* image, Image(SharedImage* image,
const image_info& info, team_id owner, const image_info& info, team_id owner,

View File

@ -32,13 +32,13 @@ ImageProfileResult::ImageProfileResult(SharedImage* image, image_id id)
fTotalHits(0), fTotalHits(0),
fImageID(id) fImageID(id)
{ {
fImage->AddReference(); fImage->AcquireReference();
} }
ImageProfileResult::~ImageProfileResult() ImageProfileResult::~ImageProfileResult()
{ {
fImage->RemoveReference(); fImage->ReleaseReference();
} }

View File

@ -45,7 +45,7 @@ struct SymbolComparator {
}; };
class SharedImage : public Referenceable { class SharedImage : public BReferenceable {
public: public:
SharedImage(); SharedImage();
~SharedImage(); ~SharedImage();

View File

@ -51,7 +51,7 @@ Team::~Team()
remove_team_debugger(fID); remove_team_debugger(fID);
for (int32 i = 0; Image* image = fImages.ItemAt(i); i++) for (int32 i = 0; Image* image = fImages.ItemAt(i); i++)
image->RemoveReference(); image->ReleaseReference();
} }
@ -277,5 +277,5 @@ Team::_RemoveImage(int32 index, int32 event)
} }
image->SetDeletionEvent(event); image->SetDeletionEvent(event);
image->RemoveReference(); image->ReleaseReference();
} }

View File

@ -113,7 +113,7 @@ public:
{ {
// release image references // release image references
for (ImageMap::iterator it = fImages.begin(); it != fImages.end(); ++it) for (ImageMap::iterator it = fImages.begin(); it != fImages.end(); ++it)
it->second->RemoveReference(); it->second->ReleaseReference();
if (fSummaryProfileResult != NULL) if (fSummaryProfileResult != NULL)
fSummaryProfileResult->ReleaseReference(); fSummaryProfileResult->ReleaseReference();

View File

@ -16,10 +16,9 @@
#endif #endif
BReferenceable::BReferenceable(bool deleteWhenUnreferenced) BReferenceable::BReferenceable()
: :
fReferenceCount(1), fReferenceCount(1)
fDeleteWhenUnreferenced(deleteWhenUnreferenced)
{ {
} }
@ -59,6 +58,5 @@ BReferenceable::FirstReferenceAcquired()
void void
BReferenceable::LastReferenceReleased() BReferenceable::LastReferenceReleased()
{ {
if (fDeleteWhenUnreferenced)
delete this; delete this;
} }

View File

@ -38,7 +38,7 @@ FontCache::~FontCache()
{ {
FontMap::Iterator iterator = fFontCacheEntries.GetIterator(); FontMap::Iterator iterator = fFontCacheEntries.GetIterator();
while (iterator.HasNext()) while (iterator.HasNext())
iterator.Next().value->RemoveReference(); iterator.Next().value->ReleaseReference();
} }
// Default // Default
@ -62,7 +62,7 @@ FontCache::FontCacheEntryFor(const ServerFont& font)
if (entry) { if (entry) {
// the entry was already there // the entry was already there
entry->AddReference(); entry->AcquireReference();
//printf("FontCacheEntryFor(%ld): %p\n", font.GetFamilyAndStyle(), entry); //printf("FontCacheEntryFor(%ld): %p\n", font.GetFamilyAndStyle(), entry);
return entry; return entry;
} }
@ -95,7 +95,7 @@ FontCache::FontCacheEntryFor(const ServerFont& font)
} }
//printf("FontCacheEntryFor(%ld): %p (insert)\n", font.GetFamilyAndStyle(), entry); //printf("FontCacheEntryFor(%ld): %p (insert)\n", font.GetFamilyAndStyle(), entry);
entry->AddReference(); entry->AcquireReference();
return entry; return entry;
} }
@ -107,7 +107,7 @@ FontCache::Recycle(FontCacheEntry* entry)
if (!entry) if (!entry)
return; return;
entry->UpdateUsage(); entry->UpdateUsage();
entry->RemoveReference(); entry->ReleaseReference();
} }
static const int32 kMaxEntryCount = 30; static const int32 kMaxEntryCount = 30;
@ -154,7 +154,7 @@ FontCache::_ConstrainEntryCount()
while (iterator.HasNext()) { while (iterator.HasNext()) {
if (iterator.Next().value == leastUsedEntry) { if (iterator.Next().value == leastUsedEntry) {
iterator.Remove(); iterator.Remove();
leastUsedEntry->RemoveReference(); leastUsedEntry->ReleaseReference();
break; break;
} }
} }

View File

@ -938,7 +938,7 @@ ServerPicture::SetOwner(ServerApp* owner)
// May remove the last reference and then we will self-destruct right then. // May remove the last reference and then we will self-destruct right then.
// Setting fOwner to NULL would access free'd memory. If owner is another // Setting fOwner to NULL would access free'd memory. If owner is another
// ServerApp, it's expected to already have a reference of course. // ServerApp, it's expected to already have a reference of course.
Reference<ServerPicture> _(this); BReference<ServerPicture> _(this);
if (fOwner != NULL) if (fOwner != NULL)
fOwner->RemovePicture(this); fOwner->RemovePicture(this);

View File

@ -159,10 +159,10 @@ SingleMessagingTargetSet::Rewind()
Besides the flattened message it also stores the when the message was Besides the flattened message it also stores the when the message was
created and when the delivery attempts shall time out. created and when the delivery attempts shall time out.
*/ */
class MessageDeliverer::Message : public Referenceable { class MessageDeliverer::Message : public BReferenceable {
public: public:
Message(void *data, int32 dataSize, bigtime_t timeout) Message(void *data, int32 dataSize, bigtime_t timeout)
: Referenceable(true), : BReferenceable(),
fData(data), fData(data),
fDataSize(dataSize), fDataSize(dataSize),
fCreationTime(system_time()), fCreationTime(system_time()),
@ -243,13 +243,13 @@ public:
fToken(token) fToken(token)
{ {
if (fMessage) if (fMessage)
fMessage->AddReference(); fMessage->AcquireReference();
} }
~TargetMessage() ~TargetMessage()
{ {
if (fMessage) if (fMessage)
fMessage->RemoveReference(); fMessage->ReleaseReference();
} }
Message *GetMessage() const Message *GetMessage() const
@ -653,7 +653,7 @@ MessageDeliverer::DeliverMessage(const void *messageData, int32 messageSize,
free(data); free(data);
return B_NO_MEMORY; return B_NO_MEMORY;
} }
Reference<Message> _(message, true); BReference<Message> _(message, true);
// add the message to the respective target ports // add the message to the respective target ports
BAutolock locker(fLock); BAutolock locker(fLock);