- hopefully stabilized the entire shared pointers deal now

Fri, 16 May 2014 21:31:20 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Fri, 16 May 2014 21:31:20 +0300
changeset 770
b04c1e6ca1fb
parent 769
8bb3bed44570
child 771
0f04e34bec54

- hopefully stabilized the entire shared pointers deal now

src/addObjectDialog.cc file | annotate | diff | comparison | revisions
src/glCompiler.cc file | annotate | diff | comparison | revisions
src/ldDocument.cc file | annotate | diff | comparison | revisions
src/ldObject.cc file | annotate | diff | comparison | revisions
src/ldObject.h file | annotate | diff | comparison | revisions
--- a/src/addObjectDialog.cc	Fri May 16 01:22:24 2014 +0300
+++ b/src/addObjectDialog.cc	Fri May 16 21:31:20 2014 +0300
@@ -317,7 +317,7 @@
 static QSharedPointer<T> initObj (LDObjectPtr& obj)
 {
 	if (obj == null)
-		obj = LDObjectPtr (new T);
+		obj = spawn<T>();
 
 	return obj.staticCast<T>();
 }
--- a/src/glCompiler.cc	Fri May 16 01:22:24 2014 +0300
+++ b/src/glCompiler.cc	Fri May 16 21:31:20 2014 +0300
@@ -311,7 +311,7 @@
 {
 //	print ("Compile %1\n", g_objectOrigins[obj]);
 
-	if (obj->document()->isImplicit())
+	if (obj == null || obj->document()->isImplicit())
 		return;
 
 	ObjectVBOInfo info;
--- a/src/ldDocument.cc	Fri May 16 01:22:24 2014 +0300
+++ b/src/ldDocument.cc	Fri May 16 21:31:20 2014 +0300
@@ -996,7 +996,7 @@
 			}
 
 			default:
-				throw LDError (line, "Unknown line code number");
+				throw LDParseError (line, "Unknown line code number");
 		}
 	}
 	catch (LDParseError& e)
--- a/src/ldObject.cc	Fri May 16 01:22:24 2014 +0300
+++ b/src/ldObject.cc	Fri May 16 21:31:20 2014 +0300
@@ -34,31 +34,45 @@
 // List of all LDObjects
 static QMap<long, LDObjectWeakPtr> g_allObjects;
 
-// Temporary resource
-LDObjectPtr g_temporaryLDObject;
+#define LDOBJ_DEFAULT_CTOR(T,BASE) \
+	T :: T (LDObjectPtr* selfptr) : \
+		BASE (selfptr) {}
 
 // =============================================================================
 // LDObject constructors
 //
-LDObject::LDObject() :
+LDObject::LDObject (LDObjectPtr* selfptr) :
 	m_isHidden (false),
 	m_isSelected (false),
 	m_isDestructed (false),
 	m_document (null),
 	qObjListEntry (null)
 {
-	LDObjectPtr selfptr (this, [](LDObject* obj){ obj->finalDelete(); });
+	*selfptr = LDObjectPtr (this, [](LDObject* obj){ obj->finalDelete(); });
 	memset (m_coords, 0, sizeof m_coords);
-	m_self = selfptr;
+	m_self = selfptr->toWeakRef();
 	chooseID();
 	g_allObjects[id()] = self();
 	setRandomColor (QColor::fromHsv (rand() % 360, rand() % 256, rand() % 96 + 128));
+}
 
-	// This prevents the object from being prematurely deleted just because
-	// selfptr falls out of scope before it's caught by spawn()
-	g_temporaryLDObject = selfptr;
+LDSubfile::LDSubfile (LDObjectPtr* selfptr) :
+	LDObject (selfptr)
+{
+	setLinkPointer (self());
 }
 
+LDOBJ_DEFAULT_CTOR (LDEmpty, LDObject)
+LDOBJ_DEFAULT_CTOR (LDError, LDObject)
+LDOBJ_DEFAULT_CTOR (LDLine, LDObject)
+LDOBJ_DEFAULT_CTOR (LDTriangle, LDObject)
+LDOBJ_DEFAULT_CTOR (LDCondLine, LDLine)
+LDOBJ_DEFAULT_CTOR (LDQuad, LDObject)
+LDOBJ_DEFAULT_CTOR (LDVertex, LDObject)
+LDOBJ_DEFAULT_CTOR (LDOverlay, LDObject)
+LDOBJ_DEFAULT_CTOR (LDBFC, LDObject)
+LDOBJ_DEFAULT_CTOR (LDComment, LDObject)
+
 // =============================================================================
 //
 void LDObject::chooseID()
@@ -69,9 +83,6 @@
 	{
 		LDObjectPtr obj = it->toStrongRef();
 
-		if (not obj)
-			fprint (stdout, "obj #%1 wasn't removed properly\n", it.key());
-
 		assert (obj != this);
 		assert (obj != null);
 
@@ -91,8 +102,6 @@
 	setVertex (i, v);
 }
 
-LDError::LDError() {}
-
 // =============================================================================
 //
 String LDComment::asText() const
@@ -246,7 +255,8 @@
 
 // =============================================================================
 //
