48#if !defined(SOFAB_CPP_HAVE_HOSTED)
49# define SOFAB_CPP_HAVE_HOSTED __STDC_HOSTED__
52#if SOFAB_CPP_HAVE_HOSTED
83#if defined(SOFAB_DISABLE_FIXLEN_SUPPORT)
84# error "sofab C++ wrapper requires FIXLEN support (strings, blobs, floats). Do not define SOFAB_DISABLE_FIXLEN_SUPPORT when building the C++ API; use the C API directly for fixlen-less builds."
86#if defined(SOFAB_DISABLE_SEQUENCE_SUPPORT)
87# error "sofab C++ wrapper requires SEQUENCE support (nested messages, variable-length array reads). Do not define SOFAB_DISABLE_SEQUENCE_SUPPORT when building the C++ API; use the C API directly for sequence-less builds."
89#if defined(SOFAB_DISABLE_LAZY_SEQ_SUPPORT)
90# error "sofab C++ wrapper requires LAZY_SEQ support (an all-default nested message is omitted, not framed empty — MESSAGE_SPEC §2). Do not define SOFAB_DISABLE_LAZY_SEQ_SUPPORT when building the C++ API; it is a footprint switch for pure-C consumers, which encode through sofab_object_encode()."
95#if defined(SOFAB_DISABLE_FP64_SUPPORT)
96# define SOFAB_CPP_HAVE_FP64 0
98# define SOFAB_CPP_HAVE_FP64 1
102#if defined(SOFAB_DISABLE_INT64_SUPPORT)
103# define SOFAB_CPP_HAVE_INT64 0
105# define SOFAB_CPP_HAVE_INT64 1
109#if defined(SOFAB_DISABLE_ARRAY_SUPPORT)
110# define SOFAB_CPP_HAVE_ARRAY 0
112# define SOFAB_CPP_HAVE_ARRAY 1
124#if defined(SOFAB_DISABLE_ENCODE_BOUNDS)
125# define SOFAB_CPP_HAVE_ENCODE_BOUNDS 0
127# define SOFAB_CPP_HAVE_ENCODE_BOUNDS 1
159 template <
typename E,
bool = std::is_enum_v<E>>
162 template <
typename E>
165 template <
typename E>
271 while (
n > 0 && (
static_cast<unsigned char>(
sv[
n]) & 0xC0u) == 0x80u)
308 template <std::
size_t N>
311 std::array<char, N + 1> buf_{};
312 std::size_t len_ = 0;
329 assign(
s ? std::string_view{
s} : std::string_view{});
341#if SOFAB_CPP_HAVE_HOSTED
358 assign(
s ? std::string_view{
s} : std::string_view{});
369#if SOFAB_CPP_HAVE_HOSTED
394 for (std::size_t
i = 0;
i < len_; ++
i)
416 len_ =
n >
N ?
N :
n;
441 const char &
operator[](std::size_t
i)
const noexcept {
return buf_[
i]; }
462 return std::string_view{buf_.data(), len_};
476#if SOFAB_CPP_HAVE_HOSTED
479 std::string
str()
const
481 return std::string{buf_.data(), len_};
501 template <std::
size_t N>
504 template <
typename T>
539 template <std::
size_t N>
542 std::array<std::uint8_t, N> buf_{};
543 std::size_t len_ = 0;
579 for (std::uint8_t
b :
init)
630 const std::uint8_t &
operator[](std::size_t
i)
const noexcept {
return buf_[
i]; }
648 for (std::size_t
i = 0;
i < len_; ++
i)
650 if (buf_[
i] !=
o.buf_[
i])
665 template <std::
size_t N>
668 template <
typename T>
701 template <
typename T, std::
size_t N>
704 std::array<T, N> buf_{};
705 std::size_t len_ = 0;
793 for (std::size_t
i =
n;
i < len_; ++
i)
797 for (std::size_t
i = len_;
i <
n; ++
i)
813 std::size_t
i = len_ <
N ? len_++ :
N - 1;
828 void push_back(
T &&
v)
noexcept {
if (len_ <
N) buf_[len_++] =
static_cast<T &&
>(
v); }
861 for (std::size_t
i = 0;
i < len_; ++
i)
863 if (!(buf_[
i] ==
o.buf_[
i]))
879 class OStreamMessage;
901#if SOFAB_CPP_HAVE_HOSTED
931 void *usrptr)
noexcept
975 template <
typename T>
983 auto res = ostream_.
write(
id, value);
999 template <
typename T>
1024 template <
typename T>
1119 return !(*
this ==
e);
1194 template <
typename T>
1199 if constexpr (std::is_integral_v<T> && !std::is_same_v<T, bool>)
1201#if !SOFAB_CPP_HAVE_INT64
1202 static_assert(
sizeof(
T) <= 4,
1203 "64-bit integer fields require INT64 support, disabled via "
1204 "SOFAB_DISABLE_INT64_SUPPORT");
1206 if constexpr (std::is_unsigned_v<T>)
1217 else if constexpr (std::is_same_v<T, bool>)
1221 else if constexpr (std::is_same_v<T, float>)
1225 else if constexpr (std::is_same_v<T, double>)
1227#if SOFAB_CPP_HAVE_FP64
1231 "double (FP64) fields require FP64 support, disabled via "
1232 "SOFAB_DISABLE_FP64_SUPPORT");
1235 else if constexpr (std::is_convertible_v<T, std::string_view>)
1247 std::string_view
sv{value};
1270 else if constexpr (std::is_base_of_v<OStreamMessage, T>)
1292 ret = value.serialize(
static_cast<OStreamImpl&
>(*
this)).rawCode();
1302 typename T::value_type;
1303 std::span{ std::declval<const T&>() };
1306#if SOFAB_CPP_HAVE_ARRAY
1307 using Elem =
typename T::value_type;
1308 std::span<const Elem>
span{value};
1329 if constexpr ((std::is_integral_v<Elem> || std::is_enum_v<Elem>) &&
1330 !std::is_same_v<Elem, bool>)
1337#if !SOFAB_CPP_HAVE_INT64
1338 static_assert(
sizeof(
U) <= 4,
1339 "64-bit integer arrays require INT64 support, disabled "
1340 "via SOFAB_DISABLE_INT64_SUPPORT");
1342 if constexpr (std::is_unsigned_v<U>)
1359 else if constexpr (std::is_same_v<Elem, bool>)
1372 else if constexpr (std::is_same_v<Elem, float>)
1379 else if constexpr (std::is_same_v<Elem, double>)
1381#if SOFAB_CPP_HAVE_FP64
1388 "double (FP64) arrays require FP64 support, disabled "
1389 "via SOFAB_DISABLE_FP64_SUPPORT");
1395 "Unsupported span element type in OStream::write()");
1399 "array/span fields require ARRAY support, disabled via "
1400 "SOFAB_DISABLE_ARRAY_SUPPORT");
1406 "Unsupported type passed to OStream::write()");
1436 template <
typename T>
1439 static_assert(std::is_base_of_v<OStreamMessage, T>,
1440 "writeLazy() takes a nested message; plain write() covers every other type");
1444 ret = value.serialize(
static_cast<OStreamImpl&
>(*
this)).rawCode();
1474 template <
typename T>
1479 return write(
id, value);
1562 return Result{*
this,
ret};
1615#if SOFAB_CPP_HAVE_HOSTED
1627 class OStream :
public OStreamImpl
1630 std::shared_ptr<uint8_t[]> bufferOwner_;
1642 bufferOwner_ = std::make_shared<uint8_t[]>(
buflen);
1643 buffer_ = bufferOwner_.get();
1656 : bufferOwner_{buffer}
1658 buffer_ = bufferOwner_.get();
1673 : bufferOwner_{buffer}
1676 buffer_ = bufferOwner_.get();
1690 bufferOwner_ = buffer;
1691 buffer_ = bufferOwner_.get();
1701 return bufferOwner_;
1715 template <
size_t N,
size_t Offset = 0>
1718 static_assert(
N > 0,
"Buffer size N must be greater than zero");
1719 static_assert(
Offset <
N,
"Offset must be less than buffer size N");
1720 std::array<uint8_t, N> bufferOwner_ = {};
1726 buffer_ = bufferOwner_.data();
1736 buffer_ = bufferOwner_.data();
1789 class OStreamMessage;
1794 requires { { T::MAX_SIZE } -> std::convertible_to<std::size_t>; } &&
1795 std::is_same_v<
decltype(T::MAX_SIZE),
const std::size_t>;
1800 requires { { T::_maxSize } -> std::convertible_to<std::size_t>; } &&
1801 std::is_same_v<
decltype(T::_maxSize),
const std::size_t>;
1811 else {
return T::_maxSize; }
1833 std::derived_from<T, OStreamMessage> &&
1866 template <OutputMessage MessageType,
size_t N = max_size_v<MessageType>,
size_t Offset = 0>
1869 MessageType message_;
1897 auto result = message_.serialize(
static_cast<OStreamImpl&
>(*
this));
1913 template <OutputMessage MessageType,
size_t Offset = 0>
1926 template <
typename T>
1947 template <
typename C>
1949 requires { { C::capacity() } -> std::convertible_to<std::size_t>; };
1990 template <
typename C>
1993 template <HasFixedCapacity C>
2024 std::size_t value_ = 0;
2025 bool stated_ =
false;
2035 template <
typename T>
2036 requires std::is_signed_v<T>
2135#if SOFAB_SKIP_COUNTER
2136 if (
ctx_.skipped < 0xFF)
2165 return (
diff << 26) == 0
u;
2177 template <
typename T>
2185 if constexpr (
requires { value.set_len(size); })
2187 value.set_len(size);
2201 template <
typename T>
2209 if constexpr (
requires { value.set_len(size); })
2211 value.set_len(size);
2221 read(value.data(), value.size());
2226 template <
typename C>
2229 using Elem =
typename C::value_type;
2231 if constexpr (std::is_same_v<Elem, float>)
2235 else if constexpr (std::is_same_v<Elem, double>)
2239 else if constexpr (std::is_signed_v<Elem>)
2253 template <
typename C>
2259 return static_cast<long>(out.size());
2270 template <
typename C>
2278 if constexpr (
requires { out.resize(
wireCount); })
2331 return !(*
this ==
e);
2505 if (
room >= 0 &&
n >
static_cast<size_t>(
room))
2718 template <
typename T>
2721 static_assert(std::is_arithmetic_v<T>,
2722 "readMatch binds a scalar: an integer, bool, float or double");
2723 if constexpr (std::is_integral_v<T> && !std::is_same_v<T, bool>)
2725#if !SOFAB_CPP_HAVE_INT64
2726 static_assert(
sizeof(
T) <= 4,
2727 "64-bit integer fields require INT64 support, disabled "
2728 "via SOFAB_DISABLE_INT64_SUPPORT");
2731 reinterpret_cast<void*
>(&value),
sizeof(
T),
2732 std::is_unsigned_v<T>
2736 else if constexpr (std::is_same_v<T, bool>)
2742 else if constexpr (std::is_same_v<T, float>)
2750 static_assert(std::is_same_v<T, double>,
2751 "readMatch binds a scalar: an integer, bool, float or double");
2752#if SOFAB_CPP_HAVE_FP64
2758 "double (FP64) fields require FP64 support, disabled "
2759 "via SOFAB_DISABLE_FP64_SUPPORT");
2777 template <
typename T>
2780 if constexpr (!std::is_const_v<T>)
2782 if constexpr (std::is_integral_v<T> && !std::is_same_v<T, bool>)
2784#if !SOFAB_CPP_HAVE_INT64
2785 static_assert(
sizeof(
T) <= 4,
2786 "64-bit integer fields require INT64 support, disabled "
2787 "via SOFAB_DISABLE_INT64_SUPPORT");
2789 if constexpr (std::is_unsigned_v<T>)
2792 &
ctx_,
reinterpret_cast<void*
>(&value),
sizeof(
T),
2798 &
ctx_,
reinterpret_cast<void*
>(&value),
sizeof(
T),
2802 else if constexpr (std::is_same_v<T, bool>)
2806 else if constexpr (std::is_same_v<T, float>)
2810 else if constexpr (std::is_same_v<T, double>)
2812#if SOFAB_CPP_HAVE_FP64
2816 "double (FP64) fields require FP64 support, disabled "
2817 "via SOFAB_DISABLE_FP64_SUPPORT");
2820#if SOFAB_CPP_HAVE_HOSTED
2821 else if constexpr (std::is_same_v<T, std::string>)
2857 value.readNested_(&
ctx_,
this);
2861 typename T::value_type;
2862 std::span{ std::declval<const T&>() };
2864 !std::is_const_v<typename T::value_type>)
2866#if SOFAB_CPP_HAVE_ARRAY
2867 using Elem =
typename T::value_type;
2868 std::span<Elem>
span{value};
2870 if constexpr (std::is_integral_v<Elem> && !std::is_same_v<Elem, bool>)
2872#if !SOFAB_CPP_HAVE_INT64
2873 static_assert(
sizeof(Elem) <= 4,
2874 "64-bit integer arrays require INT64 support, "
2875 "disabled via SOFAB_DISABLE_INT64_SUPPORT");
2877 if constexpr (std::is_unsigned_v<Elem>)
2896 else if constexpr (std::is_same_v<Elem, bool>)
2910 else if constexpr (std::is_same_v<Elem, float>)
2917 else if constexpr (std::is_same_v<Elem, double>)
2919#if SOFAB_CPP_HAVE_FP64
2926 "double (FP64) arrays require FP64 support, "
2927 "disabled via SOFAB_DISABLE_FP64_SUPPORT");
2933 "Unsupported span element type in IStream::read()");
2937 "array/span fields require ARRAY support, disabled via "
2938 "SOFAB_DISABLE_ARRAY_SUPPORT");
2944 "Unsupported type passed to IStream::read()");
2950 "Cannot read into const variable in IStream::read()");
3046 template <
typename T>
3082 template <
typename Select,
3083 std::enable_if_t<std::is_invocable_v<Select &>,
int> = 0>
3086 using T = std::remove_reference_t<std::invoke_result_t<Select &>>;
3130 template <
typename T>
3166 template <
typename T>
3192 template <
typename Select,
3193 std::enable_if_t<std::is_invocable_v<Select &>,
int> = 0>
3196 using T = std::remove_reference_t<std::invoke_result_t<Select &>>;
3223 template <
typename T>
3265 template <
typename C>
3301 template <
typename Select,
3302 std::enable_if_t<std::is_invocable_v<Select &>,
int> = 0>
3305 using C = std::remove_reference_t<std::invoke_result_t<Select &>>;
3314 requires(
C &
c,
size_t n) {
c.resize(
n); })
3358 template <
typename C>
3388 template <
typename C,
typename Out>
3412 template <
typename C,
typename Select,
3413 std::enable_if_t<std::is_invocable_v<Select &>,
int> = 0>
3440#if SOFAB_SKIP_COUNTER
3461#if SOFAB_CPP_HAVE_HOSTED
3462 void read(std::vector<std::string> &out)
noexcept
3473 void read(std::vector<std::vector<uint8_t>> &out)
noexcept
3481#if SOFAB_CPP_HAVE_HOSTED
3486 auto *out =
static_cast<std::vector<std::string>*
>(usrptr);
3487 out->emplace_back(size,
'\0');
3500 auto *out =
static_cast<std::vector<std::vector<uint8_t>
>*>(usrptr);
3501 out->emplace_back(size);
3526#if SOFAB_CPP_HAVE_HOSTED
3536 static void field_callback_(
3570 template <InputMessage MessageType>
3572 template <InputMessage MessageType>
3581 Context context_{
nullptr,
nullptr};
3583 static void field_callback_(
3588 auto context =
static_cast<Context*
>(usrptr);
3598 context_ = Context{istream,
this};
3649 template <InputMessage MessageType>
3658 data_.context_ = IStreamMessage::Context{
this, &data_};
3718 template <InputMessage MessageType>
3731 data_->context_ = IStreamMessage::Context{
this, data_};
3774 template <
typename Container>
3782 if (
static_cast<size_t>(
id) >=
out->capacity())
3787 while (
out->size() <=
static_cast<size_t>(
id))
out->emplace_back();
3788 auto &
s = (*out)[
id];
3790 if (size)
is.read(
s);
3798 template <
typename Container>
3806 if (
static_cast<size_t>(
id) >=
out->capacity())
3811 while (
out->size() <=
static_cast<size_t>(
id))
out->emplace_back();
3812 auto &
b = (*out)[
id];
3814 if (size)
is.read(
b.data(),
b.size());
3853#if SOFAB_CPP_HAVE_HOSTED
3878 std::vector<std::string> *out =
nullptr;
3884 void deserialize(IStreamImpl &
is,
sofab_id_t id,
size_t size,
size_t)
noexcept override
3894 if (
seqRefuse(
is,
static_cast<size_t>(
id) + 1, cap, dynCap))
3902 while (out->size() <=
static_cast<size_t>(
id)) out->emplace_back();
3903 auto &
s = (*out)[
id];
3904 s.assign(size,
'\0');
3905 if (size)
is.read(
s);
3913 struct BlobSeq : IStreamMessage
3915 std::vector<std::vector<uint8_t>> *out =
nullptr;
3921 void deserialize(IStreamImpl &
is,
sofab_id_t id,
size_t size,
size_t)
noexcept override
3927 if (
seqRefuse(
is,
static_cast<size_t>(
id) + 1, cap, dynCap))
3935 while (out->size() <=
static_cast<size_t>(
id)) out->emplace_back();
3936 auto &
b = (*out)[
id];
3938 if (size)
is.read(
b.data(),
b.size());
3966 template <
typename C>
3967 std::span<const typename C::value_type>
trimTail(
const C &
a)
noexcept
3969 using Elem =
typename C::value_type;
3971 size_t n =
a.size();
3972 while (
n > 0 && std::memcmp(&
a[
n - 1], &
zero,
sizeof(Elem)) == 0) --
n;
3973 return std::span<const Elem>(
a.data(),
n);
A CORELIB_PLAN §6.2.1 receiver cap, exactly as the caller states it.
constexpr std::size_t value() const noexcept
The stated maximum. Meaningless unless stated.
constexpr DynCap(T)=delete
Deleted: a signed cap would let -1 re-enter as an unlimited mode.
constexpr bool stated() const noexcept
Whether the caller stated a number at all.
constexpr DynCap() noexcept=default
The unstated cap: missing, not unlimited.
Fixed-capacity, heap-free byte blob of up to N bytes.
bool empty() const noexcept
True if the blob is empty.
void set_len(std::size_t n) noexcept
Decode hook: set the logical length (clamped to N).
std::size_t size_type
Size type (mirrors std::vector).
std::size_t size() const noexcept
Number of bytes currently stored.
void clear() noexcept
Reset to an empty blob.
static constexpr std::size_t capacity() noexcept
Maximum number of bytes (the template parameter N).
std::uint8_t value_type
Element type (mirrors std::vector).
const std::uint8_t * end() const noexcept
Const iterator past the last byte.
bool operator!=(const FixedBytes &o) const noexcept
Negated operator==.
bool operator==(const FixedBytes &o) const noexcept
Content equality (same logical length and bytes).
const std::uint8_t * begin() const noexcept
Const iterator to the first byte.
FixedBytes & operator=(std::initializer_list< std::uint8_t > init) noexcept
Replace the contents from a brace-enclosed list (clamped to N; extra bytes are dropped).
const std::uint8_t & operator[](std::size_t i) const noexcept
Access the byte at i (no bounds checking).
void push_back(std::uint8_t b) noexcept
Append one byte (no-op once at capacity N).
static constexpr std::size_t max_size() noexcept
Alias of capacity.
std::uint8_t * end() noexcept
Iterator past the last byte.
FixedBytes() noexcept=default
Construct an empty blob.
const std::uint8_t * data() const noexcept
Const pointer to the byte buffer.
std::uint8_t & operator[](std::size_t i) noexcept
Access the byte at i (no bounds checking).
FixedBytes & assign(std::initializer_list< std::uint8_t > init) noexcept
Replace the contents from a brace-enclosed list (clamped to N; extra bytes are dropped).
std::uint8_t * begin() noexcept
Iterator to the first byte.
std::uint8_t * data() noexcept
Mutable pointer to the byte buffer (decode target).
Fixed-capacity, heap-free string of up to N characters.
char * end() noexcept
Iterator past the last character.
bool operator!=(std::string_view rhs) const noexcept
Inequality against any string view-like operand.
const char * end() const noexcept
Const iterator past the last character.
FixedString & operator=(const char *s) noexcept
Assign from a NUL-terminated C string (clamped to N, see assign).
static constexpr std::size_t max_size() noexcept
Alias of capacity.
void clear() noexcept
Reset to an empty string.
std::string_view view() const noexcept
Non-owning view over the current characters.
const char * data() const noexcept
Const pointer to the character buffer.
char & operator[](std::size_t i) noexcept
Access the character at i (no bounds checking).
const char * c_str() const noexcept
NUL-terminated view of the contents.
const char * begin() const noexcept
Const iterator to the first character.
char * begin() noexcept
Iterator to the first character.
FixedString & assign(std::string_view sv) noexcept
Replace the contents with sv, clamped to N bytes.
FixedString & operator=(std::string_view sv) noexcept
Assign from a string view (clamped to N, see assign).
const char & operator[](std::size_t i) const noexcept
Access the character at i (no bounds checking).
FixedString(std::string_view sv) noexcept
Construct from a string view (clamped to N, see assign).
bool empty() const noexcept
True if the string is empty.
static constexpr std::size_t capacity() noexcept
Maximum number of characters (the template parameter N).
char * data() noexcept
Mutable pointer to the character buffer (decode target).
std::size_t length() const noexcept
Alias of size.
std::size_t size() const noexcept
Number of characters currently stored.
char value_type
Character type (mirrors std::string).
bool operator==(std::string_view rhs) const noexcept
Equality against any string view-like operand.
std::size_t size_type
Size type (mirrors std::string).
void set_len(std::size_t n) noexcept
Decode hook: set the logical length and (re)place the trailing NUL.
FixedString() noexcept=default
Construct an empty string.
bool ok() const noexcept
True if the consumed bytes end exactly on a field boundary (a complete message so far)....
bool invalid() const noexcept
True if the bytes are malformed (Error::InvalidMessage). Deliberately false for limitExceeded(): a re...
bool operator==(Error e) const noexcept
Compare the held error code to e.
bool incomplete() const noexcept
True if the consumed bytes end mid-field or with an open sequence: a valid but partial decode (Error:...
bool operator!=(Error e) const noexcept
Negated operator==().
Error code() const noexcept
The Error result of the feed.
bool limitExceeded() const noexcept
True if a configured receiver limit was exceeded on a schema-unbounded field (Error::LimitExceeded,...
Base input stream: incrementally decodes fed bytes into bound targets.
void bindString_(T &value, size_t size) noexcept
void readSequence(C &collector, Select &&select) noexcept
readSequence into a destination that comes into existence only once the field matched: select is call...
void read(T &value) noexcept
Bind the current field to a typed destination, deducing the wire type from T.
void readBlob(T &value, size_t size, long maxlen=-1) noexcept
Bind a blob field, sizing the destination first.
void invalidate() noexcept
Reject the message in progress as malformed, from within a field callback (§6.3's first tier).
void readSequence(C &collector, Out &out) noexcept
Bind a wrapper-array field through a collector, clearing the destination first.
bool readMatch(T &value) noexcept
read for a scalar, reporting whether the field will be decoded into value.
IStreamImpl() noexcept=default
Wire wire() const noexcept
Wire type of the field currently being delivered.
void readStringCapped(T &value, size_t size, size_t dynCap) noexcept
readString for a schema-unbounded string, bounded by the receiver cap the caller supplies (§6....
IStreamImpl & operator=(const IStreamImpl &)=delete
Copy assignment is deleted (the context owns raw pointers).
sofab_istream_t ctx_
Underlying C input stream context.
void readArray(Select &&select, size_t wireCount=0, long cap=-1) noexcept
readArray into a destination that comes into existence only once the field matched: select is called ...
void readString(T &value, size_t size, long maxlen=-1) noexcept
Bind a string field, sizing the destination first.
Result feed(const uint8_t *buffer, size_t buflen) noexcept
Feed raw encoded bytes to the decoder.
bool refuseUnbounded(long schemaBound, long room) noexcept
Refuse a read that was given no ceiling of any kind.
void refuseArgument() noexcept
Reject the message because the destination this caller handed over cannot hold the value (§6....
void latchRefusal_(sofab_ret_t category) noexcept
Fix fixType() const noexcept
Fixlen sub-type of the field currently being delivered.
bool arrayTagOk_() noexcept
sofab_istream_decoder_t arrayDecoder_
bool refuseBound(size_t n, long schemaBound, DynCap dynCap) noexcept
The two message-side refusals of §6.3 in one call, in the order §6.2.1 fixes: the schema first,...
void readString(Select &&select, size_t size, long maxlen=-1) noexcept
readString into a destination that comes into existence only once the field matched: select is called...
static long arrayRoom_(C &out, size_t wireCount) noexcept
IStreamImpl(const IStreamImpl &)=delete
Copy construction is deleted (the context owns raw pointers).
bool delivered(Wire w, Fix f) const noexcept
Whether the field currently being delivered has wire type w and fixlen subtype f.
void readBlob(Select &&select, size_t size, long maxlen=-1) noexcept
readBlob into a destination that comes into existence only once the field matched – the blob counterp...
void bindArray_(C &out, size_t wireCount, long room) noexcept
void noteSkip_() noexcept
bool refuse(size_t n, long room) noexcept
Refuse an announced length or element count the caller's own destination cannot hold.
void bindBlob_(T &value, size_t size) noexcept
void readBlobCapped(T &value, size_t size, size_t dynCap) noexcept
readBlob for a schema-unbounded blob, bounded by the receiver cap the caller supplies (§6....
size_t read(void *dst, size_t maxlen) noexcept
Decode a blob field into a caller-owned, address-stable buffer.
void exceedLimit() noexcept
Reject the message because a configured receiver limit was exceeded (§6.3's second tier,...
void resetRefusal_() noexcept
Clear a latched refusal, for a subclass that re-initialises the stream itself.
bool delivered(Wire w) const noexcept
Whether the field currently being delivered has wire type w.
bool refuseCap(size_t n, size_t dynCap) noexcept
§6.3's second tier: the receiver cap on a schema-unbounded field (§6.2.1).
void readArrayCapped(C &out, size_t wireCount, size_t dynCap) noexcept
readArray for a schema-unbounded array, bounded by the receiver cap the caller supplies (§6....
bool refuseSchema(size_t n, long schemaBound) noexcept
§6.3's first tier: the bound the schema declares.
bool bindMatch_(void *var, size_t len, uint8_t opt) noexcept
IStreamImpl(IStreamImpl &&) noexcept=default
Move construction (transfers the underlying context).
void readArray(C &out, size_t wireCount=0, long cap=-1) noexcept
Bind a native scalar array, preparing the destination first.
Input stream driven by a user-supplied per-field callback.
void(*)(sofab::id _id, size_t _size, size_t _count) fieldCallback
Per-field callback signature: (field id, value size, element count).
IStreamInline(fieldCallback callback) noexcept
Construct with the per-field callback.
Decoder that decodes into a message instance the CALLER owns.
IStreamInto(MessageType &message) noexcept
Bind message to this stream.
Base class for decodable message objects.
virtual void deserialize(sofab::IStreamImpl &_istream, sofab::id _id, size_t _size, size_t _count) noexcept=0
Dispatch one decoded field to the appropriate read.
Self-contained decoder that owns a message instance.
MessageType & operator*() noexcept
Reference to the owned message.
IStreamObject() noexcept
Construct and bind the owned message to this stream.
MessageType & operator->() noexcept
Member-access to the owned message (e.g. obj->field).
const MessageType & operator->() const noexcept
Const member-access to the owned message.
const MessageType & operator*() const noexcept
Const reference to the owned message.
Fixed-capacity, heap-free sequence of up to N elements of type T.
void push_back(T &&v) noexcept
Append v by move; dropped once at capacity N (see above).
InlineVector & operator=(std::initializer_list< T > init) noexcept
Replace the contents from a brace-enclosed list (clamped to N; extra elements are dropped).
static constexpr std::size_t capacity() noexcept
Maximum number of elements (the template parameter N).
std::size_t size_type
Size type (mirrors std::vector).
void push_back(const T &v) noexcept
Append a copy of v; once at capacity N, v is dropped.
T & operator[](std::size_t i) noexcept
Access the element at i (no bounds checking).
const T * begin() const noexcept
Const iterator to the first element.
bool empty() const noexcept
True if the sequence is empty.
T value_type
Element type (mirrors std::vector).
T * begin() noexcept
Iterator to the first element.
const T & back() const noexcept
Const reference to the last element.
void reserve(std::size_t) noexcept
No-op (inline storage never reallocates); present for API parity.
bool operator==(const InlineVector &o) const noexcept
Content equality (same logical length and elements).
void resize(std::size_t n) noexcept
Set the logical length to n, value-initializing what changes.
InlineVector() noexcept=default
Construct an empty sequence.
T & back() noexcept
Reference to the last element.
static constexpr std::size_t max_size() noexcept
Alias of capacity.
void clear() noexcept
Reset to an empty sequence (logical length only).
const T * data() const noexcept
Const pointer to the underlying storage.
const T & operator[](std::size_t i) const noexcept
Access the element at i (no bounds checking).
bool operator!=(const InlineVector &o) const noexcept
Negated operator==.
const T * end() const noexcept
Const iterator past the last element.
T & emplace_back() noexcept
Append a default-constructed element and return a reference to it.
T * end() noexcept
Iterator past the last element.
T * data() noexcept
Mutable pointer to the underlying storage.
std::size_t size() const noexcept
Number of elements currently stored.
InlineVector & assign(std::initializer_list< T > init) noexcept
Replace the contents from a brace-enclosed list (clamped to N; extra elements are dropped).
Outcome of an encode operation, supporting fluent chaining.
Result sequenceEndKeep() noexcept
Chained frame-keeping sequence-end (no-op if a prior call failed).
Result writeIf(sofab_id_t id, const T &value, bool condition) noexcept
Chained conditional write (no-op if a prior call failed).
Result sequenceBeginLazy(sofab_id_t id) noexcept
Chained lazy sequence-begin (no-op if a prior call failed).
Result write(sofab_id_t id, const T &value) noexcept
Chained write of a field (no-op if a prior call failed).
bool operator==(Error e) const noexcept
Compare the held error code to e.
Result sequenceEnd() noexcept
Chained sequence-end marker (no-op if a prior call failed).
Result writeLazy(sofab_id_t id, const T &value) noexcept
Chained nested-message field write that is omitted when the message is all-default (no-op if a prior ...
bool operator!=(Error e) const noexcept
Negated operator==().
Error code() const noexcept
The first Error encountered in the chain.
bool ok() const noexcept
True if the operation (and the chain so far) succeeded.
Base output stream: encodes fields into a caller-provided buffer.
OStreamImpl() noexcept=default
Result sequenceBeginLazy(sofab_id_t id) noexcept
Open a nested sequence whose header is held back until it turns out to have content.
Error error() const noexcept
The first failure this stream saw, or Error::None.
Result sequenceEnd() noexcept
Close the current nested sequence, letting it vanish if it got no content (MESSAGE_SPEC §2).
sofab_ostream_t ctx_
Underlying C output stream context.
void onFlushCallback(size_t len) noexcept
Invoke the user flush callback (if any) with len buffered bytes.
Result rejectArgument() noexcept
Refuse the value being encoded: latch Error::InvalidArgument.
Result write(sofab_id_t id, const void *value, int32_t size) noexcept
Encode a raw binary blob field.
flushCallback flushCallback_
Optional user flush callback.
const uint8_t * data() const noexcept
Pointer to the start of the active encode buffer.
OStreamImpl(OStreamImpl &&) noexcept=default
Move construction (transfers the underlying context).
size_t flush() noexcept
Flush buffered bytes through the flush callback (if any).
size_t bytesUsed() noexcept
Number of bytes written to the active buffer since the last flush.
uint8_t * buffer_
Pointer to the active encode buffer.
OStreamImpl & operator=(const OStreamImpl &)=delete
Copy assignment is deleted (the context owns raw buffer pointers).
uint8_t failed_
Sticky: first failing write's sofab_ret_t (0 = none).
Result sequenceEndKeep() noexcept
Close the current nested sequence, keeping its frame even without content.
Result writeLazy(sofab_id_t id, const T &value) noexcept
Encode a nested message field, omitting it when all-default.
bool ok() const noexcept
Whether every write on this stream has succeeded.
Result write(sofab_id_t id, const T &value) noexcept
Encode a single field, deducing the wire type from T.
Result writeIf(sofab_id_t id, const T &value, bool condition) noexcept
Encode a field only when condition is true.
OStreamImpl(const OStreamImpl &)=delete
Copy construction is deleted (the context owns raw buffer pointers).
static void static_flush_callback(sofab_ostream_t *ctx, const uint8_t *data, size_t len, void *usrptr) noexcept
C-ABI flush trampoline: forwards to onFlushCallback() via usrptr.
void(*)(std::span< const uint8_t >) flushCallback
Callback invoked with the bytes to flush (buffer full or on flush()).
Output stream backed by an inline, fixed-size buffer.
OStreamInline() noexcept
Construct with no flush callback.
OStreamInline(flushCallback callback) noexcept
Construct with a flush callback.
Base class for serializable message objects.
virtual OStreamImpl::Result serialize(OStreamImpl &_ostream) const noexcept=0
Serialize this message's fields into _ostream.
OStreamObject variant parameterized by offset only.
Self-contained encoder that owns a message and an inline buffer.
OStreamObject() noexcept=default
Construct with no flush callback.
OStreamImpl::Result serialize() noexcept
Serialize the owned message and flush.
MessageType & operator->() noexcept
Member-access to the owned message (e.g. obj->field = ...).
Output stream over a buffer the caller already owns.
OStreamView(uint8_t *buffer, size_t buflen, size_t offset=0) noexcept
Construct over caller storage.
OStreamView(flushCallback callback, uint8_t *buffer, size_t buflen, size_t offset=0) noexcept
Construct over caller storage with a flush callback.
Detects a container that states its own fixed capacity: the condition behind fixed_capacity_v.
Detects the pre-0.11 spelling. Deprecated; see OutputMessage.
Detects the CORELIB_PLAN §6.1.1 spelling of the worst-case size.
Concept: a serializable message type usable with OStreamObject.
SofaBuffers C++ - bit-pattern equality for float arrays.
sofab_ret_t sofab_ostream_write_sequence_end(sofab_ostream_t *ctx)
End encoding a nested sequence (end marker).
size_t sofab_ostream_flush(sofab_ostream_t *ctx)
Flush any pending bytes from the output stream.
static void sofab_istream_read_blob(sofab_istream_t *ctx, void *var, size_t varlen)
Reads a binary blob of fixed maximum size.
sofab_ret_t sofab_ostream_write_array_of_unsigned(sofab_ostream_t *ctx, sofab_id_t id, const void *data, int32_t element_count, int32_t element_size)
Write an array of unsigned integers.
void sofab_istream_invalidate(sofab_istream_t *ctx)
Reject the message in progress from within a field callback.
sofab_ret_t sofab_ostream_write_fixlen(sofab_ostream_t *ctx, sofab_id_t id, const void *data, int32_t datalen, sofab_fixlentype_t type)
Write a fixed-length field.
void sofab_istream_read_field(sofab_istream_t *ctx, void *var, size_t varlen, uint8_t opt)
Number of fields skipped because their wire type contradicted the destination a field callback bound.
sofab_ret_t sofab_ostream_write_sequence_begin_lazy(sofab_ostream_t *ctx, sofab_id_t id)
Begin a nested sequence whose header is held back until it has content.
void sofab_istream_read_sequence(sofab_istream_t *ctx, sofab_istream_decoder_t *decoder, sofab_istream_field_cb_t field_callback, void *usrptr)
Begins decoding a nested Sofab sequence.
static void sofab_istream_read_fp64(sofab_istream_t *ctx, double *var)
Reads a 64-bit floating-point value.
static sofab_ret_t sofab_ostream_write_array_of_fp32(sofab_ostream_t *ctx, sofab_id_t id, const float *data, int32_t element_count)
Write an array of 32-bit floating-point values.
sofab_ret_t sofab_ostream_write_signed(sofab_ostream_t *ctx, sofab_id_t id, sofab_signed_t value)
Write a signed integer field.
#define SOFAB_ISTREAM_OPT_FIELDTYPE(type)
#define SOFAB_ISTREAM_OPT_FIXLENTYPE(type)
uint32_t sofab_id_t
Field identifier type.
static sofab_ret_t sofab_ostream_write_fp64(sofab_ostream_t *ctx, sofab_id_t id, double value)
Write a 64-bit floating-point value.
static void sofab_istream_read_array_of_bool(sofab_istream_t *ctx, bool *var, size_t element_count)
Reads an array of booleans.
sofab_ret_t sofab_ostream_write_array_of_signed(sofab_ostream_t *ctx, sofab_id_t id, const void *data, int32_t element_count, int32_t element_size)
Write an array of signed integers.
uint64_t sofab_unsigned_t
Scalar varint value types.
static sofab_ret_t sofab_ostream_write_fp32(sofab_ostream_t *ctx, sofab_id_t id, float value)
Write a 32-bit floating-point value.
static void sofab_istream_read_string_noterm(sofab_istream_t *ctx, char *var, size_t varlen)
Reads a string of fixed maximum size.
static void sofab_istream_read_fp32(sofab_istream_t *ctx, float *var)
Reads a 32-bit floating-point value.
#define SOFAB_ISTREAM_OPT_BOOLEAN
void sofab_ostream_buffer_set(sofab_ostream_t *ctx, uint8_t *buffer, size_t buflen, size_t offset)
Replace the active buffer used by the output stream.
size_t sofab_ostream_bytes_used(sofab_ostream_t *ctx)
Get the number of bytes used in the current buffer.
static void sofab_istream_read_bool(sofab_istream_t *ctx, bool *var)
Reads a boolean value.
sofab_ret_t sofab_ostream_write_sequence_end_keep(sofab_ostream_t *ctx)
End a nested sequence, keeping its frame even without content.
void sofab_ostream_init(sofab_ostream_t *ctx, uint8_t *buffer, size_t buflen, size_t offset, sofab_ostream_flush_cb_t flush, void *usrptr)
Initialize an Sofab output stream context.
static sofab_ret_t sofab_ostream_write_array_of_fp64(sofab_ostream_t *ctx, sofab_id_t id, const double *data, int32_t element_count)
Write an array of 64-bit floating-point values.
sofab_ret_t sofab_ostream_write_unsigned(sofab_ostream_t *ctx, sofab_id_t id, sofab_unsigned_t value)
Write an unsigned integer field.
sofab_ret_t
Return status codes.
void sofab_istream_init(sofab_istream_t *ctx, sofab_istream_field_cb_t field_callback, void *usrptr)
Initializes a Sofab input stream context.
int64_t sofab_signed_t
Signed value type.
static sofab_ret_t sofab_ostream_write_boolean(sofab_ostream_t *ctx, sofab_id_t id, bool value)
Write a boolean value (1 byte).
static void sofab_istream_read_array_of_fp32(sofab_istream_t *ctx, float *var, size_t element_count)
Reads an array of 32-bit floating-point values.
void sofab_istream_read_array(sofab_istream_t *ctx, void *var, size_t element_count, size_t element_size, uint8_t opt)
Reads an array of elements.
static void sofab_istream_read_array_of_fp64(sofab_istream_t *ctx, double *var, size_t element_count)
Reads an array of 64-bit floating-point values.
sofab_ret_t sofab_istream_feed(sofab_istream_t *ctx, const void *data, size_t datalen)
Feeds raw Sofab-encoded bytes into the input stream.
static sofab_ret_t sofab_ostream_write_blob(sofab_ostream_t *ctx, sofab_id_t id, const void *data, size_t size)
Write a binary blob field.
@ SOFAB_FIXLENTYPE_STRING
@ SOFAB_RET_INCOMPLETE
Consumed bytes end inside a field or with an open sequence: a valid but partial decode.
@ SOFAB_RET_E_INVALID_MSG
Sofab deserialization failed: the message is malformed, or it carries a wire construct this build was...
@ SOFAB_RET_E_LIMIT_EXCEEDED
A configured receiver-side limit (CORELIB_PLAN §6.2.1) was exceeded on a schema-**unbounded** field.
@ SOFAB_RET_E_ARGUMENT
The caller handed the library a value it cannot act on: an id above SOFAB_ID_MAX, a destination or de...
@ SOFAB_RET_E_BUFFER_FULL
Sofab serialization failed due to buffer overflow.
@ SOFAB_TYPE_VARINTARRAY_SIGNED
@ SOFAB_TYPE_VARINT_SIGNED
@ SOFAB_TYPE_SEQUENCE_START
@ SOFAB_TYPE_SEQUENCE_END
@ SOFAB_TYPE_VARINT_UNSIGNED
@ SOFAB_TYPE_VARINTARRAY_UNSIGNED
#define SOFAB_CPP_HAVE_ENCODE_BOUNDS
1 if generated code refuses an over-bound value at encode, else 0.
SofaBuffers C - Input stream decoder for Sofab messages.
std::size_t utf8Prefix(std::string_view sv, std::size_t n) noexcept
Length of the longest prefix of sv that is at most n bytes and does not end inside a UTF-8 character;...
constexpr int API_VERSION
SofaBuffers C++ API version (mirrors SOFAB_API_VERSION).
Wire
Wire type of a decoded field (the 3-bit type tag of its header).
@ ArrayUnsigned
Count-prefixed array of unsigned varints.
@ Fixlen
Length-prefixed payload (float, string or blob).
@ Signed
Signed integer, zig-zag encoded as a varint.
@ ArraySigned
Count-prefixed array of zig-zag varints.
@ Unsigned
Unsigned integer encoded as a varint.
@ ArrayFixlen
Count-prefixed array of fixed-size elements.
@ SequenceStart
Opens a nested sub-message.
@ SequenceEnd
Closes the most recently opened sub-message.
constexpr bool is_fixed_bytes_v
Convenience value for is_fixed_bytes.
constexpr bool always_false_v
Always-false trait used to trigger a dependent static_assert in a discarded if constexpr branch (so i...
constexpr long fixed_capacity_v
Compile-time capacity a heap-free container publishes, or -1.
constexpr long fixed_capacity_v< C >
bool bitsEqual(const T *a, std::size_t na, const T *b, std::size_t nb) noexcept
True iff both float arrays have the same length and the same bit pattern at every index.
constexpr size_t max_size_v
The worst-case encoded size of a generated message, whichever spelling it declares....
bool seqRefuse(IStreamImpl &is, size_t n, long schemaBound, DynCap dynCap) noexcept
Apply a collector's message-side bounds and report the verdict.
constexpr bool ENCODE_BOUNDS
Whether generated encode refuses an over-bound value (SOFAB_CPP_HAVE_ENCODE_BOUNDS; off with SOFAB_DI...
sofab_id_t id
Field identifier type (alias of sofab_id_t).
std::span< const typename C::value_type > trimTail(const C &a) noexcept
Narrow a fixed-count array to its non-default prefix, for encode.
typename wire_int< E >::type wire_int_t
Alias for wire_int.
constexpr bool is_fixed_string_v
Convenience value for is_fixed_string.
Error
Result/error code returned by the stream APIs (wraps sofab_ret_t).
@ BufferFull
Output buffer overflowed during encoding.
@ LimitExceeded
A configured receiver limit (§6.2.1) was.
@ InvalidArgument
A field id out of range, a scalar width that is not 1/2/4/8 — or a destination too short for the valu...
@ Incomplete
Consumed bytes end inside a field or with an open sequence: a valid but partial decode.
@ None
Success (a complete message boundary).
@ InvalidMessage
Malformed message encountered while decoding.
Fix
Sub-type of a length-prefixed (Wire::Fixlen) payload.
@ Fp32
32-bit IEEE-754 float.
@ Fp64
64-bit IEEE-754 double.
@ Blob
Opaque byte string.
SofaBuffers C - Output stream encoder for Sofab messages.
SofaBuffers C++ - wrapper-array collectors for object and row elements.
void deserialize(IStreamImpl &is, sofab_id_t id, size_t size, size_t) noexcept override
Dispatch one decoded field to the appropriate read.
Collects a string wrapper sequence into inline storage.
void deserialize(IStreamImpl &is, sofab_id_t id, size_t size, size_t) noexcept override
Dispatch one decoded field to the appropriate read.
A message that both encodes and decodes: exactly OStreamMessage + IStreamMessage.
Trait: true only for FixedBytes specializations.
Trait: true only for FixedString specializations.
std::underlying_type_t< E > type
The integer type an element travels as on the wire: identity for an integer, the declared underlying ...
Decoder context used for Sofab sequences.
Internal state of the Sofab input stream.