2
0
mirror of https://github.com/boostorg/outcome.git synced 2026-02-18 14:22:08 +00:00
Files
outcome/tutorial/payload.html
2018-12-07 15:37:11 +00:00

88 lines
5.0 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Custom payloads - Boost.Outcome documentation</title>
<link rel="stylesheet" href="../css/boost.css" type="text/css">
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>
<link rel="icon" href="../images/favicon.ico" type="image/ico"/>
<body><div id="boost-common-heading-doc" style="background: #574D74 url('../images/header-bg.png') repeat-x top left;">
<div class="heading-inner" style="background: url('../images/header-fg.png') no-repeat top left;">
<div class="heading-placard"></div>
<h1 class="heading-title">
<a href="../">
<img src="../images/space.png" alt="Boost C++ Libraries" class="heading-logo" />
<span class="heading-boost">Boost</span>
<span class="heading-cpplibraries">C++ Libraries</span>
</a>
</h1>
<p class="heading-quote">
<q>...one of the most highly
regarded and expertly designed C++ library projects in the
world.</q> <span class="heading-attribution">&mdash; <a href=
"http://www.gotw.ca/" class="external">Herb Sutter</a> and <a href=
"http://en.wikipedia.org/wiki/Andrei_Alexandrescu" class="external">Andrei
Alexandrescu</a>, <a href=
"http://safari.awprofessional.com/?XmlId=0321113586" class="external">C++
Coding Standards</a></span></p>
</div>
</div>
<div id="boost-common-heading-doc-spacer"></div>
<div class="spirit-nav">
<a accesskey="p" href="../tutorial/no-value/builtin.html"><img src="../images/prev.png" alt="Prev"></a>
<a accesskey="u" href="../tutorial.html"><img src="../images/up.png" alt="Up"></a>
<a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="../tutorial/payload/copy_file.html"><img src="../images/next.png" alt="Next"></a></div><div id="content">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">Custom payloads</h2></div></div></div>
<p>So far in this tutorial, type <code>EC</code> in <code>result&lt;T, EC&gt;</code> has always been a
<code>std::error_code</code> (though it can be of any type you wish instead). <code>EC</code> needs
in fact to merely satisfy
<a href="reference/success_failure/#standardese-outcome_v2_xxx__trait__has_error_code-T-" class="api-reference"><i class="fa fa-book" aria-hidden="true"></i> trait::has_error_code_v&lt;EC&gt;</a>
for <code>EC</code> to be treated as if an <code>std::error_code</code>.</p>
<p>In turn, <code>trait::has_error_code_v&lt;EC&gt;</code> is true if there exists some ADL discovered free
function:</p>
<ol>
<li><code>make_error_code(EC)</code> returning a <code>std::error_code</code>.</li>
<li><code>make_error_code(get&lt;0&gt;(EC))</code> returning a <code>std::error_code</code> (i.e. EC might be
<code>pair&lt;std::error_code, U&gt;</code> or <code>tuple&lt;std::error_code, ...&gt;</code>).</li>
</ol>
<p>&hellip; or if <code>EC</code> is implicitly convertible into a <code>std::error_code</code>.</p>
<p>Thus, we can in fact use any custom <code>EC</code> type we like, including one carrying additional
information, or <em>payload</em>. This payload can carry anything you like, and you can tell
Outcome to do various things with that payload under various circumstances. For example:</p>
<ol>
<li>If the user tries to observe an unsuccessful <code>result</code>, throw a custom exception
containing the cause of failure with accompanying context from the payload.</li>
<li>If the user ever constructs an <code>outcome</code> from a payload carrying <code>result</code>,
set the exception ptr in the constructed <code>outcome</code> to a custom exception
containing the cause of the failure with accompanying context from the payload.</li>
<li>Transmit a stack backtrace specifying the exact point at which failure occurred,
symbolising that backtrace into human readable text at the moment of conversion into human
readable text.</li>
<li>Upon a namespace-localised <code>result</code> from library A being copy/moved into a
namespace-localised <code>result</code> from C bindings library B, set the C <code>errno</code> if
the error code and category map onto the <code>errno</code> domain.</li>
</ol>
<p>There are many, many other options of course. This tutorial can only cover a
reasonable subset. This section covers Example 1 above, throwing custom exceptions
with payload upon observation of an unsuccessful <code>result</code>.</p>
</div><p><small>Last revised: December 05, 2018 at 14:18:52 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../tutorial/no-value/builtin.html"><img src="../images/prev.png" alt="Prev"></a>
<a accesskey="u" href="../tutorial.html"><img src="../images/up.png" alt="Up"></a>
<a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="../tutorial/payload/copy_file.html"><img src="../images/next.png" alt="Next"></a></div></body>
</html>