copy_file.html 4.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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>The Filesystem TS - 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/payload.html"><img src="../../../images/prev.png" alt="Prev"></a>
  10. <a accesskey="u" href="../../../tutorial/advanced/payload.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/payload/copy_file2.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content">
  12. <div class="titlepage"><div><div><h1 style="clear: both">The Filesystem TS</h1></div></div></div>
  13. <p>Something which has long annoyed the purists in the C++ leadership is the problem of dual
  14. overloads in <code>error_code</code> capable standard library APIs.</p>
  15. <p>Consider the
  16. <a href="http://en.cppreference.com/w/cpp/filesystem/copy_file"><code>copy_file()</code></a>
  17. API from the Filesystem TS:</p>
  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">filesystem</span>
  19. <span class="p">{</span>
  20. <span class="cm">/*! Copies the file at path `from` to path `to`.
  21. </span><span class="cm"> \returns True if file was successfully copied.
  22. </span><span class="cm"> \throws On failure throws `filesystem_error(ec.message(), from, to, ec)` with
  23. </span><span class="cm"> `ec` being the error code reported by the operating system.
  24. </span><span class="cm"> */</span>
  25. <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>
  26. <span class="cm">/*! Copies the file at path `from` to path `to`.
  27. </span><span class="cm"> \returns True if file was successfully copied. If false, `ec` is written with
  28. </span><span class="cm"> the error code reported by the operating system.
  29. </span><span class="cm"> \throws May throw an exception if there is some &#34;catastrophic&#34; failure
  30. </span><span class="cm"> e.g. failure to allocate memory.
  31. </span><span class="cm"> */</span>
  32. <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>
  33. <span class="p">}</span>
  34. </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>
  35. <p>Before Outcome, the common design pattern was to provide throwing and non-throwing overloads
  36. 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>
  37. exception type which carries additional information, specifically two paths. These paths may
  38. refer to the files which were the source of any failure. However the non-throwing overload
  39. does <strong>not</strong> provide this additional information, which can make it more annoying to use the
  40. non-throwing overload sometimes.</p>
  41. <p>What if we could replace these two overloads of every API in the Filesystem TS with a single API,
  42. and additionally have the non-throwing edition return the exact same additional information
  43. as the throwing edition?</p>
  44. </div><p><small>Last revised: February 08, 2019 at 22:18:08 UTC</small></p>
  45. <hr>
  46. <div class="spirit-nav">
  47. <a accesskey="p" href="../../../tutorial/advanced/payload.html"><img src="../../../images/prev.png" alt="Prev"></a>
  48. <a accesskey="u" href="../../../tutorial/advanced/payload.html"><img src="../../../images/up.png" alt="Up"></a>
  49. <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>
  50. </html>