cpp¶
The C++ target, cpp, generates the idiomatic C++ layer of the API, into
<output>/cpp. Its raw layer is the C API, which C++ includes as it is, so
the target also emits the C API into <output>/capi, as the C target does:
<prefix>.hppThe layer, header-only, including
lib<prefix>.h. It needs C++20, or C++17 where the optionstdsays so, see The C++ standard.<prefix>_check.cppThe check, not part of the API: it asserts the layout of the records, and prints a zeroed instance of every record, through the layer, as YAML or JSON.
The check is built with $CXX, by default c++, as the standard the layer
is generated for, with all warnings as errors, and run; yace then parses what it printed, as YAML and
as JSON. The check asserts the layout of the C API’s records, which the layer
uses, as the C check does.
The C++ standard¶
The option std names the standard that the layer and the check are
generated for: c++20, the default, or c++17, given as
--target-option cpp.std=c++17. The layer for C++17 is for a program that
is built as C++17, and it also compiles as C++20.
std::span is all that the layer takes from C++20. The layer for C++17
declares a class of its own instead, detail::span, in the namespace of the
layer, and names it wherever the layer for C++20 names std::span. A span
converts from what std::span converts from: a std::vector, a
std::array, a C array, another span, a pointer and a length in braces,
{data, size}, and {} for none. It has data(), size(),
size_bytes(), empty(), operator[], front(), back(),
begin(), end(), first(), last() and subspan(). It has no
static extent, no reverse iterators, and no std::as_bytes(). A call
written with those conversions and members compiles against both layers.
std::span(buffer), which deduces the type of the element, compiles only
against the layer for C++20.
The alternative was a pointer and a length for each array. With that, a
function that fills a buffer returns a count instead of the part of the buffer
it wrote, a caller with a std::vector passes data() and size()
itself, and every signature with an array differs between the two standards.
With the class, the two layers differ in the name of one type.
The layer¶
Everything is in a namespace named for the prefix, its names without the
prefix, so nothing clashes with the macros of the C API’s #define
constants, which C++ uses as they are. Where the C API declares something named
as the prefix, the namespace is the prefix and _cpp. From what the header
says beyond C, see The C Subset, and as derived in
yace.targets.idiom:
- Handles
An opaque record is a move-only class, named without the prefix, owning the pointer by a
std::unique_ptrwhose deleter is the destructor:struct foo_handleisfoo::handle. Its constructor is a function of the namespace,foo::open(), astd::optionalwhere it has no@errno;close()releases it before it goes out of scope; the functions taking it first are its methods,constwhere the pointer is to const.native_handle(), asstd::threadnames it, is the raw pointer, for the C API.- Buffers
A buffer that
<stem>_allocallocates, see the buffer pattern in The C Subset, is returned as afoo::buffer, a move-only class, astd::optionalwhere@nullable, and otherwisestd::bad_allocis thrown where the function returns NULL:dev.buf_alloc(4096).data()andsize()are its address and size in bytes, andrealloc(nbytes)resizes it with<stem>_realloc. Destroying it frees it with<stem>_free, which is given the arguments it was allocated with; neither has a name of its own in the layer. The handles it was allocated with must outlive it. The memory may be a device’s, such as GPU memory, which the CPU cannot read, and nothing in the API says which;bytes(), the memory as a span ofstd::byte, is for memory of the host only. The class isbuffer_where the namespace or the C API has the namebuffer.- Registrations
Memory that
<stem>_registerregisters, see the registration pattern in The C Subset, is given as a span ofstd::byte, whose size is the size:dev.buf_register(memory). The function returns afoo::registration, a move-only class whosememory()is the span. Destroying it, or callingunregister(), which throws where unregistering fails, unregisters the memory with<stem>_unregister, which has no name of its own in the layer. The memory, and the handles it was registered with, must outlive it. The class isregistration_where the namespace or the C API has the nameregistration.- Errors
A function documented
@errnothrowsstd::system_error, of thestd::generic_category(), when it returns a negative errno. One returningintreturns nothing otherwise; one returning another integer, such as a count, astd::size_t. Where the meta names the function of the library that returns the message of the call that failed last, aserrmsg: foo_errmsg, the layer calls it when a function has failed, and throwsdetail::failure, astd::system_errorwhosewhat()is the name of the function and the message:foo_point_add: the handle 'points' holds 8 points, as many as it can.code()is the errno either way, and a handler ofstd::system_errorcatches the exception.what()does not add the text of the errno to the message, asstd::system_errorwould, since the message of a library may end in that text already. Where the function returns NULL or"",what()is the name of the function and the text of the errno, as withouterrmsg.- Results
What a function writes through an
[out]pointer is returned rather than passed, several as astd::tuple. An[out]array is written to astd::spanthe caller gives, whose size is the capacity, and the part written is returned, as astd::spanof as many elements as the function says it wrote; nothing is allocated. A returned string is astd::string_view, astd::optionalof one where@nullable, borrowed from the handle or the library. A returned pointer to a record is a copy of it, astd::optionalwhere@nullable.- Arguments
A string is a
const std::string &, or aconst char *where@nullable. An array read through a pointer is astd::span; spans sharing one length must be as long, orstd::invalid_argumentis thrown; an empty one,{}, gives NULL where@nullable. Avoid *@arrparamis astd::spanofstd::byte. The size of a@recparamrecord is not given; the layer passessizeofof the record’s type. A handle given is a reference to its class, a pointer where@nullable, toconstwhere the C parameter points to const. A pointer to a record or value is a reference, or the pointer where@nullable. A callback is any callable, called through anoexcepttrampoline with thevoid *context, so an exception thrown by it terminates, as it cannot cross C; the pointers to records it receives are references. A@nullablecallback has an overload takingnullptr. One of@lifetime once(see Callback lifetimes) is moved to the heap and deleted once called, or when the call fails; one of@lifetime handleis kept by the handle, unless the call fails, until it is closed, and for good where destroying it fails or nothing destroys it, as C may still hold it.- Records and enums
The records are named without the prefix,
foo::point, the C records themselves.foo::yaml()andfoo::json()print a record, as the C API’s pretty-printers print it, without calling them, andoperator<<prints its YAML. Each part of a field of bits, see The C Subset, is read by a function of the namespace overloaded on the record,foo::code_type(status), and written by another,foo::set_code_type(status, value), which throwsstd::out_of_rangefor a value that does not fit the part’s bits; the records being C’s, they have no methods. Each enum is anenum class, its members without the prefix of the enum, in lower case:FOO_SHAPE_POINTisfoo::shape::point.foo::name()is the C name of a value, none when it has none.
The comment of a function is that of the C function, whose text may name a
parameter that the signature does not have: a count that is the size of a
span, the size of a @recparam record, which is that of the record’s type,
or an [out] pointer whose value is returned. yace cannot reword the
text, so the comment gets a @note that says what the signature has for
each such parameter that the text names in quotes: In this signature: 'max'
is the size of 'entries'; 'remaining' is returned.
A constructor whose name without the handle’s is a C++ keyword keeps the name
of the handle: foo_req_new() is foo::req_new(). Any other name that is
a C++ keyword gets a trailing underscore, as does a method named
native_handle, deleter or as a handle class, and a parameter named as
what its function names unqualified, such as ret, the local of the result,
a C name or a handle class, or ending in _, so no two parameters share a
name. A type or function whose name would hide a C name, or one the namespace
declares or uses, yaml, json, name, detail and std, gets
_handle for a handle class and _ otherwise; a function also gets _
where an enum class or handle class has its name. A record has no alias where a
function has its name, or where its tag has no prefix: its C name is the one.
Example¶
yace output/foo.yaml --emit cpp --output output
output/cpp
├── cc.log
├── check.json
├── check.yaml
├── foo.hpp
├── foo_check
└── foo_check.cpp
1 directory, 6 files
The fixture library is called through the layer by
examples/libfoo/checks/cpp/check.cpp:
/**
* The check of the C++ layer of libfoo: calls the library through foo.hpp, its
* handle, exceptions and returned values, and prints what it got back, as YAML
* documents, or with argument "json" as JSON objects, each holding what
* 'expected.yaml' says. An error is printed as the negative errno the C
* function returned. The raw layer of C++ is the C API, checked by the C check;
* what the layer does not take, a NULL where none is allowed, is called raw.
*/
#include <cstdint>
#include <cstring>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include <foo.hpp>
namespace {
bool as_json = false;
struct field {
std::string key;
std::string value; // as JSON, which is also YAML
};
std::string
quoted(const std::string &text)
{
return "\"" + text + "\"";
}
std::string
num(std::int64_t value)
{
return std::to_string(value);
}
void
document(const std::string &name, const std::vector<field> &fields)
{
if (as_json) {
std::cout << "{\"" << name << "\": {";
for (std::size_t idx = 0; idx < fields.size(); ++idx) {
std::cout << (idx ? ", " : "") << "\"" << fields[idx].key
<< "\": " << fields[idx].value;
}
std::cout << "}}\n";
return;
}
std::cout << "---\n" << name << ":\n";
for (const auto &f : fields) {
std::cout << " " << f.key << ": " << f.value << "\n";
}
}
/// Print a record, or none, with the printers of the C++ layer
template <class T>
void
record(const std::string &name, const std::optional<T> &obj)
{
if (as_json) {
std::cout << "{\"" << name << "\": ";
if (obj) {
std::cout << foo::json(*obj);
} else {
std::cout << "null\n";
}
std::cout << "}\n";
return;
}
std::cout << "---\n";
if (obj) {
std::cout << *obj;
} else {
std::cout << name << ": ~\n";
}
}
/// What 'call' returns, 0 for nothing, or the negative errno it throws
template <class F>
std::int64_t
errno_of(F &&call)
{
try {
if constexpr (std::is_void_v<decltype(call())>) {
call();
return 0;
} else {
return static_cast<std::int64_t>(call());
}
} catch (const std::system_error &err) {
return -err.code().value();
}
}
std::string
names(const std::vector<int> &values, const char *(*str)(int))
{
std::string text = "[";
for (std::size_t idx = 0; idx < values.size(); ++idx) {
text += (idx ? ", " : "") + quoted(str(values[idx]));
}
return text + "]";
}
} // namespace
int
main(int argc, char **argv)
{
as_json = argc > 1 && !std::strcmp(argv[1], "json");
document("defines", {
{"FOO_NAME", quoted(FOO_NAME)},
{"FOO_MAGIC", num(FOO_MAGIC)},
{"FOO_VERSION_MAJOR", num(FOO_VERSION_MAJOR)},
{"FOO_VERSION_MINOR", num(FOO_VERSION_MINOR)},
{"FOO_VERSION_PATCH", num(FOO_VERSION_PATCH)},
{"FOO_POINTS_MAX", num(FOO_POINTS_MAX)},
{"FOO_DEFAULT_SHAPE_SIDES", num(FOO_DEFAULT_SHAPE_SIDES)},
});
auto shape_name = [](int value) -> const char * {
auto found = foo::name(static_cast<foo::shape>(value));
return found ? found->data() : "UNKNOWN";
};
auto option_name = [](int value) -> const char * {
auto found = foo::name(static_cast<foo::option>(value));
return found ? found->data() : "UNKNOWN";
};
document("enums", {
{"foo_shape", names({-1, 0, 1, 2, 3}, shape_name)},
{"foo_option", names({1, 2, 3}, option_name)},
});
std::vector<foo::point> points = {{1, 2}, {3, 4}, {5, 6}};
// Declared before the handle, which keeps the watcher until it is closed
std::int64_t watched = 0, watched_x = 0;
auto handle = foo::open("points");
::foo_handle *unopened = nullptr;
std::int64_t open_without_name = ::foo_open(nullptr, &unopened);
std::int64_t watch = errno_of([&] {
handle.point_watch([&](const foo::point &p) {
watched += 1;
watched_x += p.x;
});
});
std::int64_t watch_busy =
errno_of([&] { handle.point_watch([](const foo::point &) {}); });
std::int64_t added = 0;
for (const auto &point : points) {
added += errno_of([&] { handle.point_add(point); }) == 0;
}
std::int64_t count = static_cast<std::int64_t>(handle.point_count());
std::int64_t sum_x = 0, sum_y = 0, seen = 0, with_x = 0;
std::int64_t visit = static_cast<std::int64_t>(handle.point_visit([&](const foo::point &p) {
sum_x += p.x;
sum_y += p.y;
return 0;
}));
std::int64_t stopped = static_cast<std::int64_t>(
handle.point_visit([&](const foo::point &) { return ++seen == 2 ? 1 : 0; }));
std::int64_t without = ::foo_point_visit(handle.native_handle(), nullptr, nullptr);
foo::visitor visitor = {
[](void *arg, const ::foo_point *p) -> int {
*static_cast<std::int64_t *>(arg) += p->x;
return 0;
},
&with_x,
};
std::int64_t visit_with = static_cast<std::int64_t>(handle.point_visit_with(visitor));
std::int64_t unknown = errno_of([] { foo::figure_fill(3); });
std::int64_t add_many = static_cast<std::int64_t>(handle.point_add_many(points));
std::int64_t count_after = static_cast<std::int64_t>(handle.point_count());
std::int64_t watched_after = watched, watched_x_after = watched_x;
std::vector<::foo_point> buffer(4);
auto copied = handle.point_copy(buffer);
std::int64_t copied_x = 0;
for (const auto &p : copied) {
copied_x += p.x;
}
std::size_t done_count = 0;
auto counter = [&done_count](std::size_t n) { done_count = n; };
std::int64_t submit = errno_of([&] { handle.count_submit(counter); });
std::int64_t submit_busy = errno_of([&] { handle.count_submit(counter); });
std::int64_t complete = static_cast<std::int64_t>(handle.count_complete());
std::int64_t complete_again = static_cast<std::int64_t>(handle.count_complete());
document("calls", {
{"version", quoted(std::string(foo::version()))},
{"open", num(0)},
{"open_without_name", num(open_without_name)},
{"name", quoted(std::string(handle.name()))},
{"watch", num(watch)},
{"watch_busy", num(watch_busy)},
{"added", num(added)},
{"count", num(count)},
{"visit", num(visit)},
{"visit_sum_x", num(sum_x)},
{"visit_sum_y", num(sum_y)},
{"visit_stopped", num(stopped)},
{"visit_without_callback", num(without)},
{"visit_with", num(visit_with)},
{"visit_with_sum_x", num(with_x)},
{"figure_of_unknown_shape", num(unknown)},
{"add_many", num(add_many)},
{"count_after_add_many", num(count_after)},
{"watched", num(watched_after)},
{"watched_sum_x", num(watched_x_after)},
{"points_copied", num(static_cast<std::int64_t>(copied.size()))},
{"points_sum_x", num(copied_x)},
{"submit", num(submit)},
{"submit_busy", num(submit_busy)},
{"complete", num(complete)},
{"done_count", num(static_cast<std::int64_t>(done_count))},
{"complete_again", num(complete_again)},
});
record("foo_point", handle.point_get(1));
record("foo_point", handle.point_get(6));
while (errno_of([&] { handle.point_add(points[0]); }) == 0) {
}
document("full", {
{"count", num(static_cast<std::int64_t>(handle.point_count()))},
{"add", num(errno_of([&] { handle.point_add(points[0]); }))},
{"add_many",
num(errno_of([&] {
return handle.point_add_many({points.data(), 1});
}))},
{"watched", num(watched)},
});
auto status = handle.status_get();
record("foo_status", std::optional(status));
document("status", {
{"pending", num(foo::code_pending(status))},
{"points", num(foo::code_points(status))},
{"full", num(foo::code_full(status))},
});
foo::status made = {};
made.max = FOO_POINTS_MAX;
foo::set_code_points(made, 5);
foo::set_code_pending(made, 1);
record("foo_status", std::optional(made));
handle.close();
record("foo_scalars", std::optional(foo::scalars_fill()));
for (auto shape : {foo::shape::point, foo::shape::line, foo::shape::polygon}) {
record("foo_figure", std::optional(foo::figure_fill(static_cast<std::uint32_t>(shape))));
}
record("foo_value", std::optional(foo::value_set(-1)));
record("foo_wire", std::optional(foo::wire_fill()));
return 0;
}
The layer¶
/**
* libfoo: An example library described in the C subset of yace
*
* The idiomatic C++ layer over the C API in libfoo.h, which is its
* raw layer. Everything is in the namespace foo, named without the prefix.
* Generated for C++20.
*
* SPDX-License-Identifier: GPL-2.0-only
*
* @file foo.hpp
*
* Generated by yace from foo.h; do not edit.
*/
#ifndef FOO_HPP
#define FOO_HPP
#include <algorithm>
#include <cerrno>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <functional>
#include <memory>
#include <new>
#include <optional>
#include <ostream>
#include <span>
#include <stdexcept>
#include <string>
#include <string_view>
#include <system_error>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
// The C header is C, which may declare what ISO C++ has not, such as a
// flexible array member
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#include <libfoo.h>
#pragma GCC diagnostic pop
namespace foo {
namespace detail {
inline void
put_int(std::string &out, std::intmax_t value)
{
out += std::to_string(value);
}
inline void
put_uint(std::string &out, std::uintmax_t value)
{
out += std::to_string(value);
}
inline void
put_hex(std::string &out, std::uintmax_t value)
{
char buf[32];
int len = std::snprintf(buf, sizeof(buf), "0x%jx", value);
out.append(buf, static_cast<std::size_t>(len));
}
/// 'value' as printf() prints it with "%g", through C's snprintf(), so that the
/// C and C++ printers agree
inline void
put_g(std::string &out, double value)
{
char buf[32];
int len = std::snprintf(buf, sizeof(buf), "%g", value);
out.append(buf, static_cast<std::size_t>(len));
}
inline void
put_strn(std::string &out, const char *str, std::size_t len)
{
if (!str) {
out += "null";
return;
}
out += '"';
for (std::size_t i = 0; i < len && str[i]; ++i) {
auto chr = static_cast<unsigned char>(str[i]);
if (chr == '"' || chr == '\\') {
out += '\\';
out += static_cast<char>(chr);
} else if (chr < 0x20 || chr == 0x7f) {
char buf[8];
std::snprintf(buf, sizeof(buf), "\\u%04x", chr);
out += buf;
} else {
out += static_cast<char>(chr);
}
}
out += '"';
}
inline void
put_str(std::string &out, const char *str)
{
put_strn(out, str, SIZE_MAX);
}
inline void
put_ptr(std::string &out, std::uintptr_t addr)
{
if (!addr) {
out += "null";
return;
}
out += '"';
put_hex(out, addr);
out += '"';
}
/**
* The error of a failed call: a std::system_error whose code() is the errno,
* and whose what() is the name of the function and the message that
* foo_errmsg() gives of the failure, or the text of the errno where it
* gives none
*/
class failure : public std::system_error {
public:
failure(int ev, const char *function)
: std::system_error(ev, std::generic_category()), what_(function)
{
const char *msg = ::foo_errmsg();
what_ += ": ";
what_ += msg && *msg ? std::string(msg) : code().message();
}
const char *
what() const noexcept override
{
return what_.c_str();
}
private:
std::string what_;
};
inline void
json(std::string &out, const struct ::foo_point &obj);
inline void
yaml(std::string &out, const struct ::foo_point &obj, int indent);
inline void
json(std::string &out, const struct ::foo_visitor &obj);
inline void
yaml(std::string &out, const struct ::foo_visitor &obj, int indent);
inline void
json(std::string &out, const struct ::foo_scalars &obj);
inline void
yaml(std::string &out, const struct ::foo_scalars &obj, int indent);
inline void
json(std::string &out, const struct ::foo_figure &obj);
inline void
yaml(std::string &out, const struct ::foo_figure &obj, int indent);
inline void
json(std::string &out, const union ::foo_value &obj);
inline void
yaml(std::string &out, const union ::foo_value &obj, int indent);
inline void
json(std::string &out, const struct ::foo_wire &obj);
inline void
yaml(std::string &out, const struct ::foo_wire &obj, int indent);
inline void
json(std::string &out, const struct ::foo_status &obj);
inline void
yaml(std::string &out, const struct ::foo_status &obj, int indent);
inline void
json(std::string &out, const struct ::foo_point &obj)
{
out += '{';
out += "\"x\": ";
detail::put_int(out, static_cast<std::intmax_t>(obj.x));
out += ", \"y\": ";
detail::put_int(out, static_cast<std::intmax_t>(obj.y));
out += '}';
}
inline void
yaml(std::string &out, const struct ::foo_point &obj, int indent)
{
out.append(static_cast<std::size_t>(indent), ' ');
out += "x: ";
detail::put_int(out, static_cast<std::intmax_t>(obj.x));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "y: ";
detail::put_int(out, static_cast<std::intmax_t>(obj.y));
out += '\n';
}
inline void
json(std::string &out, const struct ::foo_visitor &obj)
{
out += '{';
out += "\"visit\": ";
detail::put_ptr(out, reinterpret_cast<std::uintptr_t>(obj.visit));
out += ", \"arg\": ";
detail::put_ptr(out, reinterpret_cast<std::uintptr_t>(obj.arg));
out += '}';
}
inline void
yaml(std::string &out, const struct ::foo_visitor &obj, int indent)
{
out.append(static_cast<std::size_t>(indent), ' ');
out += "visit: ";
detail::put_ptr(out, reinterpret_cast<std::uintptr_t>(obj.visit));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "arg: ";
detail::put_ptr(out, reinterpret_cast<std::uintptr_t>(obj.arg));
out += '\n';
}
inline void
json(std::string &out, const struct ::foo_scalars &obj)
{
out += '{';
out += "\"flag\": ";
out += obj.flag ? "true" : "false";
out += ", \"letter\": ";
detail::put_int(out, static_cast<std::intmax_t>(obj.letter));
out += ", \"schar\": ";
detail::put_int(out, static_cast<std::intmax_t>(obj.schar));
out += ", \"uchar\": ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.uchar));
out += ", \"s\": ";
detail::put_int(out, static_cast<std::intmax_t>(obj.s));
out += ", \"us\": ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.us));
out += ", \"i\": ";
detail::put_int(out, static_cast<std::intmax_t>(obj.i));
out += ", \"ui\": ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.ui));
out += ", \"l\": ";
detail::put_int(out, static_cast<std::intmax_t>(obj.l));
out += ", \"ul\": ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.ul));
out += ", \"ll\": ";
detail::put_int(out, static_cast<std::intmax_t>(obj.ll));
out += ", \"ull\": ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.ull));
out += ", \"f\": ";
detail::put_g(out, static_cast<double>(obj.f));
out += ", \"d\": ";
detail::put_g(out, static_cast<double>(obj.d));
out += ", \"i8\": ";
detail::put_int(out, static_cast<std::intmax_t>(obj.i8));
out += ", \"i16\": ";
detail::put_int(out, static_cast<std::intmax_t>(obj.i16));
out += ", \"i32\": ";
detail::put_int(out, static_cast<std::intmax_t>(obj.i32));
out += ", \"i64\": ";
detail::put_int(out, static_cast<std::intmax_t>(obj.i64));
out += ", \"u8\": ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.u8));
out += ", \"u16\": ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.u16));
out += ", \"u32\": ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.u32));
out += ", \"u64\": ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.u64));
out += ", \"iptr\": ";
detail::put_int(out, static_cast<std::intmax_t>(obj.iptr));
out += ", \"uptr\": ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.uptr));
out += ", \"size\": ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.size));
out += ", \"ssize\": ";
detail::put_int(out, static_cast<std::intmax_t>(obj.ssize));
out += ", \"diff\": ";
detail::put_int(out, static_cast<std::intmax_t>(obj.diff));
out += '}';
}
inline void
yaml(std::string &out, const struct ::foo_scalars &obj, int indent)
{
out.append(static_cast<std::size_t>(indent), ' ');
out += "flag: ";
out += obj.flag ? "true" : "false";
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "letter: ";
detail::put_int(out, static_cast<std::intmax_t>(obj.letter));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "schar: ";
detail::put_int(out, static_cast<std::intmax_t>(obj.schar));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "uchar: ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.uchar));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "s: ";
detail::put_int(out, static_cast<std::intmax_t>(obj.s));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "us: ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.us));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "i: ";
detail::put_int(out, static_cast<std::intmax_t>(obj.i));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "ui: ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.ui));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "l: ";
detail::put_int(out, static_cast<std::intmax_t>(obj.l));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "ul: ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.ul));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "ll: ";
detail::put_int(out, static_cast<std::intmax_t>(obj.ll));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "ull: ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.ull));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "f: ";
detail::put_g(out, static_cast<double>(obj.f));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "d: ";
detail::put_g(out, static_cast<double>(obj.d));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "i8: ";
detail::put_int(out, static_cast<std::intmax_t>(obj.i8));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "i16: ";
detail::put_int(out, static_cast<std::intmax_t>(obj.i16));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "i32: ";
detail::put_int(out, static_cast<std::intmax_t>(obj.i32));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "i64: ";
detail::put_int(out, static_cast<std::intmax_t>(obj.i64));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "u8: ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.u8));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "u16: ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.u16));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "u32: ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.u32));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "u64: ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.u64));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "iptr: ";
detail::put_int(out, static_cast<std::intmax_t>(obj.iptr));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "uptr: ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.uptr));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "size: ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.size));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "ssize: ";
detail::put_int(out, static_cast<std::intmax_t>(obj.ssize));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "diff: ";
detail::put_int(out, static_cast<std::intmax_t>(obj.diff));
out += '\n';
}
inline void
json(std::string &out, const struct ::foo_figure &obj)
{
out += '{';
out += "\"shape\": ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.shape));
out += ", \"options\": ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.options));
out += ", \"origin\": ";
detail::json(out, obj.origin);
out += ", \"corners\": ";
out += '[';
for (std::size_t i0 = 0; i0 < 4; ++i0) {
out += i0 ? ", " : "";
detail::json(out, obj.corners[i0]);
}
out += ']';
out += ", \"ncorners\": ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.ncorners));
out += ", \"name\": ";
detail::put_strn(out, obj.name, 16);
out += ", \"grid\": ";
out += '[';
for (std::size_t i0 = 0; i0 < 2; ++i0) {
out += i0 ? ", " : "";
out += '[';
for (std::size_t i1 = 0; i1 < 3; ++i1) {
out += i1 ? ", " : "";
detail::put_int(out, static_cast<std::intmax_t>(obj.grid[i0][i1]));
}
out += ']';
}
out += ']';
out += ", \"label\": ";
detail::put_str(out, obj.label);
out += ", \"scale\": ";
detail::put_g(out, static_cast<double>(obj.scale));
out += ", \"area\": ";
detail::put_g(out, static_cast<double>(obj.area));
out += '}';
}
inline void
yaml(std::string &out, const struct ::foo_figure &obj, int indent)
{
out.append(static_cast<std::size_t>(indent), ' ');
out += "shape: ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.shape));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "options: ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.options));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "origin:\n";
detail::yaml(out, obj.origin, indent + 2);
out.append(static_cast<std::size_t>(indent), ' ');
out += "corners: ";
out += '[';
for (std::size_t i0 = 0; i0 < 4; ++i0) {
out += i0 ? ", " : "";
detail::json(out, obj.corners[i0]);
}
out += ']';
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "ncorners: ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.ncorners));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "name: ";
detail::put_strn(out, obj.name, 16);
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "grid: ";
out += '[';
for (std::size_t i0 = 0; i0 < 2; ++i0) {
out += i0 ? ", " : "";
out += '[';
for (std::size_t i1 = 0; i1 < 3; ++i1) {
out += i1 ? ", " : "";
detail::put_int(out, static_cast<std::intmax_t>(obj.grid[i0][i1]));
}
out += ']';
}
out += ']';
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "label: ";
detail::put_str(out, obj.label);
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "scale: ";
detail::put_g(out, static_cast<double>(obj.scale));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "area: ";
detail::put_g(out, static_cast<double>(obj.area));
out += '\n';
}
inline void
json(std::string &out, const union ::foo_value &obj)
{
out += '{';
out += "\"i\": ";
detail::put_int(out, static_cast<std::intmax_t>(obj.i));
out += ", \"u\": ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.u));
out += ", \"point\": ";
detail::json(out, obj.point);
out += '}';
}
inline void
yaml(std::string &out, const union ::foo_value &obj, int indent)
{
out.append(static_cast<std::size_t>(indent), ' ');
out += "i: ";
detail::put_int(out, static_cast<std::intmax_t>(obj.i));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "u: ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.u));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "point:\n";
detail::yaml(out, obj.point, indent + 2);
}
inline void
json(std::string &out, const struct ::foo_wire &obj)
{
out += '{';
out += "\"kind\": ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.kind));
out += ", \"length\": ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.length));
out += ", \"at\": ";
{
const auto v0 = obj.at;
detail::json(out, v0);
}
out += ", \"pair\": ";
out += '[';
for (std::size_t i0 = 0; i0 < 2; ++i0) {
out += i0 ? ", " : "";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.pair[i0]));
}
out += ']';
out += '}';
}
inline void
yaml(std::string &out, const struct ::foo_wire &obj, int indent)
{
out.append(static_cast<std::size_t>(indent), ' ');
out += "kind: ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.kind));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "length: ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.length));
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "at:\n";
{
const auto v0 = obj.at;
detail::yaml(out, v0, indent + 2);
}
out.append(static_cast<std::size_t>(indent), ' ');
out += "pair: ";
out += '[';
for (std::size_t i0 = 0; i0 < 2; ++i0) {
out += i0 ? ", " : "";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.pair[i0]));
}
out += ']';
out += '\n';
}
inline void
json(std::string &out, const struct ::foo_status &obj)
{
out += '{';
out += "\"code\": ";
out += "{\"val\": ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.code));
out += ", \"pending\": ";
detail::put_uint(out, (static_cast<std::uintmax_t>(obj.code) & 0x1u) >> 0);
out += ", \"points\": ";
detail::put_uint(out, (static_cast<std::uintmax_t>(obj.code) & 0xff0u) >> 4);
out += ", \"full\": ";
detail::put_uint(out, (static_cast<std::uintmax_t>(obj.code) & 0x8000u) >> 15);
out += '}';
out += ", \"max\": ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.max));
out += '}';
}
inline void
yaml(std::string &out, const struct ::foo_status &obj, int indent)
{
out.append(static_cast<std::size_t>(indent), ' ');
out += "code: ";
out += "{val: ";
detail::put_hex(out, static_cast<std::uintmax_t>(obj.code));
out += ", pending: ";
detail::put_hex(out, (static_cast<std::uintmax_t>(obj.code) & 0x1u) >> 0);
out += ", points: ";
detail::put_hex(out, (static_cast<std::uintmax_t>(obj.code) & 0xff0u) >> 4);
out += ", full: ";
detail::put_hex(out, (static_cast<std::uintmax_t>(obj.code) & 0x8000u) >> 15);
out += '}';
out += '\n';
out.append(static_cast<std::size_t>(indent), ' ');
out += "max: ";
detail::put_uint(out, static_cast<std::uintmax_t>(obj.max));
out += '\n';
}
} // namespace detail
/// The YAML of a struct foo_point, as foo_point_pr() prints it
inline std::string
yaml(const struct ::foo_point &obj)
{
std::string out = "foo_point:\n";
detail::yaml(out, obj, 2);
return out;
}
/// The JSON of a struct foo_point, on one line, as foo_point_pr() prints it
inline std::string
json(const struct ::foo_point &obj)
{
std::string out;
detail::json(out, obj);
return out += '\n';
}
/// The YAML of a struct foo_visitor, as foo_visitor_pr() prints it
inline std::string
yaml(const struct ::foo_visitor &obj)
{
std::string out = "foo_visitor:\n";
detail::yaml(out, obj, 2);
return out;
}
/// The JSON of a struct foo_visitor, on one line, as foo_visitor_pr() prints it
inline std::string
json(const struct ::foo_visitor &obj)
{
std::string out;
detail::json(out, obj);
return out += '\n';
}
/// The YAML of a struct foo_scalars, as foo_scalars_pr() prints it
inline std::string
yaml(const struct ::foo_scalars &obj)
{
std::string out = "foo_scalars:\n";
detail::yaml(out, obj, 2);
return out;
}
/// The JSON of a struct foo_scalars, on one line, as foo_scalars_pr() prints it
inline std::string
json(const struct ::foo_scalars &obj)
{
std::string out;
detail::json(out, obj);
return out += '\n';
}
/// The YAML of a struct foo_figure, as foo_figure_pr() prints it
inline std::string
yaml(const struct ::foo_figure &obj)
{
std::string out = "foo_figure:\n";
detail::yaml(out, obj, 2);
return out;
}
/// The JSON of a struct foo_figure, on one line, as foo_figure_pr() prints it
inline std::string
json(const struct ::foo_figure &obj)
{
std::string out;
detail::json(out, obj);
return out += '\n';
}
/// The YAML of a union foo_value, as foo_value_pr() prints it
inline std::string
yaml(const union ::foo_value &obj)
{
std::string out = "foo_value:\n";
detail::yaml(out, obj, 2);
return out;
}
/// The JSON of a union foo_value, on one line, as foo_value_pr() prints it
inline std::string
json(const union ::foo_value &obj)
{
std::string out;
detail::json(out, obj);
return out += '\n';
}
/// The YAML of a struct foo_wire, as foo_wire_pr() prints it
inline std::string
yaml(const struct ::foo_wire &obj)
{
std::string out = "foo_wire:\n";
detail::yaml(out, obj, 2);
return out;
}
/// The JSON of a struct foo_wire, on one line, as foo_wire_pr() prints it
inline std::string
json(const struct ::foo_wire &obj)
{
std::string out;
detail::json(out, obj);
return out += '\n';
}
/// The YAML of a struct foo_status, as foo_status_pr() prints it
inline std::string
yaml(const struct ::foo_status &obj)
{
std::string out = "foo_status:\n";
detail::yaml(out, obj, 2);
return out;
}
/// The JSON of a struct foo_status, on one line, as foo_status_pr() prints it
inline std::string
json(const struct ::foo_status &obj)
{
std::string out;
detail::json(out, obj);
return out += '\n';
}
/**
* Kinds of figure
*/
enum class shape : int {
none = ::FOO_SHAPE_NONE,
point = ::FOO_SHAPE_POINT,
line = ::FOO_SHAPE_LINE,
polygon = ::FOO_SHAPE_POLYGON,
};
/// The C name of the value, none when it is none of those named
inline std::optional<std::string_view>
name(shape value)
{
switch (value) {
case shape::none:
return "FOO_SHAPE_NONE";
case shape::point:
return "FOO_SHAPE_POINT";
case shape::line:
return "FOO_SHAPE_LINE";
case shape::polygon:
return "FOO_SHAPE_POLYGON";
default:
return std::nullopt;
}
}
/**
* Options of a figure, combined as bits
*/
enum class option : unsigned int {
closed = ::FOO_OPTION_CLOSED,
filled = ::FOO_OPTION_FILLED,
};
/// The C name of the value, none when it is none of those named
inline std::optional<std::string_view>
name(option value)
{
switch (value) {
case option::closed:
return "FOO_OPTION_CLOSED";
case option::filled:
return "FOO_OPTION_FILLED";
default:
return std::nullopt;
}
}
using point = struct ::foo_point;
using visitor = struct ::foo_visitor;
using scalars = struct ::foo_scalars;
using figure = struct ::foo_figure;
using value = union ::foo_value;
using wire = struct ::foo_wire;
using status = struct ::foo_status;
/**
* A count is submitted and not yet completed; bit 0 of 'code'
*/
inline uint16_t
code_pending(const struct ::foo_status &obj) noexcept
{
return static_cast<uint16_t>((obj.code & 0x1u) >> 0);
}
/// Set the part of 'code' that code_pending() reads;
/// throws std::out_of_range when 'value' does not fit in 1 bit
inline void
set_code_pending(struct ::foo_status &obj, uint16_t value)
{
if (value > 0x1u) {
throw std::out_of_range("code_pending is 1 bit");
}
const uint16_t mask = 0x1u;
obj.code = static_cast<uint16_t>((obj.code & static_cast<uint16_t>(~mask)) |
static_cast<uint16_t>(value << 0));
}
/**
* Points held; bits 11:4 of 'code'
*/
inline uint16_t
code_points(const struct ::foo_status &obj) noexcept
{
return static_cast<uint16_t>((obj.code & 0xff0u) >> 4);
}
/// Set the part of 'code' that code_points() reads;
/// throws std::out_of_range when 'value' does not fit in 8 bits
inline void
set_code_points(struct ::foo_status &obj, uint16_t value)
{
if (value > 0xffu) {
throw std::out_of_range("code_points is 8 bits");
}
const uint16_t mask = 0xff0u;
obj.code = static_cast<uint16_t>((obj.code & static_cast<uint16_t>(~mask)) |
static_cast<uint16_t>(value << 4));
}
/**
* The handle holds FOO_POINTS_MAX points; bit 15 of 'code'
*/
inline uint16_t
code_full(const struct ::foo_status &obj) noexcept
{
return static_cast<uint16_t>((obj.code & 0x8000u) >> 15);
}
/// Set the part of 'code' that code_full() reads;
/// throws std::out_of_range when 'value' does not fit in 1 bit
inline void
set_code_full(struct ::foo_status &obj, uint16_t value)
{
if (value > 0x1u) {
throw std::out_of_range("code_full is 1 bit");
}
const uint16_t mask = 0x8000u;
obj.code = static_cast<uint16_t>((obj.code & static_cast<uint16_t>(~mask)) |
static_cast<uint16_t>(value << 15));
}
class handle;
/**
* A collection of points, opened with foo_open() and closed with foo_close()
*/
class handle {
public:
explicit handle(struct ::foo_handle *ptr) noexcept : ptr_(ptr) {}
/// The raw pointer, still owned by this
struct ::foo_handle *
native_handle() const noexcept
{
return ptr_.get();
}
handle(handle &&) noexcept = default;
handle &
operator=(handle &&other) noexcept
{
if (this != &other) {
ptr_ = std::move(other.ptr_);
keep_ = std::move(other.keep_);
}
return *this;
}
/**
* The name the handle was opened with
*
* @return The name, owned by the handle
*/
std::string_view
name() const;
/**
* Add a point to the handle
*
* @param point The point to add, copied
* @throws std::system_error foo_point_add() returns: 0 on success, -ENOSPC when the handle
* holds FOO_POINTS_MAX points
*/
void
point_add(const struct ::foo_point &point);
/**
* Add points to the handle, as many as it has room for
*
* @param points The points to add, copied
* @throws std::system_error foo_point_add_many() returns: The number of points added,
* -ENOSPC when there was room for none
*/
std::size_t
point_add_many(std::span<const struct ::foo_point> points);
/**
* The number of points held
*
* @return The number of points
*/
size_t
point_count() const;
/**
* A point held by the handle
*
* @param idx Index of the point
* @return The point, NULL when 'idx' is past the last one
*/
std::optional<struct ::foo_point>
point_get(size_t idx) const;
/**
* Copy the points held, as many as fit
*
* @param points Where to copy the points to
* @throws std::system_error foo_point_copy() returns: The number of points copied
*/
std::span<struct ::foo_point>
point_copy(std::span<struct ::foo_point> points) const;
/**
* Watch the points added to the handle, from now until it is closed
*
* @param added Called with each point added, until the handle is closed
* @throws std::system_error foo_point_watch() returns: 0 on success, -EBUSY when the
* handle is watched already
*/
template <class F>
void
point_watch(F &&added);
/**
* Visit the points in the order they were added
*
* @param visit Called for each point, until it returns non-zero
* @throws std::system_error foo_point_visit() returns: The number of points visited,
* -EINVAL when 'visit' is NULL
*/
template <class F>
std::size_t
point_visit(F &&visit);
/**
* Visit the points with a visitor; as foo_point_visit()
*
* @param visitor The callback and its argument
* @throws std::system_error foo_point_visit_with() returns: The number of points visited,
* -EINVAL when the callback is NULL
*/
std::size_t
point_visit_with(const struct ::foo_visitor &visitor);
/**
* Submit a count of the points, completed by foo_count_complete()
*
* @param done Called once, by foo_count_complete(), with the number of points
* @throws std::system_error foo_count_submit() returns: 0, -EBUSY when a count is
* submitted and not yet completed
*/
template <class F>
void
count_submit(F &&done);
/**
* Complete the count submitted, if any, calling its callback
*
* @return The number of counts completed, 0 or 1
*/
size_t
count_complete();
/**
* The status of the handle
*/
struct ::foo_status
status_get() const;
/**
* Close a handle
*/
void
close();
private:
struct deleter {
void
operator()(struct ::foo_handle *ptr) const noexcept
{
(void)::foo_close(ptr);
}
};
mutable std::vector<std::shared_ptr<void>> keep_;
std::unique_ptr<struct ::foo_handle, deleter> ptr_;
};
inline std::string_view
handle::name() const
{
auto ret = ::foo_name(ptr_.get());
return std::string_view(ret);
}
inline void
handle::point_add(const struct ::foo_point &point)
{
auto ret = ::foo_point_add(ptr_.get(), &point);
if (ret < 0) {
throw detail::failure(static_cast<int>(-ret), "foo_point_add");
}
}
inline std::size_t
handle::point_add_many(std::span<const struct ::foo_point> points)
{
auto ret = ::foo_point_add_many(ptr_.get(), points.data(), points.size());
if (ret < 0) {
throw detail::failure(static_cast<int>(-ret), "foo_point_add_many");
}
return static_cast<std::size_t>(ret);
}
inline size_t
handle::point_count() const
{
auto ret = ::foo_point_count(ptr_.get());
return ret;
}
inline std::optional<struct ::foo_point>
handle::point_get(size_t idx) const
{
auto ret = ::foo_point_get(ptr_.get(), idx);
return ret ? std::optional<struct ::foo_point>(*ret) : std::nullopt;
}
inline std::span<struct ::foo_point>
handle::point_copy(std::span<struct ::foo_point> points) const
{
auto ret = ::foo_point_copy(ptr_.get(), points.data(), static_cast<size_t>(points.size()));
const auto points_len_ = std::min(static_cast<std::size_t>(ret), points.size());
if (ret < 0) {
throw detail::failure(static_cast<int>(-ret), "foo_point_copy");
}
return points.first(points_len_);
}
template <class F>
inline void
handle::point_watch(F &&added)
{
using added_t = std::decay_t<F>;
auto added_tramp = [](void *a0, const struct ::foo_point *a1) noexcept -> void {
(*static_cast<added_t *>(a0))(*a1);
};
::foo_added_cb added_cb = added_tramp;
auto added_held = std::make_shared<added_t>(std::forward<F>(added));
void *added_ctx = added_held.get();
keep_.push_back(added_held);
auto ret = ::foo_point_watch(ptr_.get(), added_cb, added_ctx);
if (ret < 0) {
auto call_error = detail::failure(static_cast<int>(-ret), "foo_point_watch");
keep_.pop_back();
throw call_error;
}
}
template <class F>
inline std::size_t
handle::point_visit(F &&visit)
{
std::conditional_t<std::is_function_v<std::remove_reference_t<F>>, std::decay_t<F>,
std::remove_reference_t<F> &>
visit_ref = visit;
using visit_fn = std::remove_reference_t<decltype(visit_ref)>;
auto visit_tramp = [](void *a0, const struct ::foo_point *a1) noexcept -> int {
return (*static_cast<visit_fn *>(a0))(*a1);
};
::foo_visit_cb visit_cb = visit_tramp;
void *visit_ctx = const_cast<void *>(static_cast<const void *>(std::addressof(visit_ref)));
auto ret = ::foo_point_visit(ptr_.get(), visit_cb, visit_ctx);
if (ret < 0) {
throw detail::failure(static_cast<int>(-ret), "foo_point_visit");
}
return static_cast<std::size_t>(ret);
}
inline std::size_t
handle::point_visit_with(const struct ::foo_visitor &visitor)
{
auto ret = ::foo_point_visit_with(ptr_.get(), &visitor);
if (ret < 0) {
throw detail::failure(static_cast<int>(-ret), "foo_point_visit_with");
}
return static_cast<std::size_t>(ret);
}
template <class F>
inline void
handle::count_submit(F &&done)
{
using done_t = std::decay_t<F>;
auto done_tramp = [](void *a0, size_t a1) noexcept -> void {
std::unique_ptr<done_t> f(static_cast<done_t *>(a0));
(*f)(a1);
};
::foo_done_cb done_cb = done_tramp;
void *done_ctx = new done_t(std::forward<F>(done));
auto ret = ::foo_count_submit(ptr_.get(), done_cb, done_ctx);
if (ret < 0) {
auto call_error = detail::failure(static_cast<int>(-ret), "foo_count_submit");
delete static_cast<done_t *>(done_ctx);
throw call_error;
}
}
inline size_t
handle::count_complete()
{
auto ret = ::foo_count_complete(ptr_.get());
return ret;
}
inline struct ::foo_status
handle::status_get() const
{
struct ::foo_status status_out{};
::foo_status_get(ptr_.get(), &status_out);
return status_out;
}
inline void
handle::close()
{
ptr_.reset();
keep_.clear();
}
/**
* Open a handle holding no points
*
* @note In this signature: 'handle' is returned.
* @param name Name of the handle, copied
* @throws std::system_error foo_open() returns: 0 on success, -EINVAL when 'name' or 'handle' is
* NULL, -ENOMEM when memory ran out
*/
inline handle
open(const std::string &name)
{
struct ::foo_handle *handle__out = nullptr;
auto ret = ::foo_open(name.c_str(), &handle__out);
if (ret < 0) {
throw detail::failure(static_cast<int>(-ret), "foo_open");
}
return handle(handle__out);
}
/**
* The version of the library
*
* @return The version, as "MAJOR.MINOR.PATCH"
*/
inline std::string_view
version()
{
auto ret = ::foo_version();
return std::string_view(ret);
}
/**
* Why the call that failed last on the calling thread failed
*
* A function that returns a negative errno keeps a message saying what the errno does not, such as
* the name of the handle that is full. The meta names this function as 'errmsg', so that the error
* of a binding holds the message.
*
* @return The message, owned by the library and valid until the next call fails on the calling
* thread; "" when no call has failed
*/
inline std::string_view
errmsg()
{
auto ret = ::foo_errmsg();
return std::string_view(ret);
}
/**
* Fill in a value of every kind of scalar, the limits of the fixed-width ones
*/
inline struct ::foo_scalars
scalars_fill()
{
struct ::foo_scalars scalars_out{};
::foo_scalars_fill(&scalars_out);
return scalars_out;
}
/**
* Fill in a figure of the given shape
*
* @param shape One of enum foo_shape
* @throws std::system_error foo_figure_fill() returns: 0 on success, -EINVAL when 'shape' is none
* of enum foo_shape
*/
inline struct ::foo_figure
figure_fill(uint32_t shape)
{
struct ::foo_figure figure_out{};
auto ret = ::foo_figure_fill(&figure_out, shape);
if (ret < 0) {
throw detail::failure(static_cast<int>(-ret), "foo_figure_fill");
}
return figure_out;
}
/**
* Set a value, clearing the bits it does not use
*
* @param i What to set it to
*/
inline union ::foo_value
value_set(int32_t i)
{
union ::foo_value value_out{};
::foo_value_set(&value_out, i);
return value_out;
}
/**
* Fill in a packed record
*/
inline struct ::foo_wire
wire_fill()
{
struct ::foo_wire wire_out{};
::foo_wire_fill(&wire_out);
return wire_out;
}
} // namespace foo
inline std::ostream &
operator<<(std::ostream &os, const struct ::foo_point &obj)
{
return os << foo::yaml(obj);
}
inline std::ostream &
operator<<(std::ostream &os, const struct ::foo_visitor &obj)
{
return os << foo::yaml(obj);
}
inline std::ostream &
operator<<(std::ostream &os, const struct ::foo_scalars &obj)
{
return os << foo::yaml(obj);
}
inline std::ostream &
operator<<(std::ostream &os, const struct ::foo_figure &obj)
{
return os << foo::yaml(obj);
}
inline std::ostream &
operator<<(std::ostream &os, const union ::foo_value &obj)
{
return os << foo::yaml(obj);
}
inline std::ostream &
operator<<(std::ostream &os, const struct ::foo_wire &obj)
{
return os << foo::yaml(obj);
}
inline std::ostream &
operator<<(std::ostream &os, const struct ::foo_status &obj)
{
return os << foo::yaml(obj);
}
#endif /* FOO_HPP */
Implementation¶
The C++ target: the idiomatic C++ layer of the API, over the C API, which is its raw layer, as C++ includes C headers
Emits, into <output>/cpp:
<prefix>.hpp, the layer, header-only<prefix>_check.cpp, the check program
for the C++ standard that the option std names, c++20 or c++17,
and the C API into <output>/capi, as the C target does, which the layer
includes; the layer has printers of its own, printing what the C API’s
pretty-printers print.
- class yace.targets.cpp.target.CPP(output: Path, include=None, options=None)¶
- yace.targets.cpp.target.cxxflags(std: str) list¶
The flags that the check is built with, as the C++ standard ‘std’
The idiomatic C++ layer of the API, over the C API the C target generates,
which is its raw layer, as C++ includes C headers: handles as move-only RAII
classes, errors as std::system_error, what is written through [out]
pointers returned, std::span for arrays, std::string_view for strings,
std::optional where NULL is allowed, and any callable for callbacks. See
yace.targets.idiom for what is derived from where.
The layer is generated for C++20 or for C++17. C++17 has no std::span, so
the layer for C++17 declares SPAN, the class detail::span, and
names it where the layer for C++20 names std::span.
Everything is in a namespace named for the prefix, its names without it, so
nothing clashes with the macros of the C API’s #define constants; where the
C API has a declaration named as the prefix, the namespace is the prefix and
_cpp.
- class yace.targets.cpp.cppgen.Printer(model)¶
The C++ statements printing a value of a type into the std::string ‘out’, as the pretty-printers of the C API print it
- named(record) list¶
The fields printed: those with a name, not padding bit-fields
- parts(field, expr: str, put: str, quote: str) List[str]¶
The value of a field of bits and of each of its parts, as a mapping on one line, with the keys quoted by ‘quote’, the values put by ‘put’
- value(typ, expr: str, depth: int = 0, packed: bool = False) List[str]¶
Statements printing ‘expr’; of a field of a ‘packed’ record, a record is printed through a copy, as a reference to it may not be aligned
- yace.targets.cpp.cppgen.comment(doc, indent: str = '', errors: str = '') List[str]¶
‘doc’ as a Doxygen comment; ‘errors’ says when it throws
- yace.targets.cpp.cppgen.copied(expr: str, local: str, call: str) List[str]¶
The statement ‘call’, formatted with a local, on a copy of ‘expr’ in it
- yace.targets.cpp.cppgen.failure(errmsg: str) List[str]¶
detail::failure, the exception of a failed call where the meta names ‘errmsg’, the function of the library that returns its message. It is a std::system_error, so its code() is the errno and a handler of std::system_error catches it, and it overrides what(), since std::system_error appends the text of the errno to what it is given, and the message of a library often ends in that text already
- yace.targets.cpp.cppgen.ident(name: str) str¶
‘name’ usable in C++, and other than ‘ret’, the local of the result