From 37be543c4c78b44eb31dfc70e6892e35e76d0a47 Mon Sep 17 00:00:00 2001 From: Garrett Leach Date: Wed, 19 Mar 2025 22:24:43 -0500 Subject: [PATCH 1/3] Add Guard::Assert function with std::source_location default parameter Part of #12489 --- contributors.md | 1 + src/openrct2/core/Guard.cpp | 10 ++++++++++ src/openrct2/core/Guard.hpp | 4 +++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/contributors.md b/contributors.md index 007ba77088..06dfa6535c 100644 --- a/contributors.md +++ b/contributors.md @@ -249,6 +249,7 @@ Appreciation for contributors who have provided substantial work, but are no lon * Brendan Heinonen (staticinvocation) * (QuestionableDeer) * David Sungaila (sungaila) +* Garrett Leach (GarrettLeach) ## Toolchain * (Balletie) - macOS diff --git a/src/openrct2/core/Guard.cpp b/src/openrct2/core/Guard.cpp index e602dc8d75..a140232784 100644 --- a/src/openrct2/core/Guard.cpp +++ b/src/openrct2/core/Guard.cpp @@ -23,6 +23,7 @@ #include #include #include +#include namespace OpenRCT2::Guard { @@ -54,6 +55,15 @@ namespace OpenRCT2::Guard _assertBehaviour = behaviour; } + void Assert(bool expression, const std::source_location& location) + { + if (expression) + return; + + std::string message = std::format("Assertion failed in {}:{}", location.function_name(), location.line()); + Assert(expression, message.c_str()); + } + void Assert(bool expression, const char* message, ...) { va_list args; diff --git a/src/openrct2/core/Guard.hpp b/src/openrct2/core/Guard.hpp index bcb0b8d254..50604e2cde 100644 --- a/src/openrct2/core/Guard.hpp +++ b/src/openrct2/core/Guard.hpp @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -30,7 +31,8 @@ namespace OpenRCT2::Guard ASSERT_BEHAVIOUR GetAssertBehaviour(); void SetAssertBehaviour(ASSERT_BEHAVIOUR behaviour); - void Assert(bool expression, const char* message = nullptr, ...); + void Assert(bool expression, const std::source_location& location = std::source_location::current()); + void Assert(bool expression, const char* message, ...); void Assert_VA(bool expression, const char* message, va_list args); void Fail(const char* message = nullptr, ...); void Fail_VA(const char* message, va_list args); From 89515edbd8742ed323e24f491a3686f969c03a3e Mon Sep 17 00:00:00 2001 From: Garrett Leach Date: Wed, 19 Mar 2025 23:08:48 -0500 Subject: [PATCH 2/3] Use stringstream until std::format is available Part of #12489 --- src/openrct2/core/Guard.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/openrct2/core/Guard.cpp b/src/openrct2/core/Guard.cpp index a140232784..ef6dfa7ed0 100644 --- a/src/openrct2/core/Guard.cpp +++ b/src/openrct2/core/Guard.cpp @@ -23,7 +23,7 @@ #include #include #include -#include +#include namespace OpenRCT2::Guard { @@ -60,7 +60,11 @@ namespace OpenRCT2::Guard if (expression) return; - std::string message = std::format("Assertion failed in {}:{}", location.function_name(), location.line()); + std::stringstream messageStream; + messageStream << "Assertion failed in " << location.function_name() << ":" << location.line(); + + std::string message = messageStream.str(); + Assert(expression, message.c_str()); } From 8e9944d6eb12a4e34a5081297627da7dcee285ad Mon Sep 17 00:00:00 2001 From: Garrett Leach Date: Thu, 20 Mar 2025 09:14:51 -0500 Subject: [PATCH 3/3] Use more common filename:line format for assertion failure messages --- src/openrct2/core/Guard.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openrct2/core/Guard.cpp b/src/openrct2/core/Guard.cpp index ef6dfa7ed0..f38792f28e 100644 --- a/src/openrct2/core/Guard.cpp +++ b/src/openrct2/core/Guard.cpp @@ -61,7 +61,7 @@ namespace OpenRCT2::Guard return; std::stringstream messageStream; - messageStream << "Assertion failed in " << location.function_name() << ":" << location.line(); + messageStream << "Assertion failed in " << location.file_name() << ":" << location.line(); std::string message = messageStream.str();