WPILibC++ 2024.3.2
wpi::StructSerializable Concept Reference

Specifies that a type is capable of raw struct serialization and deserialization. More...

#include <wpi/struct/Struct.h>

Concept definition

template<typename T, typename... I>
concept wpi::StructSerializable = requires(std::span<const uint8_t> in,
std::span<uint8_t> out, T&& value,
const I&... info) {
typename Struct<typename std::remove_cvref_t<T>,
typename std::remove_cvref_t<I>...>;
{
Struct<typename std::remove_cvref_t<T>,
typename std::remove_cvref_t<I>...>::GetTypeString(info...)
} -> std::convertible_to<std::string_view>;
{
typename std::remove_cvref_t<I>...>::GetSize(info...)
} -> std::convertible_to<size_t>;
{
Struct<typename std::remove_cvref_t<T>,
typename std::remove_cvref_t<I>...>::GetSchema(info...)
} -> std::convertible_to<std::string_view>;
{
Struct<typename std::remove_cvref_t<T>,
typename std::remove_cvref_t<I>...>::Unpack(in, info...)
} -> std::same_as<typename std::remove_cvref_t<T>>;
Struct<typename std::remove_cvref_t<T>,
typename std::remove_cvref_t<I>...>::Pack(out, std::forward<T>(value),
info...);
}
Specifies that a type is capable of raw struct serialization and deserialization.
Definition: Struct.h:68
constexpr auto in(type t, int set) -> bool
Definition: core.h:611
Struct serialization template.
Definition: Struct.h:38
static void Pack(std::span< uint8_t > data, const frc::DifferentialDriveWheelVoltages &value)
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

Detailed Description

Specifies that a type is capable of raw struct serialization and deserialization.

This is designed for serializing small fixed-size data structures in the fastest and most compact means possible. Serialization consists of writing values into a mutable std::span and deserialization consists of reading values from an immutable std::span.

Implementations must define a template specialization for wpi::Struct with T being the type that is being serialized/deserialized, with the following static members (as enforced by this concept):

  • std::string_view GetTypeString(): function that returns the type string
  • size_t GetSize(): function that returns the structure size in bytes
  • std::string_view GetSchema(): function that returns the struct schema
  • T Unpack(std::span<const uint8_t>): function for deserialization
  • void Pack(std::span<uint8_t>, T&& value): function for serialization

If possible, the GetTypeString(), GetSize(), and GetSchema() functions should be marked constexpr. GetTypeString() and GetSchema() may return types other than std::string_view, as long as the return value is convertible to std::string_view.

If the struct has nested structs, implementations should also meet the requirements of HasNestedStruct<T>.