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 8<link rel="icon" href="../../../images/favicon.ico" type="image/ico"/> 9<body><div class="spirit-nav"> 10<a accesskey="p" href="../../../tutorial/advanced/hooks/adl_bridging.html"><img src="../../../images/prev.png" alt="Prev"></a> 11 <a accesskey="u" href="../../../tutorial/advanced/hooks.html"><img src="../../../images/up.png" alt="Up"></a> 12 <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"> 13 <div class="titlepage"><div><div><h1 style="clear: both">Hook result</h1></div></div></div> 14 <p>We now tell Outcome that for every instance of our localised <code>result<T></code>, that 15on failure construction only, we want custom code to be run which increments the current 16slot in TLS storage and writes the current stack backtrace into it.</p> 17 18<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> 19<span class="p">{</span> 20 <span class="c1">// Specialise the result construction hook for our localised result 21</span><span class="c1"></span> <span class="c1">// We hook any non-copy, non-move, non-inplace construction, capturing a stack backtrace 22</span><span class="c1"></span> <span class="c1">// if the result is errored. 23</span><span class="c1"></span> <span class="k">template</span> <span class="o"><</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">></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"><</span><span class="n">T</span><span class="o">></span> <span class="o">*</span><span class="n">res</span><span class="p">,</span> <span class="n">U</span> <span class="o">&&</span> <span class="cm">/*unused*/</span><span class="p">)</span> <span class="k">noexcept</span> 24 <span class="p">{</span> 25 <span class="k">if</span><span class="p">(</span><span class="n">res</span><span class="o">-></span><span class="n">has_error</span><span class="p">())</span> 26 <span class="p">{</span> 27 <span class="c1">// Grab the next extended info slot in the TLS 28</span><span class="c1"></span> <span class="n">extended_error_info</span> <span class="o">&</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> 29 30 <span class="c1">// Write the index just grabbed into the spare uint16_t 31</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> 32 33 <span class="c1">// Capture a backtrace into my claimed extended info slot in the TLS 34</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> 35 <span class="p">}</span> 36 <span class="p">}</span> 37<span class="p">}</span> 38</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> 39 40 41<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> 42.</p> 43 44<p>Both <code>result</code> and <code>outcome</code> keep their internal state metadata in a <code>uint32_t</code>, 45half of which is not used by Outcome. As it can be very useful to keep a small 46unique number attached to any particular <code>result</code> or <code>outcome</code> instance, we 47permit user code to set those sixteen bits to anything they feel like. 48The 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> 49.</p> 50 51<p>The state of the sixteen bits of spare storage are ignored during comparison operations.</p> 52 53<p>The sixteen bits of spare storage propagate during the following operations:</p> 54 55<ol> 56<li>Copy and move construction between <code>result</code>’s.</li> 57<li>Copy and move construction between <code>outcome</code>’s.</li> 58<li>Copy and move construction from a <code>result</code> to an <code>outcome</code>.</li> 59<li>Converting copy and move constructions for all the above.</li> 60<li>Assignment for all of the above.</li> 61</ol> 62 63<p>They are NOT propagated in these operations:</p> 64 65<ol> 66<li>Any conversion or translation which goes through a <code>failure_type</code> or <code>success_type</code>.</li> 67<li>Any conversion or translation which goes through a <code>ValueOrError</code> concept match.</li> 68<li>Any unpacking or repacking of value/error/exception e.g. a manual repack of an 69<code>outcome</code> into a <code>result</code>.</li> 70</ol> 71 72 73 </div><p><small>Last revised: February 08, 2019 at 22:18:08 UTC</small></p> 74<hr> 75<div class="spirit-nav"> 76<a accesskey="p" href="../../../tutorial/advanced/hooks/adl_bridging.html"><img src="../../../images/prev.png" alt="Prev"></a> 77 <a accesskey="u" href="../../../tutorial/advanced/hooks.html"><img src="../../../images/up.png" alt="Up"></a> 78 <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> 79</html> 80