- refactor

Sat, 26 Jul 2014 03:43:37 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sat, 26 Jul 2014 03:43:37 +0300
changeset 847
274a7fac44fc
parent 846
e4ad109f33c6
child 848
68410477c8bb

- refactor

src/actions.cc file | annotate | diff | comparison | revisions
src/addObjectDialog.cc file | annotate | diff | comparison | revisions
src/basics.h 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
src/macros.h file | annotate | diff | comparison | revisions
src/mainWindow.cc file | annotate | diff | comparison | revisions
src/primitives.cc file | annotate | diff | comparison | revisions
--- a/src/actions.cc	Sun Jul 20 05:01:51 2014 +0300
+++ b/src/actions.cc	Sat Jul 26 03:43:37 2014 +0300
@@ -90,11 +90,11 @@
 
 	newFile();
 
-	const LDBFC::Statement bfctype =
-		ui.rb_bfc_ccw->isChecked() ? LDBFC::CertifyCCW :
-		ui.rb_bfc_cw->isChecked()  ? LDBFC::CertifyCW : LDBFC::NoCertify;
+	BFCStatement const bfctype = ui.rb_bfc_ccw->isChecked() ? BFCStatement::CertifyCCW
+							   : ui.rb_bfc_cw->isChecked()  ? BFCStatement::CertifyCW
+							   : BFCStatement::NoCertify;
 
-	const QString license =
+	QString const license =
 		ui.rb_license_ca->isChecked()    ? g_CALicense :
 		ui.rb_license_nonca->isChecked() ? g_nonCALicense : "";
 
@@ -110,9 +110,7 @@
 	objs << spawn<LDEmpty>();
 	objs << spawn<LDBFC> (bfctype);
 	objs << spawn<LDEmpty>();
-
 	getCurrentDocument()->addObjects (objs);
-
 	doFullRefresh();
 }
 
@@ -741,22 +739,22 @@
 	if (selection().size() == 0)
 		return;
 
-	QString			parentpath = getCurrentDocument()->fullPath();
+	QString			parentpath (getCurrentDocument()->fullPath());
 
 	// BFC type of the new subfile - it shall inherit the BFC type of the parent document
-	LDBFC::Statement		bfctype = LDBFC::NoCertify;
+	BFCStatement	bfctype (BFCStatement::NoCertify);
 
 	// Dirname of the new subfile
-	QString			subdirname = dirname (parentpath);
+	QString			subdirname (dirname (parentpath));
 
 	// Title of the new subfile
 	QString			subtitle;
 
 	// Comment containing the title of the parent document
-	LDCommentPtr	titleobj = getCurrentDocument()->getObject (0).dynamicCast<LDComment>();
+	LDCommentPtr	titleobj (getCurrentDocument()->getObject (0).dynamicCast<LDComment>());
 
 	// License text for the subfile
-	QString			license = getLicenseText (cfg::defaultLicense);
+	QString			license (getLicenseText (cfg::defaultLicense));
 
 	// LDraw code body of the new subfile (i.e. code of the selection)
 	QStringList		code;
@@ -765,7 +763,7 @@
 	QString			fullsubname;
 
 	// Where to insert the subfile reference?
-	int				refidx = selection()[0]->lineNumber();
+	int				refidx (selection()[0]->lineNumber());
 
 	// Determine title of subfile
 	if (titleobj != null)
@@ -836,9 +834,9 @@
 		if (obj->type() != OBJ_BFC)
 			continue;
 
-		LDBFC::Statement a = obj.staticCast<LDBFC>()->statement();
+		BFCStatement a = obj.staticCast<LDBFC>()->statement();
 
