@@ -71,20 +71,23 @@ 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
+ {
76
+ }
77
+
78
+ constexpr SharedPtr (std::nullptr_t ) : SharedPtr()
75
79
{
76
80
}
77
81
78
82
/* *
79
83
* @brief Create new SharedPtr
80
84
* @param ptr Pointer to take control over
81
85
*/
82
- SharedPtr (T *ptr): _ptr(ptr), _counter(NULL )
86
+ SharedPtr (T *ptr): _ptr(ptr), _counter()
83
87
{
84
88
// 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 );
88
91
}
89
92
}
90
93
@@ -106,13 +109,25 @@ class SharedPtr {
106
109
SharedPtr (const SharedPtr &source): _ptr(source._ptr), _counter(source._counter)
107
110
{
108
111
// Increment reference counter
109
- if (_ptr != NULL ) {
112
+ if (_ptr != nullptr ) {
110
113
core_util_atomic_incr_u32 (_counter, 1 );
111
114
}
112
115
}
113
116
114
117
/* *
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.
116
131
* @details Cleanup previous reference and assign new pointer and counter.
117
132
* @param source Object being assigned from.
118
133
* @return Object being assigned.
@@ -128,14 +143,37 @@ class SharedPtr {
128
143
_counter = source.get_counter ();
129
144
130
145
// Increment new counter
131
- if (_ptr != NULL ) {
146
+ if (_ptr != nullptr ) {
132
147
core_util_atomic_incr_u32 (_counter, 1 );
133
148
}
134
149
}
135
150
136
151
return *this ;
137
152
}
138
153
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
+
139
177
/* *
140
178
* @brief Replaces the managed pointer with a new unmanaged pointer.
141
179
* @param[in] ptr the new raw pointer to manage.
@@ -146,21 +184,24 @@ class SharedPtr {
146
184
decrement_counter ();
147
185
148
186
_ptr = ptr;
149
- if (ptr != NULL ) {
187
+ if (ptr != nullptr ) {
150
188
// Allocate counter on the heap, so it can be shared
151
- _counter = new uint32_t ;
152
- *_counter = 1 ;
189
+ _counter = new uint32_t (1 );
153
190
} else {
154
- _counter = NULL ;
191
+ _counter = nullptr ;
155
192
}
156
193
}
157
194
158
195
/* *
159
- * @brief Replace the managed pointer with a NULL pointer.
196
+ * @brief Replace the managed pointer with a null pointer.
160
197
*/
161
198
void reset ()
162
199
{
163
- reset (NULL );
200
+ // Clean up by decrementing counter
201
+ decrement_counter ();
202
+
203
+ _ptr = nullptr ;
204
+ _counter = nullptr ;
164
205
}
165
206
166
207
/* *
@@ -179,7 +220,7 @@ class SharedPtr {
179
220
*/
180
221
uint32_t use_count () const
181
222
{
182
- if (_ptr != NULL ) {
223
+ if (_ptr != nullptr ) {
183
224
return core_util_atomic_load_u32 (_counter);
184
225
} else {
185
226
return 0 ;
@@ -206,11 +247,11 @@ class SharedPtr {
206
247
207
248
/* *
208
249
* @brief Boolean conversion operator.
209
- * @return Whether or not the pointer is NULL .
250
+ * @return Whether or not the pointer is null .
210
251
*/
211
252
operator bool () const
212
253
{
213
- return ( _ptr != NULL ) ;
254
+ return _ptr != nullptr ;
214
255
}
215
256
216
257
private:
@@ -231,7 +272,7 @@ class SharedPtr {
231
272
*/
232
273
void decrement_counter ()
233
274
{
234
- if (_ptr != NULL ) {
275
+ if (_ptr != nullptr ) {
235
276
if (core_util_atomic_decr_u32 (_counter, 1 ) == 0 ) {
236
277
delete _counter;
237
278
delete _ptr;
0 commit comments