Replace reinterpret_cast from/to pointers to BitCast

This commit is contained in:
SirLynix 2024-01-03 17:19:42 +01:00
parent 6d1e7d7852
commit cd8c1cb160
10 changed files with 25 additions and 23 deletions

View File

@ -520,7 +520,7 @@ namespace Nz
UInt64 VulkanHandleToInteger(T handle) UInt64 VulkanHandleToInteger(T handle)
{ {
if constexpr (std::is_pointer_v<T>) if constexpr (std::is_pointer_v<T>)
return static_cast<UInt64>(reinterpret_cast<std::uintptr_t>(handle)); return static_cast<UInt64>(BitCast<std::uintptr_t>(handle));
else else
{ {
static_assert(std::is_integral_v<T>); static_assert(std::is_integral_v<T>);

View File

@ -33,7 +33,7 @@ namespace Nz
if (m_character) if (m_character)
{ {
JPH::BodyInterface& bodyInterface = m_world->GetPhysicsSystem()->GetBodyInterfaceNoLock(); JPH::BodyInterface& bodyInterface = m_world->GetPhysicsSystem()->GetBodyInterfaceNoLock();
bodyInterface.SetUserData(m_character->GetBodyID(), SafeCast<UInt64>(reinterpret_cast<std::uintptr_t>(this))); bodyInterface.SetUserData(m_character->GetBodyID(), SafeCast<UInt64>(BitCast<std::uintptr_t>(this)));
} }
if (m_world) if (m_world)
@ -149,7 +149,7 @@ namespace Nz
if (m_character) if (m_character)
{ {
JPH::BodyInterface& bodyInterface = m_world->GetPhysicsSystem()->GetBodyInterfaceNoLock(); JPH::BodyInterface& bodyInterface = m_world->GetPhysicsSystem()->GetBodyInterfaceNoLock();
bodyInterface.SetUserData(m_character->GetBodyID(), SafeCast<UInt64>(reinterpret_cast<std::uintptr_t>(this))); bodyInterface.SetUserData(m_character->GetBodyID(), SafeCast<UInt64>(BitCast<std::uintptr_t>(this)));
} }
return *this; return *this;
@ -175,7 +175,7 @@ namespace Nz
m_bodyIndex = m_character->GetBodyID().GetIndex(); m_bodyIndex = m_character->GetBodyID().GetIndex();
JPH::BodyInterface& bodyInterface = m_world->GetPhysicsSystem()->GetBodyInterfaceNoLock(); JPH::BodyInterface& bodyInterface = m_world->GetPhysicsSystem()->GetBodyInterfaceNoLock();
bodyInterface.SetUserData(m_character->GetBodyID(), SafeCast<UInt64>(reinterpret_cast<std::uintptr_t>(this))); bodyInterface.SetUserData(m_character->GetBodyID(), SafeCast<UInt64>(BitCast<std::uintptr_t>(this)));
m_world->RegisterStepListener(this); m_world->RegisterStepListener(this);
} }

View File

@ -19,7 +19,7 @@ namespace Nz
m_constraint(std::move(constraint.m_constraint)) m_constraint(std::move(constraint.m_constraint))
{ {
if (m_constraint) if (m_constraint)
m_constraint->SetUserData(SafeCast<UInt64>(reinterpret_cast<std::uintptr_t>(this))); m_constraint->SetUserData(SafeCast<UInt64>(BitCast<std::uintptr_t>(this)));
} }
JoltConstraint3D::~JoltConstraint3D() JoltConstraint3D::~JoltConstraint3D()
@ -29,24 +29,24 @@ namespace Nz
JoltRigidBody3D& JoltConstraint3D::GetBodyA() JoltRigidBody3D& JoltConstraint3D::GetBodyA()
{ {
return *reinterpret_cast<JoltRigidBody3D*>(static_cast<std::uintptr_t>(m_constraint->GetBody1()->GetUserData())); return *BitCast<JoltRigidBody3D*>(static_cast<std::uintptr_t>(m_constraint->GetBody1()->GetUserData()));
} }
const JoltRigidBody3D& JoltConstraint3D::GetBodyA() const const JoltRigidBody3D& JoltConstraint3D::GetBodyA() const
{ {
return *reinterpret_cast<JoltRigidBody3D*>(static_cast<std::uintptr_t>(m_constraint->GetBody1()->GetUserData())); return *BitCast<JoltRigidBody3D*>(static_cast<std::uintptr_t>(m_constraint->GetBody1()->GetUserData()));
} }
JoltRigidBody3D& JoltConstraint3D::GetBodyB() JoltRigidBody3D& JoltConstraint3D::GetBodyB()
{ {
NazaraAssert(!IsSingleBody(), "constraint is not attached to a second body"); NazaraAssert(!IsSingleBody(), "constraint is not attached to a second body");
return *reinterpret_cast<JoltRigidBody3D*>(static_cast<std::uintptr_t>(m_constraint->GetBody2()->GetUserData())); return *BitCast<JoltRigidBody3D*>(static_cast<std::uintptr_t>(m_constraint->GetBody2()->GetUserData()));
} }
const JoltRigidBody3D& JoltConstraint3D::GetBodyB() const const JoltRigidBody3D& JoltConstraint3D::GetBodyB() const
{ {
NazaraAssert(!IsSingleBody(), "constraint is not attached to a second body"); NazaraAssert(!IsSingleBody(), "constraint is not attached to a second body");
return *reinterpret_cast<JoltRigidBody3D*>(static_cast<std::uintptr_t>(m_constraint->GetBody2()->GetUserData())); return *BitCast<JoltRigidBody3D*>(static_cast<std::uintptr_t>(m_constraint->GetBody2()->GetUserData()));
} }
JoltPhysWorld3D& JoltConstraint3D::GetWorld() JoltPhysWorld3D& JoltConstraint3D::GetWorld()
@ -71,7 +71,7 @@ namespace Nz
m_constraint = std::move(constraint.m_constraint); m_constraint = std::move(constraint.m_constraint);
if (m_constraint) if (m_constraint)
m_constraint->SetUserData(SafeCast<UInt64>(reinterpret_cast<std::uintptr_t>(this))); m_constraint->SetUserData(SafeCast<UInt64>(BitCast<std::uintptr_t>(this)));
return *this; return *this;
} }
@ -92,7 +92,7 @@ namespace Nz
assert(!m_constraint); assert(!m_constraint);
m_constraint = std::move(constraint); m_constraint = std::move(constraint);
m_constraint->SetEmbedded(); m_constraint->SetEmbedded();
m_constraint->SetUserData(SafeCast<UInt64>(reinterpret_cast<std::uintptr_t>(this))); m_constraint->SetUserData(SafeCast<UInt64>(BitCast<std::uintptr_t>(this)));
JPH::PhysicsSystem* physicsSystem = GetWorld().GetPhysicsSystem(); JPH::PhysicsSystem* physicsSystem = GetWorld().GetPhysicsSystem();
physicsSystem->AddConstraint(m_constraint.get()); physicsSystem->AddConstraint(m_constraint.get());

