diff --git a/CMakeLists.txt b/CMakeLists.txt index 3264055..a8cf40b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -388,6 +388,14 @@ else() add_bundled_fmtlib() endif() +check_cxx_source_compiles( + " + #include + int main() { throw std::runtime_error(\"\"); } + " + HAVE_EXCEPTIONS +) + check_cxx_source_compiles( " #include diff --git a/aulib_config.h.in b/aulib_config.h.in index 82598f6..88f31d6 100644 --- a/aulib_config.h.in +++ b/aulib_config.h.in @@ -19,6 +19,7 @@ #cmakedefine USE_RESAMP_SOXR 1 #cmakedefine USE_RESAMP_SRC 1 +#cmakedefine HAVE_EXCEPTIONS 1 #cmakedefine HAVE_STD_CLAMP 1 /* diff --git a/include/aulib_global.h b/include/aulib_global.h index 8b4a398..df0512a 100644 --- a/include/aulib_global.h +++ b/include/aulib_global.h @@ -1,8 +1,32 @@ // This is copyrighted software. More information is at the end of this file. #pragma once +#include "aulib_config.h" #include "aulib_export.h" +#if not HAVE_EXCEPTIONS +#include +#include +#include +#endif + +namespace Aulib { +namespace priv { + +template +[[noreturn]] void throw_(Exception&& e) +{ +#if HAVE_EXCEPTIONS + throw e; +#else + std::fprintf(stderr, "exception: %s\n", e.what()); + std::abort(); +#endif +} + +} // namespace priv +} // namespace Aulib + /* Copyright (C) 2014, 2015, 2016, 2017, 2018, 2019 Nikos Chantziaras. diff --git a/src/SdlMutex.cpp b/src/SdlMutex.cpp index 7163e55..dfa3cf5 100644 --- a/src/SdlMutex.cpp +++ b/src/SdlMutex.cpp @@ -1,25 +1,26 @@ // This is copyrighted software. More information is at the end of this file. #include "SdlMutex.h" +#include "aulib_global.h" #include SdlMutex::SdlMutex() { if (not mutex_) { - throw std::runtime_error(SDL_GetError()); + Aulib::priv::throw_(std::runtime_error(SDL_GetError())); } } void SdlMutex::lock() { if (SDL_LockMutex(mutex_) != 0) { - throw std::runtime_error(SDL_GetError()); + Aulib::priv::throw_(std::runtime_error(SDL_GetError())); } } void SdlMutex::unlock() { if (SDL_UnlockMutex(mutex_) != 0) { - throw std::runtime_error(SDL_GetError()); + Aulib::priv::throw_(std::runtime_error(SDL_GetError())); } }