1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-24 00:03:11 +01:00

Remove Nullable and use std::optional instead

This commit is contained in:
ζeh Matt
2021-09-12 15:44:28 +03:00
parent 65a484105e
commit f999b0acb2
6 changed files with 9 additions and 67 deletions

View File

@@ -1685,7 +1685,6 @@
F76C838B1EC4E7CC00FA49E2 /* Memory.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Memory.hpp; sourceTree = "<group>"; };
F76C838C1EC4E7CC00FA49E2 /* MemoryStream.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = MemoryStream.cpp; sourceTree = "<group>"; };
F76C838D1EC4E7CC00FA49E2 /* MemoryStream.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MemoryStream.h; sourceTree = "<group>"; };
F76C838E1EC4E7CC00FA49E2 /* Nullable.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Nullable.hpp; sourceTree = "<group>"; };
F76C838F1EC4E7CC00FA49E2 /* Path.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Path.cpp; sourceTree = "<group>"; };
F76C83901EC4E7CC00FA49E2 /* Path.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Path.hpp; sourceTree = "<group>"; };
F76C83921EC4E7CC00FA49E2 /* String.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = String.cpp; sourceTree = "<group>"; };

View File

@@ -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 <cstddef>
template<typename T> 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;
};

View File

@@ -175,7 +175,6 @@
<ClInclude Include="core\Memory.hpp" />
<ClInclude Include="core\MemoryStream.h" />
<ClInclude Include="core\Meta.hpp" />
<ClInclude Include="core\Nullable.hpp" />
<ClInclude Include="core\Numerics.hpp" />
<ClInclude Include="core\Path.hpp" />
<ClInclude Include="core\Random.hpp" />

View File

@@ -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);
}

View File

@@ -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;
}
}
}

View File

@@ -11,9 +11,9 @@
#include "../common.h"
#include "../core/JsonFwd.hpp"
#include "../core/Nullable.hpp"
#include <map>
#include <optional>
#include <string>
class NetworkUser final
@@ -21,7 +21,7 @@ class NetworkUser final
public:
std::string Hash;
std::string Name;
Nullable<uint8_t> GroupId;
std::optional<uint8_t> GroupId;
bool Remove;
/**