Skip to content

Commit 88fde6b

Browse files
committed
add support for -fno-exception, calls terminate instead of throwing
1 parent bbd49be commit 88fde6b

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
22
cmake_policy(SET CMP0097 NEW)
33

4-
project(GeodeResult VERSION 1.3.3 LANGUAGES C CXX)
4+
project(GeodeResult VERSION 1.3.4 LANGUAGES C CXX)
55

66
add_library(GeodeResult INTERFACE)
77

include/Geode/Result.hpp

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define GEODE_RESULT_HPP
33

44
#include <concepts>
5+
#include <exception>
56
#include <optional>
67
#include <sstream>
78
#include <stdexcept>
@@ -128,6 +129,17 @@
128129
(errVariable = std::move(GEODE_CONCAT(res, __LINE__)).unwrapErr(), false)
129130
#endif
130131

132+
// Internal macros
133+
134+
#if !defined(GEODE_RESULT_IMPL_THROW)
135+
#if __cpp_exceptions
136+
#define GEODE_RESULT_IMPL_THROW(expr) throw expr
137+
#else
138+
// used when `-fno-exceptions` is active
139+
#define GEODE_RESULT_IMPL_THROW(expr) std::terminate()
140+
#endif
141+
#endif
142+
131143
namespace geode {
132144
template <class OkType, class ErrType>
133145
class Result;
@@ -514,7 +526,7 @@ namespace geode {
514526
return std::get<0>(std::move(m_data));
515527
}
516528
else {
517-
throw UnwrapException(ErrTag{}, std::get<1>(m_data));
529+
GEODE_RESULT_IMPL_THROW(UnwrapException(ErrTag{}, std::get<1>(m_data)));
518530
}
519531
}
520532

@@ -526,7 +538,7 @@ namespace geode {
526538
return std::get<0>(m_data);
527539
}
528540
else {
529-
throw UnwrapException(ErrTag{}, std::get<1>(m_data));
541+
GEODE_RESULT_IMPL_THROW(UnwrapException(ErrTag{}, std::get<1>(m_data)));
530542
}
531543
}
532544

@@ -538,7 +550,7 @@ namespace geode {
538550
return std::get<0>(m_data);
539551
}
540552
else {
541-
throw UnwrapException(ErrTag{}, std::get<1>(m_data));
553+
GEODE_RESULT_IMPL_THROW(UnwrapException(ErrTag{}, std::get<1>(m_data)));
542554
}
543555
}
544556

@@ -550,7 +562,7 @@ namespace geode {
550562
return std::get<1>(std::move(m_data));
551563
}
552564
else {
553-
throw UnwrapException(OkTag{}, std::get<0>(m_data));
565+
GEODE_RESULT_IMPL_THROW(UnwrapException(OkTag{}, std::get<0>(m_data)));
554566
}
555567
}
556568

@@ -562,7 +574,7 @@ namespace geode {
562574
return std::get<1>(m_data);
563575
}
564576
else {
565-
throw UnwrapException(OkTag{}, std::get<0>(m_data));
577+
GEODE_RESULT_IMPL_THROW(UnwrapException(OkTag{}, std::get<0>(m_data)));
566578
}
567579
}
568580

@@ -574,7 +586,7 @@ namespace geode {
574586
return std::get<1>(m_data);
575587
}
576588
else {
577-
throw UnwrapException(OkTag{}, std::get<0>(m_data));
589+
GEODE_RESULT_IMPL_THROW(UnwrapException(OkTag{}, std::get<0>(m_data)));
578590
}
579591
}
580592

@@ -769,7 +781,7 @@ namespace geode {
769781
/// @throw UnwrapException if the Result is Err
770782
constexpr void unwrap() {
771783
if (isErr()) {
772-
throw UnwrapException(ErrTag{}, std::get<1>(m_data));
784+
GEODE_RESULT_IMPL_THROW(UnwrapException(ErrTag{}, std::get<1>(m_data)));
773785
}
774786
}
775787

@@ -781,7 +793,7 @@ namespace geode {
781793
return std::get<1>(std::move(m_data));
782794
}
783795
else {
784-
throw UnwrapException(OkTag{}, std::get<0>(m_data));
796+
GEODE_RESULT_IMPL_THROW(UnwrapException(OkTag{}, std::get<0>(m_data)));
785797
}
786798
}
787799

@@ -793,7 +805,7 @@ namespace geode {
793805
return std::get<1>(m_data);
794806
}
795807
else {
796-
throw UnwrapException(OkTag{}, std::get<0>(m_data));
808+
GEODE_RESULT_IMPL_THROW(UnwrapException(OkTag{}, std::get<0>(m_data)));
797809
}
798810
}
799811

@@ -805,7 +817,7 @@ namespace geode {
805817
return std::get<1>(m_data);
806818
}
807819
else {
808-
throw UnwrapException(OkTag{}, std::get<0>(m_data));
820+
GEODE_RESULT_IMPL_THROW(UnwrapException(OkTag{}, std::get<0>(m_data)));
809821
}
810822
}
811823

@@ -905,7 +917,7 @@ namespace geode {
905917
return std::get<0>(std::move(m_data));
906918
}
907919
else {
908-
throw UnwrapException(ErrTag{}, std::get<1>(m_data));
920+
GEODE_RESULT_IMPL_THROW(UnwrapException(ErrTag{}, std::get<1>(m_data)));
909921
}
910922
}
911923

@@ -917,7 +929,7 @@ namespace geode {
917929
return std::get<0>(m_data);
918930
}
919931
else {
920-
throw UnwrapException(ErrTag{}, std::get<1>(m_data));
932+
GEODE_RESULT_IMPL_THROW(UnwrapException(ErrTag{}, std::get<1>(m_data)));
921933
}
922934
}
923935

@@ -929,7 +941,7 @@ namespace geode {
929941
return std::get<0>(m_data);
930942
}
931943
else {
932-
throw UnwrapException(ErrTag{}, std::get<1>(m_data));
944+
GEODE_RESULT_IMPL_THROW(UnwrapException(ErrTag{}, std::get<1>(m_data)));
933945
}
934946
}
935947

@@ -938,7 +950,7 @@ namespace geode {
938950
/// @return the Err value
939951
constexpr void unwrapErr() {
940952
if (isOk()) {
941-
throw UnwrapException(OkTag{}, std::get<0>(m_data));
953+
GEODE_RESULT_IMPL_THROW(UnwrapException(OkTag{}, std::get<0>(m_data)));
942954
}
943955
}
944956

@@ -1093,7 +1105,7 @@ namespace geode {
10931105
/// @throw UnwrapException if the Result is Err
10941106
constexpr void unwrap() {
10951107
if (isErr()) {
1096-
throw UnwrapException(ErrTag{}, std::get<1>(m_data));
1108+
GEODE_RESULT_IMPL_THROW(UnwrapException(ErrTag{}, std::get<1>(m_data)));
10971109
}
10981110
}
10991111

@@ -1102,7 +1114,7 @@ namespace geode {
11021114
/// @return the Err value
11031115
constexpr void unwrapErr() {
11041116
if (isOk()) {
1105-
throw UnwrapException(OkTag{}, std::get<0>(m_data));
1117+
GEODE_RESULT_IMPL_THROW(UnwrapException(OkTag{}, std::get<0>(m_data)));
11061118
}
11071119
}
11081120

0 commit comments

Comments
 (0)