From 24890ecebda6bc39aaef2b90e6fffc0493a35637 Mon Sep 17 00:00:00 2001 From: Leonardo Zide Date: Mon, 9 Dec 2019 11:43:54 -0800 Subject: [PATCH] Added array count macro. --- common/lc_colors.cpp | 2 +- common/lc_commands.cpp | 2 +- common/lc_global.h | 5 ++++- common/lc_library.cpp | 8 ++++---- common/lc_synth.cpp | 8 ++++---- common/minifig.cpp | 2 +- qt/lc_qglwidget.cpp | 2 +- qt/lc_qpreferencesdialog.cpp | 4 ++-- qt/qtmain.cpp | 2 +- 9 files changed, 19 insertions(+), 16 deletions(-) diff --git a/common/lc_colors.cpp b/common/lc_colors.cpp index 3a042b7b..29ba8224 100644 --- a/common/lc_colors.cpp +++ b/common/lc_colors.cpp @@ -204,7 +204,7 @@ int lcGetBrickLinkColor(int ColorIndex) int ColorCode = gColorList[ColorIndex].Code; - for (unsigned int Color = 0; Color < sizeof(BrickLinkColors) / sizeof(BrickLinkColors[0]); Color++) + for (unsigned int Color = 0; Color < LC_ARRAY_COUNT(BrickLinkColors); Color++) if (BrickLinkColors[Color].LDraw == ColorCode) return BrickLinkColors[Color].BrickLink; diff --git a/common/lc_commands.cpp b/common/lc_commands.cpp index dd206ff8..e775aa6e 100644 --- a/common/lc_commands.cpp +++ b/common/lc_commands.cpp @@ -1565,4 +1565,4 @@ const char* gToolNames[LC_NUM_TOOLS] = QT_TRANSLATE_NOOP("Mouse", "ZoomRegion") // LC_TOOL_ZOOM_REGION }; -static_assert(sizeof(gToolNames) / sizeof(gToolNames[0]) == LC_NUM_TOOLS, "Array size mismatch."); +static_assert(LC_ARRAY_COUNT(gToolNames) == LC_NUM_TOOLS, "Array size mismatch."); diff --git a/common/lc_global.h b/common/lc_global.h index d6f449d9..6c9a0807 100644 --- a/common/lc_global.h +++ b/common/lc_global.h @@ -20,9 +20,12 @@ #endif #ifndef QT_STRINGIFY -#define QT_STRINGIFY(s) "" +#define QT_STRINGIFY2(x) #x +#define QT_STRINGIFY(x) QT_STRINGIFY2(x) #endif +#define LC_ARRAY_COUNT(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x]))))) + #if !defined(EGL_VERSION_1_0) && !defined(GL_ES_VERSION_2_0) && !defined(GL_ES_VERSION_3_0) && !defined(QT_OPENGL_ES) #undef GL_LINES_ADJACENCY_EXT #undef GL_LINE_STRIP_ADJACENCY_EXT diff --git a/common/lc_library.cpp b/common/lc_library.cpp index 0b0c63f4..ae3073d0 100644 --- a/common/lc_library.cpp +++ b/common/lc_library.cpp @@ -498,7 +498,7 @@ void lcPiecesLibrary::ReadArchiveDescriptions(const QString& OfficialFileName, c bool lcPiecesLibrary::OpenDirectory(const QDir& LibraryDir, bool ShowProgress) { const QLatin1String BaseFolders[LC_NUM_FOLDERTYPES] = { QLatin1String("unofficial/"), QLatin1String("") }; - const int NumBaseFolders = sizeof(BaseFolders) / sizeof(BaseFolders[0]); + const int NumBaseFolders = LC_ARRAY_COUNT(BaseFolders); QFileInfoList FileLists[NumBaseFolders]; @@ -515,13 +515,13 @@ bool lcPiecesLibrary::OpenDirectory(const QDir& LibraryDir, bool ShowProgress) mHasUnofficial = !FileLists[LC_FOLDER_UNOFFICIAL].isEmpty(); ReadDirectoryDescriptions(FileLists, ShowProgress); - for (unsigned int BaseFolderIdx = 0; BaseFolderIdx < sizeof(BaseFolders) / sizeof(BaseFolders[0]); BaseFolderIdx++) + for (unsigned int BaseFolderIdx = 0; BaseFolderIdx < LC_ARRAY_COUNT(BaseFolders); BaseFolderIdx++) { const char* PrimitiveDirectories[] = { "p/", "parts/s/" }; bool SubFileDirectories[] = { false, false, true }; QDir BaseDir(LibraryDir.absoluteFilePath(QLatin1String(BaseFolders[BaseFolderIdx]))); - for (int DirectoryIdx = 0; DirectoryIdx < (int)(sizeof(PrimitiveDirectories) / sizeof(PrimitiveDirectories[0])); DirectoryIdx++) + for (int DirectoryIdx = 0; DirectoryIdx < (int)(LC_ARRAY_COUNT(PrimitiveDirectories)); DirectoryIdx++) { QString ChildPath = BaseDir.absoluteFilePath(QLatin1String(PrimitiveDirectories[DirectoryIdx])); QDirIterator DirIterator(ChildPath, QStringList() << QLatin1String("*.dat"), QDir::Files | QDir::Hidden | QDir::Readable, QDirIterator::Subdirectories); @@ -567,7 +567,7 @@ bool lcPiecesLibrary::OpenDirectory(const QDir& LibraryDir, bool ShowProgress) } } - for (unsigned int BaseFolderIdx = 0; BaseFolderIdx < sizeof(BaseFolders) / sizeof(BaseFolders[0]); BaseFolderIdx++) + for (unsigned int BaseFolderIdx = 0; BaseFolderIdx < LC_ARRAY_COUNT(BaseFolders); BaseFolderIdx++) { QDir BaseDir(LibraryDir.absoluteFilePath(QLatin1String(BaseFolders[BaseFolderIdx]))); QDir Dir(BaseDir.absoluteFilePath(QLatin1String("parts/textures/")), QLatin1String("*.png"), QDir::SortFlags(QDir::Name | QDir::IgnoreCase), QDir::Files | QDir::Hidden | QDir::Readable); diff --git a/common/lc_synth.cpp b/common/lc_synth.cpp index f0119c2d..53395dfe 100644 --- a/common/lc_synth.cpp +++ b/common/lc_synth.cpp @@ -93,7 +93,7 @@ void lcSynthInit() { "61927.dat", lcSynthType::ACTUATOR, 170.00f, 1 } // Technic Power Functions Linear Actuator (Contracted) }; - for (unsigned int InfoIdx = 0; InfoIdx < sizeof(HoseInfo) / sizeof(HoseInfo[0]); InfoIdx++) + for (unsigned int InfoIdx = 0; InfoIdx < LC_ARRAY_COUNT(HoseInfo); InfoIdx++) { PieceInfo* Info = Library->FindPiece(HoseInfo[InfoIdx].PartID, nullptr, false, false); @@ -628,8 +628,8 @@ void lcSynthInfo::AddFlexHoseParts(lcMemFile& File, lcLibraryMeshData& MeshData, } }; - AddSectionVertices(OutsideSectionVertices, sizeof(OutsideSectionVertices) / sizeof(OutsideSectionVertices[0])); - AddSectionVertices(InsideSectionVertices, sizeof(InsideSectionVertices) / sizeof(InsideSectionVertices[0])); + AddSectionVertices(OutsideSectionVertices, LC_ARRAY_COUNT(OutsideSectionVertices)); + AddSectionVertices(InsideSectionVertices, LC_ARRAY_COUNT(InsideSectionVertices)); } void lcSynthInfo::AddRibbedHoseParts(lcMemFile& File, const lcArray& Sections) const @@ -745,7 +745,7 @@ void lcSynthInfo::AddFlexibleAxleParts(lcMemFile& File, lcLibraryMeshData& MeshD { lcVector3(-5.602f, 0.0f, -2.000f), lcVector3(-0.942f, 0.0f, -0.336f), 4.0f, lcVector2(0.0f, 0.0f), 0 } }; - const int NumSectionVertices = sizeof(SectionVertices) / sizeof(SectionVertices[0]); + const int NumSectionVertices = LC_ARRAY_COUNT(SectionVertices); int BaseVertex; lcLibraryMeshVertex* VertexBuffer; diff --git a/common/minifig.cpp b/common/minifig.cpp index 343eae25..318c26b5 100644 --- a/common/minifig.cpp +++ b/common/minifig.cpp @@ -95,7 +95,7 @@ void MinifigWizard::OnInitialUpdate() MakeCurrent(); mContext->SetDefaultState(); - static_assert(sizeof(MinifigWizard::mSectionNames) / sizeof(MinifigWizard::mSectionNames[0]) == LC_MFW_NUMITEMS, "Array size mismatch."); + static_assert(LC_ARRAY_COUNT(MinifigWizard::mSectionNames) == LC_MFW_NUMITEMS, "Array size mismatch."); const int ColorCodes[LC_MFW_NUMITEMS] = { 4, 7, 14, 7, 1, 0, 7, 4, 4, 14, 14, 7, 7, 0, 0, 7, 7 }; const char* Pieces[LC_MFW_NUMITEMS] = { "3624.dat", "", "3626bp01.dat", "", "973.dat", "3815.dat", "", "3819.dat", "3818.dat", "3820.dat", "3820.dat", "", "", "3817.dat", "3816.dat", "", "" }; diff --git a/qt/lc_qglwidget.cpp b/qt/lc_qglwidget.cpp index bd2f574f..8a87cfa6 100644 --- a/qt/lc_qglwidget.cpp +++ b/qt/lc_qglwidget.cpp @@ -66,7 +66,7 @@ void lcGLWidget::SetCursor(LC_CURSOR_TYPE CursorType) { 15, 15, ":/resources/cursor_rotate_view" }, // LC_CURSOR_ROTATE_VIEW }; - static_assert(sizeof(Cursors) / sizeof(Cursors[0]) == LC_CURSOR_COUNT, "Array size mismatch"); + static_assert(LC_ARRAY_COUNT(Cursors) == LC_CURSOR_COUNT, "Array size mismatch"); QGLWidget* widget = (QGLWidget*)mWidget; diff --git a/qt/lc_qpreferencesdialog.cpp b/qt/lc_qpreferencesdialog.cpp index c938514a..8fee63db 100644 --- a/qt/lc_qpreferencesdialog.cpp +++ b/qt/lc_qpreferencesdialog.cpp @@ -46,7 +46,7 @@ lcQPreferencesDialog::lcQPreferencesDialog(QWidget* Parent, lcPreferencesDialogO ui->lgeoPath->setText(mOptions->LGEOPath); ui->authorName->setText(mOptions->DefaultAuthor); ui->mouseSensitivity->setValue(mOptions->Preferences.mMouseSensitivity); - for (unsigned int LanguageIdx = 0; LanguageIdx < sizeof(gLanguageLocales) / sizeof(gLanguageLocales[0]); LanguageIdx++) + for (unsigned int LanguageIdx = 0; LanguageIdx < LC_ARRAY_COUNT(gLanguageLocales); LanguageIdx++) { if (mOptions->Language == gLanguageLocales[LanguageIdx]) { @@ -168,7 +168,7 @@ void lcQPreferencesDialog::accept() mOptions->Preferences.mMouseSensitivity = ui->mouseSensitivity->value(); int Language = ui->Language->currentIndex(); - if (Language < 0 || Language > sizeof(gLanguageLocales) / sizeof(gLanguageLocales[0])) + if (Language < 0 || Language > LC_ARRAY_COUNT(gLanguageLocales)) Language = 0; mOptions->Language = gLanguageLocales[Language]; diff --git a/qt/qtmain.cpp b/qt/qtmain.cpp index de2333f1..04fbbac5 100644 --- a/qt/qtmain.cpp +++ b/qt/qtmain.cpp @@ -71,7 +71,7 @@ static LONG WINAPI lcSehHandler(PEXCEPTION_POINTERS exceptionPointers) static void lcSehInit() { - if (GetTempPath(sizeof(gMinidumpPath) / sizeof(gMinidumpPath[0]), gMinidumpPath)) + if (GetTempPath(LC_ARRAY_SIZE(gMinidumpPath), gMinidumpPath)) lstrcat(gMinidumpPath, TEXT("leocad.dmp")); SetUnhandledExceptionFilter(lcSehHandler);