forked from HeliumProject/Foundation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SmartPtr.h
185 lines (155 loc) · 4.04 KB
/
SmartPtr.h
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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#pragma once
#include "Platform/Atomic.h"
#include "Platform/Utility.h"
namespace Helium
{
//
// This is a delete-helper for a heap allocated object.
// We don't use auto_ptr because its generally terrible.
// This class is probably only good for the simplest use cases.
//
// example:
// AutoPtr<Foo> foo( new Foo );
//
template <typename T>
class AutoPtr
{
HELIUM_COMPILE_ASSERT( sizeof( T ) > 1 );
template< typename U > friend class AutoPtr;
public:
AutoPtr( T* ptr = NULL );
AutoPtr( const AutoPtr& rhs );
template< typename U > AutoPtr( const AutoPtr< U >& rhs );
~AutoPtr();
T* Ptr() const;
T* operator->() const;
T& operator*() const;
operator T*() const;
AutoPtr& operator=( const AutoPtr& rhs );
bool IsOrphan();
void Orphan( bool orphan = true );
void Reset(T *ptr, bool carryOrphan = true );
T* Release();
private:
mutable T* m_Ptr;
};
//
// This is a simple manager for a C-style array allocated with new [].
// We don't use AutoPtr because of the delete semantics.
// This class is probably only good for the simplest use cases.
//
// example:
// ArrayPtr<char> array( new char[24] );
//
template <typename T>
class ArrayPtr
{
public:
ArrayPtr( T* ptr = NULL );
ArrayPtr( const ArrayPtr& rhs );
~ArrayPtr();
T* Ptr() const;
T& operator[]( size_t i ) const;
operator T*() const;
ArrayPtr& operator=( const ArrayPtr& rhs );
private:
mutable T* m_Ptr;
};
/// Base class for non-atomic reference counting support.
template< typename T >
class RefCountBase
{
private:
/// Reference count.
mutable uint32_t m_RefCount;
public:
/// @name Construction/Destruction
//@{
RefCountBase();
RefCountBase( const RefCountBase& rSource );
virtual ~RefCountBase();
//@}
/// @name Reference Counting
//@{
uint32_t GetRefCount() const;
uint32_t IncrRefCount() const;
uint32_t DecrRefCount() const;
//@}
/// @name Overloaded Operators
//@{
RefCountBase& operator=( const RefCountBase& rSource );
//@}
};
/// Base class for atomic reference counting support.
template< typename T >
class AtomicRefCountBase
{
private:
/// Reference count.
mutable volatile int32_t m_RefCount;
public:
/// @name Construction/Destruction
//@{
AtomicRefCountBase();
AtomicRefCountBase( const AtomicRefCountBase& rSource );
virtual ~AtomicRefCountBase();
//@}
/// @name Reference Counting
//@{
uint32_t GetRefCount() const;
uint32_t IncrRefCount() const;
uint32_t DecrRefCount() const;
//@}
/// @name Overloaded Operators
//@{
AtomicRefCountBase& operator=( const AtomicRefCountBase& rSource );
//@}
};
/// SmartPtr safely manages the reference count on the stack.
template< typename T >
class SmartPtr
{
template< typename U > friend class SmartPtr;
public:
/// @name Construction/Destruction
//@{
SmartPtr();
SmartPtr( const T* pPointer );
SmartPtr( const SmartPtr& rPointer );
template< typename U > SmartPtr( const SmartPtr< U >& rPointer );
~SmartPtr();
//@}
/// @name Data Access
//@{
T* Get() const;
T* Ptr() const;
void Set( const T* pResource );
void Release();
bool ReferencesObject() const;
//@}
/// @name Overloaded Operators
//@{
T& operator*() const;
T* operator->() const;
operator T* const&() const;
SmartPtr& operator=( const T* pPointer );
SmartPtr& operator=( const SmartPtr& rPointer );
template< typename U > SmartPtr& operator=( const SmartPtr< U >& rPointer );
//@}
private:
/// Object referenced by this smart pointer.
T* m_Pointer;
};
/// DeepCompareSmartPtr is used in containers to compare what's being pointed to by the SmartPtrs rather than the pointers themselves
template < typename T >
class DeepCompareSmartPtr : public SmartPtr< T >
{
public:
DeepCompareSmartPtr();
DeepCompareSmartPtr( const T* pPointer );
DeepCompareSmartPtr( const Helium::SmartPtr<T>& pPointer );
inline bool operator<( const DeepCompareSmartPtr& rhs ) const;
inline bool operator==( const DeepCompareSmartPtr& rhs ) const;
};
}
#include "Foundation/SmartPtr.inl"