Browse Source

Change `JoltShapeInstance3D` to use move semantics

Mikael Hermansson 4 months ago
parent
commit
92da11f69c

+ 7 - 14
modules/jolt_physics/shapes/jolt_shape_instance_3d.cpp

@@ -42,12 +42,11 @@ JoltShapeInstance3D::ShapeReference::ShapeReference(JoltShapedObject3D *p_parent
 	}
 }
 
-JoltShapeInstance3D::ShapeReference::ShapeReference(const ShapeReference &p_other) :
+JoltShapeInstance3D::ShapeReference::ShapeReference(ShapeReference &&p_other) :
 		parent(p_other.parent),
 		shape(p_other.shape) {
-	if (shape != nullptr) {
-		shape->add_owner(parent);
-	}
+	p_other.parent = nullptr;
+	p_other.shape = nullptr;
 }
 
 JoltShapeInstance3D::ShapeReference::~ShapeReference() {
@@ -56,16 +55,10 @@ JoltShapeInstance3D::ShapeReference::~ShapeReference() {
 	}
 }
 
-JoltShapeInstance3D::ShapeReference &JoltShapeInstance3D::ShapeReference::operator=(const ShapeReference &p_other) {
-	if (shape != nullptr) {
-		shape->remove_owner(parent);
-	}
-
-	parent = p_other.parent;
-	shape = p_other.shape;
-
-	if (shape != nullptr) {
-		shape->add_owner(parent);
+JoltShapeInstance3D::ShapeReference &JoltShapeInstance3D::ShapeReference::operator=(ShapeReference &&p_other) {
+	if (this != &p_other) {
+		SWAP(parent, p_other.parent);
+		SWAP(shape, p_other.shape);
 	}
 
 	return *this;

+ 5 - 6
modules/jolt_physics/shapes/jolt_shape_instance_3d.h

@@ -40,20 +40,19 @@ class JoltShapedObject3D;
 class JoltShape3D;
 
 class JoltShapeInstance3D {
-	// This RAII helper exists solely to avoid needing to maintain copy construction/assignment in the shape instance.
-	// Ideally this would be move-only instead, but Godot's containers don't support that at the moment.
+	// This RAII helper exists solely to avoid needing to maintain move construction/assignment in `JoltShapeInstance3D`.
 	struct ShapeReference {
 		JoltShapedObject3D *parent = nullptr;
 		JoltShape3D *shape = nullptr;
 
 		ShapeReference() = default;
 		ShapeReference(JoltShapedObject3D *p_parent, JoltShape3D *p_shape);
-		ShapeReference(const ShapeReference &p_other);
-		ShapeReference(ShapeReference &&p_other) = delete;
+		ShapeReference(const ShapeReference &p_other) = delete;
+		ShapeReference(ShapeReference &&p_other);
 		~ShapeReference();
 
-		ShapeReference &operator=(const ShapeReference &p_other);
-		ShapeReference &operator=(ShapeReference &&p_other) = delete;
+		ShapeReference &operator=(const ShapeReference &p_other) = delete;
+		ShapeReference &operator=(ShapeReference &&p_other);
 
 		JoltShape3D *operator*() const { return shape; }
 		JoltShape3D *operator->() const { return shape; }