• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 HTTP library into the Application &lt;sup&gt;2&lt;/sup&gt;&frasl;&lt;sub&gt;2&lt;/sub&gt; - 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-httplib1.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-filelib.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 HTTP library into the Application <code>2/2</code></h1></div></div></div>
14  <p>If you remember the tutorial section on the <a href="../value-or-error"><code>ValueOrError</code> Concept</a>,
15this is an example of how to implement a custom <code>ValueOrError</code> Concept converter
16in Outcome:</p>
17
18<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="c1">// Inject custom ValueOrError conversion
19</span><span class="c1"></span><span class="n">BOOST_OUTCOME_V2_NAMESPACE_BEGIN</span>
20<span class="k">namespace</span> <span class="n">convert</span>
21<span class="p">{</span>
22  <span class="c1">// Provide custom ValueOrError conversion from
23</span><span class="c1"></span>  <span class="c1">// httplib::result&lt;U&gt; into any app::outcome&lt;T&gt;
24</span><span class="c1"></span>  <span class="k">template</span> <span class="o">&lt;</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">&gt;</span>  <span class="c1">//
25</span><span class="c1"></span>  <span class="k">struct</span> <span class="n">value_or_error</span><span class="o">&lt;</span><span class="n">app</span><span class="o">::</span><span class="n">outcome</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">,</span> <span class="n">httplib</span><span class="o">::</span><span class="n">result</span><span class="o">&lt;</span><span class="n">U</span><span class="o">&gt;&gt;</span>
26  <span class="p">{</span>
27    <span class="c1">// False to indicate that this converter wants `result`/`outcome`
28</span><span class="c1"></span>    <span class="c1">// to NOT reject all other `result`
29</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>
30    <span class="c1">// False to indicate that this converter wants `outcome` to NOT
31</span><span class="c1"></span>    <span class="c1">// reject all other `outcome`
32</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>
33
34    <span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span><span class="err"> </span><span class="nc">X</span><span class="p">,</span>                                                                              <span class="c1">//
35</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">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">is_same</span><span class="o">&lt;</span><span class="n">httplib</span><span class="o">::</span><span class="n">result</span><span class="o">&lt;</span><span class="n">U</span><span class="o">&gt;</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">decay_t</span><span class="o">&lt;</span><span class="n">X</span><span class="o">&gt;&gt;::</span><span class="n">value</span>  <span class="c1">//
36</span><span class="c1"></span>                                          <span class="o">&amp;&amp;</span> <span class="n">std</span><span class="o">::</span><span class="n">is_constructible</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span> <span class="n">U</span><span class="o">&gt;::</span><span class="n">value</span><span class="o">&gt;&gt;</span>                   <span class="c1">//
37</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">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="k">operator</span><span class="p">()(</span><span class="n">X</span> <span class="o">&amp;&amp;</span><span class="n">src</span><span class="p">)</span>
38    <span class="p">{</span>
39      <span class="c1">// Forward any successful value, else synthesise an exception ptr
40</span><span class="c1"></span>      <span class="k">return</span> <span class="n">src</span><span class="p">.</span><span class="n">has_value</span><span class="p">()</span> <span class="o">?</span>                              <span class="c1">//
41</span><span class="c1"></span>             <span class="n">app</span><span class="o">::</span><span class="n">outcome</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">{</span><span class="n">std</span><span class="o">::</span><span class="n">forward</span><span class="o">&lt;</span><span class="n">X</span><span class="o">&gt;</span><span class="p">(</span><span class="n">src</span><span class="p">).</span><span class="n">value</span><span class="p">()}</span>  <span class="c1">//
42</span><span class="c1"></span>             <span class="o">:</span>
43             <span class="n">app</span><span class="o">::</span><span class="n">outcome</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">{</span><span class="n">app</span><span class="o">::</span><span class="n">make_httplib_exception</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">forward</span><span class="o">&lt;</span><span class="n">X</span><span class="o">&gt;</span><span class="p">(</span><span class="n">src</span><span class="p">))};</span>
44    <span class="p">}</span>
45  <span class="p">};</span>
46<span class="p">}</span>  <span class="c1">// namespace convert
47</span><span class="c1"></span><span class="n">BOOST_OUTCOME_V2_NAMESPACE_END</span>
48</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/finale.cpp#L257" class="code-snippet-url" target="_blank">View this code on Github</a></div>
49
50
51<p>The first thing that you should note is that these custom converters must be injected
52directly into the <code>BOOST_OUTCOME_V2_NAMESPACE::convert</code> namespace, and they must partially
53or completely specialise <a href="../../../reference/converters/value_or_error.html" class="api-reference"><code>value_or_error&lt;T, U&gt;</code></a>
54. Here we specialise the
55converter for <code>ValueOrError</code> conversions from <code>httplib::result&lt;U&gt;</code> to <code>app::outcome&lt;T&gt;</code>
56i.e. from our third party HTTP library&rsquo;s error type into our application&rsquo;s <code>outcome</code>
57type (which is unique to our application, as we hard code an <code>app</code>-local error type).</p>
58
59<p>The second thing to note is that you need to set <code>enable_result_inputs</code> and <code>enable_outcome_inputs</code>
60appropriately, otherwise <code>result</code> and <code>outcome</code> inputs will not be matched by this
61converter<sup class="footnote-ref" id="fnref:1"><a href="#fn:1">1</a></sup>. In this converter, we really do wish to convert other <code>result</code> and
62<code>outcome</code> inputs, so we mark these booleans as <code>true</code>.</p>
63
64<p>The third thing to note is the requirements on <code>operator()</code>. If the requirements are
65not met, the <code>ValueOrError</code> converting constructor in <code>basic_result</code> and <code>basic_outcome</code>
66disables. Note the requirement that the decayed <code>operator()</code> input <code>X</code> matches
67<code>httplib::result&lt;U&gt;</code>, and that <code>T</code> is constructible from <code>U</code>. This means that the
68<a href="../../../reference/types/basic_result/explicit_valueorerror_converting_constructor.html" class="api-reference"><code>explicit basic_result(ValueOrError&lt;T, E&gt; &amp;&amp;)</code></a>
69 and <a href="../../../reference/types/basic_outcome/explicit_valueorerror_converting_constructor.html" class="api-reference"><code>explicit basic_outcome(ValueOrError&lt;T, E&gt; &amp;&amp;)</code></a>
70
71constructors are available if, and only if, the input type is a <code>httplib::result&lt;U&gt;</code>,
72and the result&rsquo;s value type is constructible from the input&rsquo;s value type.</p>
73
74<p>If <code>operator()</code> is available, it naturally converts a <code>httplib::result&lt;U&gt;</code> into an
75<code>app::outcome&lt;T&gt;</code> by either forwarding any success as-is, or calling <code>app::make_httplib_exception()</code>
76to type erase the <code>httplib::failure</code> into an <code>app::httplib_error</code>.</p>
77<div class="footnotes">
78
79<hr />
80
81<ol>
82<li id="fn:1">Here we refer to <code>result</code> and <code>outcome</code> as defined by this specific Outcome library. If <code>result</code> or <code>outcome</code> from another Outcome implementation is seen, those <strong>always</strong> must get parsed via the <code>ValueOrError</code> matching conversion framework.
83 <a class="footnote-return" href="#fnref:1"><sup>[return]</sup></a></li>
84</ol>
85</div>
86
87
88        </div><p><small>Last revised: February 11, 2019 at 13:38:04 UTC</small></p>
89<hr>
90<div class="spirit-nav">
91<a accesskey="p" href="../../../tutorial/advanced/interop/app-map-httplib1.html"><img src="../../../images/prev.png" alt="Prev"></a>
92    <a accesskey="u" href="../../../tutorial/advanced/interop.html"><img src="../../../images/up.png" alt="Up"></a>
93    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/interop/app-map-filelib.html"><img src="../../../images/next.png" alt="Next"></a></div></body>
94</html>
95