payload.html 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  3. <title>Custom payloads - Boost.Outcome documentation</title>
  4. <link rel="stylesheet" href="../../css/boost.css" type="text/css">
  5. <meta name="generator" content="Hugo 0.52 with Boostdoc theme">
  6. <meta name="viewport" content="width=device-width,initial-scale=1.0"/>
  7. <link rel="icon" href="../../images/favicon.ico" type="image/ico"/>
  8. <body><div class="spirit-nav">
  9. <a accesskey="p" href="../../tutorial/advanced.html"><img src="../../images/prev.png" alt="Prev"></a>
  10. <a accesskey="u" href="../../tutorial/advanced.html"><img src="../../images/up.png" alt="Up"></a>
  11. <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../tutorial/advanced/payload/copy_file.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content">
  12. <div class="titlepage"><div><div><h1 style="clear: both">Custom payloads</h1></div></div></div>
  13. <p>So far in this tutorial, type <code>EC</code> in <code>result&lt;T, EC&gt;</code> has always been a
  14. <code>std::error_code</code> or <code>boost::system::error_code</code>. With <code>NoValuePolicy</code>
  15. set to <a href="../../reference/aliases/default_policy.html" class="api-reference"><code>default_policy&lt;T, EC, EP&gt;</code></a>
  16. , <code>EC</code> needs
  17. in fact to merely satisfy the trait
  18. <a href="../../reference/traits/is_error_code_available.html" class="api-reference"><code>is_error_code_available&lt;T&gt;</code></a>
  19. for <code>EC</code> to be treated as if an <code>error_code</code>. Outcome specialises that
  20. trait for <code>std::error_code</code> and <code>boost::system::error_code</code>,
  21. hence they &ldquo;just work&rdquo;.</p>
  22. <p>If no specialisation exists, <code>trait::is_error_code_available&lt;EC&gt;</code> is true
  23. if there exists some ADL discovered free function <code>make_error_code(EC)</code>.</p>
  24. <p>Thus, we can in fact use any custom <code>EC</code> type we like, including one carrying additional
  25. information, or <em>payload</em>. This payload can carry anything you like, and you can tell
  26. Outcome to do various things with that payload under various circumstances. For example:</p>
  27. <ol>
  28. <li>If the user tries to observe an unsuccessful <code>result</code>, throw a custom exception
  29. containing the cause of failure with accompanying context from the payload.</li>
  30. <li>If the user ever constructs an <code>outcome</code> from a payload carrying <code>result</code>,
  31. set the exception ptr in the constructed <code>outcome</code> to a custom exception
  32. containing the cause of the failure with accompanying context from the payload.</li>
  33. <li>Transmit a stack backtrace specifying the exact point at which failure occurred,
  34. symbolising that backtrace into human readable text at the moment of conversion into human
  35. readable text.</li>
  36. <li>Upon a namespace-localised <code>result</code> from library A being copy/moved into a
  37. namespace-localised <code>result</code> from C bindings library B, set the C <code>errno</code> if
  38. the error code and category map onto the <code>errno</code> domain.</li>
  39. </ol>
  40. <p>There are many, many other options of course. This tutorial can only cover a
  41. reasonable subset. This section covers Example 1 above, throwing custom exceptions
  42. with payload upon observation of an unsuccessful <code>result</code>.</p>
  43. </div><p><small>Last revised: February 08, 2019 at 22:18:08 UTC</small></p>
  44. <hr>
  45. <div class="spirit-nav">
  46. <a accesskey="p" href="../../tutorial/advanced.html"><img src="../../images/prev.png" alt="Prev"></a>
  47. <a accesskey="u" href="../../tutorial/advanced.html"><img src="../../images/up.png" alt="Up"></a>
  48. <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../tutorial/advanced/payload/copy_file.html"><img src="../../images/next.png" alt="Next"></a></div></body>
  49. </html>