From d582b9e6fa27b1f8d52e028ca641c402eb68d502 Mon Sep 17 00:00:00 2001 From: Angie Date: Sat, 25 May 2024 15:45:44 -0400 Subject: [PATCH] error_handling.hpp --- ultramodern/CMakeLists.txt | 1 + .../include/ultramodern/error_handling.hpp | 24 +++++++++++++++++++ .../include/ultramodern/ultramodern.hpp | 1 + .../include/ultramodern/user_callbacks.hpp | 6 ----- ultramodern/src/error_handling.cpp | 19 +++++++++++++++ ultramodern/src/events.cpp | 11 +-------- 6 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 ultramodern/include/ultramodern/error_handling.hpp create mode 100644 ultramodern/src/error_handling.cpp diff --git a/ultramodern/CMakeLists.txt b/ultramodern/CMakeLists.txt index ac1662c..ca4b15c 100644 --- a/ultramodern/CMakeLists.txt +++ b/ultramodern/CMakeLists.txt @@ -11,6 +11,7 @@ set(CMAKE_CXX_EXTENSIONS OFF) add_library(ultramodern STATIC "${CMAKE_CURRENT_SOURCE_DIR}/src/audio.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/src/error_handling.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/events.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/mesgqueue.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/misc_ultra.cpp" diff --git a/ultramodern/include/ultramodern/error_handling.hpp b/ultramodern/include/ultramodern/error_handling.hpp new file mode 100644 index 0000000..d814c5a --- /dev/null +++ b/ultramodern/include/ultramodern/error_handling.hpp @@ -0,0 +1,24 @@ +#ifndef __ERROR_HANDLING_HPP__ +#define __ERROR_HANDLING_HPP__ + + +namespace ultramodern { + namespace error_handling { + struct error_handling_callbacks_t { + using message_box_t = void(const char* msg); + + /** + * Show an OS dialog with the given `msg`. + * + * The `msg` parameter is always non-`nullptr`. + */ + message_box_t *message_box; + }; + + void set_error_handling_callbacks(const error_handling_callbacks_t& callbacks); + + void message_box(const char* msg); + } +} + +#endif diff --git a/ultramodern/include/ultramodern/ultramodern.hpp b/ultramodern/include/ultramodern/ultramodern.hpp index 5efc140..1652a19 100644 --- a/ultramodern/include/ultramodern/ultramodern.hpp +++ b/ultramodern/include/ultramodern/ultramodern.hpp @@ -26,6 +26,7 @@ #endif #include +#include struct UltraThreadContext { std::thread host_thread; diff --git a/ultramodern/include/ultramodern/user_callbacks.hpp b/ultramodern/include/ultramodern/user_callbacks.hpp index af87736..364977f 100644 --- a/ultramodern/include/ultramodern/user_callbacks.hpp +++ b/ultramodern/include/ultramodern/user_callbacks.hpp @@ -13,12 +13,6 @@ namespace ultramodern { void (*update_rumble)(); void (*update_supported_options)(); - - /** - * Show an OS dialog with the given `msg`. - * `msg` is non-`nullptr`. - */ - void (*message_box)(const char* msg); }; /** diff --git a/ultramodern/src/error_handling.cpp b/ultramodern/src/error_handling.cpp new file mode 100644 index 0000000..24baa07 --- /dev/null +++ b/ultramodern/src/error_handling.cpp @@ -0,0 +1,19 @@ +#include + +#include "ultramodern/error_handling.hpp" + +static ultramodern::error_handling::error_handling_callbacks_t error_handling_callbacks{}; + +void ultramodern::error_handling::set_error_handling_callbacks(const ultramodern::error_handling::error_handling_callbacks_t& callbacks) { + error_handling_callbacks = callbacks; +} + +void ultramodern::error_handling::message_box(const char* msg) { + // We print the message to stderr since the user may not have provided a message_box callback + // TODO: is fprintf ok? or do we prefer using something more C++'ish? + fprintf(stderr, "%s\n", msg); + + if (error_handling_callbacks.message_box != nullptr) { + error_handling_callbacks.message_box(msg); + } +} diff --git a/ultramodern/src/events.cpp b/ultramodern/src/events.cpp index ca7bda9..6dbd8bd 100644 --- a/ultramodern/src/events.cpp +++ b/ultramodern/src/events.cpp @@ -200,8 +200,6 @@ void task_thread_func(uint8_t* rdram, moodycamel::LightweightSemaphore* thread_r // Notify the caller thread that this thread is ready. thread_ready->signal(); - auto& user_callbacks = ultramodern::get_user_callbacks(); - while (true) { // Wait until an RSP task has been sent OSTask* task; @@ -543,16 +541,9 @@ void ultramodern::init_events(RDRAM_ARG ultramodern::WindowHandle window_handle) ultramodern::RT64SetupResult setup_result = rt64_setup_result.load(); if (rt64_setup_result != ultramodern::RT64SetupResult::Success) { auto show_rt64_error = [](const std::string& msg) { - auto& user_callbacks = ultramodern::get_user_callbacks(); std::string error_msg = "An error has been encountered on startup: " + msg; - // We print the message to stderr since the user may not have provided a message_box callback - // TODO: is fprintf ok? or do we prefer using something more C++'ish? - fprintf(stderr, "%s\n", error_msg.c_str()); - - if (user_callbacks.message_box != nullptr) { - user_callbacks.message_box(error_msg.c_str()); - } + ultramodern::error_handling::message_box(error_msg.c_str()); }; const std::string driver_os_suffix = "\nPlease make sure your GPU drivers and your OS are up to date.";