@@ -71,7 +71,7 @@ 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()
7575 {
7676 }
7777
@@ -86,12 +86,11 @@ class SharedPtr {
8686 * @brief Create new SharedPtr
8787 * @param ptr Pointer to take control over
8888 */
89- SharedPtr (T *ptr): _ptr(ptr), _counter(NULL )
89+ SharedPtr (T *ptr): _ptr(ptr), _counter()
9090 {
9191 // 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 );
9594 }
9695 }
9796
@@ -113,13 +112,25 @@ class SharedPtr {
113112 SharedPtr (const SharedPtr &source): _ptr(source._ptr), _counter(source._counter)
114113 {
115114 // Increment reference counter
116- if (_ptr != NULL ) {
115+ if (_ptr != nullptr ) {
117116 core_util_atomic_incr_u32 (_counter, 1 );
118117 }
119118 }
120119
121120 /* *
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.
123134 * @details Cleanup previous reference and assign new pointer and counter.
124135 * @param source Object being assigned from.
125136 * @return Object being assigned.
@@ -135,14 +146,37 @@ class SharedPtr {
135146 _counter = source.get_counter ();
136147
137148 // Increment new counter
138- if (_ptr != NULL ) {
149+ if (_ptr != nullptr ) {
139150 core_util_atomic_incr_u32 (_counter, 1 );
140151 }
141152 }
142153
143154 return *this ;
144155 }
145156
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+
146180 /* *
147181 * @brief Replaces the managed pointer with a new unmanaged pointer.
148182 * @param[in] ptr the new raw pointer to manage.
@@ -153,21 +187,24 @@ class SharedPtr {
153187 decrement_counter ();
154188
155189 _ptr = ptr;
156- if (ptr != NULL ) {
190+ if (ptr != nullptr ) {
157191 // Allocate counter on the heap, so it can be shared
158- _counter = new uint32_t ;
159- *_counter = 1 ;
192+ _counter = new uint32_t (1 );
160193 } else {
161- _counter = NULL ;
194+ _counter = nullptr ;
162195 }
163196 }
164197
165198 /* *
166- * @brief Replace the managed pointer with a NULL pointer.
199+ * @brief Replace the managed pointer with a null pointer.
167200 */
168201 void reset ()
169202 {
170- reset (NULL );
203+ // Clean up by decrementing counter
204+ decrement_counter ();
205+
206+ _ptr = nullptr ;
207+ _counter = nullptr ;
171208 }
172209
173210 /* *
@@ -186,7 +223,7 @@ class SharedPtr {
186223 */
187224 uint32_t use_count () const
188225 {
189- if (_ptr != NULL ) {
226+ if (_ptr != nullptr ) {
190227 return core_util_atomic_load_u32 (_counter);
191228 } else {
192229 return 0 ;
@@ -213,11 +250,11 @@ class SharedPtr {
213250
214251 /* *
215252 * @brief Boolean conversion operator.
216- * @return Whether or not the pointer is NULL .
253+ * @return Whether or not the pointer is null .
217254 */
218255 operator bool () const
219256 {
220- return ( _ptr != NULL ) ;
257+ return _ptr != nullptr ;
221258 }
222259
223260private:
@@ -238,7 +275,7 @@ class SharedPtr {
238275 */
239276 void decrement_counter ()
240277 {
241- if (_ptr != NULL ) {
278+ if (_ptr != nullptr ) {
242279 if (core_util_atomic_decr_u32 (_counter, 1 ) == 0 ) {
243280 delete _counter;
244281 delete _ptr;
0 commit comments