From d4352bd65af3565307d97f04088ce1eb5c96ea88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Wed, 4 Dec 2024 16:32:48 +0200 Subject: [PATCH] Use std::variant for Intent data --- src/openrct2/windows/Intent.cpp | 55 ++++++++++++--------------------- src/openrct2/windows/Intent.h | 24 ++------------ 2 files changed, 23 insertions(+), 56 deletions(-) diff --git a/src/openrct2/windows/Intent.cpp b/src/openrct2/windows/Intent.cpp index 04d38a6f21..74432d8eec 100644 --- a/src/openrct2/windows/Intent.cpp +++ b/src/openrct2/windows/Intent.cpp @@ -32,55 +32,35 @@ Intent::Intent(IntentAction intentAction) Intent* Intent::PutExtra(uint32_t key, uint32_t value) { - IntentData data = {}; - data.intVal.unsignedInt = value; - data.type = IntentData::DataType::Int; - - _Data.insert(std::make_pair(key, data)); + _Data.emplace(key, static_cast(value)); return this; } Intent* Intent::PutExtra(uint32_t key, void* value) { - IntentData data = {}; - data.pointerVal = value; - data.type = IntentData::DataType::Pointer; - - _Data.insert(std::make_pair(key, data)); + _Data.emplace(key, value); return this; } Intent* Intent::PutExtra(uint32_t key, int32_t value) { - IntentData data = {}; - data.intVal.signedInt = value; - data.type = IntentData::DataType::Int; - - _Data.insert(std::make_pair(key, data)); + _Data.emplace(key, static_cast(value)); return this; } Intent* Intent::PutExtra(uint32_t key, std::string value) { - IntentData data = {}; - data.stringVal = std::move(value); - data.type = IntentData::DataType::String; - - _Data.insert(std::make_pair(key, data)); + _Data.emplace(key, value); return this; } Intent* Intent::PutExtra(uint32_t key, close_callback value) { - IntentData data = {}; - data.closeCallbackVal = value; - data.type = IntentData::DataType::CloseCallback; - - _Data.insert(std::make_pair(key, data)); + _Data.emplace(key, value); return this; } @@ -108,8 +88,9 @@ void* Intent::GetPointerExtra(uint32_t key) const } auto data = _Data.at(key); - Guard::Assert(data.type == IntentData::DataType::Pointer, "Actual type doesn't match requested type"); - return static_cast(data.pointerVal); + + assert(std::holds_alternative(data)); + return std::get(data); } uint32_t Intent::GetUIntExtra(uint32_t key) const @@ -120,8 +101,9 @@ uint32_t Intent::GetUIntExtra(uint32_t key) const } auto data = _Data.at(key); - Guard::Assert(data.type == IntentData::DataType::Int, "Actual type doesn't match requested type"); - return data.intVal.unsignedInt; + + assert(std::holds_alternative(data)); + return static_cast(std::get(data)); } int32_t Intent::GetSIntExtra(uint32_t key) const @@ -132,8 +114,9 @@ int32_t Intent::GetSIntExtra(uint32_t key) const } auto data = _Data.at(key); - Guard::Assert(data.type == IntentData::DataType::Int, "Actual type doesn't match requested type"); - return data.intVal.signedInt; + + assert(std::holds_alternative(data)); + return static_cast(std::get(data)); } std::string Intent::GetStringExtra(uint32_t key) const @@ -144,8 +127,9 @@ std::string Intent::GetStringExtra(uint32_t key) const } auto data = _Data.at(key); - Guard::Assert(data.type == IntentData::DataType::String, "Actual type doesn't match requested type"); - return data.stringVal; + + assert(std::holds_alternative(data)); + return std::get(data); } close_callback Intent::GetCloseCallbackExtra(uint32_t key) const @@ -156,6 +140,7 @@ close_callback Intent::GetCloseCallbackExtra(uint32_t key) const } auto data = _Data.at(key); - Guard::Assert(data.type == IntentData::DataType::CloseCallback, "Actual type doesn't match requested type"); - return data.closeCallbackVal; + + assert(std::holds_alternative(data)); + return std::get(data); } diff --git a/src/openrct2/windows/Intent.h b/src/openrct2/windows/Intent.h index ed0ffa0715..f859bf1b2a 100644 --- a/src/openrct2/windows/Intent.h +++ b/src/openrct2/windows/Intent.h @@ -14,6 +14,7 @@ #include #include +#include enum IntentAction { @@ -57,29 +58,10 @@ enum IntentAction INTENT_ACTION_NULL = 255, }; -struct IntentData -{ - enum class DataType - { - Int, - String, - Pointer, - CloseCallback - } type; - - union - { - uint32_t unsignedInt; - int32_t signedInt; - } intVal; - std::string stringVal; - close_callback closeCallbackVal; - void* pointerVal; -}; - class Intent { -private: + using IntentData = std::variant; + WindowClass _Class{ WindowClass::Null }; WindowDetail _WindowDetail{ WD_NULL }; IntentAction _Action{ INTENT_ACTION_NULL };