From e52ea0ab6370fd4762bca90d4ea32ef9bb15fb51 Mon Sep 17 00:00:00 2001 From: Leonardo Zide Date: Sun, 6 Dec 2020 11:26:55 -0800 Subject: [PATCH] Added LC_ARRAY_SIZE_CHECK for static array checks. --- common/lc_commands.cpp | 6 +++--- common/lc_commands.h | 2 +- common/lc_context.cpp | 8 ++++++-- common/lc_global.h | 1 + common/lc_glwidget.cpp | 12 ++++++------ common/lc_mainwindow.cpp | 4 +++- common/minifig.cpp | 2 +- common/view.cpp | 4 +++- 8 files changed, 24 insertions(+), 15 deletions(-) diff --git a/common/lc_commands.cpp b/common/lc_commands.cpp index 6a9548c3..5e315f89 100644 --- a/common/lc_commands.cpp +++ b/common/lc_commands.cpp @@ -1657,9 +1657,9 @@ lcCommand gCommands[LC_NUM_COMMANDS] = } }; -static_assert(sizeof(gCommands)/sizeof(gCommands[0]) == LC_NUM_COMMANDS, "Array size mismatch."); +LC_ARRAY_SIZE_CHECK(gCommands, LC_NUM_COMMANDS); -const char* gToolNames[static_cast(lcTool::Count)] = +const char* gToolNames[] = { QT_TRANSLATE_NOOP("Mouse", "NewPiece"), // lcTool::Insert QT_TRANSLATE_NOOP("Mouse", "NewPointLight"), // lcTool::Light @@ -1678,4 +1678,4 @@ const char* gToolNames[static_cast(lcTool::Count)] = QT_TRANSLATE_NOOP("Mouse", "ZoomRegion") // lcTool::ZoomRegion }; -static_assert(LC_ARRAY_COUNT(gToolNames) == static_cast(lcTool::Count), "Array size mismatch."); +LC_ARRAY_SIZE_CHECK(gToolNames, lcTool::Count); diff --git a/common/lc_commands.h b/common/lc_commands.h index acc56cf2..fa61f1d6 100644 --- a/common/lc_commands.h +++ b/common/lc_commands.h @@ -287,5 +287,5 @@ enum class lcTool Count }; -extern const char* gToolNames[static_cast(lcTool::Count)]; +extern const char* gToolNames[]; diff --git a/common/lc_context.cpp b/common/lc_context.cpp index 5812ecf1..a237e3dc 100644 --- a/common/lc_context.cpp +++ b/common/lc_context.cpp @@ -102,7 +102,7 @@ void lcContext::CreateShaderPrograms() " LC_SHADER_PRECISION float Diffuse = min(abs(dot(Normal, LightDirection)) * 0.6 + 0.65, 1.0);\n" }; - const char* const VertexShaders[static_cast(lcMaterialType::Count)] = + const char* const VertexShaders[] = { ":/resources/shaders/unlit_color_vs.glsl", // UnlitColor ":/resources/shaders/unlit_texture_modulate_vs.glsl", // UnlitTextureModulate @@ -113,7 +113,9 @@ void lcContext::CreateShaderPrograms() ":/resources/shaders/fakelit_texture_decal_vs.glsl" // FakeLitTextureDecal }; - const char* const FragmentShaders[static_cast(lcMaterialType::Count)] = + LC_ARRAY_SIZE_CHECK(VertexShaders, lcMaterialType::Count); + + const char* const FragmentShaders[] = { ":/resources/shaders/unlit_color_ps.glsl", // UnlitColor ":/resources/shaders/unlit_texture_modulate_ps.glsl", // UnlitTextureModulate @@ -124,6 +126,8 @@ void lcContext::CreateShaderPrograms() ":/resources/shaders/fakelit_texture_decal_ps.glsl" // FakeLitTextureDecal }; + LC_ARRAY_SIZE_CHECK(FragmentShaders, lcMaterialType::Count); + const auto LoadShader = [ShaderPrefix](const char* FileName, GLuint ShaderType) -> GLuint { QResource Resource(FileName); diff --git a/common/lc_global.h b/common/lc_global.h index 9632742f..bf3dcdcd 100644 --- a/common/lc_global.h +++ b/common/lc_global.h @@ -26,6 +26,7 @@ #endif #define LC_ARRAY_COUNT(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x]))))) +#define LC_ARRAY_SIZE_CHECK(a,s) static_assert(LC_ARRAY_COUNT(a) == static_cast(s), QT_STRINGIFY(a) " size mismatch.") #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 diff --git a/common/lc_glwidget.cpp b/common/lc_glwidget.cpp index cfddcb59..701b0b97 100644 --- a/common/lc_glwidget.cpp +++ b/common/lc_glwidget.cpp @@ -70,7 +70,7 @@ lcCursor lcGLWidget::GetCursor() const return lcCursor::SelectRemove; } - const lcCursor CursorFromTrackTool[static_cast(lcTrackTool::Count)] = + constexpr lcCursor CursorFromTrackTool[] = { lcCursor::Select, // lcTrackTool::None lcCursor::Brick, // lcTrackTool::Insert @@ -104,7 +104,7 @@ lcCursor lcGLWidget::GetCursor() const lcCursor::ZoomRegion // lcTrackTool::ZoomRegion }; - static_assert(LC_ARRAY_COUNT(CursorFromTrackTool) == static_cast(lcTrackTool::Count), "Array size mismatch."); + LC_ARRAY_SIZE_CHECK(CursorFromTrackTool, lcTrackTool::Count); if (mTrackTool >= lcTrackTool::None && mTrackTool < lcTrackTool::Count) return CursorFromTrackTool[static_cast(mTrackTool)]; @@ -123,7 +123,7 @@ void lcGLWidget::SetCursor(lcCursor CursorType) const char* Name; }; - const lcCursorInfo Cursors[static_cast(lcCursor::Count)] = + constexpr lcCursorInfo Cursors[] = { { 0, 0, "" }, // lcCursor::Hidden { 0, 0, "" }, // lcCursor::Default @@ -148,7 +148,7 @@ void lcGLWidget::SetCursor(lcCursor CursorType) { 15, 15, ":/resources/cursor_rotate_view" }, // lcCursor::RotateView }; - static_assert(LC_ARRAY_COUNT(Cursors) == static_cast(lcCursor::Count), "Array size mismatch"); + LC_ARRAY_SIZE_CHECK(Cursors, lcCursor::Count); if (CursorType == lcCursor::Hidden) { @@ -175,7 +175,7 @@ void lcGLWidget::UpdateCursor() lcTool lcGLWidget::GetCurrentTool() const { - const lcTool ToolFromTrackTool[static_cast(lcTrackTool::Count)] = + constexpr lcTool ToolFromTrackTool[] = { lcTool::Select, // lcTrackTool::None lcTool::Insert, // lcTrackTool::Insert @@ -209,7 +209,7 @@ lcTool lcGLWidget::GetCurrentTool() const lcTool::ZoomRegion // lcTrackTool::ZoomRegion }; - static_assert(LC_ARRAY_COUNT(ToolFromTrackTool) == static_cast(lcTrackTool::Count), "Array size mismatch."); + LC_ARRAY_SIZE_CHECK(ToolFromTrackTool, lcTrackTool::Count); if (mTrackTool >= lcTrackTool::None && mTrackTool < lcTrackTool::Count) return ToolFromTrackTool[static_cast(mTrackTool)]; diff --git a/common/lc_mainwindow.cpp b/common/lc_mainwindow.cpp index 2fd38aa9..cbf7e218 100644 --- a/common/lc_mainwindow.cpp +++ b/common/lc_mainwindow.cpp @@ -1771,7 +1771,7 @@ void lcMainWindow::SetTransformType(lcTransformType TransformType) mTransformType = TransformType; - const char* IconNames[static_cast(lcTransformType::Count)] = + constexpr const char* IconNames[] = { ":/resources/edit_transform_absolute_translation.png", ":/resources/edit_transform_relative_translation.png", @@ -1779,6 +1779,8 @@ void lcMainWindow::SetTransformType(lcTransformType TransformType) ":/resources/edit_transform_relative_rotation.png" }; + LC_ARRAY_SIZE_CHECK(IconNames, lcTransformType::Count); + int TransformIndex = static_cast(TransformType); mActions[LC_EDIT_TRANSFORM_ABSOLUTE_TRANSLATION + TransformIndex]->setChecked(true); mActions[LC_EDIT_TRANSFORM]->setIcon(QIcon(IconNames[TransformIndex])); diff --git a/common/minifig.cpp b/common/minifig.cpp index eae024ab..f2dff1ea 100644 --- a/common/minifig.cpp +++ b/common/minifig.cpp @@ -96,7 +96,7 @@ void MinifigWizard::OnInitialUpdate() MakeCurrent(); mContext->SetDefaultState(); - static_assert(LC_ARRAY_COUNT(MinifigWizard::mSectionNames) == LC_MFW_NUMITEMS, "Array size mismatch."); + LC_ARRAY_SIZE_CHECK(MinifigWizard::mSectionNames, LC_MFW_NUMITEMS); 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* const 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/common/view.cpp b/common/view.cpp index d778c8dd..1202016e 100644 --- a/common/view.cpp +++ b/common/view.cpp @@ -1714,7 +1714,7 @@ lcTrackTool View::GetOverrideTrackTool(Qt::MouseButton Button) const if (OverrideTool == lcTool::Count) return lcTrackTool::None; - lcTrackTool TrackToolFromTool[static_cast(lcTool::Count)] = + constexpr lcTrackTool TrackToolFromTool[] = { lcTrackTool::Insert, // lcTool::Insert lcTrackTool::PointLight, // lcTool::Light @@ -1733,6 +1733,8 @@ lcTrackTool View::GetOverrideTrackTool(Qt::MouseButton Button) const lcTrackTool::ZoomRegion // lcTool::ZoomRegion }; + LC_ARRAY_SIZE_CHECK(TrackToolFromTool, lcTool::Count); + return TrackToolFromTool[static_cast(OverrideTool)]; }