From 2c3e5e205fbd860109a751e547fa39dd71178f2e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 17 Apr 2020 13:29:20 -0400 Subject: [PATCH] MkCastTo: Make operator bool instances explicit Prevents error prone implicit conversions. --- Runtime/GameObjectLists.cpp | 14 ++++++++------ Runtime/MP1/CSamusHud.cpp | 2 +- Runtime/MkCastTo.py | 6 ++++-- Runtime/World/CPlayer.cpp | 4 ++-- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Runtime/GameObjectLists.cpp b/Runtime/GameObjectLists.cpp index bc5545853..024cbc7ed 100644 --- a/Runtime/GameObjectLists.cpp +++ b/Runtime/GameObjectLists.cpp @@ -14,15 +14,15 @@ namespace urde { CActorList::CActorList() : CObjectList(EGameObjectList::Actor) {} -bool CActorList::IsQualified(const CEntity& ent) const { return TCastToConstPtr(ent); } +bool CActorList::IsQualified(const CEntity& ent) const { return TCastToConstPtr(ent).IsValid(); } CPhysicsActorList::CPhysicsActorList() : CObjectList(EGameObjectList::PhysicsActor) {} -bool CPhysicsActorList::IsQualified(const CEntity& ent) const { return TCastToConstPtr(ent); } +bool CPhysicsActorList::IsQualified(const CEntity& ent) const { return TCastToConstPtr(ent).IsValid(); } CGameCameraList::CGameCameraList() : CObjectList(EGameObjectList::GameCamera) {} -bool CGameCameraList::IsQualified(const CEntity& ent) const { return TCastToConstPtr(ent); } +bool CGameCameraList::IsQualified(const CEntity& ent) const { return TCastToConstPtr(ent).IsValid(); } CListeningAiList::CListeningAiList() : CObjectList(EGameObjectList::ListeningAi) {} @@ -41,12 +41,14 @@ CPlatformAndDoorList::CPlatformAndDoorList() : CObjectList(EGameObjectList::Plat bool CPlatformAndDoorList::IsQualified(const CEntity& ent) const { return IsDoor(ent) || IsPlatform(ent); } -bool CPlatformAndDoorList::IsDoor(const CEntity& ent) const { return TCastToConstPtr(ent); } +bool CPlatformAndDoorList::IsDoor(const CEntity& ent) const { return TCastToConstPtr(ent).IsValid(); } -bool CPlatformAndDoorList::IsPlatform(const CEntity& ent) const { return TCastToConstPtr(ent); } +bool CPlatformAndDoorList::IsPlatform(const CEntity& ent) const { + return TCastToConstPtr(ent).IsValid(); +} CGameLightList::CGameLightList() : CObjectList(EGameObjectList::GameLight) {} -bool CGameLightList::IsQualified(const CEntity& lt) const { return TCastToConstPtr(lt); } +bool CGameLightList::IsQualified(const CEntity& lt) const { return TCastToConstPtr(lt).IsValid(); } } // namespace urde diff --git a/Runtime/MP1/CSamusHud.cpp b/Runtime/MP1/CSamusHud.cpp index 8fe61b2b3..e927f1a07 100644 --- a/Runtime/MP1/CSamusHud.cpp +++ b/Runtime/MP1/CSamusHud.cpp @@ -600,7 +600,7 @@ void CSamusHud::UpdateCameraDebugSettings() { } void CSamusHud::UpdateEnergyLow(float dt, const CStateManager& mgr) { - bool cineCam = TCastToConstPtr(mgr.GetCameraManager()->GetCurrentCamera(mgr)); + const bool cineCam = TCastToConstPtr(mgr.GetCameraManager()->GetCurrentCamera(mgr)).IsValid(); float oldTimer = x57c_energyLowTimer; x57c_energyLowTimer = std::fmod(x57c_energyLowTimer + dt, 0.5f); diff --git a/Runtime/MkCastTo.py b/Runtime/MkCastTo.py index 97c114642..2864c154c 100644 --- a/Runtime/MkCastTo.py +++ b/Runtime/MkCastTo.py @@ -132,7 +132,8 @@ headerf.write(''' operator T*() const { return GetPtr(); } T& operator*() const { return *GetPtr(); } T* operator->() const { return GetPtr(); } - operator bool() const { return ptr != nullptr; } + bool IsValid() const { return ptr != nullptr; } + explicit operator bool() const { return IsValid(); } }; template @@ -147,7 +148,8 @@ public: operator const T*() const { return GetPtr(); } const T& operator*() const { return *GetPtr(); } const T* operator->() const { return GetPtr(); } - operator bool() const { return TCastToPtr::ptr != nullptr; } + bool IsValid() const { return TCastToPtr::ptr != nullptr; } + explicit operator bool() const { return IsValid(); } }; } diff --git a/Runtime/World/CPlayer.cpp b/Runtime/World/CPlayer.cpp index a8a374f64..dc6cc6526 100644 --- a/Runtime/World/CPlayer.cpp +++ b/Runtime/World/CPlayer.cpp @@ -3323,7 +3323,7 @@ void CPlayer::UpdateAimTargetPrediction(const zeus::CTransform& xf, const CState return; } - x9c6_27_aimingAtProjectile = TCastToConstPtr(target.GetPtr()); + x9c6_27_aimingAtProjectile = TCastToConstPtr(target.GetPtr()).IsValid(); const zeus::CVector3f instantTarget = target->GetAimPosition(mgr, 0.f); const zeus::CVector3f gunToTarget = instantTarget - xf.origin; const float timeToTarget = gunToTarget.magnitude() / x490_gun->GetBeamVelocity(); @@ -5076,7 +5076,7 @@ bool CPlayer::ValidateOrbitTargetIdAndPointer(TUniqueId uid, CStateManager& mgr) if (uid == kInvalidUniqueId) { return false; } - return TCastToConstPtr(mgr.GetObjectById(uid)); + return TCastToConstPtr(mgr.GetObjectById(uid)).IsValid(); } zeus::CVector3f CPlayer::GetBallPosition() const {