2005-11-03 16:10:28 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2001-2005, Haiku.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* DarkWyrm <bpmagic@columbus.rr.com>
|
|
|
|
* Axel Dörfler, axeld@pinc-software.de
|
|
|
|
*/
|
|
|
|
#ifndef SHARED_OBJECT_H
|
|
|
|
#define SHARED_OBJECT_H
|
|
|
|
|
2003-02-06 04:04:14 +03:00
|
|
|
|
|
|
|
#include <SupportDefs.h>
|
|
|
|
|
2005-11-03 16:10:28 +03:00
|
|
|
|
2003-02-06 04:04:14 +03:00
|
|
|
/*!
|
2003-07-14 00:56:00 +04:00
|
|
|
\class SharedObject SharedObject.h
|
|
|
|
\brief Base class for shared objects
|
2003-02-06 04:04:14 +03:00
|
|
|
|
2003-07-14 00:56:00 +04:00
|
|
|
SharedObjects track dependencies upon a particular object. In this way, is 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.
|
2003-02-06 04:04:14 +03:00
|
|
|
*/
|
2005-10-20 13:37:04 +04:00
|
|
|
class SharedObject {
|
2005-11-03 16:10:28 +03:00
|
|
|
public:
|
|
|
|
SharedObject()
|
|
|
|
: fReferenceCount(1) {}
|
|
|
|
virtual ~SharedObject() {}
|
|
|
|
|
|
|
|
inline void Acquire();
|
|
|
|
inline bool Release();
|
|
|
|
|
|
|
|
private:
|
|
|
|
int32 fReferenceCount;
|
2003-02-06 04:04:14 +03:00
|
|
|
};
|
|
|
|
|
2005-11-03 16:10:28 +03:00
|
|
|
|
|
|
|
inline void
|
|
|
|
SharedObject::Acquire()
|
|
|
|
{
|
|
|
|
atomic_add(&fReferenceCount, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
SharedObject::Release()
|
|
|
|
{
|
|
|
|
if (atomic_add(&fReferenceCount, -1) == 1) {
|
|
|
|
delete this;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* SHARED_OBJECT_H */
|