Made all LDObject constructors protected. Emplacement is now the only way to create objects.

Sun, 29 Jan 2017 21:17:43 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 29 Jan 2017 21:17:43 +0200
changeset 1081
47cde4087cc5
parent 1080
6dac2d52bd9a
child 1082
1738bdaf36d6

Made all LDObject constructors protected. Emplacement is now the only way to create objects.

src/dialogs/newpartdialog.cpp file | annotate | diff | comparison | revisions
src/ldObject.cpp file | annotate | diff | comparison | revisions
src/ldObject.h file | annotate | diff | comparison | revisions
src/toolsets/algorithmtoolset.cpp file | annotate | diff | comparison | revisions
--- a/src/dialogs/newpartdialog.cpp	Sun Jan 29 21:07:15 2017 +0200
+++ b/src/dialogs/newpartdialog.cpp	Sun Jan 29 21:17:43 2017 +0200
@@ -68,18 +68,16 @@
 
 void NewPartDialog::fillHeader (LDDocument* newdoc) const
 {
-	LDObjectList objs;
-	objs << new LDComment (title());
-	objs << new LDComment ("Name: <untitled>.dat");
-	objs << new LDComment ("Author: " + author());
-	objs << new LDComment ("!LDRAW_ORG Unofficial_Part");
+	newdoc->emplace<LDComment>(title());
+	newdoc->emplace<LDComment>("Name: <untitled>.dat");
+	newdoc->emplace<LDComment>("Author: " + author());
+	newdoc->emplace<LDComment>("!LDRAW_ORG Unofficial_Part");
 	QString license = preferredLicenseText();
 
 	if (not license.isEmpty())
-		objs << new LDComment(license);
+		newdoc->emplace<LDComment>(license);
 
-	objs << new LDEmpty();
-	objs << new LDBfc (getWinding());
-	objs << new LDEmpty();
-	newdoc->addObjects (objs);
+	newdoc->emplace<LDEmpty>();
+	newdoc->emplace<LDBfc>(getWinding());
+	newdoc->emplace<LDEmpty>();
 }
--- a/src/ldObject.cpp	Sun Jan 29 21:07:15 2017 +0200
+++ b/src/ldObject.cpp	Sun Jan 29 21:17:43 2017 +0200
@@ -603,7 +603,7 @@
 	}
 
 	// Not inverted, thus prefix it with a new invertnext.
-	this->model()->insertObject (idx, new LDBfc (BfcStatement::InvertNext));
+	this->model()->emplaceAt<LDBfc>(idx, BfcStatement::InvertNext);
 }
 
 // =============================================================================
@@ -643,13 +643,12 @@
 //
 LDLine* LDCondLine::becomeEdgeLine()
 {
-	LDLine* replacement = new LDLine;
+	LDLine* replacement = model()->emplaceReplacement<LDLine>(this);
 
 	for (int i = 0; i < replacement->numVertices(); ++i)
 		replacement->setVertex (i, vertex (i));
 
 	replacement->setColor (color());
-	model()->replace(this, replacement);
 	return replacement;
 }
 
--- a/src/ldObject.h	Sun Jan 29 21:07:15 2017 +0200
+++ b/src/ldObject.h	Sun Jan 29 21:17:43 2017 +0200
@@ -28,7 +28,6 @@
 #define LDOBJ(T)												\
 public:															\
 	static constexpr LDObjectType SubclassType = OBJ_##T;		\
-	LD##T (Model* model = nullptr);								\
 																\
 	virtual LDObjectType type() const override					\
 	{															\
@@ -37,6 +36,9 @@
 																\
 	virtual QString asText() const override;					\
 	virtual void invert() override;								\
+protected:														\
+	friend class Model;											\
+	LD##T (Model* model = nullptr);								\
 
 #define LDOBJ_NAME(N)          public: virtual QString typeName() const override { return #N; }
 #define LDOBJ_VERTICES(V)      public: virtual int numVertices() const override { return V; }
@@ -95,8 +97,6 @@
     Q_OBJECT
 
 public:
-    LDObject (Model* model = nullptr);
-
 	virtual QString asText() const = 0; // This object as LDraw code
     LDColor color() const;
 	virtual LDColor defaultColor() const = 0; // What color does the object default to?
@@ -136,6 +136,7 @@
 
 protected:
 	friend class Model;
+	LDObject (Model* model = nullptr);
 	virtual ~LDObject();
 
 private:
@@ -162,15 +163,16 @@
 	Vertex m_position;
 
 public:
-	LDMatrixObject (Model* model = nullptr);
-	LDMatrixObject (const Matrix& transformationMatrix, const Vertex& pos, Model* model = nullptr);
-
 	const Vertex& position() const;
 	void setCoordinate (const Axis ax, double value);
 	void setPosition (const Vertex& a);
 	void setTransformationMatrix (const Matrix& value);
 	const Matrix& transformationMatrix() const;
 
+protected:
+	LDMatrixObject (Model* model = nullptr);
+	LDMatrixObject (const Matrix& transformationMatrix, const Vertex& pos, Model* model = nullptr);
+
 private:
 	Matrix m_transformationMatrix;
 };
@@ -191,12 +193,14 @@
 	LDOBJ_NO_MATRIX
 
 public:
-	LDError (QString contents, QString reason, Model* model = nullptr);
 	QString reason() const;
 	QString contents() const;
 	QString fileReferenced() const;
 	void setFileReferenced (QString value);
 
