exception_policy_concept.xml 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE section PUBLIC "-//Boost//DTD BoostBook XML V1.1//EN"
  3. "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
  4. <section id="safe_numerics.exception_policy">
  5. <title>ExceptionPolicy&lt;EP&gt;</title>
  6. <?dbhtml stop-chunking?>
  7. <section>
  8. <title>Description</title>
  9. <para>The exception policy specifies what is to occur when a safe
  10. operation cannot return a valid result. A type is an ExceptionPolicy if it
  11. has functions for handling exceptional events that occur in the course of
  12. safe numeric operations.</para>
  13. </section>
  14. <section>
  15. <title>Notation</title>
  16. <informaltable>
  17. <tgroup cols="2" colsep="1" rowsep="1">
  18. <colspec align="left" colwidth="1*"/>
  19. <colspec align="left" colwidth="6*"/>
  20. <tbody>
  21. <row>
  22. <entry><code>EP</code></entry>
  23. <entry>A type that fulfills the requirements of an
  24. ExceptionPolicy</entry>
  25. </row>
  26. <row>
  27. <entry>e</entry>
  28. <entry>A code from <link
  29. linkend="safe_numerics.safe_numerics_error"><code>safe_numerics_error</code></link></entry>
  30. </row>
  31. <row>
  32. <entry>message</entry>
  33. <entry>A const char * which refers to a text message about the
  34. cause of an exception</entry>
  35. </row>
  36. </tbody>
  37. </tgroup>
  38. </informaltable>
  39. </section>
  40. <section>
  41. <title>Valid Expressions</title>
  42. <para>Whenever an operation yield an invalid result, one of the following
  43. functions will be invoked.</para>
  44. <para><informaltable>
  45. <tgroup cols="3">
  46. <colspec align="left" colwidth="9*"/>
  47. <colspec align="left" colwidth="1*"/>
  48. <colspec align="left" colwidth="12*"/>
  49. <thead>
  50. <row>
  51. <entry align="left">Expression</entry>
  52. <entry>Return Value</entry>
  53. <entry>Invoked when:</entry>
  54. </row>
  55. </thead>
  56. <tbody>
  57. <row>
  58. <entry><code>EP::on_arithmetic_error(e, message)</code></entry>
  59. <entry>void</entry>
  60. <entry>The operation cannot produce valid arithmetic result such
  61. as overflows, divide by zero, etc.</entry>
  62. </row>
  63. <row>
  64. <entry><code>EP::on_undefined_behavior(e,
  65. message)</code></entry>
  66. <entry>void</entry>
  67. <entry>The result is undefined by the C++ standard</entry>
  68. </row>
  69. <row>
  70. <entry><code>EP::on_implementation_defined_behavior(e,
  71. message)</code></entry>
  72. <entry>void</entry>
  73. <entry>The result depends upon implementation defined behavior
  74. according to the C++ standard</entry>
  75. </row>
  76. <row>
  77. <entry><code>EP::on_uninitialized_value(e,
  78. message)</code></entry>
  79. <entry>void</entry>
  80. <entry>A variable is not initialized</entry>
  81. </row>
  82. </tbody>
  83. </tgroup>
  84. </informaltable></para>
  85. </section>
  86. <section>
  87. <title>dispatch&lt;EP&gt;(const safe_numerics_error &amp; e, const char *
  88. msg)</title>
  89. <para>This function is used to invoke the exception handling policy for a
  90. particular exception code.</para>
  91. <section>
  92. <title>Synopsis</title>
  93. <para><programlisting>template&lt;class EP&gt;
  94. constexpr void
  95. dispatch&lt;EP&gt;(const boost::numeric::safe_numerics_error &amp; e, char const * const &amp; msg);</programlisting></para>
  96. </section>
  97. <section>
  98. <title>Example of use</title>
  99. <programlisting>#include &lt;boost/safe_numerics/exception_policies.hpp"
  100. dispatch&lt;boost::numeric::loose_exception_policy&gt;(
  101. boost::numeric::safe_numerics_error::positive_overflow_error,
  102. "operation resulted in overflow"
  103. );</programlisting>
  104. </section>
  105. </section>
  106. <section>
  107. <title>Models</title>
  108. <para>The library header <ulink
  109. url="../../include/boost/safe_numerics/exception_policies.hpp"><code>&lt;boost/numerics/safe_numerics/exception_policies.hpp&gt;
  110. </code></ulink>contains a number of pre-made exception policies:</para>
  111. <itemizedlist>
  112. <listitem>
  113. <para><code id="">boost::numeric::loose_exception_policy</code></para>
  114. <para>Throw on arithmetic errors, ignore other errors. Some
  115. applications ignore these issues and still work and we don't want to
  116. update them.</para>
  117. </listitem>
  118. <listitem>
  119. <para><code
  120. id="safe_numerics.exception_policies.loose_trap_policy">boost::numeric::loose_trap_policy</code></para>
  121. <para>Same as above in that it doesn't check for various undefined
  122. behaviors but traps at compile time for hard arithmetic errors. This
  123. policy would be suitable for older embedded systems which depend on
  124. bit manipulation operations to work.</para>
  125. </listitem>
  126. <listitem>
  127. <para><code
  128. id="safe_numerics.exception_policies.strict_exception_policy">boost::numeric::strict_exception_policy</code></para>
  129. <para>Permit just about anything, throw at runtime on any kind of
  130. error. Recommended for new code. Check everything at compile time if
  131. possible and runtime if necessary. Trap or Throw as appropriate.
  132. Should guarantee code to be portable across architectures.</para>
  133. </listitem>
  134. <listitem>
  135. <para><code
  136. id="safe_numerics.exception_policies.strict_trap_policy">boost::numeric::strict_trap_policy</code></para>
  137. <para>Same as above but requires code to be written in such a way as
  138. to make it impossible for errors to occur. This naturally will require
  139. extra coding effort but might be justified for embedded and/or safety
  140. critical systems.</para>
  141. </listitem>
  142. <listitem>
  143. <para><code
  144. id="safe_numerics.exception_policies.default_exception_policy">boost::numeric::default_exception_policy</code></para>
  145. <para>Alias for <code>strict_exception_policy</code>, One would use
  146. this first. After experimentation, one might switch to one of the
  147. above policies or perhaps use a custom policy.</para>
  148. </listitem>
  149. </itemizedlist>
  150. </section>
  151. <section>
  152. <title>Header</title>
  153. <para><ulink
  154. url="../../include/boost/safe_numerics/concept/exception_policy.hpp"><code>#include
  155. &lt;boost/numeric/safe_numerics/concepts/exception_policy.hpp&gt;
  156. </code></ulink></para>
  157. </section>
  158. </section>