[Bf-blender-cvs] [a3aaf9d4f96] functions: implement move semantics properly, hopefully

Jacques Lucke noreply at git.blender.org
Sun Feb 10 20:25:03 CET 2019


Commit: a3aaf9d4f960cad39776a8098f85446f9d81b1b3
Author: Jacques Lucke
Date:   Wed Jan 23 14:44:34 2019 +0100
Branches: functions
https://developer.blender.org/rBa3aaf9d4f960cad39776a8098f85446f9d81b1b3

implement move semantics properly, hopefully

===================================================================

M	source/blender/blenlib/BLI_small_vector.hpp

===================================================================

diff --git a/source/blender/blenlib/BLI_small_vector.hpp b/source/blender/blenlib/BLI_small_vector.hpp
index f2b62c73d50..5b123e61571 100644
--- a/source/blender/blenlib/BLI_small_vector.hpp
+++ b/source/blender/blenlib/BLI_small_vector.hpp
@@ -32,58 +32,35 @@ namespace BLI {
 
 		SmallVector(const SmallVector &other)
 		{
-			if (other.is_small()) {
-				this->m_elements = this->m_small_buffer;
-				std::memcpy(this->m_small_buffer, other.m_small_buffer, sizeof(T) * other.m_size);
-			}
-			else {
-				this->m_elements = (T *)std::malloc(sizeof(T) * other.m_capacity);
-				std::memcpy(this->m_elements, other.m_elements, other.m_size);
-			}
-			this->m_capacity = other.m_capacity;
-			this->m_size = other.m_size;
+			this->copy_from_other(other);
 		}
 
 		SmallVector(SmallVector &&other)
 		{
-			if (other.is_small()) {
-				this->m_elements = this->m_small_buffer;
-				std::memcpy(this->m_small_buffer, other.m_small_buffer, sizeof(T) * other.m_size);
-			}
-			else {
-				this->m_elements = other.m_elements;
-			}
-			this->m_capacity = other.m_capacity;
-			this->m_size = other.m_size;
+			this->steal_from_other(std::forward<SmallVector>(other));
 		}
 
 		~SmallVector()
 		{
-			if (!this->is_small()) {
-				std::free(this->m_elements);
-			}
+			this->free_own_buffer();
 		}
 
-		SmallVector &operator=(SmallVector &&other)
+		SmallVector &operator=(const SmallVector &other)
 		{
 			if (this == &other) {
 				return *this;
 			}
 
-			if (!this->is_small()) {
-				std::free(this->m_elements);
-			}
+			this->free_own_buffer();
+			this->copy_from_other(other);
 
-			if (other.is_small()) {
-				this->m_elements = this->m_small_buffer;
-				std::memcpy(this->m_small_buffer, other.m_small_buffer, sizeof(T) * other.m_size);
-			}
-			else {
-				this->m_elements = other.m_elements;
-			}
+			return *this;
+		}
 
-			this->m_capacity = other.m_capacity;
-			this->m_size = other.m_size;
+		SmallVector &operator=(SmallVector &&other)
+		{
+			this->free_own_buffer();
+			this->steal_from_other(std::forward<SmallVector>(other));
 
 			return *this;
 		}
@@ -137,6 +114,47 @@ namespace BLI {
 		{
 			return this->m_elements == this->m_small_buffer;
 		}
+
+		void free_own_buffer()
+		{
+			if (!this->is_small()) {
+				/* Can be nullptr when previously stolen. */
+				if (this->m_elements != nullptr) {
+					std::free(this->m_elements);
+				}
+			}
+		}
+
+		void copy_from_other(const SmallVector &other)
+		{
+			if (other.is_small()) {
+				this->m_elements = this->m_small_buffer;
+				std::memcpy(this->m_small_buffer, other.m_small_buffer, sizeof(T) * other.m_size);
+			}
+			else {
+				this->m_elements = (T *)std::malloc(sizeof(T) * other.m_capacity);
+				std::memcpy(this->m_elements, other.m_elements, other.m_size);
+			}
+
+			this->m_capacity = other.m_capacity;
+			this->m_size = other.m_size;
+		}
+
+		void steal_from_other(SmallVector &&other)
+		{
+			if (other.is_small()) {
+				this->m_elements = this->m_small_buffer;
+				std::memcpy(this->m_small_buffer, other.m_small_buffer, sizeof(T) * other.m_size);
+			}
+			else {
+				this->m_elements = other.m_elements;
+			}
+
+			this->m_capacity = other.m_capacity;
+			this->m_size = other.m_size;
+
+			other.m_elements = nullptr;
+		}
 	};
 
 } /* namespace BLI */
\ No newline at end of file



More information about the Bf-blender-cvs mailing list