1/*
2 * Copyright 2001-2005, Haiku.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 * DarkWyrm <bpmagic@columbus.rr.com>
7 * Axel Dörfler, axeld@pinc-software.de
8 */
9#ifndef REFERENCE_COUNTING_H
10#define REFERENCE_COUNTING_H
11
12
13#include <SupportDefs.h>
14
15
16/*!
17 \class ReferenceCounting ReferenceCounting.h
18 \brief Base class for reference counting objects
19
20 ReferenceCounting objects track dependencies upon a particular object. In this way,
21 it is possible to ensure that a shared resource is not deleted if something else
22 needs it. How the dependency tracking is done largely depends on the child class.
23*/
24class ReferenceCounting {
25 public:
26 ReferenceCounting()
27 : fReferenceCount(1) {}
28 virtual ~ReferenceCounting() {}
29
30 inline void Acquire();
31 inline bool Release();
32
33 private:
34 int32 fReferenceCount;
35};
36
37
38inline void
39ReferenceCounting::Acquire()
40{
41 atomic_add(&fReferenceCount, 1);
42}
43
44inline bool
45ReferenceCounting::Release()
46{
47 if (atomic_add(&fReferenceCount, -1) == 1) {
48 delete this;
49 return true;
50 }
51
52 return false;
53}
54
55#endif /* REFERENCE_COUNTING_H */