Nebula
Loading...
Searching...
No Matches
weakptr.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
14#include "core/ptr.h"
15
16//------------------------------------------------------------------------------
17template<class TYPE>
19{
20public:
22 WeakPtr();
24 WeakPtr(TYPE* p);
26 WeakPtr(const Ptr<TYPE>& p);
28 WeakPtr(const WeakPtr<TYPE>& p);
30 ~WeakPtr();
32 void operator=(const Ptr<TYPE>& rhs);
34 void operator=(const WeakPtr<TYPE>& rhs);
36 void operator=(TYPE* rhs);
38 TYPE* operator->() const;
40 TYPE& operator*() const;
42 operator TYPE*() const;
44 bool isvalid() const;
46 TYPE* get() const;
48 TYPE* get_unsafe() const;
49
50private:
51 TYPE* ptr;
52};
53
54//------------------------------------------------------------------------------
57template<class TYPE>
59 ptr(0)
60{
61 // empty
62}
63
64//------------------------------------------------------------------------------
67template<class TYPE>
69 ptr(p)
70{
71 // empty
72}
73
74//------------------------------------------------------------------------------
77template<class TYPE>
79 ptr(p.get_unsafe())
80{
81 // empty
82}
83
84//------------------------------------------------------------------------------
87template<class TYPE>
89 ptr(p.ptr)
90{
91 // empty
92}
93
94//------------------------------------------------------------------------------
97template<class TYPE>
99{
100 this->ptr = 0;
101}
102
103//------------------------------------------------------------------------------
106template<class TYPE>
107void
109{
110 this->ptr = rhs.get_unsafe();
111}
112
113//------------------------------------------------------------------------------
116template<class TYPE>
117void
119{
120 this->ptr = rhs.ptr;
121}
122
123//------------------------------------------------------------------------------
126template<class TYPE>
127void
129{
130 this->ptr = rhs;
131}
132
133//------------------------------------------------------------------------------
136template<class TYPE>
137TYPE*
139{
140 n_assert2(this->ptr, "NULL pointer access in WeakPtr::operator->()!");
141 return this->ptr;
142}
143
144//------------------------------------------------------------------------------
147template<class TYPE>
148TYPE&
150{
151 n_assert2(this->ptr, "NULL pointer access in WeakPtr::operator*()!");
152 return *this->ptr;
153}
154
155//------------------------------------------------------------------------------
158template<class TYPE>
160{
161 n_assert2(this->ptr, "NULL pointer access in WeakPtr::operator TYPE*()!");
162 return this->ptr;
163}
164
165//------------------------------------------------------------------------------
168template<class TYPE>
169bool
171{
172 return (0 != this->ptr);
173}
174
175//------------------------------------------------------------------------------
178template<class TYPE>
179TYPE*
181{
182 n_assert2(this->ptr, "NULL pointer access in WeakPtr::get()!");
183 return this->ptr;
184}
185
186//------------------------------------------------------------------------------
189template<class TYPE>
190TYPE*
192{
193 return this->ptr;
194}
195//------------------------------------------------------------------------------
Nebula's smart pointer class which manages the life time of RefCounted objects.
Definition ptr.h:38
TYPE * get_unsafe() const
return direct pointer (returns null pointer)
Definition ptr.h:456
A smart pointer which does not change the reference count of the target object.
Definition weakptr.h:19
void operator=(const Ptr< TYPE > &rhs)
assignment operator from Ptr<>
Definition weakptr.h:108
bool isvalid() const
check if pointer is valid
Definition weakptr.h:170
TYPE * operator->() const
safe -> operator
Definition weakptr.h:138
TYPE * get() const
return direct pointer (asserts if null pointer)
Definition weakptr.h:180
~WeakPtr()
destructor
Definition weakptr.h:98
TYPE & operator*() const
safe dereference operator
Definition weakptr.h:149
TYPE * get_unsafe() const
return direct pointer (returns null pointer)
Definition weakptr.h:191
WeakPtr()
constructor
Definition weakptr.h:58
TYPE * ptr
Definition weakptr.h:51
#define n_assert2(exp, msg)
Definition debug.h:51