+protected:
+	LDError (QString contents, QString reason, Model* model = nullptr);
+
 private:
 	QString m_fileReferenced; // If this error was caused by inability to open a file, what file was that?
 	QString m_contents; // The LDraw code that was being parsed
@@ -231,10 +235,12 @@
 	LDOBJ_NO_MATRIX
 
 public:
-	LDComment (QString text, Model* model = nullptr);
 	QString text() const;
 	void setText (QString value);
 
+protected:
+	LDComment (QString text, Model* model = nullptr);
+
 private:
 	QString m_text;
 };
@@ -270,14 +276,15 @@
 	LDOBJ_NO_MATRIX
 
 public:
-    LDBfc (const BfcStatement type, Model* model = nullptr);
-
 	BfcStatement statement() const;
 	void setStatement (BfcStatement value);
 	QString statementToString() const;
 
 	static QString statementToString (BfcStatement statement);
 
+protected:
+	LDBfc (const BfcStatement type, Model* model = nullptr);
+
 private:
 	BfcStatement m_statement;
 };
@@ -298,8 +305,6 @@
 	LDOBJ_HAS_MATRIX
 
 public:
-	LDSubfileReference(LDDocument* reference, const Matrix& transformationMatrix, const Vertex& position, Model* model = nullptr);
-
 	// Inlines this subfile.
 	LDDocument* fileInfo() const;
 	virtual void getVertices (QSet<Vertex>& verts) const override;
@@ -308,6 +313,9 @@
 	void setFileInfo (LDDocument* fileInfo);
 	int triangleCount() const override;
 
+protected:
+	LDSubfileReference(LDDocument* reference, const Matrix& transformationMatrix, const Vertex& position, Model* model = nullptr);
+
 private:
 	LDDocument* m_fileInfo;
 };
@@ -327,7 +335,7 @@
 	LDOBJ_SCEMANTIC
 	LDOBJ_NO_MATRIX
 
-public:
+protected:
 	LDLine (Vertex v1, Vertex v2, Model* model = nullptr);
 };
 
@@ -347,8 +355,10 @@
 	LDOBJ_NO_MATRIX
 
 public:
+	LDLine* becomeEdgeLine();
+
+protected:
 	LDCondLine (const Vertex& v0, const Vertex& v1, const Vertex& v2, const Vertex& v3, Model* model = nullptr);
-	LDLine* becomeEdgeLine();
 };
 
 //
@@ -369,8 +379,10 @@
 	LDOBJ_NO_MATRIX
 
 public:
+	int triangleCount() const override;
+
+protected:
 	LDTriangle (Vertex const& v1, Vertex const& v2, Vertex const& v3, Model* model = nullptr);
-	int triangleCount() const override;
 };
 
 //
@@ -390,9 +402,10 @@
 	LDOBJ_NO_MATRIX
 
 public:
-	LDQuad (const Vertex& v1, const Vertex& v2, const Vertex& v3, const Vertex& v4, Model* model = nullptr);
+	int triangleCount() const override;
 
-	int triangleCount() const override;
+protected:
+	LDQuad (const Vertex& v1, const Vertex& v2, const Vertex& v3, const Vertex& v4, Model* model = nullptr);
 };
 
 //
@@ -443,10 +456,12 @@
 	LDOBJ_NO_MATRIX
 
 public:
-	LDBezierCurve (const Vertex& v0, const Vertex& v1, const Vertex& v2, const Vertex& v3, Model* model = nullptr);
 	Vertex pointAt (qreal t) const;
 	void rasterize(Model& model, int segments);
 	QVector<LDPolygon> rasterizePolygons (int segments);
+
+protected:
+	LDBezierCurve (const Vertex& v0, const Vertex& v1, const Vertex& v2, const Vertex& v3, Model* model = nullptr);
 };
 
 enum
--- a/src/toolsets/algorithmtoolset.cpp	Sun Jan 29 21:07:15 2017 +0200
+++ b/src/toolsets/algorithmtoolset.cpp	Sun Jan 29 21:17:43 2017 +0200
@@ -344,12 +344,6 @@
 	if (not dlg->exec())
 		return;
 
-	// Create the comment object based on input
-	LDComment* comment = new LDComment (format ("!HISTORY %1 [%2] %3",
-		ui->m_date->date().toString ("yyyy-MM-dd"),
-		ui->m_username->text(),
-		ui->m_comment->text()));
-
 	// Find a spot to place the new comment
 	for (obj = currentDocument()->getObject (0);
 		obj and obj->next() and not obj->next()->isScemantic();
@@ -367,12 +361,17 @@
 	}
 
 	int idx = obj ? obj->lineNumber() : 0;
-	currentDocument()->insertObject (idx++, comment);
+
+	// Create the comment object based on input
+	currentDocument()->emplaceAt<LDComment>(idx++, format("!HISTORY %1 [%2] %3",
+	    ui->m_date->date().toString ("yyyy-MM-dd"),
+	    ui->m_username->text(),
+	    ui->m_comment->text()));
 
 	// If we're adding a history line right before a scemantic object, pad it
 	// an empty line
 	if (obj and obj->next() and obj->next()->isScemantic())
-		currentDocument()->insertObject (idx, new LDEmpty);
+		currentDocument()->emplaceAt<LDEmpty>(idx);
 
 	m_window->buildObjectList();
 	delete ui;

mercurial