View File

@ -185,7 +185,7 @@ namespace Nz
JPH::Body& body = lock.GetBody(); JPH::Body& body = lock.GetBody();
hitInfo.hitBody = reinterpret_cast<JoltAbstractBody*>(static_cast<std::uintptr_t>(body.GetUserData())); hitInfo.hitBody = BitCast<JoltAbstractBody*>(static_cast<std::uintptr_t>(body.GetUserData()));
if (auto fractionOpt = m_callback(hitInfo)) if (auto fractionOpt = m_callback(hitInfo))
{ {
@ -235,7 +235,7 @@ namespace Nz
JPH::Body& body = lock.GetBody(); JPH::Body& body = lock.GetBody();
hitInfo.hitBody = reinterpret_cast<JoltAbstractBody*>(static_cast<std::uintptr_t>(body.GetUserData())); hitInfo.hitBody = BitCast<JoltAbstractBody*>(static_cast<std::uintptr_t>(body.GetUserData()));
if (auto fractionOpt = m_callback(hitInfo)) if (auto fractionOpt = m_callback(hitInfo))
{ {
@ -285,7 +285,7 @@ namespace Nz
JPH::Body& body = lock.GetBody(); JPH::Body& body = lock.GetBody();
hitInfo.hitBody = reinterpret_cast<JoltAbstractBody*>(static_cast<std::uintptr_t>(body.GetUserData())); hitInfo.hitBody = BitCast<JoltAbstractBody*>(static_cast<std::uintptr_t>(body.GetUserData()));
hitInfo.hitNormal = FromJolt(body.GetWorldSpaceSurfaceNormal(result.mSubShapeID2, ToJolt(hitInfo.hitPosition))); hitInfo.hitNormal = FromJolt(body.GetWorldSpaceSurfaceNormal(result.mSubShapeID2, ToJolt(hitInfo.hitPosition)));
if (auto fractionOpt = m_callback(hitInfo)) if (auto fractionOpt = m_callback(hitInfo))
@ -515,7 +515,7 @@ namespace Nz
RaycastHit hitInfo; RaycastHit hitInfo;
hitInfo.fraction = collector.mHit.GetEarlyOutFraction(); hitInfo.fraction = collector.mHit.GetEarlyOutFraction();
hitInfo.hitPosition = Lerp(from, to, hitInfo.fraction); hitInfo.hitPosition = Lerp(from, to, hitInfo.fraction);
hitInfo.hitBody = reinterpret_cast<JoltAbstractBody*>(static_cast<std::uintptr_t>(body.GetUserData())); hitInfo.hitBody = BitCast<JoltAbstractBody*>(static_cast<std::uintptr_t>(body.GetUserData()));
hitInfo.hitNormal = FromJolt(body.GetWorldSpaceSurfaceNormal(collector.mHit.mSubShapeID2, rayCast.GetPointOnRay(collector.mHit.GetEarlyOutFraction()))); hitInfo.hitNormal = FromJolt(body.GetWorldSpaceSurfaceNormal(collector.mHit.mSubShapeID2, rayCast.GetPointOnRay(collector.mHit.GetEarlyOutFraction())));
callback(hitInfo); callback(hitInfo);

View File

@ -27,7 +27,7 @@ namespace Nz
body.m_bodyIndex = std::numeric_limits<UInt32>::max(); body.m_bodyIndex = std::numeric_limits<UInt32>::max();
if (m_body) if (m_body)
m_body->SetUserData(SafeCast<UInt64>(reinterpret_cast<std::uintptr_t>(this))); m_body->SetUserData(SafeCast<UInt64>(BitCast<std::uintptr_t>(this)));
} }
JoltRigidBody3D::~JoltRigidBody3D() JoltRigidBody3D::~JoltRigidBody3D()
@ -319,7 +319,7 @@ namespace Nz
body.m_bodyIndex = std::numeric_limits<UInt32>::max(); body.m_bodyIndex = std::numeric_limits<UInt32>::max();
if (m_body) if (m_body)
m_body->SetUserData(SafeCast<UInt64>(reinterpret_cast<std::uintptr_t>(this))); m_body->SetUserData(SafeCast<UInt64>(BitCast<std::uintptr_t>(this)));
return *this; return *this;
} }
@ -431,7 +431,7 @@ namespace Nz
creationSettings.mPosition = ToJolt(settings.position); creationSettings.mPosition = ToJolt(settings.position);
creationSettings.mRotation = ToJolt(settings.rotation); creationSettings.mRotation = ToJolt(settings.rotation);
creationSettings.mUserData = SafeCast<UInt64>(reinterpret_cast<std::uintptr_t>(this)); creationSettings.mUserData = SafeCast<UInt64>(BitCast<std::uintptr_t>(this));
} }
bool JoltRigidBody3D::ShouldActivate() const bool JoltRigidBody3D::ShouldActivate() const

