# HG changeset patch # User Teemu Piippo # Date 1406335417 -10800 # Node ID 274a7fac44fcb100f2971208d004c91642af4bd2 # Parent e4ad109f33c66615a0f9e4e8493c94a774b505bc - refactor diff -r e4ad109f33c6 -r 274a7fac44fc src/actions.cc --- 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(); objs << spawn (bfctype); objs << spawn(); - 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(); + LDCommentPtr titleobj (getCurrentDocument()->getObject (0).dynamicCast()); // 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()->statement(); + BFCStatement a = obj.staticCast()->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; diff -r e4ad109f33c6 -r 274a7fac44fc src/addObjectDialog.cc --- 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 (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: diff -r e4ad109f33c6 -r 274a7fac44fc src/basics.h --- 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 +inline bool within (T a, T b, T c) +{ + return a >= b and a <= c; +} + +template void removeDuplicates (T& a) { std::sort (a.begin(), a.end()); diff -r e4ad109f33c6 -r 274a7fac44fc src/ldDocument.cc --- 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()->reason()); + print ("Couldn't parse line #%1: %2", + progress() + 1, obj.staticCast()->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::Statement) i); + for_enum (BFCStatement, i) + { + if (commentTextSimplified == format ("BFC %1", + LDBFC::StatementStrings[int (i)])) + { + return spawn (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::InvertNext); + return spawn (BFCStatement::InvertNext); elif (commentTextSimplified == "BFC CERTIFY CLIP") - return spawn (LDBFC::Clip); + return spawn (BFCStatement::Clip); elif (commentTextSimplified == "BFC CERTIFY NOCLIP") - return spawn (LDBFC::NoClip); + return spawn (BFCStatement::NoClip); } if (tokens.size() > 2 and tokens[1] == "!LDFORGE") @@ -929,7 +937,8 @@ LDVertexPtr obj = spawn(); 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") diff -r e4ad109f33c6 -r 274a7fac44fc src/ldObject.cc --- 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 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()->statement() == LDBFC::InvertNext) + if (prev != null and prev->type() == OBJ_BFC and + prev.staticCast()->statement() == BFCStatement::InvertNext) { ptr = prev.staticCast(); return true; @@ -712,7 +719,7 @@ { LDBFCPtr bfc = previous().dynamicCast(); - 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::InvertNext)); + document().toStrongRef()->insertObj (idx, spawn (BFCStatement::InvertNext)); } // ============================================================================= diff -r e4ad109f33c6 -r 274a7fac44fc src/ldObject.h --- 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; diff -r e4ad109f33c6 -r 274a7fac44fc src/macros.h --- 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::type FOR_ENUM_NAME (__LINE__) = \ + std::underlying_type::type (ENUM::FirstValue); \ + FOR_ENUM_NAME (__LINE__) < std::underlying_type::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 diff -r e4ad109f33c6 -r 274a7fac44fc src/mainWindow.cc --- 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()->statement()]; + descr = LDBFC::StatementStrings[int (obj.staticCast()->statement())]; break; } diff -r e4ad109f33c6 -r 274a7fac44fc src/primitives.cc --- 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 (format ("!LDRAW_ORG Unofficial_%1Primitive", divs == g_hires ? "48_" : "")) << spawn (license) << spawn() - << spawn (LDBFC::CertifyCCW) + << spawn (BFCStatement::CertifyCCW) << spawn(); f->addObjects (objs);