• 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>Example C&#43;&#43; function - 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/c-api/limitations.html"><img src="../../images/prev.png" alt="Prev"></a>
11    <a accesskey="u" href="../../experimental/c-api.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/c-api/example2.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content">
13  <div class="titlepage"><div><div><h1 style="clear: both">Example C++ function</h1></div></div></div>
14  <p>Let us start with a simple C++ function which we wish to make available to C code:</p>
15
16<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">outcome_e</span> <span class="o">=</span> <span class="n">BOOST_OUTCOME_V2_NAMESPACE</span><span class="o">::</span><span class="n">experimental</span><span class="p">;</span>
17
18<span class="c1">// Fill the supplied buffer with the integer v converted to a string,
19</span><span class="c1">// returning length of string minus null terminator
20</span><span class="c1"></span><span class="n">outcome_e</span><span class="o">::</span><span class="n">status_result</span><span class="o">&lt;</span><span class="n">size_t</span><span class="o">&gt;</span> <span class="n">to_string</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">buffer</span><span class="p">,</span> <span class="n">size_t</span> <span class="n">bufferlen</span><span class="p">,</span> <span class="kt">int</span> <span class="n">v</span><span class="p">)</span> <span class="k">noexcept</span>
21<span class="p">{</span>
22  <span class="k">try</span>
23  <span class="p">{</span>
24    <span class="c1">// Could throw an exception!
25</span><span class="c1"></span>    <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">temp</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">to_string</span><span class="p">(</span><span class="n">v</span><span class="p">));</span>
26
27    <span class="c1">// Will this string exceed the supplied buffer?
28</span><span class="c1"></span>    <span class="k">if</span><span class="p">(</span><span class="n">temp</span><span class="p">.</span><span class="n">size</span><span class="p">()</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">&gt;</span> <span class="n">bufferlen</span><span class="p">)</span>
29      <span class="k">return</span> <span class="n">outcome_e</span><span class="o">::</span><span class="n">errc</span><span class="o">::</span><span class="n">no_buffer_space</span><span class="p">;</span>
30
31    <span class="c1">// Copy the string into the supplied buffer, and return length of string
32</span><span class="c1"></span>    <span class="n">memcpy</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="n">temp</span><span class="p">.</span><span class="n">data</span><span class="p">(),</span> <span class="n">temp</span><span class="p">.</span><span class="n">size</span><span class="p">()</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
33    <span class="k">return</span> <span class="n">temp</span><span class="p">.</span><span class="n">size</span><span class="p">();</span>
34  <span class="p">}</span>
35  <span class="k">catch</span><span class="p">(...)</span>
36  <span class="p">{</span>
37    <span class="c1">// This is the &lt;system_error2&gt; analogue of Standard Outcome&#39;s
38</span><span class="c1"></span>    <span class="c1">// error_from_exception() utility function. It consumes an exception
39</span><span class="c1"></span>    <span class="c1">// ptr (defaulted to current exception), and tries to match it to a
40</span><span class="c1"></span>    <span class="c1">// standard C++ library exception type, returning a system_code
41</span><span class="c1"></span>    <span class="c1">// with an appropriate code domain (generic_code, posix_code,
42</span><span class="c1"></span>    <span class="c1">// win32_code).
43</span><span class="c1"></span>    <span class="c1">//
44</span><span class="c1"></span>    <span class="c1">// Note that using this function requires including
45</span><span class="c1"></span>    <span class="c1">// &lt;boost/outcome/experimental/status-code/system_code_from_exception.hpp&gt;
46</span><span class="c1"></span>    <span class="c1">// It is NOT included by Experimental Outcome by default.
47</span><span class="c1"></span>    <span class="k">return</span> <span class="n">outcome_e</span><span class="o">::</span><span class="n">system_code_from_exception</span><span class="p">();</span>
48  <span class="p">}</span>
49<span class="p">}</span>
50</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/cpp_api.cpp#L37" class="code-snippet-url" target="_blank">View this code on Github</a></div>
51
52
53<p>As the alias <code>status_result&lt;size_t&gt;</code> defaults the erased type to the alias <code>system_code</code>,
54the <code>to_string()</code> function returns (in concrete types) <code>basic_result&lt;size_t, status_code&lt;erased&lt;intptr_t&gt;&gt;&gt;</code>.</p>
55
56<p>The standard Outcome function referenced is documented at
57
58<a href="https://en.cppreference.com/w/cpp/error/error_code" class="api-reference" target="_blank"><i class="fa fa-book" aria-hidden="true"></i> <code>std::error_code error_from_exception(std::exception_ptr &amp;&amp;ep = std::current_exception(), std::error_code not_matched = std::make_error_code(std::errc::resource_unavailable_try_again)) noexcept</code></a>
59.
60The proposed <code>&lt;system_error2&gt;</code> reference library implementation provides an identically named
61function taking similar parameters, but it returns a <code>outcome_e::system_code</code> (<code>status_code&lt;erased&lt;intptr_t&gt;&gt;</code>) instead of a <code>std::error_code</code>.</p>
62
63
64        </div><p><small>Last revised: February 05, 2019 at 17:14:18 UTC</small></p>
65<hr>
66<div class="spirit-nav">
67<a accesskey="p" href="../../experimental/c-api/limitations.html"><img src="../../images/prev.png" alt="Prev"></a>
68    <a accesskey="u" href="../../experimental/c-api.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="../../experimental/c-api/example2.html"><img src="../../images/next.png" alt="Next"></a></div></body>
70</html>
71