Removed the LDSpawn function and replaced its last use cases.

Sun, 29 Jan 2017 21:07:15 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 29 Jan 2017 21:07:15 +0200
changeset 1080
6dac2d52bd9a
parent 1079
67c6e5d32e68
child 1081
47cde4087cc5

Removed the LDSpawn function and replaced its last use cases.

src/ldObject.cpp file | annotate | diff | comparison | revisions
src/ldObject.h file | annotate | diff | comparison | revisions
src/primitives.cpp file | annotate | diff | comparison | revisions
src/primitives.h file | annotate | diff | comparison | revisions
--- a/src/ldObject.cpp	Sun Jan 29 21:02:11 2017 +0200
+++ b/src/ldObject.cpp	Sun Jan 29 21:07:15 2017 +0200
@@ -929,9 +929,8 @@
 
 	for (LDPolygon& poly : polygons)
 	{
-		LDLine* line = LDSpawn<LDLine> (poly.vertices[0], poly.vertices[1]);
+		LDLine* line = model.emplace<LDLine>(poly.vertices[0], poly.vertices[1]);
 		line->setColor (poly.color);
-		model.addObject(line);
 	}
 }
 
--- a/src/ldObject.h	Sun Jan 29 21:02:11 2017 +0200
+++ b/src/ldObject.h	Sun Jan 29 21:07:15 2017 +0200
@@ -148,22 +148,6 @@
 	Vertex m_coords[4];
 };
 
-template<typename T, typename... Args>
-T* LDSpawn (Args... args)
-{
-	static_assert (std::is_base_of<LDObject, T>::value,
-		"spawn may only be used with LDObject-derivatives");
-	T* result = new T (args..., nullptr);
-
-	// Set default color. Relying on virtual functions, this cannot be done in the c-tor.
-	// TODO: store -1 as the default color
-	if (result->isColored())
-		result->setColor (result->defaultColor());
-
-	return result;
-}
-
-//
 //
 // Common code for objects with matrices. This class is multiple-derived in
 // and thus not used directly other than as a common storage point for matrices
--- a/src/primitives.cpp	Sun Jan 29 21:02:11 2017 +0200
+++ b/src/primitives.cpp	Sun Jan 29 21:07:15 2017 +0200
@@ -247,9 +247,8 @@
 	categoriesFile.close();
 }
 
-LDObjectList PrimitiveModel::generateBody() const
+void PrimitiveModel::generateBody(Model& model) const
 {
-	LDObjectList objects;
 	QVector<int> conditionalLineSegments;
 	QVector<QLineF> circle = makeCircle(segments, divisions, 1);
 
@@ -263,12 +262,11 @@
 		switch (type)
 		{
 		case Circle:
-			{
-				LDLine* line = LDSpawn<LDLine>();
+		    {
+			    LDLine* line = model.emplace<LDLine>();
 				line->setVertex(0, Vertex {x0, 0.0f, z0});
 				line->setVertex(1, Vertex {x1, 0.0f, z1});
 				line->setColor(EdgeColor);
-				objects.append(line);
 			}
 			break;
 
@@ -314,14 +312,12 @@
 				Vertex v1 = {x1, y1, z1};
 				Vertex v2 = {x2, y2, z2};
 				Vertex v3 = {x3, y3, z3};
-				LDQuad* quad = LDSpawn<LDQuad>(v0, v1, v2, v3);
+				LDQuad* quad = model.emplace<LDQuad>(v0, v1, v2, v3);
 				quad->setColor(MainColor);
 
 				if (type == Cylinder)
 					quad->invert();
 
-				objects.append(quad);
-
 				if (type == Cylinder or type == Cone)
 					conditionalLineSegments.append(i);
 			}
@@ -348,12 +344,11 @@
 
 				// Disc negatives need to go the other way around, otherwise
 				// they'll end up upside-down.
-				LDTriangle* segment = LDSpawn<LDTriangle>();
+				LDTriangle* segment = model.emplace<LDTriangle>();
 				segment->setColor(MainColor);
 				segment->setVertex(type == Disc ? 0 : 2, v0);
 				segment->setVertex(1, v1);
 				segment->setVertex(type == Disc ? 2 : 0, v2);
-				objects.append(segment);
 			}
 			break;
 		}
@@ -381,16 +376,13 @@
 			v0 = {v0[X] * ringNumber, 1.0, v0[Z] * ringNumber};
 		}
 
-		LDCondLine* line = LDSpawn<LDCondLine>();
+		LDCondLine* line = model.emplace<LDCondLine>();
 		line->setColor(EdgeColor);
 		line->setVertex(0, v0);
 		line->setVertex(1, v1);
 		line->setVertex(2, v2);
 		line->setVertex(3, v3);
-		objects.append(line);
 	}
-
-	return objects;
 }
 
 
@@ -482,20 +474,17 @@
 		author = format("%1 [%2]", m_config->defaultName(), m_config->defaultUser());
 	}
 
-	LDObjectList objs;
-
-	objs.append(LDSpawn<LDComment>(description));
-	objs.append(LDSpawn<LDComment>(format("Name: %1", fileName)));
-	objs.append(LDSpawn<LDComment>(format("Author: %1", author)));
-	objs.append(LDSpawn<LDComment>(format("!LDRAW_ORG Unofficial_%1Primitive", hires ?  "48_" : "")));
-	objs.append(LDSpawn<LDComment>(license));
-	objs.append(LDSpawn<LDEmpty>());
-	objs.append(LDSpawn<LDBfc>(BfcStatement::CertifyCCW));
-	objs.append(LDSpawn<LDEmpty>());
 	document->openForEditing();
 	document->history()->setIgnoring(false);
-	document->addObjects(objs);
-	document->addObjects(spec.generateBody());
+	document->emplace<LDComment>(description);
+	document->emplace<LDComment>(format("Name: %1", fileName));
+	document->emplace<LDComment>(format("Author: %1", author));
+	document->emplace<LDComment>(format("!LDRAW_ORG Unofficial_%1Primitive", hires ?  "48_" : ""));
+	document->emplace<LDComment>(license);
+	document->emplace<LDEmpty>();
+	document->emplace<LDBfc>(BfcStatement::CertifyCCW);
+	document->emplace<LDEmpty>();
+	spec.generateBody(*document);
 	document->addHistoryStep();
 	return document;
 }
--- a/src/primitives.h	Sun Jan 29 21:02:11 2017 +0200
+++ b/src/primitives.h	Sun Jan 29 21:07:15 2017 +0200
@@ -22,6 +22,7 @@
 #include <QTreeWidgetItem>
 #include <QDirIterator>
 #include "main.h"
+#include "model.h"
 
 class LDDocument;
 class Ui_GeneratePrimitiveDialog;
@@ -51,7 +52,7 @@
 	int ringNumber;
 
 	QString typeName() const;
-	LDObjectList generateBody() const;
+	void generateBody(Model& model) const;
 	static QString typeName(Type type);
 	QString makeFileName() const;
 };

mercurial