@@ -71,20 +71,19 @@ 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
78
78
/* *
79
79
* @brief Create new SharedPtr
80
80
* @param ptr Pointer to take control over
81
81
*/
82
- SharedPtr (T *ptr): _ptr(ptr), _counter(NULL )
82
+ SharedPtr (T *ptr): _ptr(ptr), _counter()
83
83
{
84
84
// Allocate counter on the heap, so it can be shared
85
- if (_ptr != NULL ) {
86
- _counter = new uint32_t ;
87
- *_counter = 1 ;
85
+ if (_ptr != nullptr ) {
86
+ _counter = new uint32_t (1 );
88
87
}
89
88
}
90
89
@@ -106,13 +105,25 @@ class SharedPtr {
106
105
SharedPtr (const SharedPtr &source): _ptr(source._ptr), _counter(source._counter)
107
106
{
108
107
// Increment reference counter
109
- if (_ptr != NULL ) {
108
+ if (_ptr != nullptr ) {
110
109
core_util_atomic_incr_u32 (_counter, 1 );
111
110
}
112
111
}
113
112
114
113
/* *
115
- * @brief Assignment operator.
114
+ * @brief Move constructor.
115
+ * @details Create new SharedPtr from other SharedPtr by
116
+ * moving pointer to original object and pointer to counter.
117
+ * @param source Object being copied from.
118
+ */
119
+ SharedPtr (SharedPtr &&source): _ptr(source._ptr), _counter(source._counter)
120
+ {
121
+ source._ptr = nullptr ;
122
+ source._counter = nullptr ;
123
+ }
124
+
125
+ /* *
126
+ * @brief Copy assignment operator.
116
127
* @details Cleanup previous reference and assign new pointer and counter.
117
128
* @param source Object being assigned from.
118
129
* @return Object being assigned.
@@ -128,14 +139,37 @@ class SharedPtr {
128
139
_counter = source.get_counter ();
129
140
130
141
// Increment new counter
131
- if (_ptr != NULL ) {
142
+ if (_ptr != nullptr ) {
132
143
core_util_atomic_incr_u32 (_counter, 1 );
133
144
}
134
145
}
135
146
136
147
return *this ;
137
148
}
138
149
150
+ /* *
151
+ * @brief Move assignment operator.
152
+ * @details Cleanup previous reference and assign new pointer and counter.
153
+ * @param source Object being assigned from.
154
+ * @return Object being assigned.
155
+ */
156
+ SharedPtr operator =(SharedPtr &&source)
157
+ {
158
+ if (this != &source) {
159
+ // Clean up by decrementing counter
160
+ decrement_counter ();
161
+
162
+ // Assign new values
163
+ _ptr = source._ptr ;
164
+ _counter = source._counter ;
165
+
166
+ source._ptr = nullptr ;
167
+ source._counter = nullptr ;
168
+ }
169
+
170
+ return *this ;
171
+ }
172
+
139
173
/* *
140
174
* @brief Replaces the managed pointer with a new unmanaged pointer.
141
175
* @param[in] ptr the new raw pointer to manage.
@@ -146,21 +180,24 @@ class SharedPtr {
146
180
decrement_counter ();
147
181
148
182
_ptr = ptr;
149
- if (ptr != NULL ) {
183
+ if (ptr != nullptr ) {
150
184
// Allocate counter on the heap, so it can be shared
151
- _counter = new uint32_t ;
152
- *_counter = 1 ;
185
+ _counter = new uint32_t (1 );
153
186
} else {
154
- _counter = NULL ;
187
+ _counter = nullptr ;
155
188
}
156
189
}
157
190
158
191
/* *
159
- * @brief Replace the managed pointer with a NULL pointer.
192
+ * @brief Replace the managed pointer with a null pointer.
160
193
*/
161
194
void reset ()
162
195
{
163
- reset (NULL );
196
+ // Clean up by decrementing counter
197
+ decrement_counter ();
198
+
199
+ _ptr = nullptr ;
200
+ _counter = nullptr ;
164
201
}
165
202
166
203
/* *
@@ -179,7 +216,7 @@ class SharedPtr {
179
216
*/
180
217
uint32_t use_count () const
181
218
{
182
- if (_ptr != NULL ) {
219
+ if (_ptr != nullptr ) {
183
220
return core_util_atomic_load_u32 (_counter);
184
221
} else {
185
222
return 0 ;
@@ -206,11 +243,11 @@ class SharedPtr {
206
243
207
244
/* *
208
245
* @brief Boolean conversion operator.
209
- * @return Whether or not the pointer is NULL .
246
+ * @return Whether or not the pointer is null .
210
247
*/
211
248
operator bool () const
212
249
{
213
- return ( _ptr != NULL ) ;
250
+ return _ptr != nullptr ;
214
251
}
215
252
216
253
private:
@@ -231,7 +268,7 @@ class SharedPtr {
231
268
*/
232
269
void decrement_counter ()
233
270
{
234
- if (_ptr != NULL ) {
271
+ if (_ptr != nullptr ) {
235
272
if (core_util_atomic_decr_u32 (_counter, 1 ) == 0 ) {
236
273
delete _counter;
237
274
delete _ptr;
0 commit comments