Browse Source

Implemented a fallback implementation for list reallocation with non-movable types.

David Piuva 10 months ago
parent
commit
9a7b9ceb4a
1 changed files with 13 additions and 5 deletions
  1. 13 5
      Source/DFPSR/collection/List.h

+ 13 - 5
Source/DFPSR/collection/List.h

@@ -56,11 +56,19 @@ private:
 			// Use all available space.
 			uintptr_t availableSize = heap_getAllocationSize(newAllocation.header);
 			heap_setUsedSize(newAllocation.header, availableSize);
-			// To work with element types that do not follow the rule of three, we can copy the data directly to prevent leaking memory from calling a copy constructor instead of a move constructor.
-			memcpy((void*)newElements, (void*)this->impl_elements, this->impl_buffer_length * sizeof(T));
-			//for (intptr_t e = 0; e < this->impl_buffer_length; e++) {
-			//	new (newElements + e) T(std::move(this->impl_elements[e]));
-			//}
+			// Move the data from the old allocation to the new allocation.
+			if (std::is_move_constructible<T>::value) {
+				// If T is move constructible, we do not have to clone the elements.
+				for (intptr_t e = 0; e < this->impl_buffer_length; e++) {
+					new (newElements + e) T(std::move(this->impl_elements[e]));
+				}
+			} else {
+				// If T is not move constructible, we have to create a copy and then destroy the original.
+				for (intptr_t e = 0; e < this->impl_buffer_length; e++) {
+					new (newElements + e) T(this->impl_elements[e]);
+					this->impl_elements[e].~T();
+				}
+			}
 			// Transfer ownership to the new allocation.
 			heap_decreaseUseCount(this->impl_elements);
 			this->impl_elements = newElements;