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>Redefining message() - 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="../../experimental/worked-example/string_ref.html"><img src="../../images/prev.png" alt="Prev"></a> 11 <a accesskey="u" href="../../experimental/worked-example.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="../../experimental/worked-example/source.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content"> 13 <div class="titlepage"><div><div><h1 style="clear: both">Redefining <code>message()</code></h1></div></div></div> 14 <p>You may remember that our custom <code>_file_io_error_domain</code> inherits from 15<code>outcome_e::posix_code::domain_type</code>, and thus does not have to 16implement the many pure virtual functions required by <code>outcome_e::status_code_domain</code>.</p> 17 18<p>What we do need to do is reimplement <code>_do_message()</code> to append the 19file and line information to the POSIX error description string 20returned by <code>outcome_e::posix_code::domain_type</code>. This causes 21the status code’s <code>.message()</code> observer to return a string 22with the extra payload information represented in text.</p> 23 24<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"> <span class="c1">// Return a string describing a specific code. We will return the 25</span><span class="c1"></span> <span class="c1">// string returned by our POSIX code base domain, with the source 26</span><span class="c1"></span> <span class="c1">// file and line number appended 27</span><span class="c1"></span> <span class="k">virtual</span> <span class="n">_base</span><span class="o">::</span><span class="n">string_ref</span> <span class="n">_do_message</span><span class="p">(</span><span class="k">const</span> <span class="n">outcome_e</span><span class="o">::</span><span class="n">status_code</span><span class="o"><</span><span class="kt">void</span><span class="o">></span> <span class="o">&</span><span class="n">code</span><span class="p">)</span> <span class="k">const</span> <span class="k">noexcept</span> <span class="k">override</span> <span class="k">final</span> <span class="c1">// NOLINT 28</span><span class="c1"></span> <span class="p">{</span> 29 <span class="n">assert</span><span class="p">(</span><span class="n">code</span><span class="p">.</span><span class="n">domain</span><span class="p">()</span> <span class="o">==</span> <span class="o">*</span><span class="k">this</span><span class="p">);</span> 30 31 <span class="c1">// Fetch message from base domain (POSIX) 32</span><span class="c1"></span> <span class="k">auto</span> <span class="n">msg</span> <span class="o">=</span> <span class="n">_base</span><span class="o">::</span><span class="n">_do_message</span><span class="p">(</span><span class="n">code</span><span class="p">);</span> 33 <span class="k">const</span> <span class="k">auto</span> <span class="o">&</span><span class="n">c1</span> <span class="o">=</span> <span class="k">static_cast</span><span class="o"><</span><span class="k">const</span> <span class="n">file_io_error</span> <span class="o">&></span><span class="p">(</span><span class="n">code</span><span class="p">);</span> <span class="c1">// NOLINT 34</span><span class="c1"></span> <span class="k">const</span> <span class="n">value_type</span> <span class="o">&</span><span class="n">v</span> <span class="o">=</span> <span class="n">c1</span><span class="p">.</span><span class="n">value</span><span class="p">();</span> 35 36 <span class="c1">// Append my source file and line number 37</span><span class="c1"></span> <span class="k">if</span><span class="p">(</span><span class="n">v</span><span class="p">.</span><span class="n">file</span> <span class="o">==</span> <span class="k">nullptr</span><span class="p">)</span> 38 <span class="p">{</span> 39 <span class="k">return</span> <span class="n">msg</span><span class="p">;</span> 40 <span class="p">}</span> 41 <span class="n">size_t</span> <span class="n">length</span> <span class="o">=</span> <span class="n">strlen</span><span class="p">(</span><span class="n">v</span><span class="p">.</span><span class="n">file</span><span class="p">)</span> <span class="o">+</span> <span class="mi">16</span> <span class="o">+</span> <span class="n">msg</span><span class="p">.</span><span class="n">size</span><span class="p">();</span> 42 <span class="k">auto</span> <span class="o">*</span><span class="n">p</span> <span class="o">=</span> <span class="k">static_cast</span><span class="o"><</span><span class="kt">char</span> <span class="o">*></span><span class="p">(</span><span class="n">malloc</span><span class="p">(</span><span class="n">length</span><span class="p">));</span> <span class="c1">// NOLINT 43</span><span class="c1"></span> <span class="k">if</span><span class="p">(</span><span class="n">p</span> <span class="o">==</span> <span class="k">nullptr</span><span class="p">)</span> 44 <span class="p">{</span> 45 <span class="k">return</span> <span class="n">_base</span><span class="o">::</span><span class="n">string_ref</span><span class="p">(</span><span class="s">"failed to get message from system"</span><span class="p">);</span> 46 <span class="p">}</span> 47 <span class="n">sprintf</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="s">"%s (%s:%d)"</span><span class="p">,</span> <span class="n">msg</span><span class="p">.</span><span class="n">data</span><span class="p">(),</span> <span class="n">v</span><span class="p">.</span><span class="n">file</span><span class="p">,</span> <span class="n">v</span><span class="p">.</span><span class="n">lineno</span><span class="p">);</span> 48 49 <span class="c1">// Return as atomically reference counted string 50</span><span class="c1"></span> <span class="k">return</span> <span class="n">_base</span><span class="o">::</span><span class="n">atomic_refcounted_string_ref</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">length</span><span class="p">);</span> 51 <span class="p">}</span> 52<span class="p">};</span> 53</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/experimental_status_code.cpp#L111" class="code-snippet-url" target="_blank">View this code on Github</a></div> 54 55 56 57 </div><p><small>Last revised: January 26, 2019 at 23:38:56 UTC</small></p> 58<hr> 59<div class="spirit-nav"> 60<a accesskey="p" href="../../experimental/worked-example/string_ref.html"><img src="../../images/prev.png" alt="Prev"></a> 61 <a accesskey="u" href="../../experimental/worked-example.html"><img src="../../images/up.png" alt="Up"></a> 62 <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../experimental/worked-example/source.html"><img src="../../images/next.png" alt="Next"></a></div></body> 63</html> 64