exception_policies.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #ifndef BOOST_NUMERIC_EXCEPTION_POLICIES_HPP
  2. #define BOOST_NUMERIC_EXCEPTION_POLICIES_HPP
  3. // Copyright (c) 2015 Robert Ramey
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #include <boost/mp11.hpp>
  9. #include "exception.hpp"
  10. namespace boost {
  11. namespace safe_numerics {
  12. template<
  13. typename AE,
  14. typename IDB,
  15. typename UB,
  16. typename UV
  17. >
  18. struct exception_policy {
  19. static constexpr void on_arithmetic_error(
  20. const safe_numerics_error & e,
  21. const char * msg
  22. ){
  23. AE(e, msg);
  24. }
  25. static constexpr void on_implementation_defined_behavior(
  26. const safe_numerics_error & e,
  27. const char * msg
  28. ){
  29. IDB(e, msg);
  30. }
  31. static constexpr void on_undefined_behavior(
  32. const safe_numerics_error & e,
  33. const char * msg
  34. ){
  35. UB(e, msg);
  36. }
  37. static constexpr void on_uninitialized_value(
  38. const safe_numerics_error & e,
  39. const char * msg
  40. ){
  41. UV(e, msg);
  42. }
  43. };
  44. ////////////////////////////////////////////////////////////////////////////////
  45. // pre-made error action handers
  46. // ignore any error and just return.
  47. struct ignore_exception {
  48. constexpr ignore_exception(const safe_numerics_error &, const char * ){}
  49. };
  50. // If an exceptional condition is detected at runtime throw the exception.
  51. struct throw_exception {
  52. throw_exception(const safe_numerics_error & e, const char * message){
  53. throw std::system_error(std::error_code(e), message);
  54. }
  55. };
  56. // emit compile time error if this is invoked.
  57. struct trap_exception {};
  58. // given an error code - return the action code which it corresponds to.
  59. constexpr safe_numerics_actions
  60. make_safe_numerics_action(const safe_numerics_error & e){
  61. // we can't use standard algorithms since we want this to be constexpr
  62. // this brute force solution is simple and pretty fast anyway
  63. switch(e){
  64. case safe_numerics_error::negative_overflow_error:
  65. case safe_numerics_error::underflow_error:
  66. case safe_numerics_error::range_error:
  67. case safe_numerics_error::domain_error:
  68. case safe_numerics_error::positive_overflow_error:
  69. case safe_numerics_error::precision_overflow_error:
  70. return safe_numerics_actions::arithmetic_error;
  71. case safe_numerics_error::negative_value_shift:
  72. case safe_numerics_error::negative_shift:
  73. case safe_numerics_error::shift_too_large:
  74. return safe_numerics_actions::implementation_defined_behavior;
  75. case safe_numerics_error::uninitialized_value:
  76. return safe_numerics_actions::uninitialized_value;
  77. case safe_numerics_error::success:
  78. return safe_numerics_actions::no_action;
  79. default:
  80. assert(false);
  81. }
  82. // should never arrive here
  83. //include to suppress bogus warning
  84. return safe_numerics_actions::no_action;
  85. }
  86. ////////////////////////////////////////////////////////////////////////////////
  87. // compile time error dispatcher
  88. // note slightly baroque implementation of a compile time switch statement
  89. // which instatiates oonly those cases which are actually invoked. This is
  90. // motivated to implement the "trap" functionality which will generate a syntax
  91. // error if and only a function which might fail is called.
  92. namespace dispatch_switch {
  93. template<class EP, safe_numerics_actions>
  94. struct dispatch_case {};
  95. template<class EP>
  96. struct dispatch_case<EP, safe_numerics_actions::uninitialized_value> {
  97. constexpr static void invoke(const safe_numerics_error & e, const char * msg){
  98. EP::on_uninitialized_value(e, msg);
  99. }
  100. };
  101. template<class EP>
  102. struct dispatch_case<EP, safe_numerics_actions::arithmetic_error> {
  103. constexpr static void invoke(const safe_numerics_error & e, const char * msg){
  104. EP::on_arithmetic_error(e, msg);
  105. }
  106. };
  107. template<class EP>
  108. struct dispatch_case<EP, safe_numerics_actions::implementation_defined_behavior> {
  109. constexpr static void invoke(const safe_numerics_error & e, const char * msg){
  110. EP::on_implementation_defined_behavior(e, msg);
  111. }
  112. };
  113. template<class EP>
  114. struct dispatch_case<EP, safe_numerics_actions::undefined_behavior> {
  115. constexpr static void invoke(const safe_numerics_error & e, const char * msg){
  116. EP::on_undefined_behavior(e, msg);
  117. }
  118. };
  119. } // dispatch_switch
  120. template<class EP, safe_numerics_error E>
  121. constexpr void
  122. dispatch(const char * msg){
  123. constexpr safe_numerics_actions a = make_safe_numerics_action(E);
  124. dispatch_switch::dispatch_case<EP, a>::invoke(E, msg);
  125. }
  126. template<class EP, class R>
  127. class dispatch_and_return {
  128. public:
  129. template<safe_numerics_error E>
  130. constexpr static checked_result<R> invoke(
  131. char const * const & msg
  132. ) {
  133. dispatch<EP, E>(msg);
  134. return checked_result<R>(E, msg);
  135. }
  136. };
  137. ////////////////////////////////////////////////////////////////////////////////
  138. // pre-made error policy classes
  139. // loose exception
  140. // - throw on arithmetic errors
  141. // - ignore other errors.
  142. // Some applications ignore these issues and still work and we don't
  143. // want to update them.
  144. using loose_exception_policy = exception_policy<
  145. throw_exception, // arithmetic error
  146. ignore_exception, // implementation defined behavior
  147. ignore_exception, // undefined behavior
  148. ignore_exception // uninitialized value
  149. >;
  150. // loose trap
  151. // same as above in that it doesn't check for various undefined behaviors
  152. // but traps at compile time for hard arithmetic errors. This policy
  153. // would be suitable for older embedded systems which depend on
  154. // bit manipulation operations to work.
  155. using loose_trap_policy = exception_policy<
  156. trap_exception, // arithmetic error
  157. ignore_exception, // implementation defined behavior
  158. ignore_exception, // undefined behavior
  159. ignore_exception // uninitialized value
  160. >;
  161. // strict exception
  162. // - throw at runtime on any kind of error
  163. // recommended for new code. Check everything at compile time
  164. // if possible and runtime if necessary. Trap or Throw as
  165. // appropriate. Should guarantee code to be portable across
  166. // architectures.
  167. using strict_exception_policy = exception_policy<
  168. throw_exception,
  169. throw_exception,
  170. throw_exception,
  171. ignore_exception
  172. >;
  173. // strict trap
  174. // Same as above but requires code to be written in such a way as to
  175. // make it impossible for errors to occur. This naturally will require
  176. // extra coding effort but might be justified for embedded and/or
  177. // safety critical systems.
  178. using strict_trap_policy = exception_policy<
  179. trap_exception,
  180. trap_exception,
  181. trap_exception,
  182. trap_exception
  183. >;
  184. // default policy
  185. // One would use this first. After experimentation, one might
  186. // replace some actions with ignore_exception
  187. using default_exception_policy = strict_exception_policy;
  188. } // namespace safe_numerics
  189. } // namespace boost
  190. #endif // BOOST_NUMERIC_EXCEPTION_POLICIES_HPP