@@ -71,7 +71,7 @@ class SharedPtr {
71
71
* @brief Create empty SharedPtr not pointing to anything.
72
72
* @details Used for variable declaration.
73
73
*/
74
- SharedPtr (): _ptr(NULL ), _counter(NULL )
74
+ constexpr SharedPtr (): _ptr(), _counter()
75
75
{
76
76
}
77
77
@@ -86,12 +86,11 @@ class SharedPtr {
86
86
* @brief Create new SharedPtr
87
87
* @param ptr Pointer to take control over
88
88
*/
89
- SharedPtr (T *ptr): _ptr(ptr), _counter(NULL )
89
+ SharedPtr (T *ptr): _ptr(ptr), _counter()
90
90
{
91
91
// Allocate counter on the heap, so it can be shared
92
- if (_ptr != NULL ) {
93
- _counter = new uint32_t ;
94
- *_counter = 1 ;
92
+ if (_ptr != nullptr ) {
93
+ _counter = new uint32_t (1 );
95
94
}
96
95
}
97
96
@@ -113,13 +112,25 @@ class SharedPtr {
113
112
SharedPtr (const SharedPtr &source): _ptr(source._ptr), _counter(source._counter)
114
113
{
115
114
// Increment reference counter
116
- if (_ptr != NULL ) {
115
+ if (_ptr != nullptr ) {
117
116
core_util_atomic_incr_u32 (_counter, 1 );
118
117
}
119
118
}
120
119
121
120
/* *
122
- * @brief Assignment operator.
121
+ * @brief Move constructor.
122
+ * @details Create new SharedPtr from other SharedPtr by
123
+ * moving pointer to original object and pointer to counter.
124
+ * @param source Object being copied from.
125
+ */
126
+ SharedPtr (SharedPtr &&source): _ptr(source._ptr), _counter(source._counter)
127
+ {
128
+ source._ptr = nullptr ;
129
+ source._counter = nullptr ;
130
+ }
131
+
132
+ /* *
133
+ * @brief Copy assignment operator.
123
134
* @details Cleanup previous reference and assign new pointer and counter.
124
135
* @param source Object being assigned from.
125
136
* @return Object being assigned.
@@ -135,14 +146,37 @@ class SharedPtr {
135
146
_counter = source.get_counter ();
136
147
137
148
// Increment new counter
138
- if (_ptr != NULL ) {
149
+ if (_ptr != nullptr ) {
139
150
core_util_atomic_incr_u32 (_counter, 1 );
140
151
}
141
152
}
142
153
143
154
return *this ;
144
155
}
145
156
157
+ /* *
158
+ * @brief Move assignment operator.
159
+ * @details Cleanup previous reference and assign new pointer and counter.
160
+ * @param source Object being assigned from.
161
+ * @return Object being assigned.
162
+ */
163
+ SharedPtr operator =(SharedPtr &&source)
164
+ {
165
+ if (this != &source) {
166
+ // Clean up by decrementing counter
167
+ decrement_counter ();
168
+
169
+ // Assign new values
170
+ _ptr = source._ptr ;
171
+ _counter = source._counter ;
172
+
173
+ source._ptr = nullptr ;
174
+ source._counter = nullptr ;
175
+ }
176
+
177
+ return *this ;
178
+ }
179
+
146
180
/* *
147
181
* @brief Replaces the managed pointer with a new unmanaged pointer.
148
182
* @param[in] ptr the new raw pointer to manage.
@@ -153,21 +187,24 @@ class SharedPtr {
153
187
decrement_counter ();
154
188
155
189
_ptr = ptr;
156
- if (ptr != NULL ) {
190
+ if (ptr != nullptr ) {
157
191
// Allocate counter on the heap, so it can be shared
158
- _counter = new uint32_t ;
159
- *_counter = 1 ;
192
+ _counter = new uint32_t (1 );
160
193
} else {
161
- _counter = NULL ;
194
+ _counter = nullptr ;
162
195
}
163
196
}
164
197
165
198
/* *
166
- * @brief Replace the managed pointer with a NULL pointer.
199
+ * @brief Replace the managed pointer with a null pointer.
167
200
*/
168
201
void reset ()
169
202
{
170
- reset (NULL );
203
+ // Clean up by decrementing counter
204
+ decrement_counter ();
205
+
206
+ _ptr = nullptr ;
207
+ _counter = nullptr ;
171
208
}
172
209
173
210
/* *
@@ -186,7 +223,7 @@ class SharedPtr {
186
223
*/
187
224
uint32_t use_count () const
188
225
{
189
- if (_ptr != NULL ) {
226
+ if (_ptr != nullptr ) {
190
227
return core_util_atomic_load_u32 (_counter);
191
228
} else {
192
229
return 0 ;
@@ -213,11 +250,11 @@ class SharedPtr {
213
250
214
251
/* *
215
252
* @brief Boolean conversion operator.
216
- * @return Whether or not the pointer is NULL .
253
+ * @return Whether or not the pointer is null .
217
254
*/
218
255
operator bool () const
219
256
{
220
- return ( _ptr != NULL ) ;
257
+ return _ptr != nullptr ;
221
258
}
222
259
223
260
private:
@@ -238,7 +275,7 @@ class SharedPtr {
238
275
*/
239
276
void decrement_counter ()
240
277
{
241
- if (_ptr != NULL ) {
278
+ if (_ptr != nullptr ) {
242
279
if (core_util_atomic_decr_u32 (_counter, 1 ) == 0 ) {
243
280
delete _counter;
244
281
delete _ptr;
0 commit comments