From ff173dffce87f03fda75f01119c6322c3341ebb0 Mon Sep 17 00:00:00 2001 From: David Markowitz Date: Tue, 25 Jul 2023 19:45:22 -0700 Subject: [PATCH] move to std optional --- dDatabase/Tables/CDZoneTableTable.cpp | 9 ++------- dDatabase/Tables/CDZoneTableTable.h | 5 +++-- dGame/Entity.cpp | 6 +++--- dGame/dUtilities/SlashCommandHandler.cpp | 1 - dMasterServer/InstanceManager.cpp | 8 ++++---- dZoneManager/Zone.cpp | 4 ++-- dZoneManager/dZoneManager.cpp | 9 ++++----- 7 files changed, 18 insertions(+), 24 deletions(-) diff --git a/dDatabase/Tables/CDZoneTableTable.cpp b/dDatabase/Tables/CDZoneTableTable.cpp index 1030593e..31c0a86f 100644 --- a/dDatabase/Tables/CDZoneTableTable.cpp +++ b/dDatabase/Tables/CDZoneTableTable.cpp @@ -53,13 +53,8 @@ void CDZoneTableTable::LoadValuesFromDatabase() { } //! Queries the table with a zoneID to find. -const CDZoneTable* CDZoneTableTable::Query(unsigned int zoneID) { +const std::optional CDZoneTableTable::Query(unsigned int zoneID) { const auto& iter = m_Entries.find(zoneID); - - if (iter != m_Entries.end()) { - return &iter->second; - } - - return nullptr; + return iter != m_Entries.end() ? std::make_optional(iter->second) : std::nullopt; } diff --git a/dDatabase/Tables/CDZoneTableTable.h b/dDatabase/Tables/CDZoneTableTable.h index fef8096f..789024ad 100644 --- a/dDatabase/Tables/CDZoneTableTable.h +++ b/dDatabase/Tables/CDZoneTableTable.h @@ -1,8 +1,9 @@ #pragma once -// Custom Classes #include "CDTable.h" +#include + struct CDZoneTable { unsigned int zoneID; //!< The Zone ID of the object unsigned int locStatus; //!< The Locale Status(?) @@ -41,5 +42,5 @@ public: void LoadValuesFromDatabase(); // Queries the table with a zoneID to find. - const CDZoneTable* Query(unsigned int zoneID); + const std::optional Query(unsigned int zoneID); }; diff --git a/dGame/Entity.cpp b/dGame/Entity.cpp index 998541ad..78ba959a 100644 --- a/dGame/Entity.cpp +++ b/dGame/Entity.cpp @@ -504,11 +504,11 @@ void Entity::Initialize() { // ZoneControl script if (m_TemplateID == 2365) { - CDZoneTableTable* zoneTable = CDClientManager::Instance().GetTable(); + auto* zoneTable = CDClientManager::Instance().GetTable(); const auto zoneID = Game::zoneManager->GetZoneID(); - const CDZoneTable* zoneData = zoneTable->Query(zoneID.GetMapID()); + auto zoneData = zoneTable->Query(zoneID.GetMapID()); - if (zoneData != nullptr) { + if (zoneData) { int zoneScriptID = zoneData->scriptID; CDScriptComponent zoneScriptData = scriptCompTable->GetByID(zoneScriptID); diff --git a/dGame/dUtilities/SlashCommandHandler.cpp b/dGame/dUtilities/SlashCommandHandler.cpp index 03e3cc89..d54347f9 100644 --- a/dGame/dUtilities/SlashCommandHandler.cpp +++ b/dGame/dUtilities/SlashCommandHandler.cpp @@ -84,7 +84,6 @@ #include "eMasterMessageType.h" #include "CDObjectsTable.h" -#include "CDZoneTableTable.h" void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entity* entity, const SystemAddress& sysAddr) { auto commandCopy = command; diff --git a/dMasterServer/InstanceManager.cpp b/dMasterServer/InstanceManager.cpp index 50f55b72..18275edf 100644 --- a/dMasterServer/InstanceManager.cpp +++ b/dMasterServer/InstanceManager.cpp @@ -372,9 +372,9 @@ Instance* InstanceManager::FindPrivateInstance(const std::string& password) { int InstanceManager::GetSoftCap(LWOMAPID mapID) { CDZoneTableTable* zoneTable = CDClientManager::Instance().GetTable(); if (zoneTable) { - const CDZoneTable* zone = zoneTable->Query(mapID); + auto zone = zoneTable->Query(mapID); - if (zone != nullptr) { + if (zone) { return zone->population_soft_cap; } } @@ -385,9 +385,9 @@ int InstanceManager::GetSoftCap(LWOMAPID mapID) { int InstanceManager::GetHardCap(LWOMAPID mapID) { CDZoneTableTable* zoneTable = CDClientManager::Instance().GetTable(); if (zoneTable) { - const CDZoneTable* zone = zoneTable->Query(mapID); + auto zone = zoneTable->Query(mapID); - if (zone != nullptr) { + if (zone) { return zone->population_hard_cap; } } diff --git a/dZoneManager/Zone.cpp b/dZoneManager/Zone.cpp index 7343bb59..60860c89 100644 --- a/dZoneManager/Zone.cpp +++ b/dZoneManager/Zone.cpp @@ -165,8 +165,8 @@ void Zone::LoadZoneIntoMemory() { std::string Zone::GetFilePathForZoneID() { //We're gonna go ahead and presume we've got the db loaded already: CDZoneTableTable* zoneTable = CDClientManager::Instance().GetTable(); - const CDZoneTable* zone = zoneTable->Query(this->GetZoneID().GetMapID()); - if (zone != nullptr) { + auto zone = zoneTable->Query(this->GetZoneID().GetMapID()); + if (zone) { std::string toReturn = "maps/" + zone->zoneName; std::transform(toReturn.begin(), toReturn.end(), toReturn.begin(), ::tolower); return toReturn; diff --git a/dZoneManager/dZoneManager.cpp b/dZoneManager/dZoneManager.cpp index 51f6e640..b1fdb6a9 100644 --- a/dZoneManager/dZoneManager.cpp +++ b/dZoneManager/dZoneManager.cpp @@ -12,7 +12,6 @@ #include "CDZoneTableTable.h" #include #include "eObjectBits.h" -#include "CDZoneTableTable.h" #include "AssetManager.h" #include "../dWorldServer/ObjectIDManager.h" @@ -31,9 +30,9 @@ void dZoneManager::Initialize(const LWOZONEID& zoneID) { CDZoneTableTable* zoneTable = CDClientManager::Instance().GetTable(); if (zoneTable != nullptr) { - const CDZoneTable* zone = zoneTable->Query(zoneID.GetMapID()); + auto zone = zoneTable->Query(zoneID.GetMapID()); - if (zone != nullptr) { + if (zone) { zoneControlTemplate = zone->zoneControlTemplate != -1 ? zone->zoneControlTemplate : 2365; const auto min = zone->ghostdistance_min != -1.0f ? zone->ghostdistance_min : 100; const auto max = zone->ghostdistance != -1.0f ? zone->ghostdistance : 100; @@ -235,8 +234,8 @@ uint32_t dZoneManager::GetUniqueMissionIdStartingValue() { bool dZoneManager::CheckIfAccessibleZone(LWOMAPID zoneID) { //We're gonna go ahead and presume we've got the db loaded already: CDZoneTableTable* zoneTable = CDClientManager::Instance().GetTable(); - const CDZoneTable* zone = zoneTable->Query(zoneID); - if (zone != nullptr) { + auto zone = zoneTable->Query(zoneID); + if (zone) { return Game::assetManager->HasFile(("maps/" + zone->zoneName).c_str()); } else { return false;