Skip to content

Commit f3eb30c

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

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)
@@ -987,20 +1007,21 @@ auto as( std::variant<Ts...> const& x ) {
9871007
if constexpr (std::is_same_v< CPP2_TYPEOF(operator_as<17>(x)), T >) { if (x.index() == 17) return operator_as<7>(x); }
9881008
if constexpr (std::is_same_v< CPP2_TYPEOF(operator_as<18>(x)), T >) { if (x.index() == 18) return operator_as<8>(x); }
9891009
if constexpr (std::is_same_v< CPP2_TYPEOF(operator_as<19>(x)), T >) { if (x.index() == 19) return operator_as<9>(x); }
990-
throw std::bad_variant_access();
1010+
CPP2_THROW("std::bad_variant_access", std::bad_variant_access());
9911011
}
9921012

9931013

9941014
//-------------------------------------------------------------------------------------------------------------
9951015
// std::any is and as
9961016
//
997-
1017+
#ifdef CPP2_RTTI
9981018
// is Type
9991019
//
10001020
template<typename T, typename X>
10011021
requires (std::is_same_v<X,std::any> && !std::is_same_v<T,std::any> && !std::is_same_v<T,empty>)
10021022
constexpr auto is( X const& x ) -> bool
10031023
{ return x.type() == typeid(T); }
1024+
#endif
10041025

10051026
template<typename T, typename X>
10061027
requires (std::is_same_v<X,std::any> && std::is_same_v<T,empty>)
@@ -1273,7 +1294,7 @@ inline auto fopen( const char* filename, const char* mode ) {
12731294
#endif
12741295

12751296
if (!x) {
1276-
throw std::make_error_condition(std::errc::no_such_file_or_directory);
1297+
CPP2_THROW("std::errc::no_such_file_or_directory", std::make_error_condition(std::errc::no_such_file_or_directory));
12771298
}
12781299
return c_raii( x, &fclose );
12791300
}

0 commit comments

Comments
 (0)