2
0
mirror of https://github.com/boostorg/outcome.git synced 2026-01-19 04:22:13 +00:00
Files
outcome/tutorial/payload.html
2019-02-06 17:05:13 +00:00

81 lines
4.9 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><h1 style="clear: both">Custom payloads</h1></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> or <code>boost::system::error_code</code>. With <code>NoValuePolicy</code>
set to <a href="../reference/aliases/default_policy.html" class="api-reference">default_policy&lt;T, EC, EP&gt;</a>, <code>EC</code> needs
in fact to merely satisfy the trait
<a href="../reference/traits/is_error_code_available.html" class="api-reference">is_error_code_available&lt;T&gt;</a>
for <code>EC</code> to be treated as if an <code>error_code</code>. Outcome specialises that
trait for <code>std::error_code</code> and <code>boost::system::error_code</code>,
hence they &ldquo;just work&rdquo;.</p>
<p>If no specialisation exists, <code>trait::is_error_code_available&lt;EC&gt;</code> is true
if there exists some ADL discovered free function <code>make_error_code(EC)</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: February 06, 2019 at 16:50:01 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>