-LDLine::LDLine (Vertex v1, Vertex v2)
+LDLine::LDLine (LDObjectPtr* selfptr, Vertex v1, Vertex v2) :
+	LDObject (selfptr)
 {
 	setVertex (0, v1);
 	setVertex (1, v2);
@@ -254,7 +264,9 @@
 
 // =============================================================================
 //
-LDQuad::LDQuad (const Vertex& v0, const Vertex& v1, const Vertex& v2, const Vertex& v3)
+LDQuad::LDQuad (LDObjectPtr* selfptr, const Vertex& v0, const Vertex& v1,
+				const Vertex& v2, const Vertex& v3) :
+	LDObject (selfptr)
 {
 	setVertex (0, v0);
 	setVertex (1, v1);
@@ -264,7 +276,9 @@
 
 // =============================================================================
 //
-LDCondLine::LDCondLine (const Vertex& v0, const Vertex& v1, const Vertex& v2, const Vertex& v3)
+LDCondLine::LDCondLine (LDObjectPtr* selfptr, const Vertex& v0, const Vertex& v1,
+						const Vertex& v2, const Vertex& v3) :
+	LDLine (selfptr)
 {
 	setVertex (0, v0);
 	setVertex (1, v1);
--- a/src/ldObject.h	Fri May 16 01:22:24 2014 +0300
+++ b/src/ldObject.h	Fri May 16 21:31:20 2014 +0300
@@ -32,6 +32,8 @@
 	virtual String asText() const override;						\
 	virtual void invert() override;								\
 																\
+	LD##T (LDObjectPtr* selfptr);								\
+																\
 protected:														\
 	friend class QSharedPointer<LD##T>::ExternalRefCount;		\
 
@@ -95,7 +97,7 @@
 			EFirstType = ESubfile
 		};
 
-		LDObject();
+		LDObject (LDObjectPtr* selfptr);
 
 		// This object as LDraw code
 		virtual String				asText() const = 0;
@@ -213,8 +215,9 @@
 inline QSharedPointer<T> spawn (Args... args)
 {
 	static_assert (std::is_base_of<LDObject, T>::value, "spawn may only be used with LDObject-derivatives");
-	LDObject* obj = new T (args...);
-	return obj->self().toStrongRef().staticCast<T>();
+	LDObjectPtr ptr;
+	new T (&ptr, args...);
+	return ptr.staticCast<T>();
 }
 
 NUMERIC_ENUM_OPERATORS (LDObject::Type)
@@ -333,8 +336,8 @@
 	PROPERTY (private,	String,	reason,			setReason,			STOCK_WRITE)
 
 	public:
-		LDError();
-		LDError (String contents, String reason) :
+		LDError (LDObjectPtr* selfptr, String contents, String reason) :
+			LDObject (selfptr),
 			m_contents (contents),
 			m_reason (reason) {}
 };
@@ -374,8 +377,9 @@
 	LDOBJ_NO_MATRIX
 
 	public:
-		LDComment() {}
-		LDComment (String text) : m_text (text) {}
+		LDComment (LDObjectPtr* selfptr, String text) :
+			LDObject (selfptr),
+			m_text (text) {}
 };
 
 using LDCommentPtr = QSharedPointer<LDComment>;
@@ -413,8 +417,8 @@
 		PROPERTY (public, Statement, statement, setStatement, STOCK_WRITE)
 
 	public:
-		LDBFC() {}
-		LDBFC (const LDBFC::Statement type) :
+		LDBFC (LDObjectPtr* selfptr, const LDBFC::Statement type) :
+			LDObject (selfptr),
 			m_statement (type) {}
 
 		// Statement strings
@@ -450,12 +454,6 @@
 
 		Q_DECLARE_FLAGS (InlineFlags, InlineFlag)
 
-		LDSubfile() :
-			LDObject()
-		{
-			setLinkPointer (self());
-		}
-
 		// Inlines this subfile.
 		LDObjectList inlineContents (bool deep, bool render);
 		QList<LDPolygon> inlinePolygons();
@@ -482,8 +480,7 @@
 	LDOBJ_NO_MATRIX
 
 	public:
-		LDLine() {}
-		LDLine (Vertex v1, Vertex v2);
+		LDLine (LDObjectPtr* selfptr, Vertex v1, Vertex v2);
 };
 
 using LDLinePtr = QSharedPointer<LDLine>;
@@ -505,8 +502,8 @@
 	LDOBJ_NO_MATRIX
 
 	public:
-		LDCondLine() {}
-		LDCondLine (const Vertex& v0, const Vertex& v1, const Vertex& v2, const Vertex& v3);
+		LDCondLine (LDObjectPtr* selfptr, const Vertex& v0, const Vertex& v1,
+					const Vertex& v2, const Vertex& v3);
 		LDLinePtr demote();
 };
 
@@ -530,8 +527,8 @@
 	LDOBJ_NO_MATRIX
 
 	public:
-		LDTriangle() {}
-		LDTriangle (Vertex v0, Vertex v1, Vertex v2)
+		LDTriangle (LDObjectPtr* selfptr, Vertex v0, Vertex v1, Vertex v2) :
+			LDObject (selfptr)
 		{
 			setVertex (0, v0);
 			setVertex (1, v1);
@@ -558,8 +555,8 @@
 	LDOBJ_NO_MATRIX
 
 	public:
-		LDQuad() {}
-		LDQuad (const Vertex& v0, const Vertex& v1, const Vertex& v2, const Vertex& v3);
+		LDQuad (LDObjectPtr* selfptr, const Vertex& v0, const Vertex& v1,
+				const Vertex& v2, const Vertex& v3);
 
 		// Split this quad into two triangles (note: heap-allocated)
 		QList<LDTrianglePtr> splitToTriangles();
@@ -586,8 +583,6 @@
 	LDOBJ_NO_MATRIX
 
 	public:
-		LDVertex() {}
-
 		Vertex pos;
 };
 

mercurial