Files
haiku-beta6/headers/private/servers/app/ReferenceCounting.h
T

56 lines
1.1 KiB
C++
Raw Normal View History

/*
* Copyright 2001-2005, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* DarkWyrm <[email protected]>
* Axel Dörfler, [email protected]
*/
2005-11-10 17:32:35 +00:00
#ifndef REFERENCE_COUNTING_H
#define REFERENCE_COUNTING_H
#include <SupportDefs.h>
/*!
2005-11-10 17:32:35 +00:00
\class ReferenceCounting ReferenceCounting.h
\brief Base class for reference counting objects
ReferenceCounting objects track dependencies upon a particular object. In this way,
it is possible to ensure that a shared resource is not deleted if something else
needs it. How the dependency tracking is done largely depends on the child class.
*/
2005-11-10 17:32:35 +00:00
class ReferenceCounting {
public:
2005-11-10 17:32:35 +00:00
ReferenceCounting()
: fReferenceCount(1) {}
2005-11-10 17:32:35 +00:00
virtual ~ReferenceCounting() {}
inline void Acquire();
inline bool Release();
private:
int32 fReferenceCount;
};
inline void
2005-11-10 17:32:35 +00:00
ReferenceCounting::Acquire()
{
atomic_add(&fReferenceCount, 1);
}
inline bool
2005-11-10 17:32:35 +00:00
ReferenceCounting::Release()
{
if (atomic_add(&fReferenceCount, -1) == 1) {
delete this;
return true;
}
return false;
}
2005-11-10 17:32:35 +00:00
#endif /* REFERENCE_COUNTING_H */