From f999b0acb2a09c468d24173da145a2753e0455a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Sun, 12 Sep 2021 15:44:28 +0300 Subject: [PATCH] Remove Nullable and use std::optional instead --- OpenRCT2.xcodeproj/project.pbxproj | 1 - src/openrct2/core/Nullable.hpp | 55 ---------------------------- src/openrct2/libopenrct2.vcxproj | 1 - src/openrct2/network/NetworkBase.cpp | 7 ++-- src/openrct2/network/NetworkUser.cpp | 8 ++-- src/openrct2/network/NetworkUser.h | 4 +- 6 files changed, 9 insertions(+), 67 deletions(-) delete mode 100644 src/openrct2/core/Nullable.hpp diff --git a/OpenRCT2.xcodeproj/project.pbxproj b/OpenRCT2.xcodeproj/project.pbxproj index 1addaf0b3a..b42fb62b82 100644 --- a/OpenRCT2.xcodeproj/project.pbxproj +++ b/OpenRCT2.xcodeproj/project.pbxproj @@ -1685,7 +1685,6 @@ F76C838B1EC4E7CC00FA49E2 /* Memory.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Memory.hpp; sourceTree = ""; }; F76C838C1EC4E7CC00FA49E2 /* MemoryStream.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = MemoryStream.cpp; sourceTree = ""; }; F76C838D1EC4E7CC00FA49E2 /* MemoryStream.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MemoryStream.h; sourceTree = ""; }; - F76C838E1EC4E7CC00FA49E2 /* Nullable.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Nullable.hpp; sourceTree = ""; }; F76C838F1EC4E7CC00FA49E2 /* Path.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Path.cpp; sourceTree = ""; }; F76C83901EC4E7CC00FA49E2 /* Path.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Path.hpp; sourceTree = ""; }; F76C83921EC4E7CC00FA49E2 /* String.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = String.cpp; sourceTree = ""; }; diff --git a/src/openrct2/core/Nullable.hpp b/src/openrct2/core/Nullable.hpp deleted file mode 100644 index 96774e4c7d..0000000000 --- a/src/openrct2/core/Nullable.hpp +++ /dev/null @@ -1,55 +0,0 @@ -/***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers - * - * For a complete list of all authors, please refer to contributors.md - * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 - * - * OpenRCT2 is licensed under the GNU General Public License version 3. - *****************************************************************************/ - -#pragma once - -#include "../common.h" - -#include - -template struct Nullable -{ -public: - Nullable() - { - _value = T(); - _hasValue = false; - } - - Nullable(std::nullptr_t) - { - _value = T(); - _hasValue = false; - } - - Nullable(const T& value) - { - _value = value; - _hasValue = true; - } - - bool HasValue() const - { - return _hasValue; - } - - T GetValue() const - { - return _value; - } - - T GetValueOrDefault(T defaultValue) const - { - return _hasValue ? _value : defaultValue; - } - -private: - T _value; - bool _hasValue; -}; diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index 70da83f6af..d49c087fba 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -175,7 +175,6 @@ - diff --git a/src/openrct2/network/NetworkBase.cpp b/src/openrct2/network/NetworkBase.cpp index d2c3334245..a2cb4da7a2 100644 --- a/src/openrct2/network/NetworkBase.cpp +++ b/src/openrct2/network/NetworkBase.cpp @@ -61,7 +61,6 @@ static constexpr uint32_t MaxPacketsPerUpdate = 100; # include "../core/Console.hpp" # include "../core/FileStream.h" # include "../core/MemoryStream.h" -# include "../core/Nullable.hpp" # include "../core/Path.hpp" # include "../core/String.hpp" # include "../interface/Chat.h" @@ -912,9 +911,9 @@ uint8_t NetworkBase::GetGroupIDByHash(const std::string& keyhash) const NetworkUser* networkUser = _userManager.GetUserByHash(keyhash); uint8_t groupId = GetDefaultGroup(); - if (networkUser != nullptr && networkUser->GroupId.HasValue()) + if (networkUser != nullptr && networkUser->GroupId.has_value()) { - const uint8_t assignedGroup = networkUser->GroupId.GetValue(); + const uint8_t assignedGroup = *networkUser->GroupId; if (GetGroupByID(assignedGroup) != nullptr) { groupId = assignedGroup; @@ -2058,7 +2057,7 @@ NetworkPlayer* NetworkBase::AddPlayer(const std::string& name, const std::string } else { - player->Group = networkUser->GroupId.GetValueOrDefault(GetDefaultGroup()); + player->Group = networkUser->GroupId.has_value() ? *networkUser->GroupId : GetDefaultGroup(); player->SetName(networkUser->Name); } diff --git a/src/openrct2/network/NetworkUser.cpp b/src/openrct2/network/NetworkUser.cpp index 456262e814..9d53100687 100644 --- a/src/openrct2/network/NetworkUser.cpp +++ b/src/openrct2/network/NetworkUser.cpp @@ -52,9 +52,9 @@ json_t NetworkUser::ToJson() const jsonData["name"] = Name; json_t jsonGroupId; - if (GroupId.HasValue()) + if (GroupId.has_value()) { - jsonGroupId = GroupId.GetValue(); + jsonGroupId = *GroupId; } jsonData["groupId"] = jsonGroupId; @@ -172,9 +172,9 @@ void NetworkUserManager::UnsetUsersOfGroup(uint8_t groupId) for (const auto& kvp : _usersByHash) { NetworkUser* networkUser = kvp.second; - if (networkUser->GroupId.HasValue() && networkUser->GroupId.GetValue() == groupId) + if (networkUser->GroupId.has_value() && *networkUser->GroupId == groupId) { - networkUser->GroupId = nullptr; + networkUser->GroupId = std::nullopt; } } } diff --git a/src/openrct2/network/NetworkUser.h b/src/openrct2/network/NetworkUser.h index 4cb750dd1d..4f114ecc2b 100644 --- a/src/openrct2/network/NetworkUser.h +++ b/src/openrct2/network/NetworkUser.h @@ -11,9 +11,9 @@ #include "../common.h" #include "../core/JsonFwd.hpp" -#include "../core/Nullable.hpp" #include +#include #include class NetworkUser final @@ -21,7 +21,7 @@ class NetworkUser final public: std::string Hash; std::string Name; - Nullable GroupId; + std::optional GroupId; bool Remove; /**