123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- #ifndef BOOST_NUMERIC_EXCEPTION_POLICIES_HPP
- #define BOOST_NUMERIC_EXCEPTION_POLICIES_HPP
- #include <boost/mp11.hpp>
- #include <boost/config.hpp> // BOOST_NO_EXCEPTIONS
- #include "exception.hpp"
- namespace boost {
- namespace safe_numerics {
- template<
- typename AE,
- typename IDB,
- typename UB,
- typename UV
- >
- struct exception_policy {
- constexpr static void on_arithmetic_error(
- const safe_numerics_error & e,
- const char * msg
- ){
- AE()(e, msg);
- }
- constexpr static void on_implementation_defined_behavior(
- const safe_numerics_error & e,
- const char * msg
- ){
- IDB()(e, msg);
- }
- constexpr static void on_undefined_behavior(
- const safe_numerics_error & e,
- const char * msg
- ){
- UB()(e, msg);
- }
- constexpr static void on_uninitialized_value(
- const safe_numerics_error & e,
- const char * msg
- ){
- UV()(e, msg);
- }
- };
- struct ignore_exception {
- constexpr ignore_exception() = default;
- constexpr void operator () (
- const boost::safe_numerics::safe_numerics_error &,
- const char *
- ){}
- };
- struct trap_exception {
- constexpr trap_exception() = default;
-
-
- };
- struct throw_exception {
- constexpr throw_exception() = default;
- #ifndef BOOST_NO_EXCEPTIONS
- void operator()(
- const safe_numerics_error & e,
- const char * message
- ){
- throw std::system_error(std::error_code(e), message);
- }
- #else
- constexpr trap_exception()(const safe_numerics_error & e, const char * message);
- #endif
- };
- constexpr inline safe_numerics_actions
- make_safe_numerics_action(const safe_numerics_error & e){
-
-
- switch(e){
- case safe_numerics_error::negative_overflow_error:
- case safe_numerics_error::underflow_error:
- case safe_numerics_error::range_error:
- case safe_numerics_error::domain_error:
- case safe_numerics_error::positive_overflow_error:
- case safe_numerics_error::precision_overflow_error:
- return safe_numerics_actions::arithmetic_error;
- case safe_numerics_error::negative_value_shift:
- case safe_numerics_error::negative_shift:
- case safe_numerics_error::shift_too_large:
- return safe_numerics_actions::implementation_defined_behavior;
- case safe_numerics_error::uninitialized_value:
- return safe_numerics_actions::uninitialized_value;
- case safe_numerics_error::success:
- return safe_numerics_actions::no_action;
- default:
- assert(false);
- }
-
-
- return safe_numerics_actions::no_action;
- }
- using loose_exception_policy = exception_policy<
- throw_exception,
- ignore_exception,
- ignore_exception,
- ignore_exception
- >;
- using loose_trap_policy = exception_policy<
- trap_exception,
- ignore_exception,
- ignore_exception,
- ignore_exception
- >;
- using strict_exception_policy = exception_policy<
- throw_exception,
- throw_exception,
- throw_exception,
- ignore_exception
- >;
- using strict_trap_policy = exception_policy<
- trap_exception,
- trap_exception,
- trap_exception,
- trap_exception
- >;
- using default_exception_policy = strict_exception_policy;
- }
- }
- #endif
|