MCPcopy Create free account
hub / github.com/RavEngine/RavEngine / Ref

Class Ref

include/RavEngine/Ref.hpp:5–105  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3
4template<typename T>
5class Ref {
6 T* ptr = nullptr;
7public:
8 //default constructor
9 Ref() : ptr(nullptr) {}
10
11 /**
12 @returns if this ref is a null ref
13 */
14 bool isNull() {
15 return ptr == nullptr;
16 }
17
18 /**
19 Makes this ref object a null ref. Releases any previously held pointer.
20 */
21 void setNull() {
22 if (!isNull()) {
23 ptr->release();
24 }
25 ptr = nullptr;
26 }
27
28 //constructor
29 Ref(T* const other) {
30 other->retain();
31 ptr = other;
32 }
33
34 //construct from weak pointer
35 Ref(const WeakRef<T>& other) : Ref(other.get()) {};
36
37 //copy
38 Ref(const Ref<T>& other) : Ref(other.ptr) {}
39
40 //destructor
41 //if this crashes, I have a bug because the object was released too early
42 virtual ~Ref() {
43 if (!isNull()) {
44 ptr->release();
45 }
46 }
47
48 //copy assignment
49 Ref<T>& operator=(const Ref<T>& other) {
50 if (&other == this) {
51 return *this;
52 }
53 //copy pointer and increment its retain
54 if (!isNull()) {
55 ptr->release();
56 }
57 ptr = other.get();
58 if (ptr != nullptr) {
59 ptr->retain();
60 }
61 return *this;
62 }

Callers

nothing calls this directly

Calls 3

retainMethod · 0.80
releaseMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected