Skip to content

Commit 07069f3

Browse files
committed
Add constinit support and improve exception handling
Introduced support for the `constinit` keyword for static initializations and included necessary headers. Enhanced exception handling to provide a more robust mechanism by using `DAW_THROW_OR_TERMINATE_NA`. Added thread-safety documentation for function calls that modify handlers.
1 parent df0b59d commit 07069f3

3 files changed

Lines changed: 28 additions & 7 deletions

File tree

include/daw/daw_check_exceptions.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ namespace daw::check_except_detail {
4848
}
4949

5050
template<typename>
51-
[[noreturn, maybe_unused]] DAW_ATTRIB_NOINLINE void terminate_error( ) {
51+
[[noreturn, maybe_unused]] DAW_ATTRIB_NOINLINE inline void terminate_error( ) {
5252
std::terminate( );
5353
}
5454
} // namespace
@@ -61,7 +61,7 @@ namespace daw::check_except_detail {
6161
::daw::check_except_detail::throw_error( ExceptionType{ } )
6262
#else
6363
#define DAW_THROW_OR_TERMINATE( ExceptionType, ... ) \
64-
::daw::check_except_detail::terminate_error<ExceptionType>( )
64+
::daw::check_except_detail::terminate_error( )
6565
#define DAW_THROW_OR_TERMINATE_NA( ExceptionType ) \
66-
::daw::check_except_detail::terminate_error<ExceptionType>( )
66+
::daw::check_except_detail::terminate_error( )
6767
#endif

include/daw/daw_cpp_feature_check.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,14 @@ inline constexpr bool daw_has_cx_cmath = false;
160160
#define DAW_HAS_CPP_CONSTEVAL
161161
#endif
162162
#endif
163+
164+
#if defined( __cpp_constinit )
165+
#if __cpp_constinit >= 201907L
166+
#define DAW_HAS_CPP_CONSTINIT
167+
#define DAW_CONSTINIT constinit
168+
#endif
169+
#endif
170+
171+
#if not defined( DAW_CONSTINIT )
172+
#define DAW_CONSTINIT
173+
#endif

include/daw/integers/impl/daw_signed_error_handling.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include "daw/daw_arith_traits.h"
1212
#include "daw/daw_attributes.h"
13+
#include "daw/daw_check_exceptions.h"
14+
#include "daw/daw_cpp_feature_check.h"
1315

1416
#include <exception>
1517
#include <memory>
@@ -34,29 +36,33 @@ namespace daw::integers {
3436

3537
namespace sint_impl {
3638
inline auto &get_signed_integer_overflow_handler( ) {
37-
static struct handler_t {
39+
static DAW_CONSTINIT struct handler_t {
3840
signed_int_error_handler_t cb = nullptr;
3941
void *data = nullptr;
4042
} handler{ };
4143
return handler;
4244
}
4345

4446
inline auto &get_signed_integer_div_by_zero_handler( ) {
45-
static struct handler_t {
47+
static DAW_CONSTINIT struct handler_t {
4648
signed_int_error_handler_t cb = nullptr;
4749
void *data = nullptr;
4850
} handler{ };
4951
return handler;
5052
}
5153
} // namespace sint_impl
5254

55+
/// Caller is responsible for ensuring that this is called in a context that
56+
/// protects against multiple threads accessing/writing at the same time
5357
DAW_ATTRIB_NOINLINE inline void register_signed_overflow_handler(
5458
signed_int_error_handler_t handler = nullptr,
5559
void *data = nullptr ) noexcept {
5660
sint_impl::get_signed_integer_overflow_handler( ).cb = handler;
5761
sint_impl::get_signed_integer_overflow_handler( ).data = data;
5862
}
5963

64+
/// Caller is responsible for ensuring that this is called in a context that
65+
/// protects against multiple threads accessing/writing at the same time
6066
template<typename Func,
6167
std::enable_if_t<std::is_class_v<Func> and
6268
std::is_invocable_v<Func, SignedIntegerErrorType>,
@@ -79,13 +85,17 @@ namespace daw::integers {
7985
}
8086
}
8187

88+
/// Caller is responsible for ensuring that this is called in a context that
89+
/// protects against multiple threads accessing/writing at the same time
8290
DAW_ATTRIB_NOINLINE inline void register_signed_div_by_zero_handler(
8391
signed_int_error_handler_t handler = nullptr,
8492
void *data = nullptr ) noexcept {
8593
sint_impl::get_signed_integer_div_by_zero_handler( ).cb = handler;
8694
sint_impl::get_signed_integer_div_by_zero_handler( ).data = data;
8795
}
8896

97+
/// Caller is responsible for ensuring that this is called in a context that
98+
/// protects against multiple threads accessing/writing at the same time
8999
template<typename Func,
90100
std::enable_if_t<std::is_class_v<Func> and
91101
std::is_invocable_v<Func, SignedIntegerErrorType>,
@@ -117,7 +127,7 @@ namespace daw::integers {
117127
handler.cb( handler.data, SignedIntegerErrorType::Overflow );
118128
return;
119129
}
120-
throw signed_integer_overflow_exception( );
130+
DAW_THROW_OR_TERMINATE_NA( signed_integer_overflow_exception );
121131
}
122132

123133
DAW_ATTRIB_NOINLINE inline void on_signed_integer_div_by_zero( ) {
@@ -126,6 +136,6 @@ namespace daw::integers {
126136
handler.cb( handler.data, SignedIntegerErrorType::DivideByZero );
127137
return;
128138
}
129-
throw signed_integer_div_by_zero_exception( );
139+
DAW_THROW_OR_TERMINATE_NA( signed_integer_div_by_zero_exception );
130140
}
131141
} // namespace daw::integers

0 commit comments

Comments
 (0)