mirror of
https://github.com/boostorg/outcome.git
synced 2026-02-19 14:42:08 +00:00
53 lines
4.1 KiB
XML
53 lines
4.1 KiB
XML
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
|
|
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
|
<channel>
|
|
<title>Using result<T> from C code on Boost.Outcome documentation</title>
|
|
<link>https://ned14.github.io/boost-outcome/tutorial/c-api/</link>
|
|
<description>Recent content in Using result<T> from C code on Boost.Outcome documentation</description>
|
|
<generator>Hugo -- gohugo.io</generator>
|
|
|
|
<atom:link href="https://ned14.github.io/boost-outcome/tutorial/c-api/index.xml" rel="self" type="application/rss+xml" />
|
|
|
|
|
|
<item>
|
|
<title>Limitations</title>
|
|
<link>https://ned14.github.io/boost-outcome/tutorial/c-api/limitations/</link>
|
|
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
|
|
|
|
<guid>https://ned14.github.io/boost-outcome/tutorial/c-api/limitations/</guid>
|
|
<description>C++ has excellent two-way compatibility with the C ABI, but there are some limitations you must observe to write C++ code which C code can call without marshalling at the ABI boundary:
|
|
A C++ function may not throw exceptions if it is safe to call from C, and so should always be marked noexcept. A C++ function should be annotated with extern &quot;C&quot; to prevent its symbol being mangled, and thus give it the C rather than C++ ABI.</description>
|
|
</item>
|
|
|
|
<item>
|
|
<title>Example C++ function</title>
|
|
<link>https://ned14.github.io/boost-outcome/tutorial/c-api/example/</link>
|
|
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
|
|
|
|
<guid>https://ned14.github.io/boost-outcome/tutorial/c-api/example/</guid>
|
|
<description>Let us start with a simple C++ function which we wish to make available to C code:
|
|
// Fill the supplied buffer with the integer v converted to a string, returning // length of string minus null terminator extern &#34;C&#34; outcome::result&lt;size_t&gt; to_string(char *buffer, size_t bufferlen, int v) noexcept { try { // Could throw an exception! std::string temp(std::to_string(v)); // Will this string exceed the supplied buffer? if(temp.size() + 1 &gt; bufferlen) return std::errc::no_buffer_space; // Copy the string into the supplied buffer, and return length of string memcpy(buffer, temp.</description>
|
|
</item>
|
|
|
|
<item>
|
|
<title>Calling it from C</title>
|
|
<link>https://ned14.github.io/boost-outcome/tutorial/c-api/example2/</link>
|
|
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
|
|
|
|
<guid>https://ned14.github.io/boost-outcome/tutorial/c-api/example2/</guid>
|
|
<description>Now let us call our result returning C++ function from C:
|
|
#include &lt;stdio.h&gt;#include &lt;string.h&gt; // for strerror// This header in Outcome is pure C, it provides a suite of C helper macros #include &#34;../../../include/outcome/result.h&#34; // Declare our C++ function&#39;s returning result type. Only needs to be done once. CXX_DECLARE_RESULT_EC(size_t, size_t); // Tell C about our C++ function extern CXX_RESULT_EC(size_t) to_string(char *buffer, size_t bufferlen, int v); void print(int v) { char buffer[4]; CXX_RESULT_EC(size_t) res; res = to_string(buffer, sizeof(buffer), v); if(CXX_RESULT_HAS_VALUE(res)) { printf(&#34;to_string(%d) fills buffer with &#39;%s&#39; of %zu characters\n&#34;, v, buffer, res.</description>
|
|
</item>
|
|
|
|
<item>
|
|
<title>Variations</title>
|
|
<link>https://ned14.github.io/boost-outcome/tutorial/c-api/variations/</link>
|
|
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
|
|
|
|
<guid>https://ned14.github.io/boost-outcome/tutorial/c-api/variations/</guid>
|
|
<description>You can of course choose your own E type so long as it has standard layout and is trivially copyable. You would declare that with CXX_DECLARE_RESULT(t_ident, t_decl, e_ident, e_decl) , refer to it with CXX_RESULT(t_ident, e_ident) and need to do your own decoding of errors from your E type. By using the _EC postfixed macros, you are in fact using E =
|
|
struct cxx_error_code { int code; void *category; }; &hellip; which is declared for you by result.</description>
|
|
</item>
|
|
|
|
</channel>
|
|
</rss> |