mirror of
https://github.com/zap-proto/go.git
synced 2026-10-30 15:05:00 +00:00
A field may carry the name of its own type — `Child Child` is a natural schema — and in C++ the member then shadows the class inside the class body, so `Child Child() const;` is ill-formed. The qualified name says which is meant. Go has no such collision, which is why this only shows up on one side. Per-struct headers now also forward-declare the classes they name, not just their own, so declaration order is free there too. Two structs that point at each other still need -single: two headers cannot each be complete for the other, and the cycle compiles in one header. Both cases are checked by compiling, not by reading.
189 lines
6.9 KiB
C++
189 lines
6.9 KiB
C++
// Code generated by zapgen; DO NOT EDIT.
|
|
// source: probe.zap
|
|
// SPDX-License-Identifier: BSD-3-Clause-Eco
|
|
|
|
#pragma once
|
|
|
|
#include <array>
|
|
#include <bit>
|
|
#include <cstdint>
|
|
#include <expected>
|
|
#include <span>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
#include <zap/zap.hpp>
|
|
|
|
namespace probe {
|
|
|
|
class Inner;
|
|
class Wide;
|
|
|
|
// Field byte offsets for Inner, and the size of its fixed section.
|
|
inline constexpr std::int64_t kInnerTagOff = 0;
|
|
inline constexpr std::int64_t kInnerValOff = 4;
|
|
inline constexpr std::int64_t kInnerSize = 12;
|
|
|
|
// Inner is a zero-copy view into a ZAP-encoded Inner message. It borrows the
|
|
// bytes it was built over, which must outlive it, and copies no field.
|
|
class Inner {
|
|
public:
|
|
Inner() = default;
|
|
explicit Inner(zap::Object o) : o_(o) {}
|
|
|
|
bool is_null() const { return o_.is_null(); }
|
|
zap::Object object() const { return o_; }
|
|
|
|
std::uint32_t Tag() const { return o_.u32(kInnerTagOff); }
|
|
std::uint64_t Val() const { return o_.u64(kInnerValOff); }
|
|
|
|
private:
|
|
zap::Object o_;
|
|
};
|
|
|
|
// WrapInner parses b and returns a typed view over it, or says why b is not a
|
|
// message. The view borrows b.
|
|
inline std::expected<Inner, zap::Error> WrapInner(std::span<const std::uint8_t> b) {
|
|
auto m = zap::Message::parse(b);
|
|
if (!m) return std::unexpected(m.error());
|
|
return Inner(m->root());
|
|
}
|
|
|
|
// InnerInput collects the field values for NewInner. A borrowed field is a view:
|
|
// it must outlive the call, not the buffer the call returns.
|
|
struct InnerInput {
|
|
std::uint32_t Tag{};
|
|
std::uint64_t Val{};
|
|
};
|
|
|
|
// NewInner writes a ZAP-encoded Inner message into a fresh buffer and returns it.
|
|
inline std::vector<std::uint8_t> NewInner(const InnerInput& in) {
|
|
zap::Builder b(256, zap::kVersion1);
|
|
auto ob = b.start_object(kInnerSize);
|
|
ob.set_u32(kInnerTagOff, in.Tag);
|
|
ob.set_u64(kInnerValOff, in.Val);
|
|
ob.finish_as_root();
|
|
return b.finish();
|
|
}
|
|
|
|
// Field byte offsets for Wide, and the size of its fixed section.
|
|
inline constexpr std::int64_t kWideFlagOff = 0;
|
|
inline constexpr std::int64_t kWideA8Off = 1;
|
|
inline constexpr std::int64_t kWideA16Off = 2;
|
|
inline constexpr std::int64_t kWideA32Off = 4;
|
|
inline constexpr std::int64_t kWideA64Off = 8;
|
|
inline constexpr std::int64_t kWideS8Off = 16;
|
|
inline constexpr std::int64_t kWideS16Off = 18;
|
|
inline constexpr std::int64_t kWideS32Off = 20;
|
|
inline constexpr std::int64_t kWideS64Off = 24;
|
|
inline constexpr std::int64_t kWideF32Off = 32;
|
|
inline constexpr std::int64_t kWideF64Off = 40;
|
|
inline constexpr std::int64_t kWideIdOff = 48;
|
|
inline constexpr std::int64_t kWideNameOff = 64;
|
|
inline constexpr std::int64_t kWideBlobOff = 72;
|
|
inline constexpr std::int64_t kWideItemsOff = 80;
|
|
inline constexpr std::int64_t kWideChildOff = 88;
|
|
inline constexpr std::int64_t kWideSize = 92;
|
|
|
|
// Wide is a zero-copy view into a ZAP-encoded Wide message. It borrows the
|
|
// bytes it was built over, which must outlive it, and copies no field.
|
|
class Wide {
|
|
public:
|
|
Wide() = default;
|
|
explicit Wide(zap::Object o) : o_(o) {}
|
|
|
|
bool is_null() const { return o_.is_null(); }
|
|
zap::Object object() const { return o_; }
|
|
|
|
bool Flag() const { return o_.boolean(kWideFlagOff); }
|
|
std::uint8_t A8() const { return o_.u8(kWideA8Off); }
|
|
std::uint16_t A16() const { return o_.u16(kWideA16Off); }
|
|
std::uint32_t A32() const { return o_.u32(kWideA32Off); }
|
|
std::uint64_t A64() const { return o_.u64(kWideA64Off); }
|
|
std::int8_t S8() const { return static_cast<std::int8_t>(o_.u8(kWideS8Off)); }
|
|
std::int16_t S16() const { return static_cast<std::int16_t>(o_.u16(kWideS16Off)); }
|
|
std::int32_t S32() const { return static_cast<std::int32_t>(o_.u32(kWideS32Off)); }
|
|
std::int64_t S64() const { return static_cast<std::int64_t>(o_.u64(kWideS64Off)); }
|
|
float F32() const { return std::bit_cast<float>(o_.u32(kWideF32Off)); }
|
|
double F64() const { return std::bit_cast<double>(o_.u64(kWideF64Off)); }
|
|
std::span<const std::uint8_t> Id() const {
|
|
static constexpr std::array<std::uint8_t, 16> zero{};
|
|
const auto b = o_.bytes_fixed(kWideIdOff, 16);
|
|
return b.size() == 16 ? b : std::span<const std::uint8_t>(zero);
|
|
}
|
|
std::string_view Name() const { return o_.text(kWideNameOff); }
|
|
std::span<const std::uint8_t> Blob() const { return o_.bytes(kWideBlobOff); }
|
|
zap::List Items() const { return o_.list(kWideItemsOff); }
|
|
::probe::Inner Child() const;
|
|
|
|
private:
|
|
zap::Object o_;
|
|
};
|
|
|
|
// WrapWide parses b and returns a typed view over it, or says why b is not a
|
|
// message. The view borrows b.
|
|
inline std::expected<Wide, zap::Error> WrapWide(std::span<const std::uint8_t> b) {
|
|
auto m = zap::Message::parse(b);
|
|
if (!m) return std::unexpected(m.error());
|
|
return Wide(m->root());
|
|
}
|
|
|
|
// WideInput collects the field values for NewWide. A borrowed field is a view:
|
|
// it must outlive the call, not the buffer the call returns.
|
|
struct WideInput {
|
|
bool Flag{};
|
|
std::uint8_t A8{};
|
|
std::uint16_t A16{};
|
|
std::uint32_t A32{};
|
|
std::uint64_t A64{};
|
|
std::int8_t S8{};
|
|
std::int16_t S16{};
|
|
std::int32_t S32{};
|
|
std::int64_t S64{};
|
|
float F32{};
|
|
double F64{};
|
|
std::array<std::uint8_t, 16> Id{};
|
|
std::string_view Name{};
|
|
std::span<const std::uint8_t> Blob{};
|
|
std::vector<std::span<const std::uint8_t>> Items{};
|
|
std::span<const std::uint8_t> Child{};
|
|
};
|
|
|
|
// NewWide writes a ZAP-encoded Wide message into a fresh buffer and returns it.
|
|
inline std::vector<std::uint8_t> NewWide(const WideInput& in) {
|
|
zap::Builder b(256, zap::kVersion1);
|
|
auto ob = b.start_object(kWideSize);
|
|
ob.set_bool(kWideFlagOff, in.Flag);
|
|
ob.set_u8(kWideA8Off, in.A8);
|
|
ob.set_u16(kWideA16Off, in.A16);
|
|
ob.set_u32(kWideA32Off, in.A32);
|
|
ob.set_u64(kWideA64Off, in.A64);
|
|
ob.set_u8(kWideS8Off, static_cast<std::uint8_t>(in.S8));
|
|
ob.set_u16(kWideS16Off, static_cast<std::uint16_t>(in.S16));
|
|
ob.set_u32(kWideS32Off, static_cast<std::uint32_t>(in.S32));
|
|
ob.set_u64(kWideS64Off, static_cast<std::uint64_t>(in.S64));
|
|
ob.set_u32(kWideF32Off, std::bit_cast<std::uint32_t>(in.F32));
|
|
ob.set_u64(kWideF64Off, std::bit_cast<std::uint64_t>(in.F64));
|
|
ob.set_bytes_fixed(kWideIdOff, std::span<const std::uint8_t>(in.Id));
|
|
ob.set_text(kWideNameOff, in.Name);
|
|
ob.set_bytes(kWideBlobOff, in.Blob);
|
|
auto items_list = b.start_list(0);
|
|
for (const auto& elem : in.Items) {
|
|
items_list.add_u32(static_cast<std::uint32_t>(elem.size()));
|
|
items_list.add_bytes(elem);
|
|
}
|
|
ob.set_list(kWideItemsOff, items_list.finish().first, static_cast<std::int64_t>(in.Items.size()));
|
|
if (!in.Child.empty()) {
|
|
auto nested = b.start_object(static_cast<std::int64_t>(in.Child.size()));
|
|
nested.set_bytes_fixed(0, in.Child);
|
|
ob.set_object(kWideChildOff, nested.finish());
|
|
}
|
|
ob.finish_as_root();
|
|
return b.finish();
|
|
}
|
|
|
|
// Nested-struct accessors, defined once every class above is complete.
|
|
inline ::probe::Inner Wide::Child() const { return ::probe::Inner(o_.object(kWideChildOff)); }
|
|
|
|
} // namespace probe
|