1#ifndef DIPLOMAT_RUNTIME_CPP_H
2#define DIPLOMAT_RUNTIME_CPP_H
10#if __cplusplus >= 202002L
14#include "diplomat_runtime.h"
18extern "C" inline void Flush(capi::DiplomatWriteable* w) {
19 std::string*
string =
reinterpret_cast<std::string*
>(w->context);
20 string->resize(w->len);
23extern "C" inline bool Grow(capi::DiplomatWriteable* w, uintptr_t requested) {
24 std::string*
string =
reinterpret_cast<std::string*
>(w->context);
25 string->resize(requested);
26 w->cap =
string->length();
27 w->buf = &(*string)[0];
32 capi::DiplomatWriteable w;
35 w.len =
string.length();
38 w.cap =
string.length();
44template<
typename T>
struct WriteableTrait {
49template<>
struct WriteableTrait<std::string> {
50 static inline capi::DiplomatWriteable Construct(std::string& t) {
55template<
class T>
struct Ok {
60 template<typename X = T, typename = typename std::enable_if<std::is_trivially_copyable<X>::value>::type>
63 Ok(
Ok&&) noexcept = default;
65 Ok& operator=(const
Ok&) = default;
66 Ok& operator=(
Ok&&) noexcept = default;
69template<class T> struct
Err {
74 template<typename X = T, typename = typename std::enable_if<std::is_trivially_copyable<X>::value>::type>
79 Err& operator=(const
Err&) = default;
80 Err& operator=(
Err&&) noexcept = default;
83template<class T, class E>
86 std::variant<Ok<T>,
Err<E>> val;
97 return std::holds_alternative<Ok<T>>(this->val);
100 return std::holds_alternative<Err<E>>(this->val);
103 std::optional<T>
ok() && {
104 if (!this->
is_ok()) {
107 return std::make_optional(std::move(std::get<
Ok<T>>(std::move(this->val)).
inner));
109 std::optional<E>
err() && {
113 return std::make_optional(std::move(std::get<
Err<E>>(std::move(this->val)).
inner));
117 this->val =
Ok<T>(std::move(t));
121 this->val =
Err<E>(std::move(e));
124 template<
typename T2>
136#if __cplusplus >= 202002L
138template<
class T>
using span = std::span<T>;
150 constexpr span(std::array<
typename std::remove_const<T>::type, N>& arr)
151 : data_(const_cast<T*>(arr.
data())), size_(N) {}
152 constexpr T*
data() const noexcept {
155 constexpr size_t size() const noexcept {
result(Err< E > &&v)
Definition diplomat_runtime.hpp:89
result(const result &)=default
result & operator=(const result &)=default
result & operator=(result &&) noexcept=default
bool is_err() const
Definition diplomat_runtime.hpp:99
result(Ok< T > &&v)
Definition diplomat_runtime.hpp:88
std::optional< T > ok() &&
Definition diplomat_runtime.hpp:103
std::optional< E > err() &&
Definition diplomat_runtime.hpp:109
void set_ok(T &&t)
Definition diplomat_runtime.hpp:116
bool is_ok() const
Definition diplomat_runtime.hpp:96
result< T2, E > replace_ok(T2 &&t)
Definition diplomat_runtime.hpp:125
void set_err(E &&e)
Definition diplomat_runtime.hpp:120
constexpr T * data() const noexcept
Definition diplomat_runtime.hpp:152
constexpr size_t size() const noexcept
Definition diplomat_runtime.hpp:155
constexpr span(std::array< typename std::remove_const< T >::type, N > &arr)
Definition diplomat_runtime.hpp:150
constexpr span(T *data, size_t size)
Definition diplomat_runtime.hpp:147
Definition diplomat_runtime.hpp:16
void Flush(capi::DiplomatWriteable *w)
Definition diplomat_runtime.hpp:18
bool Grow(capi::DiplomatWriteable *w, uintptr_t requested)
Definition diplomat_runtime.hpp:23
capi::DiplomatWriteable WriteableFromString(std::string &string)
Definition diplomat_runtime.hpp:31
Definition diplomat_runtime.hpp:69
Err(T i)
Definition diplomat_runtime.hpp:75
T inner
Definition diplomat_runtime.hpp:70
Err(T &&i)
Definition diplomat_runtime.hpp:71
Err(Err &&) noexcept=default
Definition diplomat_runtime.hpp:55
Ok(T i)
Definition diplomat_runtime.hpp:61
Ok(T &&i)
Definition diplomat_runtime.hpp:57
T inner
Definition diplomat_runtime.hpp:56
Ok(Ok &&) noexcept=default