Skip to content

Commit 6ade433

Browse files
committed
SharedPtr: add move operations, make bool explicit
Optimise SharedPtr by giving it move constructor and assignment operator.
1 parent 888dfff commit 6ade433

File tree

1 file changed

+59
-18
lines changed

1 file changed

+59
-18
lines changed

platform/SharedPtr.h

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,23 @@ class SharedPtr {
7171
* @brief Create empty SharedPtr not pointing to anything.
7272
* @details Used for variable declaration.
7373
*/
74-
SharedPtr(): _ptr(NULL), _counter(NULL)
74+
constexpr SharedPtr(): _ptr(), _counter()
75+
{
76+
}
77+
78+
constexpr SharedPtr(std::nullptr_t) : SharedPtr()
7579
{
7680
}
7781

7882
/**
7983
* @brief Create new SharedPtr
8084
* @param ptr Pointer to take control over
8185
*/
82-
SharedPtr(T *ptr): _ptr(ptr), _counter(NULL)
86+
SharedPtr(T *ptr): _ptr(ptr), _counter()
8387
{
8488
// Allocate counter on the heap, so it can be shared
85-
if (_ptr != NULL) {
86-
_counter = new uint32_t;
87-
*_counter = 1;
89+
if (_ptr != nullptr) {
90+
_counter = new uint32_t(1);
8891
}
8992
}
9093

@@ -106,13 +109,25 @@ class SharedPtr {
106109
SharedPtr(const SharedPtr &source): _ptr(source._ptr), _counter(source._counter)
107110
{
108111
// Increment reference counter
109-
if (_ptr != NULL) {
112+
if (_ptr != nullptr) {
110113
core_util_atomic_incr_u32(_counter, 1);
111114
}
112115
}
113116

114117
/**
115-
* @brief Assignment operator.
118+
* @brief Move constructor.
119+
* @details Create new SharedPtr from other SharedPtr by
120+
* moving pointer to original object and pointer to counter.
121+
* @param source Object being copied from.
122+
*/
123+
SharedPtr(SharedPtr &&source): _ptr(source._ptr), _counter(source._counter)
124+
{
125+
source._ptr = nullptr;
126+
source._counter = nullptr;
127+
}
128+
129+
/**
130+
* @brief Copy assignment operator.
116131
* @details Cleanup previous reference and assign new pointer and counter.
117132
* @param source Object being assigned from.
118133
* @return Object being assigned.
@@ -128,14 +143,37 @@ class SharedPtr {
128143
_counter = source.get_counter();
129144

130145
// Increment new counter
131-
if (_ptr != NULL) {
146+
if (_ptr != nullptr) {
132147
core_util_atomic_incr_u32(_counter, 1);
133148
}
134149
}
135150

136151
return *this;
137152
}
138153

154+
/**
155+
* @brief Move assignment operator.
156+
* @details Cleanup previous reference and assign new pointer and counter.
157+
* @param source Object being assigned from.
158+
* @return Object being assigned.
159+
*/
160+
SharedPtr operator=(SharedPtr &&source)
161+
{
162+
if (this != &source) {
163+
// Clean up by decrementing counter
164+
decrement_counter();
165+
166+
// Assign new values
167+
_ptr = source._ptr;
168+
_counter = source._counter;
169+
170+
source._ptr = nullptr;
171+
source._counter = nullptr;
172+
}
173+
174+
return *this;
175+
}
176+
139177
/**
140178
* @brief Replaces the managed pointer with a new unmanaged pointer.
141179
* @param[in] ptr the new raw pointer to manage.
@@ -146,21 +184,24 @@ class SharedPtr {
146184
decrement_counter();
147185

148186
_ptr = ptr;
149-
if (ptr != NULL) {
187+
if (ptr != nullptr) {
150188
// Allocate counter on the heap, so it can be shared
151-
_counter = new uint32_t;
152-
*_counter = 1;
189+
_counter = new uint32_t(1);
153190
} else {
154-
_counter = NULL;
191+
_counter = nullptr;
155192
}
156193
}
157194

158195
/**
159-
* @brief Replace the managed pointer with a NULL pointer.
196+
* @brief Replace the managed pointer with a null pointer.
160197
*/
161198
void reset()
162199
{
163-
reset(NULL);
200+
// Clean up by decrementing counter
201+
decrement_counter();
202+
203+
_ptr = nullptr;
204+
_counter = nullptr;
164205
}
165206

166207
/**
@@ -179,7 +220,7 @@ class SharedPtr {
179220
*/
180221
uint32_t use_count() const
181222
{
182-
if (_ptr != NULL) {
223+
if (_ptr != nullptr) {
183224
return core_util_atomic_load_u32(_counter);
184225
} else {
185226
return 0;
@@ -206,11 +247,11 @@ class SharedPtr {
206247

207248
/**
208249
* @brief Boolean conversion operator.
209-
* @return Whether or not the pointer is NULL.
250+
* @return Whether or not the pointer is null.
210251
*/
211252
operator bool() const
212253
{
213-
return (_ptr != NULL);
254+
return _ptr != nullptr;
214255
}
215256

216257
private:
@@ -231,7 +272,7 @@ class SharedPtr {
231272
*/
232273
void decrement_counter()
233274
{
234-
if (_ptr != NULL) {
275+
if (_ptr != nullptr) {
235276
if (core_util_atomic_decr_u32(_counter, 1) == 0) {
236277
delete _counter;
237278
delete _ptr;

0 commit comments

Comments
 (0)