37template <
typename T,
typename... I>
67template <
typename T,
typename... I>
69 std::span<uint8_t> out, T&& value,
71 typename Struct<typename std::remove_cvref_t<T>,
72 typename std::remove_cvref_t<I>...>;
74 Struct<typename std::remove_cvref_t<T>,
76 } -> std::convertible_to<std::string_view>;
78 Struct<typename std::remove_cvref_t<T>,
79 typename std::remove_cvref_t<I>...>
::GetSize(info...)
80 } -> std::convertible_to<size_t>;
82 Struct<typename std::remove_cvref_t<T>,
83 typename std::remove_cvref_t<I>...>
::GetSchema(info...)
84 } -> std::convertible_to<std::string_view>;
86 Struct<typename std::remove_cvref_t<T>,
87 typename std::remove_cvref_t<I>...>
::Unpack(
in, info...)
88 } -> std::same_as<typename std::remove_cvref_t<T>>;
89 Struct<typename std::remove_cvref_t<T>,
90 typename std::remove_cvref_t<I>...>
::Pack(out, std::forward<T>(value),
101template <
typename T,
typename... I>
104 requires(T* out, std::span<const uint8_t>
in,
const I&... info) {
105 Struct<typename std::remove_cvref_t<T>,
106 typename std::remove_cvref_t<I>...>::UnpackInto(out,
in, info...);
118template <
typename T,
typename... I>
123 Struct<typename std::remove_cvref_t<T>,
135template <
typename T,
typename... I>
137inline T
UnpackStruct(std::span<const uint8_t> data,
const I&... info) {
139 return S::Unpack(data, info...);
152template <
typename T,
size_t Offset,
typename... I>
153 requires StructSerializable<T, I...>
154inline T
UnpackStruct(std::span<const uint8_t> data,
const I&... info) {
156 return S::Unpack(data.subspan(Offset), info...);
166template <
typename T,
typename... I>
167 requires StructSerializable<T, I...>
168inline void PackStruct(std::span<uint8_t> data, T&& value,
const I&... info) {
170 typename std::remove_cvref_t<I>...>;
171 S::Pack(data, std::forward<T>(value), info...);
183template <
size_t Offset,
typename T,
typename... I>
184 requires StructSerializable<T, I...>
185inline void PackStruct(std::span<uint8_t> data, T&& value,
const I&... info) {
187 typename std::remove_cvref_t<I>...>;
188 S::Pack(data.subspan(Offset), std::forward<T>(value), info...);
198template <
typename T,
typename... I>
199 requires StructSerializable<T, I...>
204 S::UnpackInto(out, data, info...);
206 *out = UnpackStruct<T>(data, info...);
220template <
size_t Offset,
typename T,
typename... I>
221 requires StructSerializable<T, I...>
226 S::UnpackInto(out, data.subspan(Offset), info...);
228 *out = UnpackStruct<T, Offset>(data, info...);
239template <
typename T,
typename... I>
240 requires StructSerializable<T, I...>
253template <
typename T,
typename... I>
254 requires StructSerializable<T, I...>
257 return S::GetSize(info...);
260template <
typename T,
size_t N,
typename... I>
261 requires StructSerializable<T, I...>
264 if constexpr (
sizeof...(I) == 0 &&
268 if constexpr (N == std::dynamic_extent) {
277 "["_ct_string, NumToCtString<N>(),
"]"_ct_string);
280 if constexpr (N == std::dynamic_extent) {
288template <
typename T,
size_t N,
typename... I>
289 requires StructSerializable<T, I...>
292 if constexpr (
sizeof...(I) == 0 &&
294 constexpr auto schema = S::GetSchema(info...);
296 if constexpr (N == std::dynamic_extent) {
298 ct_string<
char, std::char_traits<char>, schema.
size()>{schema},
302 ct_string<
char, std::char_traits<char>, schema.
size()>{schema},
303 "["_ct_string, NumToCtString<N>(),
"]"_ct_string);
306 if constexpr (N == std::dynamic_extent) {
309 return fmt::format(
"{}[{}]", S::GetSchema(info...), N);
314template <
typename T,
typename... I>
315 requires StructSerializable<T, I...>
318 return S::GetSchema(info...);
321template <
typename T,
typename... I>
322 requires StructSerializable<T, I...>
325 auto schema = S::GetSchema(info...);
326 return {
reinterpret_cast<const uint8_t*
>(schema.data()), schema.size()};
329template <
typename T,
typename... I>
330 requires StructSerializable<T, I...>
332 std::invocable<std::string_view, std::string_view>
auto fn,
335 typename std::remove_cvref_t<I>...>;
337 S::ForEachNested(fn, info...);
342template <
typename T,
typename... I>
343 requires StructSerializable<T, I...>
353 m_buf = std::move(rhs.m_buf);
357 template <
typename U,
typename F>
359#if __cpp_lib_ranges >= 201911L
360 std::ranges::range<U> &&
361 std::convertible_to<std::ranges::range_value_t<U>, T> &&
363 std::invocable<F, std::span<const uint8_t>>
364 void Write(U&& data,
F&& func,
const I&... info) {
365 auto size = S::GetSize(info...);
366 if ((std::size(data) * size) < 256) {
370 for (
auto&& val : data) {
371 S::Pack(std::span<uint8_t>{std::to_address(out), size},
372 std::forward<decltype(val)>(val), info...);
375 func(std::span<uint8_t>{buf, out});
377 std::scoped_lock lock{m_mutex};
378 m_buf.resize(std::size(data) * size);
379 auto out = m_buf.begin();
380 for (
auto&& val : data) {
381 S::Pack(std::span<uint8_t>{std::to_address(out), size},
382 std::forward<decltype(val)>(val), info...);
391 std::vector<uint8_t> m_buf;
397template <
typename T,
size_t N,
typename... I>
398 requires StructSerializable<T, I...>
401 return MakeStructArrayTypeString<T, N>(info...);
403 static constexpr size_t GetSize(
const I&... info) {
404 return N * GetStructSize<T>(info...);
407 return MakeStructArraySchema<T, N>(info...);
409 static std::array<T, N>
Unpack(std::span<const uint8_t> data,
411 auto size = GetStructSize<T>(info...);
412 std::array<T, N> result;
413 for (
size_t i = 0; i < N; ++i) {
414 result[i] = UnpackStruct<T, 0>(data, info...);
415 data = data.subspan(size);
419 static void Pack(std::span<uint8_t> data, std::span<const T, N> values,
421 auto size = GetStructSize<T>(info...);
422 std::span<uint8_t> unsizedData = data;
423 for (
auto&& val : values) {
425 unsizedData = unsizedData.subspan(size);
428 static void UnpackInto(std::array<T, N>* out, std::span<const uint8_t> data,
430 UnpackInto(std::span{*out}, data, info...);
433 static void UnpackInto(std::span<T, N> out, std::span<const uint8_t> data,
435 auto size = GetStructSize<T>(info...);
436 std::span<const uint8_t> unsizedData = data;
437 for (
size_t i = 0; i < N; ++i) {
439 unsizedData = unsizedData.subspan(size);
451 static constexpr size_t GetSize() {
return 1; }
453 static bool Unpack(std::span<const uint8_t> data) {
return data[0]; }
454 static void Pack(std::span<uint8_t> data,
bool value) {
455 data[0] =
static_cast<char>(value ? 1 : 0);
466 static constexpr size_t GetSize() {
return 1; }
468 static uint8_t
Unpack(std::span<const uint8_t> data) {
return data[0]; }
469 static void Pack(std::span<uint8_t> data, uint8_t value) { data[0] = value; }
479 static constexpr size_t GetSize() {
return 1; }
481 static int8_t
Unpack(std::span<const uint8_t, 1> data) {
return data[0]; }
482 static void Pack(std::span<uint8_t, 1> data, int8_t value) {
494 static constexpr size_t GetSize() {
return 2; }
496 static uint16_t
Unpack(std::span<const uint8_t> data) {
499 static void Pack(std::span<uint8_t> data, uint16_t value) {
511 static constexpr size_t GetSize() {
return 2; }
513 static int16_t
Unpack(std::span<const uint8_t> data) {
516 static void Pack(std::span<uint8_t> data, int16_t value) {
528 static constexpr size_t GetSize() {
return 4; }
530 static uint32_t
Unpack(std::span<const uint8_t> data) {
533 static void Pack(std::span<uint8_t> data, uint32_t value) {
545 static constexpr size_t GetSize() {
return 4; }
547 static int32_t
Unpack(std::span<const uint8_t> data) {
550 static void Pack(std::span<uint8_t> data, int32_t value) {
562 static constexpr size_t GetSize() {
return 8; }
564 static uint64_t
Unpack(std::span<const uint8_t> data) {
567 static void Pack(std::span<uint8_t> data, uint64_t value) {
579 static constexpr size_t GetSize() {
return 8; }
581 static int64_t
Unpack(std::span<const uint8_t> data) {
584 static void Pack(std::span<uint8_t> data, int64_t value) {
596 static constexpr size_t GetSize() {
return 4; }
598 static float Unpack(std::span<const uint8_t> data) {
601 static void Pack(std::span<uint8_t> data,
float value) {
613 static constexpr size_t GetSize() {
return 8; }
615 static double Unpack(std::span<const uint8_t> data) {
618 static void Pack(std::span<uint8_t> data,
double value) {
This file implements the C++20 <bit> header.
void Write(U &&data, F &&func, const I &... info)
Definition: Struct.h:364
StructArrayBuffer()=default
StructArrayBuffer(StructArrayBuffer &&rhs)
Definition: Struct.h:351
StructArrayBuffer & operator=(StructArrayBuffer &&rhs)
Definition: Struct.h:352
StructArrayBuffer(const StructArrayBuffer &)=delete
StructArrayBuffer & operator=(const StructArrayBuffer &)=delete
This class is a wrapper around std::array that does compile time size checking.
Definition: array.h:26
Specifies that a struct type has nested struct declarations.
Definition: Struct.h:119
Specifies that a type is capable of in-place raw struct deserialization.
Definition: Struct.h:102
Specifies that a type is capable of raw struct serialization and deserialization.
Definition: Struct.h:68
basic_string_view< char > string_view
Definition: core.h:501
constexpr auto in(type t, int set) -> bool
Definition: core.h:611
static constexpr const unit_t< compound_unit< charge::coulomb, inverse< substance::mol > > > F(N_A *e)
Faraday constant.
std::string GetTypeString(const google::protobuf::Message &msg)
uint32_t read32le(const void *P)
Definition: Endian.h:383
uint64_t read64le(const void *P)
Definition: Endian.h:384
void write32le(void *P, uint32_t V)
Definition: Endian.h:418
uint16_t read16le(const void *P)
Definition: Endian.h:382
void write64le(void *P, uint64_t V)
Definition: Endian.h:419
void write16le(void *P, uint16_t V)
Definition: Endian.h:417
Definition: ntcore_cpp.h:26
constexpr auto GetStructTypeString(const I &... info)
Get the type string for a raw struct serializable type.
Definition: Struct.h:241
constexpr auto Concat(ct_string< Char, Traits, N1 > const &s1, ct_string< Char, Traits, N > const &... s)
Concatenates multiple fixed_strings into a larger fixed_string at compile time.
Definition: ct_string.h:168
constexpr auto MakeStructArraySchema(const I &... info)
Definition: Struct.h:290
void UnpackStructInto(T *out, std::span< const uint8_t > data, const I &... info)
Unpack a serialized struct into an existing object, overwriting its contents.
Definition: Struct.h:200
constexpr auto MakeStructArrayTypeString(const I &... info)
Definition: Struct.h:262
T UnpackStruct(std::span< const uint8_t > data, const I &... info)
Unpack a serialized struct.
Definition: Struct.h:137
constexpr std::span< const uint8_t > GetStructSchemaBytes(const I &... info)
Definition: Struct.h:323
constexpr std::string_view GetStructSchema(const I &... info)
Definition: Struct.h:316
constexpr size_t GetStructSize(const I &... info)
Get the size for a raw struct serializable type.
Definition: Struct.h:255
void ForEachStructSchema(std::invocable< std::string_view, std::string_view > auto fn, const I &... info)
Definition: Struct.h:331
constexpr bool is_constexpr(Lambda)
Definition: type_traits.h:81
void PackStruct(std::span< uint8_t > data, T &&value, const I &... info)
Pack a serialized struct.
Definition: Struct.h:168
static constexpr size_t GetSize()
Definition: Struct.h:451
static constexpr std::string_view GetTypeString()
Definition: Struct.h:450
static constexpr std::string_view GetSchema()
Definition: Struct.h:452
static bool Unpack(std::span< const uint8_t > data)
Definition: Struct.h:453
static void Pack(std::span< uint8_t > data, bool value)
Definition: Struct.h:454
static constexpr std::string_view GetTypeString()
Definition: Struct.h:612
static void Pack(std::span< uint8_t > data, double value)
Definition: Struct.h:618
static constexpr std::string_view GetSchema()
Definition: Struct.h:614
static double Unpack(std::span< const uint8_t > data)
Definition: Struct.h:615
static constexpr size_t GetSize()
Definition: Struct.h:613
static constexpr std::string_view GetSchema()
Definition: Struct.h:597
static constexpr std::string_view GetTypeString()
Definition: Struct.h:595
static float Unpack(std::span< const uint8_t > data)
Definition: Struct.h:598
static void Pack(std::span< uint8_t > data, float value)
Definition: Struct.h:601
static constexpr size_t GetSize()
Definition: Struct.h:596
static constexpr size_t GetSize()
Definition: Struct.h:511
static int16_t Unpack(std::span< const uint8_t > data)
Definition: Struct.h:513
static void Pack(std::span< uint8_t > data, int16_t value)
Definition: Struct.h:516
static constexpr std::string_view GetTypeString()
Definition: Struct.h:510
static constexpr std::string_view GetSchema()
Definition: Struct.h:512
static void Pack(std::span< uint8_t > data, int32_t value)
Definition: Struct.h:550
static constexpr size_t GetSize()
Definition: Struct.h:545
static int32_t Unpack(std::span< const uint8_t > data)
Definition: Struct.h:547
static constexpr std::string_view GetTypeString()
Definition: Struct.h:544
static constexpr std::string_view GetSchema()
Definition: Struct.h:546
static constexpr size_t GetSize()
Definition: Struct.h:579
static constexpr std::string_view GetSchema()
Definition: Struct.h:580
static constexpr std::string_view GetTypeString()
Definition: Struct.h:578
static void Pack(std::span< uint8_t > data, int64_t value)
Definition: Struct.h:584
static int64_t Unpack(std::span< const uint8_t > data)
Definition: Struct.h:581
static void Pack(std::span< uint8_t, 1 > data, int8_t value)
Definition: Struct.h:482
static constexpr std::string_view GetSchema()
Definition: Struct.h:480
static constexpr size_t GetSize()
Definition: Struct.h:479
static int8_t Unpack(std::span< const uint8_t, 1 > data)
Definition: Struct.h:481
static constexpr std::string_view GetTypeString()
Definition: Struct.h:478
static void UnpackInto(std::array< T, N > *out, std::span< const uint8_t > data, const I &... info)
Definition: Struct.h:428
static std::array< T, N > Unpack(std::span< const uint8_t > data, const I &... info)
Definition: Struct.h:409
static constexpr auto GetTypeString(const I &... info)
Definition: Struct.h:400
static void UnpackInto(std::span< T, N > out, std::span< const uint8_t > data, const I &... info)
Definition: Struct.h:433
static constexpr size_t GetSize(const I &... info)
Definition: Struct.h:403
static constexpr auto GetSchema(const I &... info)
Definition: Struct.h:406
static void Pack(std::span< uint8_t > data, std::span< const T, N > values, const I &... info)
Definition: Struct.h:419
static constexpr std::string_view GetTypeString()
Definition: Struct.h:493
static void Pack(std::span< uint8_t > data, uint16_t value)
Definition: Struct.h:499
static constexpr size_t GetSize()
Definition: Struct.h:494
static constexpr std::string_view GetSchema()
Definition: Struct.h:495
static uint16_t Unpack(std::span< const uint8_t > data)
Definition: Struct.h:496
static uint32_t Unpack(std::span< const uint8_t > data)
Definition: Struct.h:530
static void Pack(std::span< uint8_t > data, uint32_t value)
Definition: Struct.h:533
static constexpr std::string_view GetSchema()
Definition: Struct.h:529
static constexpr std::string_view GetTypeString()
Definition: Struct.h:527
static constexpr size_t GetSize()
Definition: Struct.h:528
static constexpr std::string_view GetTypeString()
Definition: Struct.h:561
static uint64_t Unpack(std::span< const uint8_t > data)
Definition: Struct.h:564
static constexpr size_t GetSize()
Definition: Struct.h:562
static constexpr std::string_view GetSchema()
Definition: Struct.h:563
static void Pack(std::span< uint8_t > data, uint64_t value)
Definition: Struct.h:567
static constexpr std::string_view GetSchema()
Definition: Struct.h:467
static void Pack(std::span< uint8_t > data, uint8_t value)
Definition: Struct.h:469
static uint8_t Unpack(std::span< const uint8_t > data)
Definition: Struct.h:468
static constexpr std::string_view GetTypeString()
Definition: Struct.h:465
static constexpr size_t GetSize()
Definition: Struct.h:466
Struct serialization template.
Definition: Struct.h:38
Fixed length string (array of character) for compile time use.
Definition: ct_string.h:29
constexpr auto size() const noexcept
Definition: ct_string.h:124
static void Pack(std::span< uint8_t > data, const frc::DifferentialDriveWheelVoltages &value)
static void ForEachNested(std::invocable< std::string_view, std::string_view > auto fn)
Definition: Transform3dStruct.h:27
static constexpr std::string_view GetTypeString()
Definition: DifferentialDriveWheelVoltagesStruct.h:14
static constexpr std::string_view GetSchema()
Definition: DifferentialDriveWheelVoltagesStruct.h:18
static frc::DifferentialDriveWheelVoltages Unpack(std::span< const uint8_t > data)
static constexpr size_t GetSize()
Definition: DifferentialDriveWheelVoltagesStruct.h:17
::std::mutex mutex
Definition: mutex.h:17
#define S(label, offset, message)
Definition: Errors.h:119
auto format(wformat_string< T... > fmt, T &&... args) -> std::wstring
Definition: xchar.h:108