View File

@ -34,7 +34,7 @@ namespace Nz
NazaraAssert(shaderStages == shaderStage, "when supplying GLSL, only one shader stage type can be specified"); NazaraAssert(shaderStages == shaderStage, "when supplying GLSL, only one shader stage type can be specified");
auto& entry = m_shaders.emplace_back(); auto& entry = m_shaders.emplace_back();
entry.shader = GlslShader{ std::string(reinterpret_cast<const char*>(source), std::size_t(sourceSize)) }; entry.shader = GlslShader{ std::string(static_cast<const char*>(source), sourceSize) };
entry.stage = shaderStage; entry.stage = shaderStage;
break; break;
} }

View File

@ -124,7 +124,7 @@ namespace Nz
} }
case WindowBackend::Cocoa: systemHandle = handle.cocoa.window; break; case WindowBackend::Cocoa: systemHandle = handle.cocoa.window; break;
case WindowBackend::X11: systemHandle = reinterpret_cast<void*>(std::uintptr_t(handle.x11.window)); break; case WindowBackend::X11: systemHandle = BitCast<void*>(std::uintptr_t(handle.x11.window)); break;
case WindowBackend::Windows: systemHandle = handle.windows.window; break; case WindowBackend::Windows: systemHandle = handle.windows.window; break;
} }

