Skip to content

Commit

Permalink
add support for -fno-exception, calls terminate instead of throwing
Browse files Browse the repository at this point in the history
  • Loading branch information
matcool committed Feb 16, 2025
1 parent bbd49be commit 88fde6b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
cmake_policy(SET CMP0097 NEW)

project(GeodeResult VERSION 1.3.3 LANGUAGES C CXX)
project(GeodeResult VERSION 1.3.4 LANGUAGES C CXX)

add_library(GeodeResult INTERFACE)

Expand Down
44 changes: 28 additions & 16 deletions include/Geode/Result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define GEODE_RESULT_HPP

#include <concepts>
#include <exception>
#include <optional>
#include <sstream>
#include <stdexcept>
Expand Down Expand Up @@ -128,6 +129,17 @@
(errVariable = std::move(GEODE_CONCAT(res, __LINE__)).unwrapErr(), false)
#endif

// Internal macros

#if !defined(GEODE_RESULT_IMPL_THROW)
#if __cpp_exceptions
#define GEODE_RESULT_IMPL_THROW(expr) throw expr
#else
// used when `-fno-exceptions` is active
#define GEODE_RESULT_IMPL_THROW(expr) std::terminate()
#endif
#endif

namespace geode {
template <class OkType, class ErrType>
class Result;
Expand Down Expand Up @@ -514,7 +526,7 @@ namespace geode {
return std::get<0>(std::move(m_data));
}
else {
throw UnwrapException(ErrTag{}, std::get<1>(m_data));
GEODE_RESULT_IMPL_THROW(UnwrapException(ErrTag{}, std::get<1>(m_data)));
}
}

Expand All @@ -526,7 +538,7 @@ namespace geode {
return std::get<0>(m_data);
}
else {
throw UnwrapException(ErrTag{}, std::get<1>(m_data));
GEODE_RESULT_IMPL_THROW(UnwrapException(ErrTag{}, std::get<1>(m_data)));
}
}

Expand All @@ -538,7 +550,7 @@ namespace geode {
return std::get<0>(m_data);
}
else {
throw UnwrapException(ErrTag{}, std::get<1>(m_data));
GEODE_RESULT_IMPL_THROW(UnwrapException(ErrTag{}, std::get<1>(m_data)));
}
}

Expand All @@ -550,7 +562,7 @@ namespace geode {
return std::get<1>(std::move(m_data));
}
else {
throw UnwrapException(OkTag{}, std::get<0>(m_data));
GEODE_RESULT_IMPL_THROW(UnwrapException(OkTag{}, std::get<0>(m_data)));
}
}

Expand All @@ -562,7 +574,7 @@ namespace geode {
return std::get<1>(m_data);
}
else {
throw UnwrapException(OkTag{}, std::get<0>(m_data));
GEODE_RESULT_IMPL_THROW(UnwrapException(OkTag{}, std::get<0>(m_data)));
}
}

Expand All @@ -574,7 +586,7 @@ namespace geode {
return std::get<1>(m_data);
}
else {
throw UnwrapException(OkTag{}, std::get<0>(m_data));
GEODE_RESULT_IMPL_THROW(UnwrapException(OkTag{}, std::get<0>(m_data)));
}
}

Expand Down Expand Up @@ -769,7 +781,7 @@ namespace geode {
/// @throw UnwrapException if the Result is Err
constexpr void unwrap() {
if (isErr()) {
throw UnwrapException(ErrTag{}, std::get<1>(m_data));
GEODE_RESULT_IMPL_THROW(UnwrapException(ErrTag{}, std::get<1>(m_data)));
}
}

Expand All @@ -781,7 +793,7 @@ namespace geode {
return std::get<1>(std::move(m_data));
}
else {
throw UnwrapException(OkTag{}, std::get<0>(m_data));
GEODE_RESULT_IMPL_THROW(UnwrapException(OkTag{}, std::get<0>(m_data)));
}
}

Expand All @@ -793,7 +805,7 @@ namespace geode {
return std::get<1>(m_data);
}
else {
throw UnwrapException(OkTag{}, std::get<0>(m_data));
GEODE_RESULT_IMPL_THROW(UnwrapException(OkTag{}, std::get<0>(m_data)));
}
}

Expand All @@ -805,7 +817,7 @@ namespace geode {
return std::get<1>(m_data);
}
else {
throw UnwrapException(OkTag{}, std::get<0>(m_data));
GEODE_RESULT_IMPL_THROW(UnwrapException(OkTag{}, std::get<0>(m_data)));
}
}

Expand Down Expand Up @@ -905,7 +917,7 @@ namespace geode {
return std::get<0>(std::move(m_data));
}
else {
throw UnwrapException(ErrTag{}, std::get<1>(m_data));
GEODE_RESULT_IMPL_THROW(UnwrapException(ErrTag{}, std::get<1>(m_data)));
}
}

Expand All @@ -917,7 +929,7 @@ namespace geode {
return std::get<0>(m_data);
}
else {
throw UnwrapException(ErrTag{}, std::get<1>(m_data));
GEODE_RESULT_IMPL_THROW(UnwrapException(ErrTag{}, std::get<1>(m_data)));
}
}

Expand All @@ -929,7 +941,7 @@ namespace geode {
return std::get<0>(m_data);
}
else {
throw UnwrapException(ErrTag{}, std::get<1>(m_data));
GEODE_RESULT_IMPL_THROW(UnwrapException(ErrTag{}, std::get<1>(m_data)));
}
}

Expand All @@ -938,7 +950,7 @@ namespace geode {
/// @return the Err value
constexpr void unwrapErr() {
if (isOk()) {
throw UnwrapException(OkTag{}, std::get<0>(m_data));
GEODE_RESULT_IMPL_THROW(UnwrapException(OkTag{}, std::get<0>(m_data)));
}
}

Expand Down Expand Up @@ -1093,7 +1105,7 @@ namespace geode {
/// @throw UnwrapException if the Result is Err
constexpr void unwrap() {
if (isErr()) {
throw UnwrapException(ErrTag{}, std::get<1>(m_data));
GEODE_RESULT_IMPL_THROW(UnwrapException(ErrTag{}, std::get<1>(m_data)));
}
}

Expand All @@ -1102,7 +1114,7 @@ namespace geode {
/// @return the Err value
constexpr void unwrapErr() {
if (isOk()) {
throw UnwrapException(OkTag{}, std::get<0>(m_data));
GEODE_RESULT_IMPL_THROW(UnwrapException(OkTag{}, std::get<0>(m_data)));
}
}

Expand Down

0 comments on commit 88fde6b

Please sign in to comment.