Skip to content

Commit

Permalink
Throw exceptions through wrapper to handle platforms without exceptions
Browse files Browse the repository at this point in the history
Just log and abort on such platforms.
  • Loading branch information
realnc committed Jul 6, 2022
1 parent 0ef7386 commit 8d11c63
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,14 @@ else()
add_bundled_fmtlib()
endif()

check_cxx_source_compiles(
"
#include <stdexcept>
int main() { throw std::runtime_error(\"\"); }
"
HAVE_EXCEPTIONS
)

check_cxx_source_compiles(
"
#include <algorithm>
Expand Down
1 change: 1 addition & 0 deletions aulib_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#cmakedefine USE_RESAMP_SOXR 1
#cmakedefine USE_RESAMP_SRC 1

#cmakedefine HAVE_EXCEPTIONS 1
#cmakedefine HAVE_STD_CLAMP 1

/*
Expand Down
24 changes: 24 additions & 0 deletions include/aulib_global.h
Original file line number Diff line number Diff line change
@@ -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 <cstdio>
#include <cstdlib>
#include <exception>
#endif

namespace Aulib {
namespace priv {

template <typename Exception>
[[noreturn]] void throw_(Exception&& e)
{
#if HAVE_EXCEPTIONS
throw e;
#else
std::fprintf(stderr, "exception: %s\n", e.what());
std::abort();

This comment has been minimized.

Copy link
@glebm

glebm Jul 6, 2022

Contributor

This should probably be std::exit(1) because std::abort skips atexit handlers

This comment has been minimized.

Copy link
@realnc

realnc Jul 6, 2022

Author Owner

The library's atexit handler doesn't currently throw, but might in the future. std::terminate calls abort() by default for that reason.

This comment has been minimized.

Copy link
@glebm

glebm Jul 6, 2022

Contributor

If exceptions are not supported, as is the case in this branch, atexit handler cannot throw

#endif
}

} // namespace priv
} // namespace Aulib

/*
Copyright (C) 2014, 2015, 2016, 2017, 2018, 2019 Nikos Chantziaras.
Expand Down
7 changes: 4 additions & 3 deletions src/SdlMutex.cpp
Original file line number Diff line number Diff line change
@@ -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 <stdexcept>

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

Expand Down

0 comments on commit 8d11c63

Please sign in to comment.