View File

@ -95,8 +95,10 @@ namespace Nz
case ShaderLanguage::SpirV: case ShaderLanguage::SpirV:
{ {
NazaraAssertFmt(sourceSize % sizeof(UInt32) == 0, "sourceSize must be a multiple of sizeof(UInt32), got {0}", sourceSize);
SpirvEntryPointExtractor extractor; SpirvEntryPointExtractor extractor;
extractor.Decode(reinterpret_cast<const UInt32*>(source), sourceSize / sizeof(UInt32)); extractor.Decode(static_cast<const UInt32*>(source), sourceSize / sizeof(UInt32));
nzsl::ShaderStageTypeFlags remainingStages = shaderStages; nzsl::ShaderStageTypeFlags remainingStages = shaderStages;
for (auto& entryPoint : extractor.entryPoints) for (auto& entryPoint : extractor.entryPoints)

View File

@ -530,7 +530,7 @@ namespace Nz
{ {
NazaraAssert(windowHandle.type == WindowBackend::Windows, "expected Windows window"); NazaraAssert(windowHandle.type == WindowBackend::Windows, "expected Windows window");
HWND winHandle = reinterpret_cast<HWND>(windowHandle.windows.window); HWND winHandle = static_cast<HWND>(windowHandle.windows.window);
HINSTANCE instance = reinterpret_cast<HINSTANCE>(GetWindowLongPtrW(winHandle, GWLP_HINSTANCE)); HINSTANCE instance = reinterpret_cast<HINSTANCE>(GetWindowLongPtrW(winHandle, GWLP_HINSTANCE));
success = m_surface.Create(instance, winHandle); success = m_surface.Create(instance, winHandle);

View File

@ -100,7 +100,7 @@ SCENARIO("String", "[CORE][STRING]")
{ {
CHECK(Nz::TrimRight(Nz::PointerToString(nullptr), '0') == "0x"); CHECK(Nz::TrimRight(Nz::PointerToString(nullptr), '0') == "0x");
const void* ptr = reinterpret_cast<const void*>(static_cast<std::uintptr_t>(0xDEADBEEF)); const void* ptr = Nz::BitCast<const void*>(static_cast<std::uintptr_t>(0xDEADBEEF));
CHECK(Nz::MatchPattern(Nz::PointerToString(ptr), "0x*DEADBEEF")); CHECK(Nz::MatchPattern(Nz::PointerToString(ptr), "0x*DEADBEEF"));
} }