From 80b20450bbbf8e35a83167c468b39f65044f5ff2 Mon Sep 17 00:00:00 2001
From: Antoine Fontaine <antoine.fontaine@epfl.ch>
Date: Sun, 28 Mar 2021 13:49:11 +0200
Subject: [PATCH] radiant/brush: use std::shared_ptr

---
 radiant/brush.h       | 15 +++++++--------
 radiant/brushtokens.h | 21 ++++++++++-----------
 radiant/brushxml.h    |  2 +-
 radiant/csg.cpp       | 40 ++++++++++++++--------------------------
 4 files changed, 32 insertions(+), 46 deletions(-)

diff --git a/radiant/brush.h b/radiant/brush.h
index 01a61fec..7eeeb1df 100644
--- a/radiant/brush.h
+++ b/radiant/brush.h
@@ -1408,8 +1408,7 @@ inline bool plane3_inside( const Plane3& self, const Plane3& other, bool selfIsL
 	return true;
 }
 
-typedef SmartPointer<Face> FaceSmartPointer;
-typedef std::vector<FaceSmartPointer> Faces;
+typedef std::vector<std::shared_ptr<Face>> Faces;
 
 /// \brief Returns the unique-id of the edge adjacent to \p faceVertex in the edge-pair for the set of \p faces.
 inline FaceVertexId next_edge( const Faces& faces, FaceVertexId faceVertex ){
@@ -1888,24 +1887,24 @@ public:
 	}
 
 	/// \brief Appends a copy of \p face to the end of the face list.
