exception_policy.xml 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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_numeric.exception_policies">
  5. <title>exception_policy&lt;AE, IDB, UB, UV&gt;</title>
  6. <?dbhtml stop-chunking?>
  7. <section>
  8. <title>Description</title>
  9. <para>Create a valid exception policy from 4 function objects. This
  10. specifies the actions to be taken for different types of invalid
  11. results.</para>
  12. </section>
  13. <section>
  14. <title>Template Parameters</title>
  15. <informaltable>
  16. <tgroup cols="3">
  17. <colspec align="left" colwidth="1*"/>
  18. <colspec align="left" colwidth="6*"/>
  19. <colspec align="left" colwidth="9*"/>
  20. <thead>
  21. <row>
  22. <entry align="left">Parameter</entry>
  23. <entry align="left">Type Requirements</entry>
  24. <entry>Invoked when:</entry>
  25. </row>
  26. </thead>
  27. <tbody>
  28. <row>
  29. <entry><code>AE</code></entry>
  30. <entry>Function object callable with the expression AE(e,
  31. message)</entry>
  32. <entry><para>The operation cannot produce valid arithmetic result
  33. such as overflows, divide by zero, etc.</para></entry>
  34. </row>
  35. <row>
  36. <entry><code>UB</code></entry>
  37. <entry>Function object callable with the expression UB(e,
  38. message)</entry>
  39. <entry><para>The result is undefined by the C++
  40. standard</para></entry>
  41. </row>
  42. <row>
  43. <entry><code>IDB</code></entry>
  44. <entry>Function object callable with the expression IDB(e,</entry>
  45. <entry><para>The result depends upon implementation defined
  46. behavior according to the C++ standard</para></entry>
  47. </row>
  48. <row>
  49. <entry><code>UV</code></entry>
  50. <entry>Function object callable with the expression UV(e,
  51. message)</entry>
  52. <entry><para>A variable is not initialized</para></entry>
  53. </row>
  54. </tbody>
  55. </tgroup>
  56. </informaltable>
  57. </section>
  58. <section>
  59. <title>Model of</title>
  60. <para><link
  61. linkend="safe_numerics.promotion_policy">ExceptionPolicy</link></para>
  62. </section>
  63. <section>
  64. <title>Inherited Valid Expressions</title>
  65. <para>This class implements all the valid operations from the type
  66. requirements <link
  67. linkend="safe_numerics.promotion_policy">ExceptionPolicy</link>. Aside
  68. from these, there are no other operations implemented.</para>
  69. </section>
  70. <section>
  71. <title>Function Objects</title>
  72. <para>In order to create an exception policy, one needs some function
  73. objects. The library includes some appropriate examples of these:</para>
  74. <informaltable>
  75. <tgroup cols="2">
  76. <colspec align="left" colwidth="1*"/>
  77. <colspec align="left" colwidth="3*"/>
  78. <thead>
  79. <row>
  80. <entry align="left">Name</entry>
  81. <entry align="left">Description</entry>
  82. </row>
  83. </thead>
  84. <tbody>
  85. <row>
  86. <entry><code>ignore_exception</code></entry>
  87. <entry>Ignore any runtime exception and just return - thus
  88. propagating the error. This is what would happen with unsafe data
  89. types</entry>
  90. </row>
  91. <row>
  92. <entry><code>throw_exception</code></entry>
  93. <entry>throw an exception of type std::system_error</entry>
  94. </row>
  95. <row>
  96. <entry><code>trap_exception</code></entry>
  97. <entry>Invoke a function which is undefined. Compilers will
  98. include this function if and only if there is a possibility of a
  99. runtime error. Conversely, This will create a compile time error
  100. if there is any possibility that the operation will fail at
  101. runtime. Use the action to guarantee that your application will
  102. never produce an invalid result. Any operation invoke</entry>
  103. </row>
  104. </tbody>
  105. </tgroup>
  106. </informaltable>
  107. <para>But of course one is free to provide his own. Here is an example of
  108. a function object which would could be used exception conditions.</para>
  109. <programlisting>// log an exception condition but continue processing as though nothing has happened
  110. // this would emulate the behavior of an unsafe type.
  111. struct log_runtime_exception {
  112. log_runtime_exception(const safe_numerics_error &amp; e, const char * message){
  113. std::cout &lt;&lt; "Caught system_error with code " &lt;&lt; e.code()
  114. &lt;&lt; " meaning " &lt;&lt; e.what() &lt;&lt; '\n';
  115. }
  116. };
  117. </programlisting>
  118. </section>
  119. <section>
  120. <title>Policies Provided by the library</title>
  121. <para>The above function object can be composed into an exception policy
  122. by this class. The library provides common policies all ready to use. In
  123. the table below, the word "loose" is used to indicate that implementation
  124. defined and undefined behavior is not considered an exceptional condition,
  125. while "strict" means the opposite. The word "exception" means that a
  126. runtime exception will be thrown. The word "trap" means that the mere
  127. possibility of an error condition will result in a compile time
  128. error.</para>
  129. <para><informaltable>
  130. <tgroup cols="2">
  131. <colspec align="left" colwidth="1*"/>
  132. <colspec align="left" colwidth="3*"/>
  133. <thead>
  134. <row>
  135. <entry align="left">Name</entry>
  136. <entry align="left">Description</entry>
  137. </row>
  138. </thead>
  139. <tbody>
  140. <row>
  141. <entry
  142. id="safe_numerics.exception_policies.loose_exception_policy"><link
  143. linkend="safe_numerics.exception_policies.loose_exception_policy">loose_exception_policy</link></entry>
  144. <entry>Throws runtime exception on any arithmetic error.
  145. Undefined and implementation defined behavior is permitted as
  146. long as it does not produce an arithmetic error.</entry>
  147. </row>
  148. <row>
  149. <entry
  150. id="safe_numerics.exception_policies.loose_trap_policy"><code><link
  151. linkend="safe_numerics.exception_policies.loose_trap_policy">loose_trap_policy</link></code></entry>
  152. <entry>Invoke a compile time error in any case where it's
  153. possible to result in an arithmetic error.</entry>
  154. </row>
  155. <row>
  156. <entry
  157. id="safe_numerics.exception_policies.strict_exception_policy"><code><link
  158. linkend="safe_numerics.exception_policies.strict_exception_policy">strict_exception_policy</link></code></entry>
  159. <entry>Throws runtime exception on any arithmetic error. Any
  160. undefined or implementation defined behavior also results in
  161. throwing an exception.</entry>
  162. </row>
  163. <row>
  164. <entry
  165. id="safe_numerics.exception_policies.strict_trap_policy"><code><link
  166. linkend="safe_numerics.exception_policies.strict_trap_policy">strict_trap_policy</link></code></entry>
  167. <entry>Invoke a compile time error in any case where it's
  168. possible to result in an arithmetic error, undefined behavior or
  169. implementation defined behavior</entry>
  170. </row>
  171. <row>
  172. <entry
  173. id="safe_numerics.exception_policies.default_exception_policy"><code><link
  174. linkend="safe_numerics.exception_policies.default_exception_policy">default_exception_policy</link></code></entry>
  175. <entry>an alias for <code><link
  176. linkend="safe_numerics.exception_policies.strict_exception_policy">strict_exception_policy</link></code></entry>
  177. </row>
  178. </tbody>
  179. </tgroup>
  180. </informaltable>If none of the above suit your needs, you're free to
  181. create your own. Here is one where use the logging function object defined
  182. above as a component in a loose exception policy which logs any arithmetic
  183. errors and ignores any other types of errors.</para>
  184. <para><programlisting>// logging policy
  185. // log arithmetic errors but ignore them and continue to execute
  186. // implementation defined and undefined behavior is just executed
  187. // without logging.
  188. using logging_exception_policy = exception_policy&lt;
  189. log_runtime_exception, // arithmetic error
  190. ignore_exception, // implementation defined behavior
  191. ignore_exception, // undefined behavior
  192. ignore_exception // uninitialized value
  193. &gt;;
  194. </programlisting></para>
  195. </section>
  196. <section>
  197. <title>Header</title>
  198. <para><ulink
  199. url="../../include/boost/safe_numerics/concept/exception_policy.hpp"><code>#include
  200. &lt;boost/numeric/safe_numerics/concept/exception_policy.hpp&gt;</code></ulink></para>
  201. </section>
  202. </section>