fields builder

This commit is contained in:
2025-02-06 18:01:27 +01:00
parent c3a54d8e5b
commit 8ecfb95167
5 changed files with 28 additions and 10 deletions

View File

@@ -4,18 +4,10 @@
#include <sp/protocol/Field.h>
#include <sp/protocol/MessageBase.h>
enum PacketId {
KeepAlive = 0,
};
enum KeepAlivePacketFields {
KeepAliveId = 0
};
using KeepAliveFields = std::tuple<sp::Field<std::uint64_t>>;
using KeepAliveFields = sp::FieldsBuilder<std::uint64_t>::Type;
class KeepAlivePacket : public sp::MessageBase<sp::PacketMessage,
sp::option::StaticNumIdImpl<KeepAlive>, // provide idImpl() if needed
sp::option::DispatchImpl<KeepAlivePacket>, // provide dispatchImpl() if needed
sp::option::FieldsImpl<KeepAliveFields> // provide access to fields
> {};
DeclarePacket(KeepAlive);

View File

@@ -1,5 +1,9 @@
#pragma once
enum PacketId {
KeepAlive
};
#include <examples/KeepAlivePacket.h>
using AllPackets = std::tuple<KeepAlivePacket>;

View File

@@ -13,4 +13,10 @@ using PacketMessage = Message<
option::Handler<PacketHandler> // add dispatch() operation
>;
#define DeclarePacket(packetName) class packetName##Packet : public sp::MessageBase<sp::PacketMessage,\
sp::option::StaticNumIdImpl<packetName>, \
sp::option::DispatchImpl<packetName##Packet>, \
sp::option::FieldsImpl<packetName##Fields> \
> {}
} // namespace sp

View File

@@ -1,6 +1,7 @@
#pragma once
#include <sp/common/DataBuffer.h>
#include <sp/protocol/Templates.h>
namespace sp {
@@ -62,4 +63,18 @@ class FieldWriter {
DataBuffer& m_Buffer;
};
template <typename... TFields>
struct FieldsBuilder {};
template <>
struct FieldsBuilder<> {
using Type = std::tuple<>;
};
template <typename TField, typename... TFields>
struct FieldsBuilder<TField, TFields...> {
using Type = sp::tuple_cat_t<std::tuple<Field<TField>>, typename FieldsBuilder<TFields...>::Type>;
};
} // namespace sp

View File

@@ -16,6 +16,7 @@ int main() {
KeepAliveHandler handler;
msg->Dispatch(handler);
//TODO: constructor
keepAlive->GetField<KeepAliveId>() = 69;
sp::DataBuffer buffer;
msg->Write(buffer);