@@ -71,20 +71,19 @@ 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
7878 /* *
7979 * @brief Create new SharedPtr
8080 * @param ptr Pointer to take control over
8181 */
82- SharedPtr (T *ptr): _ptr(ptr), _counter(NULL )
82+ SharedPtr (T *ptr): _ptr(ptr), _counter()
8383 {
8484 // 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 );
8887 }
8988 }
9089
@@ -106,13 +105,25 @@ class SharedPtr {
106105 SharedPtr (const SharedPtr &source): _ptr(source._ptr), _counter(source._counter)
107106 {
108107 // Increment reference counter
109- if (_ptr != NULL ) {
108+ if (_ptr != nullptr ) {
110109 core_util_atomic_incr_u32 (_counter, 1 );
111110 }
112111 }
113112
114113 /* *
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.
116127 * @details Cleanup previous reference and assign new pointer and counter.
117128 * @param source Object being assigned from.
118129 * @return Object being assigned.
@@ -128,14 +139,37 @@ class SharedPtr {
128139 _counter = source.get_counter ();
129140
130141 // Increment new counter
131- if (_ptr != NULL ) {
142+ if (_ptr != nullptr ) {
132143 core_util_atomic_incr_u32 (_counter, 1 );
133144 }
134145 }
135146
136147 return *this ;
137148 }
138149
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+
139173 /* *
140174 * @brief Replaces the managed pointer with a new unmanaged pointer.
141175 * @param[in] ptr the new raw pointer to manage.
@@ -146,21 +180,24 @@ class SharedPtr {
146180 decrement_counter ();
147181
148182 _ptr = ptr;
149- if (ptr != NULL ) {
183+ if (ptr != nullptr ) {
150184 // Allocate counter on the heap, so it can be shared
151- _counter = new uint32_t ;
152- *_counter = 1 ;
185+ _counter = new uint32_t (1 );
153186 } else {
154- _counter = NULL ;
187+ _counter = nullptr ;
155188 }
156189 }
157190
158191 /* *
159- * @brief Replace the managed pointer with a NULL pointer.
192+ * @brief Replace the managed pointer with a null pointer.
160193 */
161194 void reset ()
162195 {
163- reset (NULL );
196+ // Clean up by decrementing counter
197+ decrement_counter ();
198+
199+ _ptr = nullptr ;
200+ _counter = nullptr ;
164201 }
165202
166203 /* *
@@ -179,7 +216,7 @@ class SharedPtr {
179216 */
180217 uint32_t use_count () const
181218 {
182- if (_ptr != NULL ) {
219+ if (_ptr != nullptr ) {
183220 return core_util_atomic_load_u32 (_counter);
184221 } else {
185222 return 0 ;
@@ -206,11 +243,11 @@ class SharedPtr {
206243
207244 /* *
208245 * @brief Boolean conversion operator.
209- * @return Whether or not the pointer is NULL .
246+ * @return Whether or not the pointer is null .
210247 */
211248 operator bool () const
212249 {
213- return ( _ptr != NULL ) ;
250+ return _ptr != nullptr ;
214251 }
215252
216253private:
@@ -231,7 +268,7 @@ class SharedPtr {
231268 */
232269 void decrement_counter ()
233270 {
234- if (_ptr != NULL ) {
271+ if (_ptr != nullptr ) {
235272 if (core_util_atomic_decr_u32 (_counter, 1 ) == 0 ) {
236273 delete _counter;
237274 delete _ptr;
0 commit comments