From af7ba1c801f633e3411074221e5a3af921148cea Mon Sep 17 00:00:00 2001 From: Paul Gessinger Date: Fri, 29 Nov 2024 11:35:56 +0100 Subject: [PATCH] use checked_cast in GridJsonConverter and PortalLink tests --- Plugins/Json/src/GridJsonConverter.cpp | 21 ++++-- .../Core/Geometry/PortalLinkTests.cpp | 64 +++++++++++-------- .../Plugins/Json/GridJsonConverterTests.cpp | 5 +- 3 files changed, 53 insertions(+), 37 deletions(-) diff --git a/Plugins/Json/src/GridJsonConverter.cpp b/Plugins/Json/src/GridJsonConverter.cpp index 23fd57d7045..47849de5730 100644 --- a/Plugins/Json/src/GridJsonConverter.cpp +++ b/Plugins/Json/src/GridJsonConverter.cpp @@ -10,6 +10,7 @@ #include "Acts/Plugins/Json/AlgebraJsonConverter.hpp" #include "Acts/Plugins/Json/UtilitiesJsonConverter.hpp" +#include "Acts/Utilities/CheckedCast.hpp" #include "Acts/Utilities/IAxis.hpp" nlohmann::json Acts::AxisJsonConverter::toJson(const IAxis& ia) { @@ -52,7 +53,8 @@ void encodeSubspace( nlohmann::json& jGlobalToGridLocal, const Acts::GridAccess::IGlobalToGridLocal& globalToGridLocal, const Subspace& /*subspace*/) { - const Subspace* subspace = dynamic_cast(&globalToGridLocal); + const Subspace* subspace = + Acts::checked_cast(&globalToGridLocal); if (subspace != nullptr) { jGlobalToGridLocal["type"] = "subspace"; jGlobalToGridLocal["accessors"] = subspace->bValues; @@ -65,7 +67,7 @@ void encodeTransformedSubspace( const Acts::GridAccess::IGlobalToGridLocal& globalToGridLocal, const Subspace& subscpace) { const Acts::GridAccess::Affine3Transformed* tsubspace = - dynamic_cast*>( + Acts::checked_cast*>( &globalToGridLocal); if (tsubspace != nullptr) { encodeSubspace(jGlobalToGridLocal, tsubspace->globalToGridLocal, subscpace); @@ -409,35 +411,40 @@ nlohmann::json Acts::GridAccessJsonConverter::toJson( nlohmann::json jBoundToGridLocal; auto localSubSpace0 = - dynamic_cast*>(&boundToGridLocal); + Acts::checked_cast*>( + &boundToGridLocal); if (localSubSpace0 != nullptr) { jBoundToGridLocal["type"] = "subspace"; jBoundToGridLocal["accessors"] = localSubSpace0->accessors; } auto localSubSpace1 = - dynamic_cast*>(&boundToGridLocal); + Acts::checked_cast*>( + &boundToGridLocal); if (localSubSpace1 != nullptr) { jBoundToGridLocal["type"] = "subspace"; jBoundToGridLocal["accessors"] = localSubSpace1->accessors; } auto localSubSpace01 = - dynamic_cast*>(&boundToGridLocal); + Acts::checked_cast*>( + &boundToGridLocal); if (localSubSpace01 != nullptr) { jBoundToGridLocal["type"] = "subspace"; jBoundToGridLocal["accessors"] = localSubSpace01->accessors; } auto localSubSpace10 = - dynamic_cast*>(&boundToGridLocal); + Acts::checked_cast*>( + &boundToGridLocal); if (localSubSpace10 != nullptr) { jBoundToGridLocal["type"] = "subspace"; jBoundToGridLocal["accessors"] = localSubSpace10->accessors; } auto boundCylinderToZPhi = - dynamic_cast(&boundToGridLocal); + Acts::checked_cast( + &boundToGridLocal); if (boundCylinderToZPhi != nullptr) { jBoundToGridLocal["type"] = "cylinder_to_zphi"; jBoundToGridLocal["radius"] = boundCylinderToZPhi->radius; diff --git a/Tests/UnitTests/Core/Geometry/PortalLinkTests.cpp b/Tests/UnitTests/Core/Geometry/PortalLinkTests.cpp index fd87545d4f0..3ac2579ca67 100644 --- a/Tests/UnitTests/Core/Geometry/PortalLinkTests.cpp +++ b/Tests/UnitTests/Core/Geometry/PortalLinkTests.cpp @@ -24,6 +24,7 @@ #include "Acts/Surfaces/SurfaceMergingException.hpp" #include "Acts/Tests/CommonHelpers/FloatComparisons.hpp" #include "Acts/Utilities/AxisFwd.hpp" +#include "Acts/Utilities/CheckedCast.hpp" #include "Acts/Utilities/ThrowAssert.hpp" #include @@ -159,7 +160,7 @@ BOOST_AUTO_TEST_CASE(Cylinder) { Axis axis2Expected{AxisBound, -100_mm, 100_mm, 10}; BOOST_CHECK_EQUAL(*axis2, axis2Expected); - auto& concrete = dynamic_cast< + auto& concrete = Acts::checked_cast< GridPortalLinkT&>( *grid2dCyl1); @@ -176,7 +177,7 @@ BOOST_AUTO_TEST_CASE(Cylinder) { BOOST_CHECK_EQUAL(*axis1, axis1Explicit); BOOST_CHECK_EQUAL(*axis2, axis2Expected); - auto& concrete2 = dynamic_cast< + auto& concrete2 = Acts::checked_cast< GridPortalLinkT&>( *grid2dCyl1Explicit); @@ -207,8 +208,8 @@ BOOST_AUTO_TEST_CASE(Cylinder) { BOOST_CHECK_EQUAL(*axis2Phi, axis2PhiExpected); auto& concrete3 = - dynamic_cast&>( + Acts::checked_cast&>( *grid2dCylPhi); checkAllBins(concrete3); @@ -223,8 +224,8 @@ BOOST_AUTO_TEST_CASE(Cylinder) { BOOST_CHECK_EQUAL(*axis2Phi, axis2PhiExpected); auto& concrete4 = - dynamic_cast&>( + Acts::checked_cast&>( *grid2dCylPhiExplicit); checkAllBins(concrete4); @@ -362,8 +363,9 @@ BOOST_AUTO_TEST_CASE(Disc) { BOOST_CHECK_EQUAL(axis2->getBoundaryType(), AxisBoundaryType::Closed); checkAllBins( - dynamic_cast>&>(*grid2d)); + Acts::checked_cast>&>( + *grid2d)); Axis axis2Explicit{AxisClosed, -180_degree, 180_degree, 3}; auto grid2dExplicit = grid1->extendTo2d(&axis2Explicit); @@ -374,8 +376,8 @@ BOOST_AUTO_TEST_CASE(Disc) { BOOST_CHECK_EQUAL(*axis1, axis1Expected); BOOST_CHECK_EQUAL(*axis2, axis2Explicit); - checkAllBins(dynamic_cast&>( + checkAllBins(Acts::checked_cast&>( *grid2dExplicit)); auto gridPhiBinnedInR = GridPortalLink::make( @@ -396,8 +398,8 @@ BOOST_AUTO_TEST_CASE(Disc) { AxisBoundaryType::Bound); checkAllBins( - dynamic_cast>&>( + Acts::checked_cast>&>( *grid2dPhiNonClosed)); Axis axisPhiNonClosedExplicit{AxisBound, -45_degree, 45_degree, 3}; @@ -410,10 +412,10 @@ BOOST_AUTO_TEST_CASE(Disc) { BOOST_CHECK_EQUAL(*grid2dPhiNonClosedExplicit->grid().axes().front(), gridPhiBinnedInRExpected); - checkAllBins( - dynamic_cast&>( - *grid2dPhiNonClosedExplicit)); + checkAllBins(Acts::checked_cast< + GridPortalLinkT&>( + *grid2dPhiNonClosedExplicit)); auto grid2dPhi = gridPhi->extendTo2d(nullptr); BOOST_REQUIRE(grid2dPhi); @@ -423,8 +425,9 @@ BOOST_AUTO_TEST_CASE(Disc) { BOOST_CHECK_EQUAL(*grid2dPhi->grid().axes().back(), axisPhi1Expected); checkAllBins( - dynamic_cast&>(*grid2dPhi)); + Acts::checked_cast&>( + *grid2dPhi)); Axis axis2dPhiExplicit{AxisBound, 30_mm, 100_mm, 3}; auto grid2dPhiExplicit = gridPhi->extendTo2d(&axis2dPhiExplicit); @@ -435,9 +438,10 @@ BOOST_AUTO_TEST_CASE(Disc) { BOOST_CHECK_EQUAL(*grid2dPhiExplicit->grid().axes().back(), axisPhi1Expected); - checkAllBins(dynamic_cast&>( - *grid2dPhiExplicit)); + checkAllBins( + Acts::checked_cast&>( + *grid2dPhiExplicit)); } BOOST_TEST_CONTEXT("2D") { @@ -1465,7 +1469,8 @@ BOOST_AUTO_TEST_CASE(BinFilling) { using merged_type = GridPortalLinkT>; - const auto* merged = dynamic_cast(mergedPtr.get()); + const auto* merged = + Acts::checked_cast(mergedPtr.get()); BOOST_REQUIRE(merged); grid1->printContents(std::cout); @@ -1503,7 +1508,8 @@ BOOST_AUTO_TEST_CASE(BinFilling) { using merged_type = GridPortalLinkT>; - const auto* merged = dynamic_cast(mergedPtr.get()); + const auto* merged = + Acts::checked_cast(mergedPtr.get()); BOOST_REQUIRE(merged); grid1->printContents(std::cout); @@ -1655,7 +1661,8 @@ BOOST_AUTO_TEST_CASE(BinFilling) { GridPortalLinkT, Axis>; - const auto* merged = dynamic_cast(mergedPtr.get()); + const auto* merged = + Acts::checked_cast(mergedPtr.get()); BOOST_REQUIRE(merged); checkCheckerBoard(merged->grid()); @@ -1666,7 +1673,7 @@ BOOST_AUTO_TEST_CASE(BinFilling) { mergedPtr = GridPortalLink::merge(*discPhiGrid1, *discPhiGrid2, BinningValue::binR, *logger); - merged = dynamic_cast(mergedPtr.get()); + merged = Acts::checked_cast(mergedPtr.get()); BOOST_REQUIRE(merged); const auto* v1 = vol1.get(); @@ -1728,7 +1735,8 @@ BOOST_AUTO_TEST_CASE(BinFilling) { GridPortalLinkT, Axis>; - const auto* merged = dynamic_cast(mergedPtr.get()); + const auto* merged = + Acts::checked_cast(mergedPtr.get()); BOOST_REQUIRE(merged); checkCheckerBoard(merged->grid()); @@ -1739,7 +1747,7 @@ BOOST_AUTO_TEST_CASE(BinFilling) { mergedPtr = GridPortalLink::merge(*grid1, *grid2, BinningValue::binPhi, *logger); - merged = dynamic_cast(mergedPtr.get()); + merged = Acts::checked_cast(mergedPtr.get()); BOOST_REQUIRE(merged); const auto* v1 = vol1.get(); @@ -2013,7 +2021,7 @@ BOOST_AUTO_TEST_CASE(PhiDirection) { GridPortalLinkT, Axis>; - const auto* merged = dynamic_cast(mergedPtr.get()); + const auto* merged = Acts::checked_cast(mergedPtr.get()); BOOST_REQUIRE_NE(merged, nullptr); BOOST_CHECK_EQUAL(merged->grid().axes().size(), 2); diff --git a/Tests/UnitTests/Plugins/Json/GridJsonConverterTests.cpp b/Tests/UnitTests/Plugins/Json/GridJsonConverterTests.cpp index cac514a18ac..5fb306c990e 100644 --- a/Tests/UnitTests/Plugins/Json/GridJsonConverterTests.cpp +++ b/Tests/UnitTests/Plugins/Json/GridJsonConverterTests.cpp @@ -10,6 +10,7 @@ #include "Acts/Plugins/Json/GridJsonConverter.hpp" #include "Acts/Tests/CommonHelpers/FloatComparisons.hpp" +#include "Acts/Utilities/CheckedCast.hpp" #include "Acts/Utilities/GridAccessHelpers.hpp" #include "Acts/Utilities/GridAxisGenerators.hpp" @@ -257,7 +258,7 @@ namespace { template bool checkType(const ReferenceType& /**unused*/, const CheckTypeUniquePtr& g2l) { - return (dynamic_cast(g2l.get()) != nullptr); + return (Acts::checked_cast(g2l.get()) != nullptr); } template @@ -476,7 +477,7 @@ BOOST_AUTO_TEST_CASE(BoundCylinderToZPhiTest) { BOOST_REQUIRE(boundCylinderToZPhiRead != nullptr); const Acts::GridAccess::BoundCylinderToZPhi* bct = - dynamic_cast( + Acts::checked_cast( boundCylinderToZPhiRead.get()); auto delegate =