checked_result.xml 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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="safenumerics.checked_result">
  5. <title>checked_result&lt;R&gt;</title>
  6. <?dbhtml stop-chunking?>
  7. <section>
  8. <title>Description</title>
  9. <para>checked_result is a special kind of variant class designed to hold
  10. the result of some operation. It can hold either the result of the
  11. operation or information on why the operation failed to produce a valid
  12. result. It is similar to other types proposed for and/or included to the
  13. C++ standard library or Boost such as<code> expected</code>,
  14. <code>variant</code>, <code>optional</code> and <code>outcome</code>. In
  15. some circumstances it may be referred to as a "monad".</para>
  16. <para><itemizedlist>
  17. <listitem>
  18. <para>All instances of <code>checked_result&lt;R&gt;</code> are
  19. immutable. That is, once constructed, they cannot be altered.</para>
  20. </listitem>
  21. <listitem>
  22. <para>There is no default constructor.</para>
  23. </listitem>
  24. <listitem>
  25. <para><code>checked_result&lt;R&gt;</code> is never empty.</para>
  26. </listitem>
  27. <listitem>
  28. <para>Binary operations supported by type R are guaranteed to be
  29. supported by checked_result&lt;R&gt;.</para>
  30. </listitem>
  31. <listitem>
  32. <para>Binary operations can be invoked on a pair of
  33. <code>checked_result&lt;R&gt;</code> instances if and only if the
  34. underlying type (R) is identical for both instances. They will
  35. return a value of type <code>checked_result&lt;R&gt;</code>.</para>
  36. </listitem>
  37. <listitem>
  38. <para>Unary operations can be invoked on
  39. <code>checked_result&lt;R&gt;</code> instances. They will return a
  40. value of type <code>checked_result&lt;R&gt;</code>.</para>
  41. </listitem>
  42. <listitem>
  43. <para>Comparison operations will return a
  44. <code>boost::logic::tribool</code>. Other binary operations will a
  45. value of the same type as the arguments.</para>
  46. </listitem>
  47. </itemizedlist></para>
  48. <para>Think of <code>checked&lt;R&gt;</code> as an "extended" version of R
  49. which can hold all the values that R can hold in addition other "special
  50. values". For example, consider checked&lt;int&gt;.</para>
  51. </section>
  52. <section>
  53. <title>Notation</title>
  54. <informaltable>
  55. <tgroup cols="2">
  56. <colspec align="left" colwidth="1*"/>
  57. <colspec align="left" colwidth="4*"/>
  58. <thead>
  59. <row>
  60. <entry align="left">Symbol</entry>
  61. <entry align="left">Description</entry>
  62. </row>
  63. </thead>
  64. <tbody>
  65. <row>
  66. <entry><code>R</code></entry>
  67. <entry>Underlying type</entry>
  68. </row>
  69. <row>
  70. <entry><code>r</code></entry>
  71. <entry>An instance of type R</entry>
  72. </row>
  73. <row>
  74. <entry><code>c, c1, c2</code></entry>
  75. <entry>an instance of checked_result&lt;R&gt;</entry>
  76. </row>
  77. <row>
  78. <entry><code>t</code></entry>
  79. <entry>an instance of checked_result&lt;T&gt; for some type T not
  80. necessarily the same as R</entry>
  81. </row>
  82. <row>
  83. <entry><code>e</code></entry>
  84. <entry>An instance of type <link
  85. linkend="safe_numerics.safe_numerics_error"><code>safe_numerics_error</code></link></entry>
  86. </row>
  87. <row>
  88. <entry><code>msg</code></entry>
  89. <entry>An instance of type const char *</entry>
  90. </row>
  91. <row>
  92. <entry><code>OS</code></entry>
  93. <entry>A type convertible to <link
  94. linkend="safe_numerics.safe_numerics_error"><code>std::basic_ostream</code></link></entry>
  95. </row>
  96. <row>
  97. <entry><code>os</code></entry>
  98. <entry>An instance of type convertible to <link
  99. linkend="safe_numerics.safe_numerics_error"><code>std::basic_ostream</code></link></entry>
  100. </row>
  101. </tbody>
  102. </tgroup>
  103. </informaltable>
  104. </section>
  105. <section>
  106. <title>Template Parameters</title>
  107. <para>R must model the type requirements of <link
  108. linkend="safe_numerics.numeric">Numeric</link></para>
  109. <informaltable>
  110. <tgroup cols="2">
  111. <colspec align="left" colwidth="1*"/>
  112. <colspec align="left" colwidth="4*"/>
  113. <thead>
  114. <row>
  115. <entry align="left">Parameter</entry>
  116. <entry>Description</entry>
  117. </row>
  118. </thead>
  119. <tbody>
  120. <row>
  121. <entry><code>R</code></entry>
  122. <entry>Underlying type</entry>
  123. </row>
  124. </tbody>
  125. </tgroup>
  126. </informaltable>
  127. </section>
  128. <section>
  129. <title>Model of</title>
  130. <para><link linkend="safe_numerics.numeric">Numeric</link></para>
  131. </section>
  132. <section>
  133. <title>Valid Expressions</title>
  134. <para>All expressions are <code>constexpr</code>.</para>
  135. <para><informaltable>
  136. <tgroup cols="3">
  137. <colspec align="left" colwidth="2*"/>
  138. <colspec align="left" colwidth="1*"/>
  139. <colspec align="left" colwidth="3*"/>
  140. <thead>
  141. <row>
  142. <entry align="left">Expression</entry>
  143. <entry>Return Type</entry>
  144. <entry>Semantics</entry>
  145. </row>
  146. </thead>
  147. <tbody>
  148. <row>
  149. <entry><code>checked_result(r)</code></entry>
  150. <entry><code>checked_result&lt;R&gt;</code></entry>
  151. <entry>constructor with valid instance of R</entry>
  152. </row>
  153. <row>
  154. <entry><code>checked_result&lt;R&gt;(t)</code></entry>
  155. <entry><code>checked_result&lt;R&gt;</code></entry>
  156. <entry>constructor with <code>checked_result&lt;T&gt;</code>
  157. where T is not R. T must be convertible to R.</entry>
  158. </row>
  159. <row>
  160. <entry><code>checked_result(e, msg)</code></entry>
  161. <entry><code>checked_result&lt;R&gt;</code></entry>
  162. <entry>constructor with error information</entry>
  163. </row>
  164. <row>
  165. <entry><code>static_cast&lt;R&gt;(c)</code></entry>
  166. <entry>R</entry>
  167. <entry>extract wrapped value - compile time error if not
  168. possible</entry>
  169. </row>
  170. <row>
  171. <entry><code>static_cast&lt;<code><link
  172. linkend="safe_numerics.safe_numerics_error"><code>safe_numerics_error</code></link></code>&gt;(c)</code></entry>
  173. <entry><link
  174. linkend="safe_numerics.safe_numerics_error"><code>safe_numerics_error</code></link></entry>
  175. <entry>extract wrapped value - may return <link
  176. linkend="safe_numerics.safe_numerics_error"><code>safe_numerics_error</code></link><code>::success</code>
  177. if there is no error</entry>
  178. </row>
  179. <row>
  180. <entry><code>static_cast&lt;const char *&gt;(c)</code></entry>
  181. <entry><code>const char *</code></entry>
  182. <entry>returns pointer to the included error message</entry>
  183. </row>
  184. <row>
  185. <entry><code>c.exception()</code></entry>
  186. <entry><code>bool</code></entry>
  187. <entry>true if <code>checked_result</code> contains an error
  188. condition.</entry>
  189. </row>
  190. <row>
  191. <entry><code><simplelist>
  192. <member>c1 &lt; c2</member>
  193. <member>c1 &gt;= c2</member>
  194. <member>c1 &gt; c2</member>
  195. <member>c1 &lt;= c2</member>
  196. <member>c1 == c2</member>
  197. <member>c1 != c2</member>
  198. </simplelist></code></entry>
  199. <entry><code>boost::logic::tribool</code></entry>
  200. <entry>compare the wrapped values of two checked_result
  201. instances. If the values are such that the result of such a
  202. comparison cannot be reasonably defined, The result of the
  203. comparison is
  204. <code>boost::logic::tribool::indeterminant</code>.</entry>
  205. </row>
  206. <row>
  207. <entry><code><simplelist>
  208. <member>c1 + c2</member>
  209. <member>c1 - c2</member>
  210. <member>c1 * c2</member>
  211. <member>c1 / c2</member>
  212. <member>c1 % c2</member>
  213. <member>c1 | c2</member>
  214. <member>c1 &amp; c2</member>
  215. <member>c1 ^ c2</member>
  216. <member>c1 &lt;&lt; c2</member>
  217. <member>c1 &gt;&gt; c2</member>
  218. </simplelist></code></entry>
  219. <entry><code>checked_result&lt;R&gt;</code></entry>
  220. <entry>returns a new instance of
  221. <code>checked_result&lt;R&gt;.</code></entry>
  222. </row>
  223. <row>
  224. <entry><code><simplelist>
  225. <member>os &lt;&lt; c</member>
  226. </simplelist></code></entry>
  227. <entry><code>OS</code></entry>
  228. <entry>writes result to output stream. If the result is an error
  229. it writes the string corresponding to the error message.
  230. Otherwise, it writes the numeric value resulting from the
  231. operation. Returns reference to output stream.</entry>
  232. </row>
  233. </tbody>
  234. </tgroup>
  235. </informaltable></para>
  236. </section>
  237. <section>
  238. <title>Example of use</title>
  239. <programlisting><xi:include href="../../example/example20.cpp"
  240. parse="text" xmlns:xi="http://www.w3.org/2001/XInclude"/></programlisting>
  241. </section>
  242. <section>
  243. <title>See Also</title>
  244. <para><link
  245. linkend="safe_numerics.exception_policy">ExceptionPolicy</link></para>
  246. </section>
  247. <section>
  248. <title>Header</title>
  249. <para><ulink
  250. url="../../include/boost/safe_numerics/checked_result.hpp"><code>#include
  251. &lt;boost/numeric/safe_numerics/checked_result.hpp&gt;</code></ulink></para>
  252. <para><ulink
  253. url="../../include/boost/safe_numerics/checked_result_operations.hpp"><code>#include
  254. &lt;boost/numeric/safe_numerics/checked_result_operations.hpp&gt;
  255. </code></ulink></para>
  256. </section>
  257. </section>