Made the lib work with -fno-exceptions (at least for now).

This commit is contained in:
Patrick 2025-03-02 14:35:37 +01:00
parent 21b3b2c03a
commit ba23cb0c70
5 changed files with 50 additions and 12 deletions

View File

@ -27,6 +27,18 @@
#include "../debug/stacktrace.hpp" #include "../debug/stacktrace.hpp"
#endif #endif
#if !defined(MIJIN_COROUTINE_ENABLE_EXCEPTION_HANDLING)
# define MIJIN_COROUTINE_ENABLE_EXCEPTION_HANDLING MIJIN_ENABLE_EXCEPTIONS
#elif !__cpp_exceptions
# error "Coroutine exception handling enabled, but exceptions are disabled."
#endif
#if !defined(MIJIN_COROUTINE_ENABLE_CANCEL)
# define MIJIN_COROUTINE_ENABLE_CANCEL MIJIN_ENABLE_EXCEPTIONS
#elif !__cpp_exceptions
# error "Cancelling tasks requires exceptions to be anbled."
#endif
namespace mijin namespace mijin
{ {
@ -56,7 +68,9 @@ class TaskLoop;
template<typename TResult = void> template<typename TResult = void>
class TaskBase; class TaskBase;
#if MIJIN_COROUTINE_ENABLE_CANCEL
struct TaskCancelled : std::exception {}; struct TaskCancelled : std::exception {};
#endif
namespace impl namespace impl
{ {
@ -585,10 +599,12 @@ extern thread_local TaskLoop::StoredTask* gCurrentTask;
inline void throwIfCancelled() inline void throwIfCancelled()
{ {
#if MIJIN_COROUTINE_ENABLE_CANCEL
if (gCurrentTask->task->sharedState()->cancelled_) if (gCurrentTask->task->sharedState()->cancelled_)
{ {
throw TaskCancelled(); throw TaskCancelled();
} }
#endif
} }
} }
@ -657,13 +673,16 @@ inline TaskStatus TaskLoop::tickTask(StoredTask& task)
while (status == TaskStatus::RUNNING); while (status == TaskStatus::RUNNING);
impl::gCurrentTask = nullptr; impl::gCurrentTask = nullptr;
#if MIJIN_COROUTINE_ENABLE_EXCEPTION_HANDLING
if (task.task && task.task->exception()) if (task.task && task.task->exception())
{ {
try try
{ {
std::rethrow_exception(task.task->exception()); std::rethrow_exception(task.task->exception());
} }
#if MIJIN_COROUTINE_ENABLE_CANCEL
catch(TaskCancelled&) {} // ignore those catch(TaskCancelled&) {} // ignore those
#endif
catch(...) catch(...)
{ {
if (uncaughtExceptionHandler_) if (uncaughtExceptionHandler_)
@ -679,6 +698,7 @@ inline TaskStatus TaskLoop::tickTask(StoredTask& task)
// TODO: handle the exception somehow, others may be waiting // TODO: handle the exception somehow, others may be waiting
return TaskStatus::FINISHED; return TaskStatus::FINISHED;
} }
#endif // MIJIN_COROUTINE_ENABLE_EXCEPTION_HANDLING
if (status == TaskStatus::YIELDED || status == TaskStatus::FINISHED) if (status == TaskStatus::YIELDED || status == TaskStatus::FINISHED)
{ {
task.setFuture(task); task.setFuture(task);

View File

@ -96,7 +96,7 @@ struct fmt::formatter<mijin::Stackframe>
auto it = ctx.begin(); auto it = ctx.begin();
auto end = ctx.end(); auto end = ctx.end();
if (it != end && *it != '}') throw format_error("invalid format"); if (it != end && *it != '}') FMT_THROW(format_error("invalid format"));
return it; return it;
} }
@ -119,7 +119,7 @@ struct fmt::formatter<mijin::Stacktrace>
auto it = ctx.begin(); auto it = ctx.begin();
auto end = ctx.end(); auto end = ctx.end();
if (it != end && *it != '}') throw format_error("invalid format"); if (it != end && *it != '}') FMT_THROW(format_error("invalid format"));
return it; return it;
} }

View File

@ -4,19 +4,21 @@
#if !defined(MIJIN_INTERNAL_EXCEPTION_HPP_INCLUDED) #if !defined(MIJIN_INTERNAL_EXCEPTION_HPP_INCLUDED)
#define MIJIN_INTERNAL_EXCEPTION_HPP_INCLUDED 1 #define MIJIN_INTERNAL_EXCEPTION_HPP_INCLUDED 1
#if !defined(MIJIN_WITH_EXCEPTIONS) #if !defined(MIJIN_ENABLE_EXCEPTIONS)
#define MIJIN_WITH_EXCEPTIONS 0 # define MIJIN_ENABLE_EXCEPTIONS (__cpp_exceptions)
#endif #endif
#define MIJIN_CATCH_EXCEPTIONS (__cpp_exceptions)
#if !defined(MIJIN_THROWING_ASSERTS) #if !defined(MIJIN_THROWING_ASSERTS)
#define MIJIN_THROWING_ASSERTS 0 # define MIJIN_THROWING_ASSERTS 0
#endif #endif
#if MIJIN_THROWING_ASSERTS && !defined(MIJIN_TEST_NO_NOEXCEPT) #if MIJIN_THROWING_ASSERTS && !defined(MIJIN_TEST_NO_NOEXCEPT)
#define MIJIN_TEST_NO_NOEXCEPT #define MIJIN_TEST_NO_NOEXCEPT
#endif #endif
#if MIJIN_WITH_EXCEPTIONS #if 0 // TODO: what? MIJIN_WITH_EXCEPTIONS
#error "Maybe someday" #error "Maybe someday"
#else #else
#if defined(MIJIN_TEST_NO_NOEXCEPT) // only use for testing #if defined(MIJIN_TEST_NO_NOEXCEPT) // only use for testing
@ -30,4 +32,12 @@
#endif #endif
#endif #endif
#if MIJIN_CATCH_EXCEPTIONS
# define MIJIN_TRY try
# define MIJIN_CATCH catch
#else
# define MIJIN_TRY
# define MIJIN_CATCH(...) if constexpr(false)
#endif
#endif // !defined(MIJIN_INTERNAL_EXCEPTION_HPP_INCLUDED) #endif // !defined(MIJIN_INTERNAL_EXCEPTION_HPP_INCLUDED)

View File

@ -457,6 +457,7 @@ inline const char* errorName(StreamError error) MIJIN_NOEXCEPT
return "<invalid error>"; return "<invalid error>";
} }
#if MIJIN_ENABLE_EXCEPTIONS // these functions don't make sense with exceptions disabled
inline void throwOnError(mijin::StreamError error) inline void throwOnError(mijin::StreamError error)
{ {
if (error == mijin::StreamError::SUCCESS) { if (error == mijin::StreamError::SUCCESS) {
@ -500,6 +501,7 @@ inline decltype(auto) throwOnError(StreamResult<TSuccess>&& result, const std::s
} }
return *result; return *result;
} }
#endif // MIJIN_ENABLE_EXCEPTIONS
} // namespace mijin } // namespace mijin
#endif // !defined(MIJIN_IO_STREAM_HPP_INCLUDED) #endif // !defined(MIJIN_IO_STREAM_HPP_INCLUDED)

