Browse Source

Added missing move assignment operator for Array (#1091)

Jorrit Rouwe 1 year ago
parent
commit
bf8274c41c
2 changed files with 35 additions and 9 deletions
  1. 26 9
      Jolt/Core/Array.h
  2. 9 0
      UnitTests/Core/ArrayTest.cpp

+ 26 - 9
Jolt/Core/Array.h

@@ -228,16 +228,12 @@ public:
 	}
 	}
 
 
 	/// Move constructor
 	/// Move constructor
-	inline					Array(Array<T, Allocator> &&inRHS) noexcept
+	inline					Array(Array<T, Allocator> &&inRHS) noexcept :
+		Allocator(std::move(inRHS.get_allocator())),
+		mSize(inRHS.mSize),
+		mCapacity(inRHS.mCapacity),
+		mElements(inRHS.mElements)
 	{
 	{
-		destroy();
-
-		get_allocator() = std::move(inRHS.get_allocator());
-
-		mSize = inRHS.mSize;
-		mCapacity = inRHS.mCapacity;
-		mElements = inRHS.mElements;
-
 		inRHS.mSize = 0;
 		inRHS.mSize = 0;
 		inRHS.mCapacity = 0;
 		inRHS.mCapacity = 0;
 		inRHS.mElements = nullptr;
 		inRHS.mElements = nullptr;
@@ -500,6 +496,27 @@ public:
 		return *this;
 		return *this;
 	}
 	}
 
 
+	/// Assignment move operator
+	Array<T, Allocator> &	operator = (Array<T, Allocator> &&inRHS) noexcept
+	{
+		if (static_cast<const void *>(this) != static_cast<const void *>(&inRHS))
+		{
+			destroy();
+
+			get_allocator() = std::move(inRHS.get_allocator());
+
+			mSize = inRHS.mSize;
+			mCapacity = inRHS.mCapacity;
+			mElements = inRHS.mElements;
+
+			inRHS.mSize = 0;
+			inRHS.mCapacity = 0;
+			inRHS.mElements = nullptr;
+		}
+
+		return *this;
+	}
+
 	/// Assignment operator
 	/// Assignment operator
 	Array<T, Allocator> &	operator = (std::initializer_list<T> inRHS)
 	Array<T, Allocator> &	operator = (std::initializer_list<T> inRHS)
 	{
 	{

+ 9 - 0
UnitTests/Core/ArrayTest.cpp

@@ -566,6 +566,15 @@ TEST_SUITE("ArrayTest")
 		CHECK(arr == Array<int>({ 7, 8, 9 }));
 		CHECK(arr == Array<int>({ 7, 8, 9 }));
 	}
 	}
 
 
+	TEST_CASE("TestAssignMove")
+	{
+		Array<int> arr({ 1, 2, 3 });
+		Array<int> arr2({ 4, 5, 6 });
+		arr = std::move(arr2);
+		CHECK(arr == Array<int>({ 4, 5, 6 }));
+		CHECK(arr2.empty());
+	}
+
 	TEST_CASE("TestEraseBegin")
 	TEST_CASE("TestEraseBegin")
 	{
 	{
 		Array<int> arr({ 1, 2, 3 });
 		Array<int> arr({ 1, 2, 3 });