29#include <wpi/Demangle.h>
30#include <wpi/ct_string.h>
31#include <wpi/struct/Struct.h>
43 typename SerdeType<typename std::remove_cvref_t<T>>;
47 SerdeType<typename std::remove_cvref_t<T>>::GetSchemaHash()
48 } -> std::convertible_to<std::string_view>;
51 SerdeType<typename std::remove_cvref_t<T>>::GetSchema()
52 } -> std::convertible_to<std::string_view>;
55 SerdeType<typename std::remove_cvref_t<T>>::Unpack(packet)
56 } -> std::same_as<typename std::remove_cvref_t<T>>;
59 SerdeType<typename std::remove_cvref_t<T>>::Pack(packet, value)
60 } -> std::same_as<void>;
71 explicit Packet(
int initialCapacity = 0) : packetData(initialCapacity) {}
77 explicit Packet(std::vector<uint8_t> data);
88 inline const std::vector<uint8_t>&
GetData() {
return packetData; }
94 inline size_t GetDataSize()
const {
return packetData.size(); }
96 template <
typename T,
typename... I>
97 requires wpi::StructSerializable<T, I...>
98 inline void Pack(
const T& value) {
101 size_t newWritePos = writePos + wpi::GetStructSize<T, I...>();
102 packetData.resize(newWritePos);
105 std::span<uint8_t>{packetData.begin() + writePos, packetData.end()},
108 writePos = newWritePos;
111 template <
typename T>
113 inline void Pack(
const T& value) {
117 template <
typename T,
typename... I>
118 requires wpi::StructSerializable<T, I...>
121 T ret = wpi::UnpackStruct<T, I...>(
122 std::span<uint8_t>{packetData.begin() + readPos, packetData.end()});
123 readPos += wpi::GetStructSize<T, I...>();
127 template <
typename T>
138 std::vector<uint8_t> packetData{};
145concept arithmetic = std::integral<T> || std::floating_point<T>;
152 uint8_t len = packet.
Unpack<uint8_t>();
155 for (
size_t i = 0; i < len; i++) {
156 ret.push_back(packet.
Unpack<T>());
160 static void Pack(
Packet& packet,
const std::vector<T>& value) {
161 packet.
Pack<uint8_t>(value.size());
162 for (
const auto& thing : value) {
163 packet.
Pack<T>(thing);
180 requires(PhotonStructSerializable<T> || arithmetic<T>)
183 if (packet.
Unpack<uint8_t>() == 1u) {
184 return packet.
Unpack<T>();
189 static void Pack(
Packet& packet,
const std::optional<T>& value) {
190 packet.
Pack<uint8_t>(value.has_value());
192 packet.
Pack<T>(*value);
A packet that holds byte-packed data to be sent over NetworkTables.
Definition Packet.h:66
void Pack(const T &value)
Definition Packet.h:113
T Unpack()
Definition Packet.h:129
size_t GetDataSize() const
Returns the number of bytes in the data.
Definition Packet.h:94
T Unpack()
Definition Packet.h:119
bool operator==(const Packet &right) const
bool operator!=(const Packet &right) const
Packet(int initialCapacity=0)
Constructs an empty packet.
Definition Packet.h:71
const std::vector< uint8_t > & GetData()
Returns the packet data.
Definition Packet.h:88
void Clear()
Clears the packet and resets the read and write positions.
void Pack(const T &value)
Definition Packet.h:98
Packet(std::vector< uint8_t > data)
Constructs a packet with the given data.
Definition VisionEstimation.h:32
static void Pack(Packet &packet, const std::optional< T > &value)
Definition Packet.h:189
static std::optional< T > Unpack(Packet &packet)
Definition Packet.h:182
static constexpr std::string_view GetSchema()
Definition Packet.h:200
static constexpr std::string_view GetSchemaHash()
Definition Packet.h:195
static constexpr std::string_view GetSchema()
Definition Packet.h:171
static std::vector< T > Unpack(Packet &packet)
Definition Packet.h:151
static constexpr std::string_view GetSchemaHash()
Definition Packet.h:166
static void Pack(Packet &packet, const std::vector< T > &value)
Definition Packet.h:160