summaryrefslogtreecommitdiff
path: root/libadhocutil/intrusivePtrBase.h
blob: e10e2f140d19c3062e1d2bf0c24d713964cf97df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef ADHOC_INTRUSIVEPTRBASE_H
#define ADHOC_INTRUSIVEPTRBASE_H

#include <boost/intrusive_ptr.hpp>

class IntrusivePtrBase {
	protected:
		inline IntrusivePtrBase() : _refCount(0) { }
		inline virtual ~IntrusivePtrBase() = 0;

		mutable unsigned int _refCount;
		friend void intrusive_ptr_release(const IntrusivePtrBase * p);
		friend void intrusive_ptr_add_ref(const IntrusivePtrBase * p);
	private:
		IntrusivePtrBase(const IntrusivePtrBase &) = delete;
		void operator=(const IntrusivePtrBase &) = delete;
};

inline
IntrusivePtrBase::~IntrusivePtrBase() = default;

inline
void
intrusive_ptr_release(const IntrusivePtrBase * p)
{
	if (p->_refCount == 1) {
		delete p;
	}
	else {
		p->_refCount -= 1;
	}
}

inline
void
intrusive_ptr_add_ref(const IntrusivePtrBase * p)
{
	p->_refCount += 1;
}

#endif