-		if (a == LDBFC::CertifyCCW or a == LDBFC::CertifyCW or a == LDBFC::NoCertify)
+		if (eq (a, BFCStatement::CertifyCCW, BFCStatement::CertifyCW, BFCStatement::NoCertify))
 		{
 			bfctype = a;
 			break;
--- a/src/addObjectDialog.cc	Sun Jul 20 05:01:51 2014 +0300
+++ b/src/addObjectDialog.cc	Sat Jul 26 03:43:37 2014 +0300
@@ -82,13 +82,13 @@
 		{
 			rb_bfcType = new RadioGroup ("Statement", {}, 0, Qt::Vertical);
 
-			for (int i = 0; i < LDBFC::NumStatements; ++i)
+			for_enum (BFCStatement, i)
 			{
 				// Separate these in two columns
-				if (i == LDBFC::NumStatements / 2)
+				if (int (i) == int (BFCStatement::NumValues) / 2)
 					rb_bfcType->rowBreak();
 
-				rb_bfcType->addButton (LDBFC::k_statementStrings[i]);
+				rb_bfcType->addButton (LDBFC::StatementStrings[int (i)]);
 			}
 
 			if (obj)
@@ -355,7 +355,8 @@
 		case OBJ_BFC:
 		{
 			LDBFCPtr bfc = initObj<LDBFC> (obj);
-			bfc->setStatement ((LDBFC::Statement) dlg.rb_bfcType->value());
+			assert (within (dlg.rb_bfcType->value(), 0, int (BFCStatement::NumValues) - 1));
+			bfc->setStatement (BFCStatement (dlg.rb_bfcType->value()));
 		} break;
 
 		case OBJ_Vertex:
--- a/src/basics.h	Sun Jul 20 05:01:51 2014 +0300
+++ b/src/basics.h	Sat Jul 26 03:43:37 2014 +0300
@@ -244,6 +244,12 @@
 }
 
 template<typename T>
+inline bool within (T a, T b, T c)
+{
+	return a >= b and a <= c;
+}
+
+template<typename T>
 void removeDuplicates (T& a)
 {
 	std::sort (a.begin(), a.end());
--- a/src/ldDocument.cc	Sun Jul 20 05:01:51 2014 +0300
+++ b/src/ldDocument.cc	Sat Jul 26 03:43:37 2014 +0300
@@ -423,7 +423,8 @@
 		// Check for parse errors and warn about tthem
 		if (obj->type() == OBJ_Error)
 		{
-			print ("Couldn't parse line #%1: %2", progress() + 1, obj.staticCast<LDError>()->reason());
+			print ("Couldn't parse line #%1: %2",
+				progress() + 1, obj.staticCast<LDError>()->reason());
 
 			if (warnings() != null)
 				(*warnings())++;
@@ -575,7 +576,7 @@
 //
 bool LDDocument::isSafeToClose()
 {
-	typedef QMessageBox msgbox;
+	using msgbox = QMessageBox;
 	setlocale (LC_ALL, "C");
 
 	// If we have unsaved changes, warn and give the option of saving.
@@ -827,24 +828,26 @@
 
 	for (int i = min; i <= max; ++i)
 	{
+		// Check for floating point
+		tokens[i].toDouble (&ok);
+		if (ok)
+			return;
+
 		// Check hex
 		if (tokens[i].startsWith ("0x"))
 		{
 			tokens[i].mid (2).toInt (&ok, 16);
 
-			if (not ok)
-			{
-				// Check for floating point
-				tokens[i].toDouble (&ok);
+			if (ok)
+				return;
+		}
 
-				// Also check scientific notation, e.g. 7.99361e-15
-				if (not ok and not scient.exactMatch (tokens[i]))
-				{
-					throw QString (format ("Token #%1 was `%2`, expected a number (matched length: %3)",
-						(i + 1), tokens[i], scient.matchedLength()));
-				}
-			}
-		}
+		// Check scientific notation, e.g. 7.99361e-15
+		if (scient.exactMatch (tokens[i]))
+			return;
+
+		throw QString (format ("Token #%1 was `%2`, expected a number (matched length: %3)",
+			(i + 1), tokens[i], scient.matchedLength()));
 	}
 }
 
@@ -903,19 +906,24 @@
 				// Handle BFC statements
 				if (tokens.size() > 2 and tokens[1] == "BFC")
 				{
-					for (int i = 0; i < LDBFC::NumStatements; ++i)
-						if (commentTextSimplified == format ("BFC %1", LDBFC::k_statementStrings [i]))
-							return spawn<LDBFC> ((LDBFC::Statement) i);
+					for_enum (BFCStatement, i)
+					{
+						if (commentTextSimplified == format ("BFC %1",
+							LDBFC::StatementStrings[int (i)]))
+						{
+							return spawn<LDBFC> (i);
+						}
+					}
 
 					// MLCAD is notorious for stuffing these statements in parts it
 					// creates. The above block only handles valid statements, so we
 					// need to handle MLCAD-style invertnext, clip and noclip separately.
 					if (commentTextSimplified == "BFC CERTIFY INVERTNEXT")
-						return spawn<LDBFC> (LDBFC::InvertNext);
+						return spawn<LDBFC> (BFCStatement::InvertNext);
 					elif (commentTextSimplified == "BFC CERTIFY CLIP")
-						return spawn<LDBFC> (LDBFC::Clip);
+						return spawn<LDBFC> (BFCStatement::Clip);
 					elif (commentTextSimplified == "BFC CERTIFY NOCLIP")
-						return spawn<LDBFC> (LDBFC::NoClip);
+						return spawn<LDBFC> (BFCStatement::NoClip);
 				}
 
 				if (tokens.size() > 2 and tokens[1] == "!LDFORGE")
@@ -929,7 +937,8 @@
 
 						LDVertexPtr obj = spawn<LDVertex>();
 						obj->setColor (LDColor::fromIndex (stringToNumber (tokens[3])));
-						obj->pos.apply ([&](Axis ax, double& value) { value = tokens[4 + ax].toDouble(); });
+						obj->pos.apply ([&](Axis ax, double& value)
+							{ value = tokens[4 + ax].toDouble(); });
 						return obj;
 					}
 					elif (tokens[2] == "OVERLAY")
--- a/src/ldObject.cc	Sun Jul 20 05:01:51 2014 +0300
+++ b/src/ldObject.cc	Sat Jul 26 03:43:37 2014 +0300
@@ -191,7 +191,7 @@
 
 // =============================================================================
 //
-const char* LDBFC::k_statementStrings[] =
+const char* LDBFC::StatementStrings[] =
 {
 	"CERTIFY CCW",
 	"CCW",
@@ -207,7 +207,7 @@
 
 QString LDBFC::asText() const
 {
-	return format ("0 BFC %1", LDBFC::k_statementStrings[m_statement]);
+	return format ("0 BFC %1", StatementStrings[int (m_statement)]);
 }
 
 // =============================================================================
@@ -391,12 +391,12 @@
 LDPolygon* LDObject::getPolygon()
 {
 	LDObjectType ot = type();
-	int num =
-		(ot == OBJ_Line)		?	2 :
-		(ot == OBJ_Triangle)	?	3 :
-		(ot == OBJ_Quad)		?	4 :
-		(ot == OBJ_CondLine)	?	5 :
-										0;
+	int num = (ot == OBJ_Line)		? 2
+			: (ot == OBJ_Triangle)	? 3
+			: (ot == OBJ_Quad)		? 4
+			: (ot == OBJ_CondLine)	? 5
+			: 0;
+
 	if (num == 0)
 		return null;
 
@@ -418,8 +418,10 @@
 	QList<LDPolygon> data = fileInfo()->inlinePolygons();
 
 	for (LDPolygon& entry : data)
+	{
 		for (int i = 0; i < entry.numVertices(); ++i)
 			entry.vertices[i].transform (transform(), position());
+	}
 
 	return data;
 }
@@ -431,8 +433,10 @@
 	assert (document() != null);
 
 	for (int i = 0; i < document().toStrongRef()->getObjectCount(); ++i)
+	{
 		if (document().toStrongRef()->getObject (i) == this)
 			return i;
+	}
 
 	return -1;
 }
@@ -445,9 +449,9 @@
 		return;
 
 	// If we move down, we need to iterate the array in reverse order.
-	const long start = up ? 0 : (objs.size() - 1);
-	const long end = up ? objs.size() : -1;
-	const long incr = up ? 1 : -1;
+	long const start = up ? 0 : (objs.size() - 1);
+	long const end = up ? objs.size() : -1;
+	long const incr = up ? 1 : -1;
 	LDObjectList objsToCompile;
 	LDDocumentPtr file = objs[0]->document();
 
@@ -455,8 +459,8 @@
 	{
 		LDObjectPtr obj = objs[i];
 
-		const long idx = obj->lineNumber(),
-				   target = idx + (up ? -1 : 1);
+		long const idx = obj->lineNumber();
+		long const target = idx + (up ? -1 : 1);
 
 		if ((up and idx == 0) or (not up and idx == (long) file->objects().size() - 1l))
 		{
@@ -502,8 +506,10 @@
 		int count = 0;
 
 		for (LDObjectPtr obj : objs)
+		{
 			if (obj->type() == objType)
 				count++;
+		}
 
 		if (count == 0)
 			continue;
@@ -570,7 +576,8 @@
 {
 	LDObjectPtr prev (previous());
 
-	if (prev != null and prev->type() == OBJ_BFC and prev.staticCast<LDBFC>()->statement() == LDBFC::InvertNext)
+	if (prev != null and prev->type() == OBJ_BFC and
+		prev.staticCast<LDBFC>()->statement() == BFCStatement::InvertNext)
 	{
 		ptr = prev.staticCast<LDBFC>();
 		return true;
@@ -712,7 +719,7 @@
 	{
 		LDBFCPtr bfc = previous().dynamicCast<LDBFC>();
 
-		if (not bfc.isNull() and bfc->statement() == LDBFC::InvertNext)
+		if (not bfc.isNull() and bfc->statement() == BFCStatement::InvertNext)
 		{
 			// This is prefixed with an invertnext, thus remove it.
 			bfc->destroy();
@@ -721,7 +728,7 @@
 	}
 
 	// Not inverted, thus prefix it with a new invertnext.
-	document().toStrongRef()->insertObj (idx, spawn<LDBFC> (LDBFC::InvertNext));
+	document().toStrongRef()->insertObj (idx, spawn<LDBFC> (BFCStatement::InvertNext));
 }
 
 // =============================================================================
--- a/src/ldObject.h	Sun Jul 20 05:01:51 2014 +0300
+++ b/src/ldObject.h	Sat Jul 26 03:43:37 2014 +0300
@@ -371,39 +371,41 @@
 //
 // Represents a 0 BFC statement in the LDraw code.
 //
+enum class BFCStatement
+{
+	CertifyCCW,
+	CCW,
+	CertifyCW,
+	CW,
+	NoCertify,
+	InvertNext,
+	Clip,
+	ClipCCW,
+	ClipCW,
+	NoClip,
+
+	NumValues,
+	FirstValue = CertifyCCW
+};
+
 class LDBFC : public LDObject
 {
 public:
-	enum Statement
-	{
-		CertifyCCW,
-		CCW,
-		CertifyCW,
-		CW,
-		NoCertify,
-		InvertNext,
-		Clip,
-		ClipCCW,
-		ClipCW,
-		NoClip,
-		NumStatements
-	};
-
 	LDOBJ (BFC)
 	LDOBJ_NAME (bfc)
 	LDOBJ_VERTICES (0)
 	LDOBJ_UNCOLORED
-	LDOBJ_CUSTOM_SCEMANTIC { return (statement() == InvertNext); }
+	LDOBJ_CUSTOM_SCEMANTIC { return (statement() == BFCStatement::InvertNext); }
 	LDOBJ_NO_MATRIX
-	PROPERTY (public, Statement, statement, setStatement, STOCK_WRITE)
+	PROPERTY (public, BFCStatement, statement, setStatement, STOCK_WRITE)
 
 public:
-	LDBFC (LDObjectPtr* selfptr, const LDBFC::Statement type) :
+	LDBFC (LDObjectPtr* selfptr, const BFCStatement type) :
 		LDObject (selfptr),
 		m_statement (type) {}
 
 	// Statement strings
-	static const char* k_statementStrings[];
+	static const char* StatementStrings[];
 };
 
 using LDBFCPtr = QSharedPointer<LDBFC>;
--- a/src/macros.h	Sun Jul 20 05:01:51 2014 +0300
+++ b/src/macros.h	Sat Jul 26 03:43:37 2014 +0300
@@ -115,6 +115,17 @@
 	inline T operator++ (T& a, int) { T result = a; a = (T) ((int) a + 1); return result; } \
 	inline T operator-- (T& a, int) { T result = a; a = (T) ((int) a - 1); return result; }
 
+#define FOR_ENUM_NAME_HELPER(LINE) enum_iterator_ ## LINE
+#define FOR_ENUM_NAME(LINE) FOR_ENUM_NAME_HELPER(LINE)
+
+#define for_enum(ENUM, NAME) \
+	for (std::underlying_type<ENUM>::type FOR_ENUM_NAME (__LINE__) = \
+		std::underlying_type<ENUM>::type (ENUM::FirstValue); \
+		FOR_ENUM_NAME (__LINE__) < std::underlying_type<ENUM>::type (ENUM::NumValues); \
+		++FOR_ENUM_NAME (__LINE__)) \
+	for (ENUM NAME = ENUM (FOR_ENUM_NAME (__LINE__)); NAME != ENUM::NumValues; \
+		NAME = ENUM::NumValues)
+
 // =============================================================================
 #ifdef IN_IDE_PARSER // KDevelop workarounds:
 # error IN_IDE_PARSER is defined (this code is only for KDevelop workarounds)
@@ -130,4 +141,4 @@
 
 static const char* __func__ = ""; // Current function name
 typedef void FILE; // :|
-#endif // IN_IDE_PARSER
\ No newline at end of file
+#endif // IN_IDE_PARSER
--- a/src/mainWindow.cc	Sun Jul 20 05:01:51 2014 +0300
+++ b/src/mainWindow.cc	Sat Jul 26 03:43:37 2014 +0300
@@ -373,7 +373,7 @@
 
 			case OBJ_BFC:
 			{
-				descr = LDBFC::k_statementStrings[obj.staticCast<LDBFC>()->statement()];
+				descr = LDBFC::StatementStrings[int (obj.staticCast<LDBFC>()->statement())];
 				break;
 			}
 
--- a/src/primitives.cc	Sun Jul 20 05:01:51 2014 +0300
+++ b/src/primitives.cc	Sat Jul 26 03:43:37 2014 +0300
@@ -636,7 +636,7 @@
 		 << spawn<LDComment> (format ("!LDRAW_ORG Unofficial_%1Primitive", divs == g_hires ? "48_" : ""))
 		 << spawn<LDComment> (license)
 		 << spawn<LDEmpty>()
-		 << spawn<LDBFC> (LDBFC::CertifyCCW)
+		 << spawn<LDBFC> (BFCStatement::CertifyCCW)
 		 << spawn<LDEmpty>();
 
 	f->addObjects (objs);

mercurial