Phased out FOREACH macro in favor of C++11-style for iteration.

Mon, 25 Mar 2013 17:04:18 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Mon, 25 Mar 2013 17:04:18 +0200
changeset 70
e6b8dab8f81a
parent 69
6790dea720a8
child 71
c9f02d2dd9eb

Phased out FOREACH macro in favor of C++11-style for iteration.

bbox.cpp file | annotate | diff | comparison | revisions
common.h file | annotate | diff | comparison | revisions
file.cpp file | annotate | diff | comparison | revisions
gldraw.cpp file | annotate | diff | comparison | revisions
gui.cpp file | annotate | diff | comparison | revisions
ldtypes.cpp file | annotate | diff | comparison | revisions
zz_configDialog.cpp file | annotate | diff | comparison | revisions
zz_configDialog.h file | annotate | diff | comparison | revisions
--- a/bbox.cpp	Mon Mar 25 16:05:03 2013 +0200
+++ b/bbox.cpp	Mon Mar 25 17:04:18 2013 +0200
@@ -34,10 +34,8 @@
 	if (!g_CurrentFile)
 		return;
 	
-	for (uint i = 0; i < g_CurrentFile->objects.size(); i++) {
-		LDObject* obj = g_CurrentFile->objects[i];
+	for (LDObject* obj : g_CurrentFile->objects) {
 		switch (obj->getType ()) {
-		
 		case OBJ_Line:
 			{
 				LDLine* line = static_cast<LDLine*> (obj);
--- a/common.h	Mon Mar 25 16:05:03 2013 +0200
+++ b/common.h	Mon Mar 25 17:04:18 2013 +0200
@@ -132,8 +132,4 @@
 typedef uint32_t xulong;
 typedef uint64_t xulonglong;
 
-#define FOREACH(T, PTRS, COUNTER, ARRAY) \
-	for (T PTRS* COUNTER##ptr = &(*ARRAY.begin ()), PTRS COUNTER = *COUNTER##ptr; \
-		COUNTER##ptr < &(*ARRAY.end ()); COUNTER = *(++COUNTER##ptr))
-
 #endif
\ No newline at end of file
--- a/file.cpp	Mon Mar 25 16:05:03 2013 +0200
+++ b/file.cpp	Mon Mar 25 17:04:18 2013 +0200
@@ -31,7 +31,7 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 OpenFile* findLoadedFile (str zName) {
-	FOREACH (OpenFile, *, file, g_LoadedFiles)
+	for (OpenFile* file : g_LoadedFiles)
 		if (file->zFileName == zName)
 			return file;
 	
@@ -60,9 +60,9 @@
 			"p",
 		};
 		
-		for (ushort i = 0; i < sizeof saSubdirectories / sizeof *saSubdirectories; ++i) {
+		for (char const* sSubdir : saSubdirectories) {
 			str zFilePath = str::mkfmt ("%s" DIRSLASH "%s" DIRSLASH "%s",
-				io_ldpath.value.chars(), saSubdirectories[i], zTruePath.chars());
+				io_ldpath.value.chars(), sSubdir, zTruePath.chars());
 			
 			fp = fopen (zFilePath.chars (), "r");
 			
@@ -97,15 +97,17 @@
 	
 	fclose (fp);
 	
-	for (ulong i = 0; i < lines.size(); ++i) {
-		LDObject* obj = parseLine (lines[i]);
+	for (str line : lines) {
+		LDObject* obj = parseLine (line);
 		load->objects.push_back (obj);
 		
-		// Check for parse errors and warn abotu tthem
+		// Check for parse errors and warn about tthem
 		if (obj->getType() == OBJ_Gibberish) {
 			logf (LOG_Warning, "Couldn't parse line #%lu: %s\n",
-				i, static_cast<LDGibberish*> (obj)->zReason.chars());
-			logf (LOG_Warning, "- Line was: %s\n", lines[i].chars());
+				(&line - &(lines[0])),
+				static_cast<LDGibberish*> (obj)->zReason.chars());
+			
+			logf (LOG_Warning, "- Line was: %s\n", line.chars());
 			numWarnings++;
 		}
 	}
@@ -123,8 +125,8 @@
 // =============================================================================
 // Clear everything from the model
 void OpenFile::close () {
-	for (ulong j = 0; j < objects.size(); ++j)
-		delete objects[j];
+	for (LDObject* obj : objects)
+		delete obj;
 	
 	delete this;
 }
@@ -137,10 +139,8 @@
 		return;
 	
 	// Remove all loaded files and the objects they contain
-	for (ushort i = 0; i < g_LoadedFiles.size(); i++) {
-		OpenFile* f = g_LoadedFiles[i];
-		f->close ();
-	}
+	for (OpenFile* file : g_LoadedFiles)
+		file->close ();
 	
 	// Clear the array
 	g_LoadedFiles.clear();
@@ -197,11 +197,9 @@
 		return false;
 	
 	// Write all entries now
-	for (ulong i = 0; i < objects.size(); ++i) {
-		LDObject* obj = objects[i];
-		
+	for (LDObject* obj : objects) {
 		// LDraw requires lines to have DOS line endings
-		str zLine = str::mkfmt ("%s\r\n",obj->getContents ().chars ());
+		str zLine = str::mkfmt ("%s\r\n", obj->getContents ().chars ());
 		
 		fwrite (zLine.chars(), 1, ~zLine, fp);
 	}
@@ -360,9 +358,10 @@
 			
 			for (short i = 0; i < 4; ++i)
 				obj->vaCoords[i] = parseVertex (tokens, 2 + (i * 3)); // 2 - 13
+			
 			return obj;
 		}
-		
+	
 	default: // Strange line we couldn't parse
 		return new LDGibberish (zLine, "Unknown line code number");
 	}
@@ -388,19 +387,17 @@
 		return;
 	
 	// First, close all but the current open file.
-	for (ushort i = 0; i < g_LoadedFiles.size(); ++i)
-		if (g_LoadedFiles[i] != g_CurrentFile)
-			g_LoadedFiles[i]->close();
+	for (OpenFile* file : g_LoadedFiles)
+		if (file != g_CurrentFile)
+			file->close ();
 	
 	g_LoadedFiles.clear ();
 	g_LoadedFiles.push_back (g_CurrentFile);
 	
-	// Go through all the current file and reload the subfiles
-	for (ulong i = 0; i < g_CurrentFile->objects.size(); ++i) {
-		LDObject* obj = g_CurrentFile->objects[i];
-		
-		// Reload subfiles
+	// Go through all objects in the current file and reload the subfiles
+	for (LDObject* obj : g_CurrentFile->objects) {
 		if (obj->getType() == OBJ_Subfile) {
+			// Note: ref->pFile is invalid right now since all subfiles were closed.
 			LDSubfile* ref = static_cast<LDSubfile*> (obj);
 			OpenFile* pFile = loadSubfile (ref->zFileName);
 			
--- a/gldraw.cpp	Mon Mar 25 16:05:03 2013 +0200
+++ b/gldraw.cpp	Mon Mar 25 17:04:18 2013 +0200
@@ -135,8 +135,8 @@
 		setMainColor ();
 		
 		// Warn about the unknown colors, but only once.
-		for (long i = 0; i < (long)g_daWarnedColors.size(); ++i)
-			if (g_daWarnedColors[i] == obj->dColor)
+		for (short i : g_daWarnedColors)
+			if (obj->dColor == i)
 				return;
 		
 		printf ("%s: Unknown color %d!\n", __func__, obj->dColor);
@@ -227,15 +227,15 @@
 		&uObjListBack,
 	};
 	
-	for (uchar j = 0; j < 2; ++j) {
-		if (j && !gl_colorbfc)
+	for (uchar i = 0; i < 2; ++i) {
+		if (i && !gl_colorbfc)
 			continue;
 		
-		*upaLists[j] = glGenLists (1);
-		glNewList (*upaLists[j], GL_COMPILE);
+		*upaLists[i] = glGenLists (1);
+		glNewList (*upaLists[i], GL_COMPILE);
 		
-		for (ulong i = 0; i < g_CurrentFile->objects.size(); i++)
-			compileOneObject (g_CurrentFile->objects[i], j);
+		for (LDObject* obj : g_CurrentFile->objects)
+			compileOneObject (obj, i);
 		
 		glEndList ();
 	}
@@ -244,7 +244,7 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-template<class T>void renderer::compileSubObject (LDObject* obj,
+template<class T> void renderer::compileSubObject (LDObject* obj,
 	const bool bBackSide, const GLenum eGLType, const short dVerts)
 {
 	setObjectColor (obj, bBackSide);
@@ -292,9 +292,9 @@
 			
 			vector<LDObject*> objs = ref->inlineContents (true, true);
 			
-			for (ulong i = 0; i < (ulong)objs.size(); ++i) {
-				compileOneObject (objs[i], bBackSide);
-				delete objs[i];
+			for (LDObject* obj : objs) {
+				compileOneObject (obj, bBackSide);
+				delete obj;
 			}
 		}
 		break;
--- a/gui.cpp	Mon Mar 25 16:05:03 2013 +0200
+++ b/gui.cpp	Mon Mar 25 17:04:18 2013 +0200
@@ -336,7 +336,7 @@
 	// Clear the clipboard. However, its contents are dynamically allocated
 	// clones of LDObjects (cannot use pointers to real objects because the
 	// cut operation deletes them!), so we have to delete said objects first.
-	FOREACH (LDObject, *, obj, g_Clipboard)
+	for (LDObject* obj : g_Clipboard)
 		delete obj;
 	
 	g_Clipboard.clear ();
@@ -372,7 +372,7 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 void ForgeWindow::slot_paste () {
-	FOREACH (LDObject, *, obj, g_Clipboard)
+	for (LDObject* obj : g_Clipboard)
 		g_CurrentFile->addObject (obj->makeClone ());
 	
 	refresh ();
@@ -399,7 +399,7 @@
 void ForgeWindow::doInline (bool bDeep) {
 	vector<LDObject*> sel = getSelectedObjects ();
 	
-	FOREACH (LDObject, *, obj, sel) {
+	for (LDObject* obj : sel) {
 		// Obviously, only subfiles can be inlined.
 		if (obj->getType() != OBJ_Subfile)
 			continue;
@@ -416,7 +416,7 @@
 		vector<LDObject*> objs = ref->inlineContents (bDeep, true);
 		
 		// Merge in the inlined objects
-		FOREACH (LDObject, *, inlineobj, objs)
+		for (LDObject* inlineobj : objs)
 			g_CurrentFile->objects.insert (g_CurrentFile->objects.begin() + idx++, inlineobj);
 		
 		// Delete the subfile now as it's been inlined.
@@ -441,7 +441,7 @@
 void ForgeWindow::slot_splitQuads () {
 	vector<LDObject*> objs = getSelectedObjects ();
 	
-	FOREACH (LDObject, *, obj, objs) {
+	for (LDObject* obj : objs) {
 		if (obj->getType() != OBJ_Quad)
 			continue;
 		
@@ -476,7 +476,7 @@
 	
 	// If all selected objects have the same color, said color is our default
 	// value to the color selection dialog.
-	FOREACH (LDObject, *, obj, objs) {
+	for (LDObject* obj : objs) {
 		if (obj->dColor == -1)
 			continue; // doesn't use color
 		
@@ -493,7 +493,7 @@
 	
 	// Show the dialog to the user now and ask for a color.
 	if (ColorSelectDialog::staticDialog (dColor, dDefault, this)) {
-		FOREACH (LDObject, *, obj, objs)
+		for (LDObject* obj : objs)
 			if (obj->dColor != -1)
 				obj->dColor = dColor;
 		
@@ -508,7 +508,7 @@
 	vector<LDObject*> objs = getSelectedObjects ();
 	
 	// Delete the objects that were being selected
-	FOREACH (LDObject, *, obj, objs) {
+	for (LDObject* obj : objs) {
 		if (obj->getType() != OBJ_Quad && obj->getType() != OBJ_Triangle)
 			continue;
 		
@@ -548,7 +548,7 @@
 	vector<LDObject*> objs = getSelectedObjects ();
 	
 	// Delete the objects that were being selected
-	FOREACH (LDObject, *, obj, objs) {
+	for (LDObject* obj : objs) {
 		g_CurrentFile->forgetObject (obj);
 		delete obj;
 	}
@@ -565,9 +565,7 @@
 	
 	qObjList->clear ();
 	
-	for (ulong i = 0; i < g_CurrentFile->objects.size(); ++i) {
-		LDObject* obj = g_CurrentFile->objects[i];
-		
+	for (LDObject* obj : g_CurrentFile->objects) {
 		str zText;
 		switch (obj->getType ()) {
 		case OBJ_Comment:
@@ -726,14 +724,11 @@
 	std::vector<LDObject*> objs;
 	
 	QList<QTreeWidgetItem*> const qaItems = qObjList->selectedItems();
-	for (ulong i = 0; i < g_CurrentFile->objects.size(); ++i) {
-		LDObject* obj = g_CurrentFile->objects[i];
-		
-		for (long j = 0; j < qaItems.size(); ++j) {
-			if (qaItems[j] == obj->qObjListEntry) {
-				objs.push_back (obj);
-				break;
-			}
+	for (LDObject* obj : g_CurrentFile->objects)
+	for (QTreeWidgetItem* qItem : qaItems) {
+		if (qItem == obj->qObjListEntry) {
+			objs.push_back (obj);
+			break;
 		}
 	}
 	
--- a/ldtypes.cpp	Mon Mar 25 16:05:03 2013 +0200
+++ b/ldtypes.cpp	Mon Mar 25 17:04:18 2013 +0200
@@ -356,11 +356,9 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 long LDObject::getIndex (OpenFile* pFile) {
-	long lIndex;
-	
-	for (lIndex = 0; lIndex < (long)pFile->objects.size(); ++lIndex)
-		if (pFile->objects[lIndex] == this)
-			return lIndex;
+	for (LDObject* obj : pFile->objects)
+		if (obj == this)
+			return (&obj - &(pFile->objects[0]));
 	
 	return -1;
 }
\ No newline at end of file
--- a/zz_configDialog.cpp	Mon Mar 25 16:05:03 2013 +0200
+++ b/zz_configDialog.cpp	Mon Mar 25 17:04:18 2013 +0200
@@ -68,19 +68,11 @@
 	connect (qGLForegroundButton, SIGNAL (clicked ()),
 		this, SLOT (slot_setGLForeground ()));
 	
-	qGLForegroundAlphaLabel = new QLabel ("Translucency:");
-	qGLForegroundAlpha = new QSlider (Qt::Horizontal);
-	qGLForegroundAlpha->setRange (1, 10);
-	qGLForegroundAlpha->setTickInterval (1);
-	qGLForegroundAlpha->setSliderPosition (gl_maincolor_alpha * 10.0f);
-	qGLForegroundAlpha->setTickPosition (QSlider::TicksAbove);
+	qGLForegroundAlphaLabel = new QLabel ("Alpha:");
+	makeSlider (qGLForegroundAlpha, 1, 10, (gl_maincolor_alpha * 10.0f));
 	
 	qGLLineThicknessLabel = new QLabel ("Line thickness:");
-	qGLLineThickness = new QSlider (Qt::Horizontal);
-	qGLLineThickness->setRange (1, 8);
-	qGLLineThickness->setSliderPosition (gl_linethickness);
-	qGLLineThickness->setTickPosition (QSlider::TicksAbove);
-	qGLLineThickness->setTickInterval (1);
+	makeSlider (qGLLineThickness, 1, 8, gl_linethickness);
 	
 	qLVColorize = new QCheckBox ("Colorize polygons in list view");
 	INIT_CHECKBOX (qLVColorize, lv_colorize)
@@ -118,6 +110,19 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
+void ConfigDialog::makeSlider (QSlider*& qSlider, short int dMin, short int dMax,
+	short dDefault)
+{
+	qSlider = new QSlider (Qt::Horizontal);
+	qSlider->setRange (dMin, dMax);
+	qSlider->setSliderPosition (dDefault);
+	qSlider->setTickPosition (QSlider::TicksAbove);
+	qSlider->setTickInterval (1);
+}
+
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
 ConfigDialog::~ConfigDialog() {
 	g_ConfigDialog = nullptr;
 }
--- a/zz_configDialog.h	Mon Mar 25 16:05:03 2013 +0200
+++ b/zz_configDialog.h	Mon Mar 25 17:04:18 2013 +0200
@@ -24,6 +24,8 @@
 #include <qpushbutton.h>
 #include <qcheckbox.h>
 
+class intconfig;
+
 class ConfigDialog : public QDialog {
 	Q_OBJECT
 	
@@ -44,6 +46,7 @@
 	static void staticDialog (ForgeWindow* window);
 	
 private:
+	void makeSlider (QSlider*& qSlider, short int dMin, short int dMax, short int dDefault);
     void setButtonBackground (QPushButton* qButton, str zValue);
     void pickColor (strconfig& cfg, QPushButton* qButton);
 	

mercurial