hook_result.html 6.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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>Hook result - 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/hooks/adl_bridging.html"><img src="../../../images/prev.png" alt="Prev"></a>
  10. <a accesskey="u" href="../../../tutorial/advanced/hooks.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/hooks/poke_exception.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content">
  12. <div class="titlepage"><div><div><h1 style="clear: both">Hook result</h1></div></div></div>
  13. <p>We now tell Outcome that for every instance of our localised <code>result&lt;T&gt;</code>, that
  14. on failure construction only, we want custom code to be run which increments the current
  15. slot in TLS storage and writes the current stack backtrace into it.</p>
  16. <div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">namespace</span> <span class="n">error_code_extended</span>
  17. <span class="p">{</span>
  18. <span class="c1">// Specialise the result construction hook for our localised result
  19. </span><span class="c1"></span> <span class="c1">// We hook any non-copy, non-move, non-inplace construction, capturing a stack backtrace
  20. </span><span class="c1"></span> <span class="c1">// if the result is errored.
  21. </span><span class="c1"></span> <span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span><span class="err"> </span><span class="nc">T</span><span class="p">,</span> <span class="k">class</span><span class="err"> </span><span class="nc">U</span><span class="o">&gt;</span> <span class="kr">inline</span> <span class="kt">void</span> <span class="n">hook_result_construction</span><span class="p">(</span><span class="n">result</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="o">*</span><span class="n">res</span><span class="p">,</span> <span class="n">U</span> <span class="o">&amp;&amp;</span> <span class="cm">/*unused*/</span><span class="p">)</span> <span class="k">noexcept</span>
  22. <span class="p">{</span>
  23. <span class="k">if</span><span class="p">(</span><span class="n">res</span><span class="o">-&gt;</span><span class="n">has_error</span><span class="p">())</span>
  24. <span class="p">{</span>
  25. <span class="c1">// Grab the next extended info slot in the TLS
  26. </span><span class="c1"></span> <span class="n">extended_error_info</span> <span class="o">&amp;</span><span class="n">eei</span> <span class="o">=</span> <span class="n">mythreadlocaldata</span><span class="p">().</span><span class="n">next</span><span class="p">();</span>
  27. <span class="c1">// Write the index just grabbed into the spare uint16_t
  28. </span><span class="c1"></span> <span class="n">BOOST_OUTCOME_V2_NAMESPACE</span><span class="o">::</span><span class="n">hooks</span><span class="o">::</span><span class="n">set_spare_storage</span><span class="p">(</span><span class="n">res</span><span class="p">,</span> <span class="n">mythreadlocaldata</span><span class="p">().</span><span class="n">current</span> <span class="o">-</span> <span class="mi">1</span><span class="p">);</span>
  29. <span class="c1">// Capture a backtrace into my claimed extended info slot in the TLS
  30. </span><span class="c1"></span> <span class="n">eei</span><span class="p">.</span><span class="n">items</span> <span class="o">=</span> <span class="o">::</span><span class="n">backtrace</span><span class="p">(</span><span class="n">eei</span><span class="p">.</span><span class="n">backtrace</span><span class="p">.</span><span class="n">data</span><span class="p">(),</span> <span class="n">eei</span><span class="p">.</span><span class="n">backtrace</span><span class="p">.</span><span class="n">size</span><span class="p">());</span>
  31. <span class="p">}</span>
  32. <span class="p">}</span>
  33. <span class="p">}</span>
  34. </code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/error_code_extended.cpp#L119" class="code-snippet-url" target="_blank">View this code on Github</a></div>
  35. <p>The only non-obvious part above is the call to <a href="../../../reference/functions/hooks/set_spare_storage.html" class="api-reference"><code>void set_spare_storage(basic_result|basic_outcome *, uint16_t) noexcept</code></a>
  36. .</p>
  37. <p>Both <code>result</code> and <code>outcome</code> keep their internal state metadata in a <code>uint32_t</code>,
  38. half of which is not used by Outcome. As it can be very useful to keep a small
  39. unique number attached to any particular <code>result</code> or <code>outcome</code> instance, we
  40. permit user code to set those sixteen bits to anything they feel like.
  41. The corresponding function to retrieve those sixteen bits is <a href="../../../reference/functions/hooks/spare_storage.html" class="api-reference"><code>uint16_t spare_storage(const basic_result|basic_outcome *) noexcept</code></a>
  42. .</p>
  43. <p>The state of the sixteen bits of spare storage are ignored during comparison operations.</p>
  44. <p>The sixteen bits of spare storage propagate during the following operations:</p>
  45. <ol>
  46. <li>Copy and move construction between <code>result</code>&rsquo;s.</li>
  47. <li>Copy and move construction between <code>outcome</code>&rsquo;s.</li>
  48. <li>Copy and move construction from a <code>result</code> to an <code>outcome</code>.</li>
  49. <li>Converting copy and move constructions for all the above.</li>
  50. <li>Assignment for all of the above.</li>
  51. </ol>
  52. <p>They are NOT propagated in these operations:</p>
  53. <ol>
  54. <li>Any conversion or translation which goes through a <code>failure_type</code> or <code>success_type</code>.</li>
  55. <li>Any conversion or translation which goes through a <code>ValueOrError</code> concept match.</li>
  56. <li>Any unpacking or repacking of value/error/exception e.g. a manual repack of an
  57. <code>outcome</code> into a <code>result</code>.</li>
  58. </ol>
  59. </div><p><small>Last revised: February 08, 2019 at 22:18:08 UTC</small></p>
  60. <hr>
  61. <div class="spirit-nav">
  62. <a accesskey="p" href="../../../tutorial/advanced/hooks/adl_bridging.html"><img src="../../../images/prev.png" alt="Prev"></a>
  63. <a accesskey="u" href="../../../tutorial/advanced/hooks.html"><img src="../../../images/up.png" alt="Up"></a>
  64. <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/hooks/poke_exception.html"><img src="../../../images/next.png" alt="Next"></a></div></body>
  65. </html>