- added a macro for adding operator++ and operator-- to enums, less cast hell this way

Sat, 03 May 2014 00:36:50 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Sat, 03 May 2014 00:36:50 +0300
changeset 762
d259256e1394
parent 761
64ea52bf685c
child 763
b682c73492f0

- added a macro for adding operator++ and operator-- to enums, less cast hell this way
- renamed GLRenderer::EFixedCamera to ECamera and moved it to global namespace

src/actions.cc file | annotate | diff | comparison | revisions
src/dialogs.cc file | annotate | diff | comparison | revisions
src/glCompiler.cc file | annotate | diff | comparison | revisions
src/glRenderer.cc file | annotate | diff | comparison | revisions
src/glRenderer.h file | annotate | diff | comparison | revisions
src/glShared.h 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
--- a/src/actions.cc	Thu May 01 14:26:10 2014 +0300
+++ b/src/actions.cc	Sat May 03 00:36:50 2014 +0300
@@ -561,7 +561,7 @@
 	if (not dlg.exec())
 		return;
 
-	R()->setupOverlay ((GL::EFixedCamera) dlg.camera(), dlg.fpath(), dlg.ofsx(),
+	R()->setupOverlay ((ECamera) dlg.camera(), dlg.fpath(), dlg.ofsx(),
 		dlg.ofsy(), dlg.lwidth(), dlg.lheight());
 }
 
@@ -605,7 +605,7 @@
 //
 DEFINE_ACTION (SetDrawDepth, 0)
 {
-	if (R()->camera() == GL::EFreeCamera)
+	if (R()->camera() == EFreeCamera)
 		return;
 
 	bool ok;
--- a/src/dialogs.cc	Thu May 01 14:26:10 2014 +0300
+++ b/src/dialogs.cc	Sat May 03 00:36:50 2014 +0300
@@ -56,18 +56,18 @@
 
 	m_cameraArgs =
 	{
-		{ ui->top,    GL::ETopCamera },
-		{ ui->bottom, GL::EBottomCamera },
-		{ ui->front,  GL::EFrontCamera },
-		{ ui->back,   GL::EBackCamera },
-		{ ui->left,   GL::ELeftCamera },
-		{ ui->right,  GL::ERightCamera }
+		{ ui->top,    ETopCamera },
+		{ ui->bottom, EBottomCamera },
+		{ ui->front,  EFrontCamera },
+		{ ui->back,   EBackCamera },
+		{ ui->left,   ELeftCamera },
+		{ ui->right,  ERightCamera }
 	};
 
-	GL::EFixedCamera cam = g_win->R()->camera();
+	ECamera cam = g_win->R()->camera();
 
-	if (cam == GL::EFreeCamera)
-		cam = GL::ETopCamera;
+	if (cam == EFreeCamera)
+		cam = ETopCamera;
 
 	connect (ui->width, SIGNAL (valueChanged (double)), this, SLOT (slot_dimensionsChanged()));
 	connect (ui->height, SIGNAL (valueChanged (double)), this, SLOT (slot_dimensionsChanged()));
@@ -139,7 +139,7 @@
 
 int OverlayDialog::camera() const
 {
-	return radioSwitch<int> (GL::ETopCamera, m_cameraArgs);
+	return radioSwitch<int> (ETopCamera, m_cameraArgs);
 }
 
 void OverlayDialog::slot_fpath()
--- a/src/glCompiler.cc	Thu May 01 14:26:10 2014 +0300
+++ b/src/glCompiler.cc	Sat May 03 00:36:50 2014 +0300
@@ -371,11 +371,11 @@
 		default: return;
 	}
 