-	Face* addFace( const Face& face ){
+	std::shared_ptr<Face> addFace( const Face& face ){
 		if ( m_faces.size() == c_brush_maxFaces ) {
 			return 0;
 		}
 		undoSave();
-		push_back( FaceSmartPointer( new Face( face, this ) ) );
+		push_back( std::make_shared<Face>( face, this ) );
 		m_faces.back()->setDetail( isDetail() );
 		planeChanged();
 		return m_faces.back();
 	}
 
 	/// \brief Appends a new face constructed from the parameters to the end of the face list.
-	Face* addPlane( const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, const TextureProjection& projection ){
+	std::shared_ptr<Face> addPlane( const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, const TextureProjection& projection ){
 		if ( m_faces.size() == c_brush_maxFaces ) {
 			return 0;
 		}
 		undoSave();
-		push_back( FaceSmartPointer( new Face( p0, p1, p2, shader, projection, this ) ) );
+		push_back( std::make_shared<Face>( p0, p1, p2, shader, projection, this ) );
 		m_faces.back()->setDetail( isDetail() );
 		planeChanged();
 		return m_faces.back();
@@ -1949,11 +1948,11 @@ public:
 		return m_faces.end();
 	}
 
-	Face* back(){
+	std::shared_ptr<Face> back(){
 		return m_faces.back();
 	}
 
-	const Face* back() const {
+	const std::shared_ptr<Face> back() const {
 		return m_faces.back();
 	}
 
diff --git a/radiant/brushtokens.h b/radiant/brushtokens.h
index 9b4d4fab..af95360a 100644
--- a/radiant/brushtokens.h
+++ b/radiant/brushtokens.h
@@ -509,59 +509,58 @@ bool importTokens( Tokeniser& tokeniser ){
 
 		tokeniser.ungetToken();
 
-		m_brush.push_back( FaceSmartPointer( new Face( &m_brush ) ) );
+		std::shared_ptr<Face> face = std::make_shared<Face>( &m_brush );
+		m_brush.push_back( face );
 
 		//!todo BP support
 		tokeniser.nextLine();
 
-		Face& face = *m_brush.back();
-
 		switch ( Brush::m_type )
 		{
 		case eBrushTypeDoom3:
 		{
-			Doom3FaceTokenImporter importer( face );
+			Doom3FaceTokenImporter importer( *face );
 			RETURN_FALSE_IF_FAIL( importer.importTokens( tokeniser ) );
 		}
 		break;
 		case eBrushTypeQuake4:
 		{
-			Quake4FaceTokenImporter importer( face );
+			Quake4FaceTokenImporter importer( *face );
 			RETURN_FALSE_IF_FAIL( importer.importTokens( tokeniser ) );
 		}
 		break;
 		case eBrushTypeQuake2:
 		{
-			Quake2FaceTokenImporter importer( face );
+			Quake2FaceTokenImporter importer( *face );
 			RETURN_FALSE_IF_FAIL( importer.importTokens( tokeniser ) );
 		}
 		break;
 		case eBrushTypeQuake3:
 		{
-			Quake3FaceTokenImporter importer( face );
+			Quake3FaceTokenImporter importer( *face );
 			RETURN_FALSE_IF_FAIL( importer.importTokens( tokeniser ) );
 		}
 		break;
 		case eBrushTypeQuake3BP:
 		{
-			Quake3BPFaceTokenImporter importer( face );
+			Quake3BPFaceTokenImporter importer( *face );
 			RETURN_FALSE_IF_FAIL( importer.importTokens( tokeniser ) );
 		}
 		break;
 		case eBrushTypeQuake:
 		{
-			QuakeFaceTokenImporter importer( face );
+			QuakeFaceTokenImporter importer( *face );
 			RETURN_FALSE_IF_FAIL( importer.importTokens( tokeniser ) );
 		}
 		break;
 		case eBrushTypeHalfLife:
 		{
-			HalfLifeFaceTokenImporter importer( face );
+			HalfLifeFaceTokenImporter importer( *face );
 			RETURN_FALSE_IF_FAIL( importer.importTokens( tokeniser ) );
 		}
 		break;
 		}
-		face.planeChanged();
+		face->planeChanged();
 	}
 	if ( Brush::m_type == eBrushTypeQuake3BP || Brush::m_type == eBrushTypeDoom3 || Brush::m_type == eBrushTypeQuake4 ) {
 		tokeniser.nextLine();
diff --git a/radiant/brushxml.h b/radiant/brushxml.h
index c1fac55b..d7add408 100644
--- a/radiant/brushxml.h
+++ b/radiant/brushxml.h
@@ -351,7 +351,7 @@ void pushElement( const XMLElement& element ){
 	case xml_state_t::eBrush:
 		ASSERT_MESSAGE( strcmp( element.name(), "plane" ) == 0, "parse error" );
 		m_xml_state.push_back( xml_state_t::eFace );
-		m_brush.push_back( FaceSmartPointer( new Face( &m_brush ) ) );
+		m_brush.push_back( std::make_shared<Face>( &m_brush ) );
 		constructor( faceImporter(), makeReference( *m_brush.back() ) );
 		m_brush.planeChanged();
 		m_brush.shaderChanged();
diff --git a/radiant/csg.cpp b/radiant/csg.cpp
index 44fefdc8..337b3941 100644
--- a/radiant/csg.cpp
+++ b/radiant/csg.cpp
@@ -33,7 +33,7 @@
 void Face_makeBrush( Face& face, const Brush& brush, brush_vector_t& out, float offset ){
 	if ( face.contributes() ) {
 		out.push_back( new Brush( brush ) );
-		Face* newFace = out.back()->addFace( face );
+		std::shared_ptr<Face> newFace = out.back()->addFace( face );
 		if ( newFace != 0 ) {
 			newFace->flipWinding();
 			newFace->getPlane().offset( offset );
@@ -47,7 +47,7 @@ void Face_makeRoom( Face &face, const Brush &brush, brush_vector_t &out, float o
 		face.getPlane().offset( offset );
 		out.push_back( new Brush( brush ) );
 		face.getPlane().offset( -offset );
-		Face* newFace = out.back()->addFace( face );
+		std::shared_ptr<Face> newFace = out.back()->addFace( face );
 		if ( newFace != 0 ) {
 			newFace->flipWinding();
 			newFace->planeChanged();
@@ -204,15 +204,6 @@ inline Dereference<Functor> makeDereference( const Functor& functor ){
 	return Dereference<Functor>( functor );
 }
 
-typedef Face* FacePointer;
-const FacePointer c_nullFacePointer = 0;
-
-template<typename Predicate>
-Face* Brush_findIf( const Brush& brush, const Predicate& predicate ){
-	Brush::const_iterator i = std::find_if( brush.begin(), brush.end(), makeDereference( predicate ) );
-	return i == brush.end() ? c_nullFacePointer : *i; // uses c_nullFacePointer instead of 0 because otherwise gcc 4.1 attempts conversion to int
-}
-
 template<typename Caller>
 class BindArguments1
 {
@@ -262,7 +253,6 @@ typedef Function<bool ( const Face &, const Plane3 &, bool ), Face_testPlane> Fa
 /// \li flipped && brush is FRONT or ON
 bool Brush_testPlane( const Brush& brush, const Plane3& plane, bool flipped ){
 	brush.evaluateBRep();
-#if 1
 	for ( Brush::const_iterator i( brush.begin() ); i != brush.end(); ++i )
 	{
 		if ( Face_testPlane( *( *i ), plane, flipped ) ) {
@@ -270,9 +260,6 @@ bool Brush_testPlane( const Brush& brush, const Plane3& plane, bool flipped ){
 		}
 	}
 	return true;
-#else
-	return Brush_findIf( brush, bindArguments( FaceTestPlane(), makeReference( plane ), flipped ) ) == 0;
-#endif
 }
 
 brushsplit_t Brush_classifyPlane( const Brush& brush, const Plane3& plane ){
@@ -293,24 +280,24 @@ bool Brush_subtract( const Brush& brush, const Brush& other, brush_vector_t& ret
 		fragments.reserve( other.size() );
 		Brush back( brush );
 
-		for ( Brush::const_iterator i( other.begin() ); i != other.end(); ++i )
+		for ( const std::shared_ptr<Face>& b : other )
 		{
-			if ( ( *i )->contributes() ) {
-				brushsplit_t split = Brush_classifyPlane( back, ( *i )->plane3() );
+			if ( b->contributes() ) {
+				brushsplit_t split = Brush_classifyPlane( back, b->plane3() );
 				if ( split.counts[ePlaneFront] != 0
 					 && split.counts[ePlaneBack] != 0 ) {
 					fragments.push_back( new Brush( back ) );
-					Face* newFace = fragments.back()->addFace( *( *i ) );
-					if ( newFace != 0 ) {
+					std::shared_ptr<Face> newFace = fragments.back()->addFace( *b );
+					if ( newFace != nullptr ) {
 						newFace->flipWinding();
 					}
-					back.addFace( *( *i ) );
+					back.addFace( *b );
 				}
 				else if ( split.counts[ePlaneBack] == 0 ) {
-					for ( brush_vector_t::iterator i = fragments.begin(); i != fragments.end(); ++i )
-					{
-						delete( *i );
+					for ( Brush *i : fragments ) {
+						delete( i );
 					}
+					fragments.clear();
 					return false;
 				}
 			}
@@ -448,7 +435,8 @@ void post( const scene::Path& path, scene::Instance& instance ) const {
 						NodeSmartReference node( ( new BrushNode() )->node() );
 						Brush* fragment = Node_getBrush( node );
 						fragment->copy( *brush );
-						Face* newFace = fragment->addPlane( m_p0, m_p1, m_p2, m_shader, m_projection );
+						std::shared_ptr<Face> newFace =
+							fragment->addPlane( m_p0, m_p1, m_p2, m_shader, m_projection );
 						if ( newFace != 0 && m_split != eFront ) {
 							newFace->flipWinding();
 						}
@@ -463,7 +451,7 @@ void post( const scene::Path& path, scene::Instance& instance ) const {
 						}
 					}
 
-					Face* newFace = brush->addPlane( m_p0, m_p1, m_p2, m_shader, m_projection );
+					std::shared_ptr<Face> newFace = brush->addPlane( m_p0, m_p1, m_p2, m_shader, m_projection );
 					if ( newFace != 0 && m_split == eFront ) {
 						newFace->flipWinding();
 					}
-- 
2.39.5