#pragma once
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <limits>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <utility>
#ifndef RP_FLATTEN
#if defined(__GNUC__) || defined(__clang__)
#define RP_FLATTEN __attribute__((flatten))
#else
#define RP_FLATTEN
#endif
#endif
#ifndef RP_NOINLINE
#if defined(__GNUC__) || defined(__clang__)
#define RP_NOINLINE __attribute__((noinline))
#else
#define RP_NOINLINE
#endif
#endif
namespace rapidproto {
using ByteView = std::string_view;
static_assert(std::is_same_v<std::uint8_t, unsigned char>,
"rapidproto reads wire bytes through uint8_t*; uint8_t must be unsigned char");
enum class WireType : std::uint8_t {
Varint = 0, I64 = 1, Len = 2, SGroup = 3, EGroup = 4, I32 = 5, };
struct Tag {
std::uint32_t field_number;
WireType wire_type;
};
enum class WireError : std::uint8_t {
None,
TruncatedVarint,
VarintOverflow,
InvalidFieldNumber,
FieldNumberRange,
ReservedWireType,
TruncatedI32,
TruncatedI64,
LengthExceedsBuffer,
LengthTooLarge,
UnexpectedEndGroup,
EndGroupMismatch,
UnterminatedGroup,
GroupTooDeep,
};
constexpr std::uint32_t raw_tag(std::uint32_t field_number, WireType wire_type) noexcept {
return (field_number << 3U) | static_cast<std::uint32_t>(wire_type);
}
inline constexpr std::uint32_t kMaxFieldNumber = (std::uint32_t{1} << 29U) - 1U;
inline constexpr int kMaxGroupDepth = 100;
inline ByteView byte_view(const std::uint8_t* data, std::size_t size) noexcept {
return {reinterpret_cast<const char*>(data), size};
}
namespace wire {
inline const std::uint8_t* byte_ptr(ByteView v) noexcept {
return reinterpret_cast<const std::uint8_t*>(v.data());
}
inline const std::uint8_t* read_varint(const std::uint8_t* p, const std::uint8_t* end,
std::uint64_t* out, WireError* err) noexcept {
if (p < end && (*p & 0x80U) == 0U) { *out = *p;
return p + 1;
}
std::uint64_t result = 0;
for (unsigned shift = 0; shift < 64U; shift += 7U) {
if (p >= end) {
*err = WireError::TruncatedVarint;
return nullptr;
}
const std::uint8_t b = *p;
if (shift == 63U && b > 1U) { *err = WireError::VarintOverflow;
return nullptr;
}
++p;
result |= static_cast<std::uint64_t>(b & 0x7FU) << shift;
if ((b & 0x80U) == 0U) {
*out = result;
return p;
}
}
*err = WireError::VarintOverflow; return nullptr;
}
inline constexpr bool kIsLittleEndian =
#if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || defined(_WIN32)
true;
#else
false; #endif
namespace swar_detail {
inline constexpr std::uint64_t kMSB = 0x8080808080808080ULL; inline constexpr std::uint64_t kLow7 = 0x7F7F7F7F7F7F7F7FULL;
template <int N>
constexpr std::uint64_t low_byte_mask() noexcept {
static_assert(N >= 1 && N <= 8, "N is a byte count within a uint64");
if constexpr (N >= 8) {
return ~std::uint64_t{0};
} else {
return (std::uint64_t{1} << (8U * static_cast<unsigned>(N))) - 1U;
}
}
static_assert(low_byte_mask<1>() == 0xFFULL);
static_assert(low_byte_mask<2>() == 0xFFFFULL);
static_assert(low_byte_mask<3>() == 0xFFFFFFULL);
static_assert(low_byte_mask<4>() == 0xFFFFFFFFULL);
static_assert(low_byte_mask<5>() == 0xFFFFFFFFFFULL);
static_assert(low_byte_mask<6>() == 0xFFFFFFFFFFFFULL);
static_assert(low_byte_mask<7>() == 0xFFFFFFFFFFFFFFULL);
static_assert(low_byte_mask<8>() == ~std::uint64_t{0});
inline std::uint64_t load64(const std::uint8_t* p) noexcept {
#if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || defined(_WIN32)
std::uint64_t w = 0;
std::memcpy(&w, p, sizeof w);
return w;
#else
return static_cast<std::uint64_t>(p[0]) | (static_cast<std::uint64_t>(p[1]) << 8U) |
(static_cast<std::uint64_t>(p[2]) << 16U) | (static_cast<std::uint64_t>(p[3]) << 24U) |
(static_cast<std::uint64_t>(p[4]) << 32U) | (static_cast<std::uint64_t>(p[5]) << 40U) |
(static_cast<std::uint64_t>(p[6]) << 48U) | (static_cast<std::uint64_t>(p[7]) << 56U);
#endif
}
inline int ctz64(std::uint64_t x) noexcept {
#if defined(__GNUC__) || defined(__clang__)
return __builtin_ctzll(x);
#else
int n = 0;
while ((x & 1U) == 0U) {
x >>= 1U;
++n;
}
return n;
#endif
}
inline std::uint64_t compact7(std::uint64_t x) noexcept {
x = ((x & 0x7F007F007F007F00ULL) >> 1U) | (x & 0x007F007F007F007FULL);
x = ((x & 0x3FFF00003FFF0000ULL) >> 2U) | (x & 0x00003FFF00003FFFULL);
x = ((x & 0x0FFFFFFF00000000ULL) >> 4U) | (x & 0x000000000FFFFFFFULL);
return x;
}
inline std::uint64_t bytemask8(std::uint64_t w) noexcept {
return ((w & kMSB) * 0x0002040810204081ULL) >> 56U;
}
inline std::uint64_t stopmask64(const std::uint8_t* p) noexcept {
std::uint64_t m = 0;
for (int j = 0; j < 8; ++j) {
m |= bytemask8(~load64(p + (8 * j))) << (8U * static_cast<unsigned>(j));
}
return m;
}
}
inline const std::uint8_t* read_varint_packed(const std::uint8_t* p, const std::uint8_t* end,
std::uint64_t* out, WireError* err) noexcept;
enum class PackedKernel : std::uint8_t {
ByteLoop, Swar, Struct, Bulk1, Bulk2, Fixed, };
inline constexpr std::size_t kPackedKernelMinSpan = 256;
inline PackedKernel packed_strategy(const std::uint8_t* p, std::size_t span, int* width) noexcept {
*width =
0; if (span < kPackedKernelMinSpan) {
return PackedKernel::ByteLoop;
}
const std::uint64_t stop =
swar_detail::stopmask64(p); if (stop == 0) {
return PackedKernel::ByteLoop;
}
std::uint64_t m = stop;
int last = -1; int g0 = -1;
int maxw = 0;
bool homo = true;
while (m != 0U) {
const int b = swar_detail::ctz64(m);
m &= m - 1U;
const int w = b - last; last = b;
if (w > maxw) {
maxw = w;
}
if (g0 < 0) {
g0 = w;
} else if (w != g0) {
homo = false;
}
}
if (!homo) {
return (maxw <= 5) ? PackedKernel::Struct : PackedKernel::Swar;
}
if (g0 == 1) {
return PackedKernel::Bulk1; }
if (g0 == 2) {
return PackedKernel::Bulk2; }
if (g0 >= 3 && g0 <= 10) {
*width = g0;
return PackedKernel::Fixed; }
return PackedKernel::
ByteLoop; }
template <class Elem, class Conv>
struct array_sink {
Elem* out;
void put(std::size_t i, std::uint64_t raw) const noexcept { out[i] = Conv{}(raw); }
void put2(std::size_t i, std::uint64_t r0, std::uint64_t r1) const noexcept {
out[i] = Conv{}(r0);
out[i + 1] = Conv{}(r1);
}
};
template <int W, class Sink>
inline const std::uint8_t* fixed_fill(const std::uint8_t* q, const std::uint8_t* end,
std::size_t* np, Sink& sink) noexcept {
constexpr std::uint64_t kLenMask = swar_detail::low_byte_mask<W>();
constexpr std::uint64_t kMsbW = swar_detail::kMSB & kLenMask; constexpr std::uint64_t kOneVarint =
kMsbW ^ (std::uint64_t{0x80} << (8U * static_cast<unsigned>(W - 1)));
std::size_t n = *np;
if constexpr (W <= 4) {
constexpr std::uint64_t kMsb2 = kMsbW | (kMsbW << (8U * static_cast<unsigned>(W)));
constexpr std::uint64_t kTwoVarints =
kOneVarint | (kOneVarint << (8U * static_cast<unsigned>(W)));
while (static_cast<std::size_t>(end - q) >= 8) {
const std::uint64_t w = swar_detail::load64(q);
if ((w & kMsb2) == kTwoVarints) { sink.put2(n, swar_detail::compact7(w & kLenMask & swar_detail::kLow7),
swar_detail::compact7((w >> (8U * static_cast<unsigned>(W))) & kLenMask &
swar_detail::kLow7));
n += 2;
q += 2 * W;
} else if ((w & kMsbW) == kOneVarint) { sink.put(n, swar_detail::compact7(w & kLenMask & swar_detail::kLow7));
++n;
q += W;
} else {
break; }
}
} else {
while (static_cast<std::size_t>(end - q) >= 8) {
const std::uint64_t w = swar_detail::load64(q);
if ((w & kMsbW) != kOneVarint) {
break; }
sink.put(n, swar_detail::compact7(w & kLenMask & swar_detail::kLow7));
++n;
q += W;
}
}
*np = n;
return q;
}
template <int W, class Sink>
inline const std::uint8_t* fixed_fill_wide(const std::uint8_t* q, const std::uint8_t* end,
std::size_t* np, Sink& sink) noexcept {
static_assert(W == 9 || W == 10, "fixed_fill_wide handles only 9/10-byte varints");
std::size_t n = *np;
while (static_cast<std::size_t>(end - q) >= W) {
const std::uint64_t w0 =
swar_detail::load64(q); if ((w0 & swar_detail::kMSB) != swar_detail::kMSB) {
break; }
std::uint64_t value = swar_detail::compact7(w0 & swar_detail::kLow7); const std::uint8_t b8 = q[8];
if constexpr (W == 9) {
if ((b8 & 0x80U) != 0U) {
break; }
value |= static_cast<std::uint64_t>(b8 & 0x7FU) << 56U; } else { const std::uint8_t b9 = q[9];
if ((b8 & 0x80U) == 0U || (b9 & 0x80U) != 0U) {
break; }
if (b9 > 1U) {
break; }
value |= static_cast<std::uint64_t>(b8 & 0x7FU) << 56U; value |= static_cast<std::uint64_t>(b9) << 63U; }
sink.put(n, value);
++n;
q += W;
}
*np = n;
return q;
}
template <class Sink>
RP_FLATTEN inline std::size_t decode_packed_varints(const std::uint8_t* p, const std::uint8_t* end,
const std::uint8_t* begin, WireError* err,
std::size_t* fail_off, Sink sink) noexcept {
std::size_t n = 0;
const std::uint8_t* q = p;
if (static_cast<std::size_t>(end - p) >= kPackedKernelMinSpan) {
for (;;) {
const std::uint8_t* const q_start = q;
int width = 0;
const PackedKernel kern = packed_strategy(q, static_cast<std::size_t>(end - q), &width);
if (kern == PackedKernel::Bulk1) {
while (static_cast<std::size_t>(end - q) >= 64) {
std::uint64_t any = 0;
for (int j = 0; j < 8; ++j) {
any |= swar_detail::load64(q + (8 * j)) & swar_detail::kMSB;
}
if (any == 0) { for (int i = 0; i < 64; ++i) {
sink.put(n + static_cast<std::size_t>(i), q[i]);
}
q += 64;
n += 64;
} else {
break; }
}
} else if (kern == PackedKernel::Bulk2) {
while (static_cast<std::size_t>(end - q) >= 8) {
const std::uint64_t w = swar_detail::load64(q);
if ((w & swar_detail::kMSB) ==
0x0080008000800080ULL) { const std::uint64_t x = w & swar_detail::kLow7;
const std::uint64_t y =
(x & 0x007F007F007F007FULL) | ((x & 0x7F007F007F007F00ULL) >> 1U);
sink.put(n + 0, static_cast<std::uint16_t>(y));
sink.put(n + 1, static_cast<std::uint16_t>(y >> 16U));
sink.put(n + 2, static_cast<std::uint16_t>(y >> 32U));
sink.put(n + 3, static_cast<std::uint16_t>(y >> 48U));
q += 8;
n += 4;
} else {
break; }
}
} else if (kern == PackedKernel::Fixed) {
switch (width) {
case 3:
q = fixed_fill<3>(q, end, &n, sink);
break;
case 4:
q = fixed_fill<4>(q, end, &n, sink);
break;
case 5:
q = fixed_fill<5>(q, end, &n, sink);
break;
case 6:
q = fixed_fill<6>(q, end, &n, sink);
break;
case 7:
q = fixed_fill<7>(q, end, &n, sink);
break;
case 8:
q = fixed_fill<8>(q, end, &n, sink);
break;
case 9:
q = fixed_fill_wide<9>(q, end, &n, sink);
break;
default: q = fixed_fill_wide<10>(q, end, &n, sink);
break;
}
} else if (kern == PackedKernel::Struct) {
while (static_cast<std::size_t>(end - q) >= 72) {
std::uint64_t t = swar_detail::stopmask64(q);
if (t == 0U) {
break; }
int start = 0;
do {
const int b = swar_detail::ctz64(t);
t &= t - 1U;
const int len = b - start + 1;
if (len <= 8) {
const std::uint64_t lenmask =
(len >= 8)
? ~std::uint64_t{0}
: ((std::uint64_t{1} << (8U * static_cast<unsigned>(len))) -
1U);
sink.put(n, swar_detail::compact7(swar_detail::load64(q + start) &
lenmask & swar_detail::kLow7));
++n;
} else { std::uint64_t raw = 0;
const std::uint8_t* const np =
read_varint_packed(q + start, end, &raw, err);
if (np == nullptr) {
*fail_off = static_cast<std::size_t>((q + start) - begin);
return SIZE_MAX;
}
sink.put(n, raw);
++n;
}
start = b + 1;
} while (t != 0U);
q += static_cast<std::size_t>(
start); }
} else if (kern == PackedKernel::Swar) {
while (static_cast<std::size_t>(end - q) >= 8) {
std::uint64_t raw = 0;
const std::uint8_t* const np = read_varint_packed(q, end, &raw, err);
if (np == nullptr) {
*fail_off = static_cast<std::size_t>(q - begin);
return SIZE_MAX;
}
q = np;
sink.put(n, raw);
++n;
}
}
if (static_cast<std::size_t>(end - q) < 512U) {
break; }
if (static_cast<std::size_t>(q - q_start) < 4096U) {
break; }
}
}
while (q < end) { std::uint64_t raw = 0;
const std::uint8_t* const np = read_varint(q, end, &raw, err);
if (np == nullptr) {
*fail_off = static_cast<std::size_t>(q - begin);
return SIZE_MAX;
}
q = np;
sink.put(n, raw);
++n;
}
return n;
}
inline const std::uint8_t* read_varint_packed(const std::uint8_t* p, const std::uint8_t* end,
std::uint64_t* out, WireError* err) noexcept {
if (*p < 0x80U) { *out = *p;
return p + 1;
}
const std::uint64_t w = swar_detail::load64(p);
const std::uint64_t stop = ~w & swar_detail::kMSB; if (stop == 0U) { return read_varint(p, end, out, err);
}
const int len = (swar_detail::ctz64(stop) >> 3U) + 1;
const std::uint64_t mask =
stop ^ (stop - 1U); *out = swar_detail::compact7(w & mask & swar_detail::kLow7);
return p + len;
}
inline const std::uint8_t* read_tag(const std::uint8_t* p, const std::uint8_t* end, Tag* out,
WireError* err) noexcept {
if (p < end && (*p & 0x80U) == 0U) { const std::uint32_t byte = *p;
const std::uint32_t field = byte >> 3U;
const std::uint32_t wire = byte & 0x07U;
if (field != 0U && wire != 6U && wire != 7U) {
*out = Tag{field, static_cast<WireType>(wire)};
return p + 1;
}
}
std::uint64_t raw =
0; const std::uint8_t* const np = read_varint(p, end, &raw, err);
if (np == nullptr) {
return nullptr;
}
const auto wire = static_cast<std::uint32_t>(raw & 0x07U);
const std::uint64_t field = raw >> 3U;
if (field == 0) {
*err = WireError::InvalidFieldNumber;
return nullptr;
}
if (field > kMaxFieldNumber) {
*err = WireError::FieldNumberRange;
return nullptr;
}
if (wire == 6U || wire == 7U) {
*err = WireError::ReservedWireType;
return nullptr;
}
*out = Tag{static_cast<std::uint32_t>(field), static_cast<WireType>(wire)};
return np;
}
enum class TagState : std::uint8_t { Tag, End, Error };
inline const std::uint8_t* read_tag_or_end(const std::uint8_t* p, const std::uint8_t* end, Tag* out,
WireError* err, TagState* state) noexcept {
if (p >= end) {
*state = TagState::End;
return p;
}
if ((*p & 0x80U) == 0U) { const std::uint32_t byte = *p;
const std::uint32_t field = byte >> 3U;
const std::uint32_t wire = byte & 0x07U;
if (field != 0U && wire != 6U && wire != 7U) {
*out = Tag{field, static_cast<WireType>(wire)};
*state = TagState::Tag;
return p + 1;
}
} else if (static_cast<std::size_t>(end - p) >= 2U &&
static_cast<std::uint8_t>(p[1] - 1U) < 0x7FU) {
const std::uint32_t value =
(static_cast<std::uint32_t>(p[0]) & 0x7FU) | (static_cast<std::uint32_t>(p[1]) << 7U);
const std::uint32_t wire = value & 0x07U;
if (wire != 6U && wire != 7U) {
*out = Tag{value >> 3U, static_cast<WireType>(wire)};
*state = TagState::Tag;
return p + 2;
}
}
const std::uint8_t* const np = read_tag(p, end, out, err); if (np == nullptr) {
*state = TagState::Error;
return p;
}
*state = TagState::Tag;
return np;
}
inline const std::uint8_t* read_fixed32(const std::uint8_t* p, const std::uint8_t* end,
std::uint32_t* out, WireError* err) noexcept {
if (static_cast<std::size_t>(end - p) < 4U) {
*err = WireError::TruncatedI32;
return nullptr;
}
*out = static_cast<std::uint32_t>(p[0]) | (static_cast<std::uint32_t>(p[1]) << 8U) |
(static_cast<std::uint32_t>(p[2]) << 16U) | (static_cast<std::uint32_t>(p[3]) << 24U);
return p + 4;
}
inline const std::uint8_t* read_fixed64(const std::uint8_t* p, const std::uint8_t* end,
std::uint64_t* out, WireError* err) noexcept {
if (static_cast<std::size_t>(end - p) < 8U) {
*err = WireError::TruncatedI64;
return nullptr;
}
std::uint64_t v = 0;
for (unsigned i = 0; i < 8U; ++i) {
v |= static_cast<std::uint64_t>(p[i]) << (i * 8U);
}
*out = v;
return p + 8;
}
inline const std::uint8_t* read_length_delimited(const std::uint8_t* p, const std::uint8_t* end,
ByteView* out, WireError* err) noexcept {
std::uint64_t len = 0;
const std::uint8_t* const np = read_varint(p, end, &len, err);
if (np == nullptr) {
return nullptr;
}
if constexpr (sizeof(std::size_t) < sizeof(std::uint64_t)) { if (len > std::numeric_limits<std::size_t>::max()) {
*err = WireError::LengthTooLarge;
return nullptr;
}
}
const auto avail = static_cast<std::uint64_t>(end - np);
if (len > avail) {
*err = WireError::LengthExceedsBuffer;
return nullptr;
}
const auto n = static_cast<std::size_t>(len);
*out = byte_view(np, n);
return np + n;
}
inline const std::uint8_t* scan_group_end(const std::uint8_t* p, const std::uint8_t* end,
const std::uint8_t* begin, std::uint32_t field_number,
int depth, WireError* err, std::size_t* fail_off,
const std::uint8_t** egroup_tag = nullptr) noexcept;
inline const std::uint8_t* skip_value(const std::uint8_t* p, const std::uint8_t* end,
const std::uint8_t* begin, Tag tag, int depth, WireError* err,
std::size_t* fail_off) noexcept {
switch (tag.wire_type) {
case WireType::Varint: {
std::uint64_t discard = 0;
const std::uint8_t* const np = read_varint(p, end, &discard, err);
if (np == nullptr) {
*fail_off = static_cast<std::size_t>(p - begin);
}
return np;
}
case WireType::I64: {
std::uint64_t discard = 0;
const std::uint8_t* const np = read_fixed64(p, end, &discard, err);
if (np == nullptr) {
*fail_off = static_cast<std::size_t>(p - begin);
}
return np;
}
case WireType::I32: {
std::uint32_t discard = 0;
const std::uint8_t* const np = read_fixed32(p, end, &discard, err);
if (np == nullptr) {
*fail_off = static_cast<std::size_t>(p - begin);
}
return np;
}
case WireType::Len: {
ByteView discard;
const std::uint8_t* const np = read_length_delimited(p, end, &discard, err);
if (np == nullptr) {
*fail_off = static_cast<std::size_t>(p - begin);
}
return np;
}
case WireType::SGroup:
return scan_group_end(p, end, begin, tag.field_number, depth + 1, err, fail_off);
case WireType::EGroup:
*err = WireError::UnexpectedEndGroup;
*fail_off = static_cast<std::size_t>(p - begin);
return nullptr;
}
return nullptr; }
inline const std::uint8_t* scan_group_end(const std::uint8_t* p, const std::uint8_t* end,
const std::uint8_t* begin, std::uint32_t field_number,
int depth, WireError* err, std::size_t* fail_off,
const std::uint8_t** egroup_tag) noexcept {
if (depth > kMaxGroupDepth) {
*err = WireError::GroupTooDeep;
*fail_off = static_cast<std::size_t>(p - begin);
return nullptr;
}
while (true) {
const std::uint8_t* const tag_start = p;
if (p >= end) {
*err = WireError::UnterminatedGroup;
*fail_off = static_cast<std::size_t>(tag_start - begin);
return nullptr;
}
Tag tag{};
const std::uint8_t* const np = read_tag(p, end, &tag, err);
if (np == nullptr) {
*fail_off = static_cast<std::size_t>(tag_start - begin); return nullptr;
}
p = np;
if (tag.wire_type == WireType::EGroup) {
if (tag.field_number != field_number) {
*err = WireError::EndGroupMismatch;
*fail_off = static_cast<std::size_t>(tag_start - begin);
return nullptr;
}
if (egroup_tag != nullptr) {
*egroup_tag = tag_start; }
return p; }
const std::uint8_t* const sp = skip_value(p, end, begin, tag, depth, err, fail_off);
if (sp == nullptr) {
return nullptr; }
p = sp;
}
}
inline const std::uint8_t* read_group(const std::uint8_t* p, const std::uint8_t* end,
const std::uint8_t* begin, std::uint32_t field_number,
ByteView* body, WireError* err,
std::size_t* fail_off) noexcept {
const std::uint8_t* egroup_tag = nullptr;
const std::uint8_t* const np =
scan_group_end(p, end, begin, field_number, 1, err, fail_off, &egroup_tag);
if (np == nullptr) {
return nullptr;
}
*body = byte_view(p, static_cast<std::size_t>(egroup_tag - p));
return np;
}
}
constexpr std::int32_t zigzag_decode_32(std::uint32_t v) noexcept {
return static_cast<std::int32_t>((v >> 1U) ^ (0U - (v & 1U)));
}
constexpr std::int64_t zigzag_decode_64(std::uint64_t v) noexcept {
return static_cast<std::int64_t>((v >> 1U) ^ (0ULL - (v & 1ULL)));
}
inline float bit_cast_float(std::uint32_t bits) noexcept {
float out = 0.0F;
std::memcpy(&out, &bits, sizeof out); return out;
}
inline double bit_cast_double(std::uint64_t bits) noexcept {
double out = 0.0;
std::memcpy(&out, &bits, sizeof out); return out;
}
constexpr bool varint_to_bool(std::uint64_t v) noexcept {
return v != 0;
}
constexpr std::int32_t varint_to_int32(std::uint64_t v) noexcept {
return static_cast<std::int32_t>(static_cast<std::uint32_t>(v));
}
constexpr std::int64_t varint_to_int64(std::uint64_t v) noexcept {
return static_cast<std::int64_t>(v);
}
namespace wire {
struct conv_int32 {
std::int32_t operator()(std::uint64_t r) const noexcept { return varint_to_int32(r); }
};
struct conv_int64 {
std::int64_t operator()(std::uint64_t r) const noexcept { return varint_to_int64(r); }
};
struct conv_uint32 {
std::uint32_t operator()(std::uint64_t r) const noexcept {
return static_cast<std::uint32_t>(r);
}
};
struct conv_uint64 {
std::uint64_t operator()(std::uint64_t r) const noexcept { return r; }
};
struct conv_sint32 {
std::int32_t operator()(std::uint64_t r) const noexcept {
return zigzag_decode_32(static_cast<std::uint32_t>(r));
}
};
struct conv_sint64 {
std::int64_t operator()(std::uint64_t r) const noexcept { return zigzag_decode_64(r); }
};
struct conv_bool {
bool operator()(std::uint64_t r) const noexcept { return varint_to_bool(r); }
};
template <class E>
struct conv_enum {
E operator()(std::uint64_t r) const noexcept { return static_cast<E>(varint_to_int32(r)); }
};
}
struct [[nodiscard]] DecodeStatus {
WireError wire = WireError::None; bool aborted = false; std::size_t offset = 0;
[[nodiscard]] constexpr bool ok() const noexcept { return wire == WireError::None && !aborted; }
explicit constexpr operator bool() const noexcept { return ok(); }
static constexpr DecodeStatus success() noexcept { return {}; }
static constexpr DecodeStatus abort() noexcept { return {WireError::None, true, 0}; }
};
template <class... Fs>
struct overloaded : Fs... {
using Fs::operator()...;
};
template <class... Fs>
constexpr overloaded<std::decay_t<Fs>...> combine(Fs&&... fs) {
return overloaded<std::decay_t<Fs>...>{std::forward<Fs>(fs)...};
}
struct generic_probe {};
template <class>
using as_generic_probe = generic_probe;
struct tag_probe {
static constexpr std::uint32_t kNumber = 0;
static constexpr std::string_view kName = {};
};
struct generic_params {};
template <class R, class C, class... A>
std::tuple<std::decay_t<A>...> param_tuple_of(R (C::*)(A...));
template <class R, class C, class... A>
std::tuple<std::decay_t<A>...> param_tuple_of(R (C::*)(A...) const);
template <class R, class C, class... A>
std::tuple<std::decay_t<A>...> param_tuple_of(R (C::*)(A...) noexcept);
template <class R, class C, class... A>
std::tuple<std::decay_t<A>...> param_tuple_of(R (C::*)(A...) const noexcept);
template <class F, class = void>
struct call_params {
using type = generic_params;
};
template <class F>
struct call_params<F, std::void_t<decltype(param_tuple_of(&F::operator()))>> {
using type = decltype(param_tuple_of(&F::operator()));
};
template <class Cb>
inline constexpr bool is_generic_callback =
std::is_same_v<typename call_params<std::decay_t<Cb>>::type, generic_params>;
template <class Cb, class Tag, class... Vs>
inline constexpr bool specifically_handles =
std::is_same_v<typename call_params<std::decay_t<Cb>>::type, std::tuple<Tag, Vs...>>;
template <class Cb, class Tag, class... Vs>
inline constexpr bool is_catch_all =
is_generic_callback<Cb> &&
std::is_invocable_v<std::decay_t<Cb>&, tag_probe, as_generic_probe<Vs>...>;
template <class Cb, class Tag, class... Vs>
inline constexpr bool handles_one =
specifically_handles<Cb, Tag, Vs...> || is_catch_all<Cb, Tag, Vs...>;
template <class Cb, class Tag, class... Vs>
inline constexpr bool is_partial_generic =
is_generic_callback<Cb> &&
(std::is_invocable_v<std::decay_t<Cb>&, Tag, Vs...> ||
std::is_invocable_v<std::decay_t<Cb>&, Tag, std::add_lvalue_reference_t<Vs>...>) &&
!is_catch_all<Cb, Tag, Vs...>;
template <class T>
struct first_param {
using type = void;
};
template <class First, class... Rest>
struct first_param<std::tuple<First, Rest...>> {
using type = First;
};
template <class Cb, class Tag, class... Vs>
inline constexpr bool targets =
std::is_same_v<typename first_param<typename call_params<std::decay_t<Cb>>::type>::type, Tag>;
template <class D, class Tag, class... Vs>
constexpr DecodeStatus invoke_field(D& dispatcher, Tag tag, Vs&&... vs) {
using Ret = std::invoke_result_t<D&, Tag, Vs...>;
static_assert(std::is_void_v<Ret> || std::is_same_v<Ret, DecodeStatus>,
"a field callback must return void or rapidproto::DecodeStatus");
if constexpr (std::is_void_v<Ret>) {
dispatcher(tag, std::forward<Vs>(vs)...);
return DecodeStatus::success();
} else {
return dispatcher(tag, std::forward<Vs>(vs)...);
}
}
template <class D, class Tag, class... Vs>
constexpr void invoke_handler(D& dispatcher, Tag tag, Vs&&... vs) {
static_assert(std::is_void_v<std::invoke_result_t<D&, Tag, Vs...>>,
"a oneof handler must return void (the message is already decoded; there is"
" nothing to abort)");
dispatcher(tag, std::forward<Vs>(vs)...);
}
struct UnknownField {
std::uint32_t field_number;
WireType wire_type;
ByteView bytes;
};
template <class Cb>
inline constexpr bool specifically_handles_unknown = std::conjunction_v<
std::is_invocable<std::decay_t<Cb>&, UnknownField>,
std::negation<std::is_invocable<std::decay_t<Cb>&, UnknownField, generic_probe>>>;
template <class Cb, class... Tags>
inline constexpr bool names_some_tag =
(... || std::is_same_v<typename first_param<typename call_params<std::decay_t<Cb>>::type>::type,
Tags>);
template <class Cb, class... Tags>
inline constexpr bool is_stray_handler = !is_generic_callback<Cb> && !names_some_tag<Cb, Tags...>;
template <class Cb, class... Tags>
inline constexpr bool is_stray_callback =
is_stray_handler<Cb, Tags...> && !specifically_handles_unknown<Cb>;
template <class D>
constexpr DecodeStatus invoke_unknown(D& dispatcher, UnknownField field) {
using Ret = std::invoke_result_t<D&, UnknownField>;
static_assert(std::is_void_v<Ret> || std::is_same_v<Ret, DecodeStatus>,
"an unknown-field callback must return void or rapidproto::DecodeStatus");
if constexpr (std::is_void_v<Ret>) {
dispatcher(field);
return DecodeStatus::success();
} else {
return dispatcher(field);
}
}
}