1#ifndef DIPLOMAT_RUNTIME_CPP_H
2#define DIPLOMAT_RUNTIME_CPP_H
9#if __cplusplus >= 202002L
20static_assert(
sizeof(char) ==
sizeof(uint8_t),
"your architecture's `char` is not 8 bits");
21static_assert(
sizeof(char16_t) ==
sizeof(uint16_t),
"your architecture's `char16_t` is not 16 bits");
22static_assert(
sizeof(char32_t) ==
sizeof(uint32_t),
"your architecture's `char32_t` is not 32 bits");
24typedef struct DiplomatWrite {
30 void (*flush)(
struct DiplomatWrite*);
31 bool (*grow)(
struct DiplomatWrite*, size_t);
34bool diplomat_is_str(
const char* buf,
size_t len);
36#define MAKE_SLICES(name, c_ty) \
37 typedef struct Diplomat##name##View { \
40 } Diplomat##name##View; \
41 typedef struct Diplomat##name##ViewMut { \
44 } Diplomat##name##ViewMut; \
45 typedef struct Diplomat##name##Array { \
48 } Diplomat##name##Array;
50#define MAKE_SLICES_AND_OPTIONS(name, c_ty) \
51 MAKE_SLICES(name, c_ty) \
52 typedef struct Option##name {union { c_ty ok; }; bool is_ok; } Option##name;
76extern "C" inline void _flush(capi::DiplomatWrite* w) {
77 std::string*
string =
reinterpret_cast<std::string*
>(w->context);
78 string->resize(w->len);
81extern "C" inline bool _grow(capi::DiplomatWrite* w, uintptr_t requested) {
82 std::string*
string =
reinterpret_cast<std::string*
>(w->context);
83 string->resize(requested);
84 w->cap =
string->length();
85 w->buf = &(*string)[0];
90 capi::DiplomatWrite w;
93 w.len =
string.length();
94 w.cap =
string.length();
96 w.grow_failed =
false;
102template<
class T>
struct Ok {
107 template<typename X = T, typename = typename std::enable_if<std::is_trivially_copyable<X>::value>::type>
112 Ok& operator=(const
Ok&) = default;
113 Ok& operator=(
Ok&&) noexcept = default;
116template<class T> struct
Err {
121 template<typename X = T, typename = typename std::enable_if<std::is_trivially_copyable<X>::value>::type>
127 Err& operator=(
Err&&) noexcept = default;
130template<class T, class E>
133 std::variant<Ok<T>,
Err<E>> val;
144 return std::holds_alternative<Ok<T>>(this->val);
147 return std::holds_alternative<Err<E>>(this->val);
150 std::optional<T>
ok() && {
151 if (!this->is_ok()) {
154 return std::make_optional(std::move(std::get<
Ok<T>>(std::move(this->val)).
inner));
156 std::optional<E>
err() && {
157 if (!this->is_err()) {
160 return std::make_optional(std::move(std::get<
Err<E>>(std::move(this->val)).
inner));
164 this->val =
Ok<T>(std::move(t));
168 this->val =
Err<E>(std::move(e));
171 template<
typename T2>
173 if (this->is_err()) {
184#if __cplusplus >= 202002L
186template<
class T>
using span = std::span<T>;
195 constexpr span(T* data,
size_t size)
196 : data_(data), size_(size) {}
198 constexpr span(std::array<
typename std::remove_const<T>::type, N>& arr)
199 : data_(const_cast<T*>(arr.data())), size_(N) {}
200 constexpr T*
data() const noexcept {
203 constexpr size_t size() const noexcept {
Definition: diplomat_runtime.hpp:181
Definition: diplomat_runtime.hpp:131
result(Err< E > &&v)
Definition: diplomat_runtime.hpp:136
result(const result &)=default
result & operator=(const result &)=default
result & operator=(result &&) noexcept=default
bool is_err() const
Definition: diplomat_runtime.hpp:146
result(Ok< T > &&v)
Definition: diplomat_runtime.hpp:135
std::optional< T > ok() &&
Definition: diplomat_runtime.hpp:150
std::optional< E > err() &&
Definition: diplomat_runtime.hpp:156
void set_ok(T &&t)
Definition: diplomat_runtime.hpp:163
result< T2, E > replace_ok(T2 &&t)
Definition: diplomat_runtime.hpp:172
void set_err(E &&e)
Definition: diplomat_runtime.hpp:167
Definition: diplomat_runtime.hpp:192
constexpr T * data() const noexcept
Definition: diplomat_runtime.hpp:200
constexpr size_t size() const noexcept
Definition: diplomat_runtime.hpp:203
constexpr span(std::array< typename std::remove_const< T >::type, N > &arr)
Definition: diplomat_runtime.hpp:198
constexpr span(T *data, size_t size)
Definition: diplomat_runtime.hpp:195
#define MAKE_SLICES(name, c_ty)
Definition: diplomat_runtime.hpp:36
#define MAKE_SLICES_AND_OPTIONS(name, c_ty)
Definition: diplomat_runtime.hpp:50
Definition: diplomat_runtime.hpp:15
bool _grow(capi::DiplomatWrite *w, uintptr_t requested)
Definition: diplomat_runtime.hpp:81
void _flush(capi::DiplomatWrite *w)
Definition: diplomat_runtime.hpp:76
capi::DiplomatWrite WriteFromString(std::string &string)
Definition: diplomat_runtime.hpp:89
Definition: diplomat_runtime.hpp:116
Err(T i)
Definition: diplomat_runtime.hpp:122
T inner
Definition: diplomat_runtime.hpp:117
Err(T &&i)
Definition: diplomat_runtime.hpp:118
Err(Err &&) noexcept=default
Definition: diplomat_runtime.hpp:102
Ok(T i)
Definition: diplomat_runtime.hpp:108
Ok(T &&i)
Definition: diplomat_runtime.hpp:104
T inner
Definition: diplomat_runtime.hpp:103
Ok(Ok &&) noexcept=default