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>Mapping the File I/O library into the Application - 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/interop/app-map-httplib2.html"><img src="../../../images/prev.png" alt="Prev"></a> 11 <a accesskey="u" href="../../../tutorial/advanced/interop.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/interop/app-map-tidylib.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content"> 13 <div class="titlepage"><div><div><h1 style="clear: both">Mapping the File I/O library into the Application</h1></div></div></div> 14 <p>To handle the File I/O library, once again we turn to custom <code>ValueOrError</code> 15converters:</p> 16 17<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="c1">// Inject custom ValueOrError conversion 18</span><span class="c1"></span><span class="n">BOOST_OUTCOME_V2_NAMESPACE_BEGIN</span> 19<span class="k">namespace</span> <span class="n">convert</span> 20<span class="p">{</span> 21 <span class="c1">// Provide custom ValueOrError conversion from filelib::result<U> 22</span><span class="c1"></span> <span class="c1">// into any app::outcome<T> 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="c1">// 24</span><span class="c1"></span> <span class="k">struct</span> <span class="n">value_or_error</span><span class="o"><</span><span class="n">app</span><span class="o">::</span><span class="n">outcome</span><span class="o"><</span><span class="n">T</span><span class="o">></span><span class="p">,</span> <span class="n">filelib</span><span class="o">::</span><span class="n">result</span><span class="o"><</span><span class="n">U</span><span class="o">>></span> 25 <span class="p">{</span> 26 <span class="c1">// True to indicate that this converter wants `result`/`outcome` 27</span><span class="c1"></span> <span class="c1">// to NOT reject all other `result` 28</span><span class="c1"></span> <span class="k">static</span> <span class="k">constexpr</span> <span class="kt">bool</span> <span class="n">enable_result_inputs</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span> 29 <span class="c1">// False to indicate that this converter wants `outcome` to NOT 30</span><span class="c1"></span> <span class="c1">// reject all other `outcome` 31</span><span class="c1"></span> <span class="k">static</span> <span class="k">constexpr</span> <span class="kt">bool</span> <span class="n">enable_outcome_inputs</span> <span class="o">=</span> <span class="nb">true</span><span class="p">;</span> 32 33 <span class="k">template</span> <span class="o"><</span><span class="k">class</span><span class="err"> </span><span class="nc">X</span><span class="p">,</span> <span class="c1">// 34</span><span class="c1"></span> <span class="k">typename</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">enable_if_t</span><span class="o"><</span><span class="n">std</span><span class="o">::</span><span class="n">is_same</span><span class="o"><</span><span class="n">filelib</span><span class="o">::</span><span class="n">result</span><span class="o"><</span><span class="n">U</span><span class="o">></span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">decay_t</span><span class="o"><</span><span class="n">X</span><span class="o">>>::</span><span class="n">value</span> <span class="c1">// 35</span><span class="c1"></span> <span class="o">&&</span> <span class="n">std</span><span class="o">::</span><span class="n">is_constructible</span><span class="o"><</span><span class="n">T</span><span class="p">,</span> <span class="n">U</span><span class="o">>::</span><span class="n">value</span><span class="o">>></span> <span class="c1">// 36</span><span class="c1"></span> <span class="k">constexpr</span> <span class="n">app</span><span class="o">::</span><span class="n">outcome</span><span class="o"><</span><span class="n">T</span><span class="o">></span> <span class="k">operator</span><span class="p">()(</span><span class="n">X</span> <span class="o">&&</span><span class="n">src</span><span class="p">)</span> 37 <span class="p">{</span> 38 <span class="c1">// Forward any successful value 39</span><span class="c1"></span> <span class="k">if</span><span class="p">(</span><span class="n">src</span><span class="p">.</span><span class="n">has_value</span><span class="p">())</span> 40 <span class="p">{</span> 41 <span class="k">return</span> <span class="p">{</span><span class="n">std</span><span class="o">::</span><span class="n">forward</span><span class="o"><</span><span class="n">X</span><span class="o">></span><span class="p">(</span><span class="n">src</span><span class="p">).</span><span class="n">value</span><span class="p">()};</span> 42 <span class="p">}</span> 43 44 <span class="c1">// Synthesise a filesystem_error, exactly as if someone had 45</span><span class="c1"></span> <span class="c1">// called src.value() 46</span><span class="c1"></span> <span class="k">auto</span> <span class="o">&</span><span class="n">fi</span> <span class="o">=</span> <span class="n">src</span><span class="p">.</span><span class="n">error</span><span class="p">();</span> 47 <span class="n">BOOST_OUTCOME_V2_NAMESPACE</span><span class="o">::</span><span class="n">try_throw_std_exception_from_error</span><span class="p">(</span><span class="n">fi</span><span class="p">.</span><span class="n">ec</span><span class="p">);</span> <span class="c1">// might throw 48</span><span class="c1"></span> <span class="k">return</span> <span class="p">{</span><span class="n">std</span><span class="o">::</span><span class="n">make_exception_ptr</span><span class="p">(</span> <span class="c1">// 49</span><span class="c1"></span> <span class="n">filelib</span><span class="o">::</span><span class="n">filesystem_error</span><span class="p">(</span><span class="n">fi</span><span class="p">.</span><span class="n">ec</span><span class="p">.</span><span class="n">message</span><span class="p">(),</span> <span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">fi</span><span class="p">.</span><span class="n">path1</span><span class="p">),</span> <span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">fi</span><span class="p">.</span><span class="n">path2</span><span class="p">),</span> <span class="n">fi</span><span class="p">.</span><span class="n">ec</span><span class="p">))};</span> 50 <span class="p">}</span> 51 <span class="p">};</span> 52<span class="p">}</span> <span class="c1">// namespace convert 53</span><span class="c1"></span><span class="n">BOOST_OUTCOME_V2_NAMESPACE_END</span> 54</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/finale.cpp#L296" class="code-snippet-url" target="_blank">View this code on Github</a></div> 55 56 57<p>Note that the conversion exactly duplicates the implementation of 58<code>throw_as_system_error_with_payload(failure_info fi)</code> from 59namespace <code>filelib</code>. In a production implementation, you probably 60ought to call that function and catch the exception it throws 61into a pointer, as that would be more long term maintainable.</p> 62 63 64 </div><p><small>Last revised: February 11, 2019 at 13:38:04 UTC</small></p> 65<hr> 66<div class="spirit-nav"> 67<a accesskey="p" href="../../../tutorial/advanced/interop/app-map-httplib2.html"><img src="../../../images/prev.png" alt="Prev"></a> 68 <a accesskey="u" href="../../../tutorial/advanced/interop.html"><img src="../../../images/up.png" alt="Up"></a> 69 <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/interop/app-map-tidylib.html"><img src="../../../images/next.png" alt="Next"></a></div></body> 70</html> 71