View File

@ -57,18 +57,22 @@ std::vector<FileInfo> OSFileSystemAdapter::listFiles(const fs::path& folder)
info.isSymlink = entry.is_symlink(err); info.isSymlink = entry.is_symlink(err);
info.isSpecial = !info.isFolder && !entry.is_regular_file(err); info.isSpecial = !info.isFolder && !entry.is_regular_file(err);
info.isHidden = info.path.filename().string().starts_with('.'); // at least for Linux info.isHidden = info.path.filename().string().starts_with('.'); // at least for Linux
if (info.isFolder) { if (info.isFolder)
try { {
MIJIN_TRY
{
info.size = std::distance(fs::directory_iterator(info.path), fs::directory_iterator()); info.size = std::distance(fs::directory_iterator(info.path), fs::directory_iterator());
} }
catch(std::runtime_error&) { MIJIN_CATCH(std::runtime_error&)
{
info.size = 0; info.size = 0;
} }
} }
else if (!info.isSpecial) else if (!info.isSpecial)
{ {
info.size = entry.file_size(err); info.size = entry.file_size(err);
if (err) { if (err)
{
info.size = 0; info.size = 0;
} }
} }
@ -89,10 +93,12 @@ FileInfo OSFileSystemAdapter::getFileInfo(const fs::path& file)
info.isSpecial = !info.isFolder && !fs::is_regular_file(file, err); info.isSpecial = !info.isFolder && !fs::is_regular_file(file, err);
info.isHidden = info.path.filename().string().starts_with('.'); // at least for Linux info.isHidden = info.path.filename().string().starts_with('.'); // at least for Linux
if (info.isFolder) { if (info.isFolder) {
try { MIJIN_TRY
{
info.size = std::distance(fs::directory_iterator(info.path), fs::directory_iterator()); info.size = std::distance(fs::directory_iterator(info.path), fs::directory_iterator());
} }
catch(std::runtime_error&) { MIJIN_CATCH(std::runtime_error&)
{
info.size = 0; info.size = 0;
} }
} }