outcome/doc/html/tutorial/advanced/payload/copy_file.html
2019-12-01 13:32:32 +00:00

60 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>The Filesystem TS - 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 class="spirit-nav">
<a accesskey="p" href="../../../tutorial/advanced/payload.html"><img src="../../../images/prev.png" alt="Prev"></a>
<a accesskey="u" href="../../../tutorial/advanced/payload.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/advanced/payload/copy_file2.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content">
<div class="titlepage"><div><div><h1 style="clear: both">The Filesystem TS</h1></div></div></div>
<p>Something which has long annoyed the purists in the C++ leadership is the problem of dual
overloads in <code>error_code</code> capable standard library APIs.</p>
<p>Consider the
<a href="http://en.cppreference.com/w/cpp/filesystem/copy_file"><code>copy_file()</code></a>
API from the Filesystem TS:</p>
<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">filesystem</span>
<span class="p">{</span>
<span class="cm">/*! Copies the file at path `from` to path `to`.
</span><span class="cm"> \returns True if file was successfully copied.
</span><span class="cm"> \throws On failure throws `filesystem_error(ec.message(), from, to, ec)` with
</span><span class="cm"> `ec` being the error code reported by the operating system.
</span><span class="cm"> */</span>
<span class="kt">bool</span> <span class="n">copy_file</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span> <span class="o">&amp;</span><span class="n">from</span><span class="p">,</span> <span class="k">const</span> <span class="n">path</span> <span class="o">&amp;</span><span class="n">to</span><span class="p">);</span>
<span class="cm">/*! Copies the file at path `from` to path `to`.
</span><span class="cm"> \returns True if file was successfully copied. If false, `ec` is written with
</span><span class="cm"> the error code reported by the operating system.
</span><span class="cm"> \throws May throw an exception if there is some &#34;catastrophic&#34; failure
</span><span class="cm"> e.g. failure to allocate memory.
</span><span class="cm"> */</span>
<span class="kt">bool</span> <span class="nf">copy_file</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span> <span class="o">&amp;</span><span class="n">from</span><span class="p">,</span> <span class="k">const</span> <span class="n">path</span> <span class="o">&amp;</span><span class="n">to</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">error_code</span> <span class="o">&amp;</span><span class="n">ec</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/outcome_payload.cpp#L49" class="code-snippet-url" target="_blank">View this code on Github</a></div>
<p>Before Outcome, the common design pattern was to provide throwing and non-throwing overloads
of every API. As you can see above, the throwing API throws a <a href="http://en.cppreference.com/w/cpp/filesystem/filesystem_error"><code>filesystem::filesystem_error</code></a>
exception type which carries additional information, specifically two paths. These paths may
refer to the files which were the source of any failure. However the non-throwing overload
does <strong>not</strong> provide this additional information, which can make it more annoying to use the
non-throwing overload sometimes.</p>
<p>What if we could replace these two overloads of every API in the Filesystem TS with a single API,
and additionally have the non-throwing edition return the exact same additional information
as the throwing edition?</p>
</div><p><small>Last revised: February 08, 2019 at 22:18:08 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/advanced/payload.html"><img src="../../../images/prev.png" alt="Prev"></a>
<a accesskey="u" href="../../../tutorial/advanced/payload.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/advanced/payload/copy_file2.html"><img src="../../../images/next.png" alt="Next"></a></div></body>
</html>