-	for (int complement = 0; complement < VBOCM_NumComplements; ++complement)
+	for (EVBOComplement complement = VBOCM_First; complement < VBOCM_NumComplements; ++complement)
 	{
-		const int vbonum			= vboNumber (surface, (EVBOComplement) complement);
+		const int vbonum			= vboNumber (surface, complement);
 		QVector<GLfloat>& vbodata	= objinfo->data[vbonum];
-		const QColor color			= getColorForPolygon (poly, topobj, (EVBOComplement) complement);
+		const QColor color			= getColorForPolygon (poly, topobj, complement);
 
 		for (int vert = 0; vert < numverts; ++vert)
 		{
--- a/src/glRenderer.cc	Thu May 01 14:26:10 2014 +0300
+++ b/src/glRenderer.cc	Sat May 03 00:36:50 2014 +0300
@@ -68,7 +68,7 @@
 CFGENTRY (Float,	mainColorAlpha,				1.0)
 CFGENTRY (Int,		lineThickness,				2)
 CFGENTRY (Bool,		bfcRedGreenView,			false)
-CFGENTRY (Int,		camera,						GLRenderer::EFreeCamera)
+CFGENTRY (Int,		camera,						EFreeCamera)
 CFGENTRY (Bool,		blackEdges,					false)
 CFGENTRY (Bool,		drawAxes,					false)
 CFGENTRY (Bool,		drawWireframe,				false)
@@ -91,17 +91,6 @@
 	QT_TRANSLATE_NOOP ("GLRenderer",  "Free")
 };
 
-const GL::EFixedCamera g_Cameras[7] =
-{
-	GL::ETopCamera,
-	GL::EFrontCamera,
-	GL::ELeftCamera,
-	GL::EBottomCamera,
-	GL::EBackCamera,
-	GL::ERightCamera,
-	GL::EFreeCamera
-};
-
 struct LDGLAxis
 {
 	const QColor col;
@@ -124,7 +113,7 @@
 GLRenderer::GLRenderer (QWidget* parent) : QGLWidget (parent)
 {
 	m_isPicking = m_rangepick = false;
-	m_camera = (EFixedCamera) cfg::camera;
+	m_camera = (ECamera) cfg::camera;
 	m_drawToolTip = false;
 	m_editMode = ESelectMode;
 	m_rectdraw = false;
@@ -146,10 +135,9 @@
 	m_thinBorderPen.setWidth (1);
 
 	// Init camera icons
-	for (const GL::EFixedCamera cam : g_Cameras)
+	for (ECamera cam = EFirstCamera; cam < ENumCameras; ++cam)
 	{
 		String iconname = format ("camera-%1", tr (g_CameraNames[cam]).toLower());
-
 		CameraIcon* info = &m_cameraIcons[cam];
 		info->img = new QPixmap (getIcon (iconname));
 		info->cam = cam;
@@ -246,11 +234,11 @@
 //
 void GLRenderer::resetAllAngles()
 {
-	EFixedCamera oldcam = camera();
+	ECamera oldcam = camera();
 
 	for (int i = 0; i < 7; ++i)
 	{
-		setCamera ((EFixedCamera) i);
+		setCamera ((ECamera) i);
 		resetAngles();
 	}
 
@@ -838,7 +826,7 @@
 		for (CameraIcon& info : m_cameraIcons)
 		{
 			// Don't draw the free camera icon when in draw mode
-			if (&info == &m_cameraIcons[GL::EFreeCamera] && editMode() != ESelectMode)
+			if (&info == &m_cameraIcons[EFreeCamera] && editMode() != ESelectMode)
 				continue;
 
 			paint.drawPixmap (info.destRect, *info.img, info.srcRect);
@@ -1215,7 +1203,7 @@
 
 // =============================================================================
 //
-void GLRenderer::setCamera (const GLRenderer::EFixedCamera cam)
+void GLRenderer::setCamera (const ECamera cam)
 {
 	m_camera = cam;
 	cfg::camera = (int) cam;
@@ -1739,9 +1727,9 @@
 
 // =============================================================================
 //
-Axis GLRenderer::getCameraAxis (bool y, GLRenderer::EFixedCamera camid)
+Axis GLRenderer::getCameraAxis (bool y, ECamera camid)
 {
-	if (camid == (GL::EFixedCamera) - 1)
+	if (camid == (ECamera) -1)
 		camid = camera();
 
 	const LDFixedCameraInfo* cam = &g_FixedCameras[camid];
@@ -1750,7 +1738,7 @@
 
 // =============================================================================
 //
-bool GLRenderer::setupOverlay (EFixedCamera cam, String file, int x, int y, int w, int h)
+bool GLRenderer::setupOverlay (ECamera cam, String file, int x, int y, int w, int h)
 {
 	QImage* img = new QImage (QImage (file).convertToFormat (QImage::Format_ARGB32));
 	LDGLOverlay& info = getOverlay (cam);
@@ -1958,11 +1946,11 @@
 //
 void GLRenderer::zoomAllToFit()
 {
-	EFixedCamera oldcam = camera();
+	ECamera oldcam = camera();
 
-	for (int i = 0; i < 7; ++i)
+	for (ECamera cam = EFirstCamera; cam < ENumCameras; ++cam)
 	{
-		setCamera ((EFixedCamera) i);
+		setCamera (cam);
 		zoomToFit();
 	}
 
@@ -2024,7 +2012,7 @@
 
 // =============================================================================
 //
-LDOverlay* GLRenderer::findOverlayObject (EFixedCamera cam)
+LDOverlay* GLRenderer::findOverlayObject (ECamera cam)
 {
 	LDOverlay* ovlobj = null;
 
@@ -2046,7 +2034,7 @@
 //
 void GLRenderer::initOverlaysFromObjects()
 {
-	for (EFixedCamera cam : g_Cameras)
+	for (ECamera cam = EFirstCamera; cam < ENumCameras; ++cam)
 	{
 		if (cam == EFreeCamera)
 			continue;
@@ -2069,7 +2057,7 @@
 //
 void GLRenderer::updateOverlayObjects()
 {
-	for (EFixedCamera cam : g_Cameras)
+	for (ECamera cam = EFirstCamera; cam < ENumCameras; ++cam)
 	{
 		if (cam == EFreeCamera)
 			continue;
--- a/src/glRenderer.h	Thu May 01 14:26:10 2014 +0300
+++ b/src/glRenderer.h	Sat May 03 00:36:50 2014 +0300
@@ -103,6 +103,22 @@
 	}
 };
 
+enum ECamera
+{
+	ETopCamera,
+	EFrontCamera,
+	ELeftCamera,
+	EBottomCamera,
+	EBackCamera,
+	ERightCamera,
+	EFreeCamera,
+
+	ENumCameras,
+	EFirstCamera = ETopCamera
+};
+
+NUMERIC_ENUM_OPERATORS (ECamera)
+
 // =============================================================================
 // The main renderer object, draws the brick on the screen, manages the camera
 // and selection picking. The instance of GLRenderer is accessible as
@@ -111,25 +127,6 @@
 class GLRenderer : public QGLWidget
 {
 	public:
-		enum EFixedCamera
-		{
-			ETopCamera,
-			EFrontCamera,
-			ELeftCamera,
-			EBottomCamera,
-			EBackCamera,
-			ERightCamera,
-			EFreeCamera
-		};
-
-		enum ListType
-		{
-			NormalList,
-			PickList,
-			BFCFrontList,
-			BFCBackList
-		};
-
 		// CameraIcon::img is a heap-allocated QPixmap because otherwise it gets
 		// initialized before program gets to main() and constructs a QApplication
 		// and Qt doesn't like that.
@@ -139,7 +136,7 @@
 			QRect			srcRect,
 							destRect,
 							selRect;
-			EFixedCamera	cam;
+			ECamera			cam;
 		};
 
 		Q_OBJECT
@@ -156,7 +153,7 @@
 		GLRenderer (QWidget* parent = null);
 		~GLRenderer();
 
-		inline EFixedCamera camera() const
+		inline ECamera camera() const
 		{
 			return m_camera;
 		}
@@ -166,7 +163,7 @@
 		void           drawGLScene();
 		void           endDraw (bool accept);
 		void           forgetObject (LDObject* obj);
-		Axis           getCameraAxis (bool y, EFixedCamera camid = (EFixedCamera) - 1);
+		Axis           getCameraAxis (bool y, ECamera camid = (ECamera) -1);
 		const char*    getCameraName() const;
 		double         getDepthValue() const;
 		LDGLOverlay&   getOverlay (int newcam);
@@ -179,9 +176,9 @@
 		void           resetAngles();
 		void           resetAllAngles();
 		void           setBackground();
-		void           setCamera (const EFixedCamera cam);
+		void           setCamera (const ECamera cam);
 		void           setDepthValue (double depth);
-		bool           setupOverlay (EFixedCamera cam, String file, int x, int y, int w, int h);
+		bool           setupOverlay (ECamera cam, String file, int x, int y, int w, int h);
 		void           updateOverlayObjects();
 		void           zoomNotch (bool inward);
 
@@ -221,7 +218,7 @@
 									m_rangeStart;
 		QPen						m_thickBorderPen,
 									m_thinBorderPen;
-		EFixedCamera				m_camera,
+		ECamera						m_camera,
 									m_toolTipCamera;
 		GLuint						m_axeslist;
 		int							m_width,
@@ -233,7 +230,7 @@
 		QColor						m_bgcolor;
 
 		void           addDrawnVertex (Vertex m_hoverpos);
-		LDOverlay*     findOverlayObject (EFixedCamera cam);
+		LDOverlay*     findOverlayObject (ECamera cam);
 		void           updateRectVerts();
 		void           getRelativeAxes (Axis& relX, Axis& relY) const;
 		Axis			getRelativeZ() const;
@@ -309,16 +306,4 @@
 		void initializeAxes();
 };
 
-// Alias for short namespaces
-typedef GLRenderer GL;
-
-static const GLRenderer::ListType g_glListTypes[] =
-{
-	GL::NormalList,
-	GL::PickList,
-	GL::BFCFrontList,
-	GL::BFCBackList,
-};
-
-extern const GL::EFixedCamera g_Cameras[7];
 extern const char* g_CameraNames[7];
--- a/src/glShared.h	Thu May 01 14:26:10 2014 +0300
+++ b/src/glShared.h	Sat May 03 00:36:50 2014 +0300
@@ -40,7 +40,9 @@
 	VBOSF_Triangles,
 	VBOSF_Quads,
 	VBOSF_CondLines,
-	VBOSF_NumSurfaces
+
+	VBOSF_NumSurfaces,
+	VBOSF_First = VBOSF_Lines
 };
 
 enum EVBOComplement
@@ -51,9 +53,14 @@
 	VBOCM_BFCFrontColors,
 	VBOCM_BFCBackColors,
 	VBOCM_RandomColors,
-	VBOCM_NumComplements
+
+	VBOCM_NumComplements,
+	VBOCM_First = VBOCM_Surfaces
 };
 
+NUMERIC_ENUM_OPERATORS (EVBOSurface)
+NUMERIC_ENUM_OPERATORS (EVBOComplement)
+
 // KDevelop doesn't seem to understand some VBO stuff
 #ifdef IN_IDE_PARSER
 using GLint = int;
--- a/src/ldObject.cc	Thu May 01 14:26:10 2014 +0300
+++ b/src/ldObject.cc	Sat May 03 00:36:50 2014 +0300
@@ -445,15 +445,13 @@
 //
 String LDObject::describeObjects (const LDObjectList& objs)
 {
-	bool firstDetails = true;
-	String text = "";
+	String text;
 
 	if (objs.isEmpty())
 		return "nothing"; // :)
 
-	for (long i = 0; i < ENumTypes; ++i)
+	for (Type objType = EFirstType; objType < ENumTypes; ++objType)
 	{
-		Type objType = (Type) i;
 		int count = 0;
 
 		for (LDObject * obj : objs)
@@ -463,7 +461,7 @@
 		if (count == 0)
 			continue;
 
-		if (not firstDetails)
+		if (not text.isEmpty())
 			text += ", ";
 
 		String noun = format ("%1%2", typeName (objType), plural (count));
@@ -473,7 +471,6 @@
 			noun = "vertices";
 
 		text += format ("%1 %2", count, noun);
-		firstDetails = false;
 	}
 
 	return text;
--- a/src/ldObject.h	Thu May 01 14:26:10 2014 +0300
+++ b/src/ldObject.h	Sat May 03 00:36:50 2014 +0300
@@ -90,7 +90,9 @@
 			EError,          // Object is the result of failed parsing
 			EEmpty,          // Object represents an empty line
 			EUnidentified,   // Unknown object type (some functions return this; TODO: they probably should not)
-			ENumTypes        // Amount of object types
+
+			ENumTypes,       // Amount of object types
+			EFirstType = ESubfile
 		};
 
 		LDObject();
@@ -193,6 +195,8 @@
 		LDSharedVertex*	m_coords[4];
 };
 
+NUMERIC_ENUM_OPERATORS (LDObject::Type)
+
 // =============================================================================
 // LDSharedVertex
 //
--- a/src/macros.h	Thu May 01 14:26:10 2014 +0300
+++ b/src/macros.h	Sat May 03 00:36:50 2014 +0300
@@ -99,11 +99,13 @@
 # define assert(N) {}
 #endif // DEBUG
 
-#ifdef foreach
-# undef foreach
-#endif
+#define for_axes(AX) for (const Axis AX : std::initializer_list<const Axis> ({X, Y, Z}))
 
-#define for_axes(AX) for (const Axis AX : std::initializer_list<const Axis> ({X, Y, Z}))
+#define NUMERIC_ENUM_OPERATORS(T) \
+	inline T operator++ (T& a) { a = (T) ((int) a + 1); return a; } \
+	inline T operator-- (T& a) { a = (T) ((int) a - 1); return a; } \
+	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; }
 
 // =============================================================================
 #ifdef IN_IDE_PARSER // KDevelop workarounds:
--- a/src/mainWindow.cc	Thu May 01 14:26:10 2014 +0300
+++ b/src/mainWindow.cc	Sat May 03 00:36:50 2014 +0300
@@ -667,7 +667,7 @@
 		contextMenu->addAction (ui->actionSubfileSelection);
 	}
 
-	if (R()->camera() != GL::EFreeCamera)
+	if (R()->camera() != EFreeCamera)
 	{
 		contextMenu->addSeparator();
 		contextMenu->addAction (ui->actionSetDrawDepth);

mercurial