refactor: added the length() function that's like Python's len()

Sat, 28 Jan 2017 13:49:09 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 28 Jan 2017 13:49:09 +0200
changeset 1062
4119185b56ca
parent 1061
273333700685
child 1063
1f15c52c11f6

refactor: added the length() function that's like Python's len()

src/basics.h file | annotate | diff | comparison | revisions
src/colors.cpp file | annotate | diff | comparison | revisions
src/editmodes/curvemode.cpp file | annotate | diff | comparison | revisions
src/glCompiler.cpp file | annotate | diff | comparison | revisions
src/ldObject.cpp file | annotate | diff | comparison | revisions
src/macros.h file | annotate | diff | comparison | revisions
src/miscallenous.cpp file | annotate | diff | comparison | revisions
src/primitives.cpp file | annotate | diff | comparison | revisions
src/toolsets/algorithmtoolset.cpp file | annotate | diff | comparison | revisions
src/types/matrix.cpp file | annotate | diff | comparison | revisions
--- a/src/basics.h	Sat Jan 28 13:32:55 2017 +0200
+++ b/src/basics.h	Sat Jan 28 13:49:09 2017 +0200
@@ -22,6 +22,7 @@
 #include <QStringList>
 #include <QMetaType>
 #include <QVector3D>
+#include <QVector>
 #include <functional>
 #include <math.h>
 #include "macros.h"
@@ -212,3 +213,23 @@
 
 double getRadialPoint(int segment, int divisions, double(*func)(double));
 QVector<QLineF> makeCircle(int segments, int divisions, double radius);
+
+//
+// Get the amount of elements in something.
+//
+template<typename T, size_t N>
+int length(T(&)[N])
+{
+	return N;
+}
+
+static inline int length(const QString& string)
+{
+	return string.size();
+}
+
+template<typename T>
+int length(const QVector<T>& vector)
+{
+	return vector.size();
+}
--- a/src/colors.cpp	Sat Jan 28 13:32:55 2017 +0200
+++ b/src/colors.cpp	Sat Jan 28 13:49:09 2017 +0200
@@ -272,7 +272,7 @@
  */
 bool ColorData::contains(int code) const
 {
-	return code >= 0 and code < countof(m_data);
+	return code >= 0 and code < length(m_data);
 }
 
 /*
--- a/src/editmodes/curvemode.cpp	Sat Jan 28 13:32:55 2017 +0200
+++ b/src/editmodes/curvemode.cpp	Sat Jan 28 13:49:09 2017 +0200
@@ -30,7 +30,7 @@
 		Vertex curve[4];
 		QPoint curve2d[4];
 
-		for (int i = 0; i < qMin (countof(curve), m_drawedVerts.size()); ++i)
+		for (int i = 0; i < qMin (length(curve), m_drawedVerts.size()); ++i)
 			curve[i] = m_drawedVerts[i];
 
 		// Factor the cursor into the preview
@@ -44,7 +44,7 @@
 		if (m_drawedVerts.size() < 3)
 			curve[3] = curve[2];
 
-		for (int i = 0; i < countof(curve); ++i)
+		for (int i = 0; i < length(curve); ++i)
 			curve2d[i] = renderer()->convert3dTo2d (curve[i]);
 
 		painter.setPen (QColor (0, 112, 112));
@@ -54,7 +54,7 @@
 		if (m_drawedVerts.size() >= 3)
 			painter.drawLine (curve2d[1], curve2d[3]);
 
-		for (int i = 0; i < qMin (countof(curve), m_drawedVerts.size() + 1); ++i)
+		for (int i = 0; i < qMin (length(curve), m_drawedVerts.size() + 1); ++i)
 		{
 			if (i < 2)
 				renderer()->drawPoint (painter, curve2d[i]);
--- a/src/glCompiler.cpp	Sat Jan 28 13:32:55 2017 +0200
+++ b/src/glCompiler.cpp	Sat Jan 28 13:49:09 2017 +0200
@@ -199,7 +199,7 @@
 
 void GLCompiler::needMerge()
 {
-	for (int i = 0; i < countof (m_vboChanged); ++i)
+	for (int i = 0; i < length (m_vboChanged); ++i)
 		m_vboChanged[i] = true;
 }
 
--- a/src/ldObject.cpp	Sat Jan 28 13:32:55 2017 +0200
+++ b/src/ldObject.cpp	Sat Jan 28 13:49:09 2017 +0200
@@ -981,7 +981,7 @@
 		"NOCLIP",
 	};
 
-	if ((int) statement >= 0 and (int) statement < countof (statementStrings))
+	if ((int) statement >= 0 and (int) statement < length (statementStrings))
 		return QString::fromLatin1 (statementStrings[(int) statement]);
 	else
 		return "";
--- a/src/macros.h	Sat Jan 28 13:32:55 2017 +0200
+++ b/src/macros.h	Sat Jan 28 13:49:09 2017 +0200
@@ -23,10 +23,6 @@
 # define __attribute__(X)
 #endif
 
-template <typename T, size_t N>
-char (&countofHelper (T(&)[N]))[N];
-#define countof(x) ((int) sizeof (countofHelper(x)))
-
 #define DEFINE_CLASS(SELF, SUPER) \
 public: \
 	using Self = SELF; \
--- a/src/miscallenous.cpp	Sat Jan 28 13:32:55 2017 +0200
+++ b/src/miscallenous.cpp	Sat Jan 28 13:49:09 2017 +0200
@@ -61,7 +61,7 @@
 void roundToDecimals (double& a, int decimals)
 {
 	static const double factors[] = { 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9 };
-	if (decimals >= 0 and decimals < countof (factors))
+	if (decimals >= 0 and decimals < length (factors))
 		a = round (a * factors[decimals]) / factors[decimals];
 }
 
--- a/src/primitives.cpp	Sat Jan 28 13:32:55 2017 +0200
+++ b/src/primitives.cpp	Sat Jan 28 13:49:09 2017 +0200
@@ -405,7 +405,7 @@
 	// Not translated as primitives are in English.
 	const char* names[] = {"Circle", "Cylinder", "Disc", "Disc Negative", "Ring", "Cone"};
 
-	if (type >= 0 and type < countof(names))
+	if (type >= 0 and type < length(names))
 		return names[type];
 	else
 		return "Unknown";
--- a/src/toolsets/algorithmtoolset.cpp	Sat Jan 28 13:32:55 2017 +0200
+++ b/src/toolsets/algorithmtoolset.cpp	Sat Jan 28 13:49:09 2017 +0200
@@ -131,7 +131,7 @@
 			lines[3] = nullptr;
 		}
 
-		for (int i = 0; i < countof (lines); ++i)
+		for (int i = 0; i < length (lines); ++i)
 		{
 			if (lines[i] == nullptr)
 				continue;
--- a/src/types/matrix.cpp	Sat Jan 28 13:32:55 2017 +0200
+++ b/src/types/matrix.cpp	Sat Jan 28 13:49:09 2017 +0200
@@ -144,7 +144,7 @@
  */
 bool Matrix::operator==(const Matrix& other) const
 {
-	for (int i = 0; i < countof(m_values); ++i)
+	for (int i = 0; i < length(m_values); ++i)
 	{
 		if (not qFuzzyCompare(m_values[i], other.m_values[i]))
 			return false;

mercurial