VAReferenceableObject.h 4.51 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
1
/*
2
 *  --------------------------------------------------------------------------------------------
Jonas Stienen's avatar
Jonas Stienen committed
3
 *
4
5
6
7
8
9
 *    VVV        VVV A           Virtual Acoustics (VA) | http://www.virtualacoustics.org
 *     VVV      VVV AAA          Licensed under the Apache License, Version 2.0
 *      VVV    VVV   AAA
 *       VVV  VVV     AAA        Copyright 2015-2017
 *        VVVVVV       AAA       Institute of Technical Acoustics (ITA)
 *         VVVV         AAA      RWTH Aachen University
Jonas Stienen's avatar
Jonas Stienen committed
10
 *
11
 *  --------------------------------------------------------------------------------------------
Jonas Stienen's avatar
Jonas Stienen committed
12
13
 */

14
15
#ifndef IW_VACORE_REFERENCEABLE_OBJECT
#define IW_VACORE_REFERENCEABLE_OBJECT
Jonas Stienen's avatar
Jonas Stienen committed
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

#include <VACoreDefinitions.h>
#include <VAException.h>

#include <assert.h>

//! Interface for referenceable objects
/**
  * This interface class can be derived from to inherit
  * functionality that keeps track of every reference
  * an object may have.
  */
class VACORE_API IVAReferenceableObject {
public:
	// Destruktor
	virtual ~IVAReferenceableObject() {};

	// Gibt zurck ob das Objekt referenziert wird (Anzahl Referenzen >= 1)
	virtual bool HasReferences() const=0;

	// Gibt die Anzahl der Referenzen zurck
	virtual int GetNumReferences() const=0;

	// Setzt die Anzahl der Referenzen auf 0
	// REMOVE:
	virtual void ResetReferences()=0;

	// Referenz hinzufgen (Referenzzhler inkrementieren)
	virtual int AddReference() const=0;

	// Referenz entfernen (Referenzzhler dekrementieren)
	virtual int RemoveReference() const=0;
};


//! Class for referenceable objects
/**
  * This interface class can be derived from to inherit
  * functionality that keeps track of every reference
  * an object may have. The reference functionality is
  * implemented using atomic reference counter.
  */
class VACORE_API CVAReferenceableObject :IVAReferenceableObject {
public:
	// Standardkonstruktor (Objekt hat keine Referenzen nach der Erzeugung)
	CVAReferenceableObject();

	// Destruktor
	virtual ~CVAReferenceableObject();

	// Gibt zurck ob das Objekt referenziert wird (Anzahl Referenzen >= 1)
	bool HasReferences() const;

	// Gibt die Anzahl der Referenzen zurck
	int GetNumReferences() const;

	// Setzt die Anzahl der Referenzen auf 0
	// REMOVE:
	virtual void ResetReferences();

	// Referenz hinzufgen (Referenzzhler inkrementieren)
	// (Rckgabe: Wert des Referenzzhlers nach Inkrementierung)
	virtual int AddReference() const;

	// Referenz entfernen (Referenzzhler dekrementieren)
	// (Rckgabe: Wert des Referenzzhlers nach Dekrementierung)
	virtual int RemoveReference() const;

private:
	mutable volatile int m_iRefCount;
};

//! Referenceable pointer type
/**
 * Pointer type for referenceable objects using and managing
 * reference counting. The template class has to be a subclass
 * of #CVAReferenceableObject.
 */
template <class T>
class VACORE_API CVARefPtr {
public:
	//! Standard constructor (No reference, nullptr pointer)
	CVARefPtr() : m_ptr(nullptr) {}

	//! Initialisation constructor, sets reference to the passed object pointer
	CVARefPtr(T* ptr) {
		*this = ptr;
	}

	//! Copy constructor, sets an autonomouse reference for the copy
	CVARefPtr(const CVARefPtr& ref) {
		*this = ref;
	}

	//! Destructor removing reference of target objects, if present
	~CVARefPtr() {
		if (m_ptr) m_ptr->RemoveReference();
	}

	//! Remove reference
	/**
	  * \note Uses assertion in debug mode that reference counter is greater then 0
	  */
	void Clear() {
		if (m_ptr) m_ptr->RemoveReference();
		m_ptr = nullptr;
	};

	//! Assignment, creates an autonomous reference count
	CVARefPtr& operator=(const CVARefPtr& rhs) {
		// Remove currrent reference
		if (m_ptr) m_ptr->RemoveReference();

		// Create new reference (note: first reference, then assignement)
		if (rhs.m_ptr) rhs.m_ptr->AddReference();
		m_ptr = rhs.m_ptr;

		return *this;
	}

	//! Assignment, creates an autonomous reference count
	CVARefPtr& operator=(T* ptr) {
		// Remove currrent reference
		if (m_ptr) m_ptr->RemoveReference();

		// Create new reference (note: first reference, then assignement)
		if (ptr) ptr->AddReference();
		m_ptr = ptr;

		return *this;
	}

	//! Cast assignment to pointer type
	operator T*() const {
		return m_ptr;
	}

	//! Dereferenciation
	T& operator*() const {
		assert( m_ptr );
		if (!m_ptr) VA_EXCEPT1("Attempt to dereference nullpointer");
		return m_ptr;
	}

	//! Member dereferenciation
	T* operator->() const {
		assert( m_ptr );
		if (!m_ptr) VA_EXCEPT1("Attempt to dereference nullpointer");
		return m_ptr;
	}

private:
	T* m_ptr; //!< References object pointer
};


172
#endif // IW_VACORE_REFERENCEABLE_OBJECT