WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
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>...>::GetTypeName(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:69
std::string GetTypeName(const T &type)
Returns the type name of an object.
Definition Demangle.h:27
Struct serialization template.
Definition Struct.h:39

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 GetTypeName(): function that returns the type name
  • 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 GetTypeName(), GetSize(), and GetSchema() functions should be marked constexpr. GetTypeName() 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>.