Skip to content

Commit e1988a9

Browse files
committed
Update cpp2util.h so that cpp2 compiled code can compile and run without exceptions and/or rtti turned on
1 parent 434688f commit e1988a9

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

include/cpp2util.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,26 @@
2020
#ifndef __CPP2_UTIL
2121
#define __CPP2_UTIL
2222

23+
#if __EXCEPTIONS
24+
#define CPP2_EXCEPTIONS
25+
#endif
26+
27+
#ifdef __RTTI
28+
#define CPP2_RTTI
29+
#endif
30+
31+
#if defined CPP2_EXCEPTIONS
32+
#if defined CPP2_THROW
33+
#error "Do not redefine CPP2_THROW when using cpp2 with exceptions enabled."
34+
#endif
35+
#define CPP2_THROW(DESC,EXCEPTION) throw EXCEPTION
36+
#else
37+
#if !defined CPP2_THROW
38+
// TODO: This should call a utility function that prints DESC and a backtrace.
39+
#define CPP2_THROW(DESC,EXCEPTION) std::terminate()
40+
#endif
41+
#endif
42+
2343
// If this implementation doesn't support source_location yet, disable it
2444
// TODO: technically this test should have <version> included first, but GEFN
2545
#if !defined(_MSC_VER) && !defined(__cpp_lib_source_location)
@@ -856,20 +876,21 @@ auto as( std::variant<Ts...> const& x ) {
856876
if constexpr (std::is_same_v< CPP2_TYPEOF(operator_as<17>(x)), T >) { if (x.index() == 17) return operator_as<7>(x); }
857877
if constexpr (std::is_same_v< CPP2_TYPEOF(operator_as<18>(x)), T >) { if (x.index() == 18) return operator_as<8>(x); }
858878
if constexpr (std::is_same_v< CPP2_TYPEOF(operator_as<19>(x)), T >) { if (x.index() == 19) return operator_as<9>(x); }
859-
throw std::bad_variant_access();
879+
CPP2_THROW("std::bad_variant_access", std::bad_variant_access());
860880
}
861881

862882

863883
//-------------------------------------------------------------------------------------------------------------
864884
// std::any is and as
865885
//
866-
886+
#ifdef CPP2_RTTI
867887
// is Type
868888
//
869889
template<typename T, typename X>
870890
requires (std::is_same_v<X,std::any> && !std::is_same_v<T,std::any> && !std::is_same_v<T,empty>)
871891
constexpr auto is( X const& x ) -> bool
872892
{ return x.type() == typeid(T); }
893+
#endif
873894

874895
template<typename T, typename X>
875896
requires (std::is_same_v<X,std::any> && std::is_same_v<T,empty>)
@@ -1137,7 +1158,7 @@ inline auto fopen( const char* filename, const char* mode ) {
11371158
#endif
11381159

11391160
if (!x) {
1140-
throw std::make_error_condition(std::errc::no_such_file_or_directory);
1161+
CPP2_THROW("std::errc::no_such_file_or_directory", std::make_error_condition(std::errc::no_such_file_or_directory));
11411162
}
11421163
return c_raii( x, &fclose );
11431164
}

0 commit comments

Comments
 (0)