From 8ecfb951674b7cbb9c111f883056dce71f43c175 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Thu, 6 Feb 2025 18:01:27 +0100 Subject: [PATCH] fields builder --- include/examples/KeepAlivePacket.h | 12 ++---------- include/examples/PacketExample.h | 4 ++++ include/sp/default/DefaultPacket.h | 6 ++++++ include/sp/protocol/Field.h | 15 +++++++++++++++ src/main.cpp | 1 + 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/include/examples/KeepAlivePacket.h b/include/examples/KeepAlivePacket.h index 980ffa1..9bad650 100644 --- a/include/examples/KeepAlivePacket.h +++ b/include/examples/KeepAlivePacket.h @@ -4,18 +4,10 @@ #include #include -enum PacketId { - KeepAlive = 0, -}; - enum KeepAlivePacketFields { KeepAliveId = 0 }; -using KeepAliveFields = std::tuple>; +using KeepAliveFields = sp::FieldsBuilder::Type; -class KeepAlivePacket : public sp::MessageBase, // provide idImpl() if needed - sp::option::DispatchImpl, // provide dispatchImpl() if needed - sp::option::FieldsImpl // provide access to fields - > {}; \ No newline at end of file +DeclarePacket(KeepAlive); \ No newline at end of file diff --git a/include/examples/PacketExample.h b/include/examples/PacketExample.h index c4f068d..c679026 100644 --- a/include/examples/PacketExample.h +++ b/include/examples/PacketExample.h @@ -1,5 +1,9 @@ #pragma once +enum PacketId { + KeepAlive +}; + #include using AllPackets = std::tuple; diff --git a/include/sp/default/DefaultPacket.h b/include/sp/default/DefaultPacket.h index bf2beb0..fb629f4 100644 --- a/include/sp/default/DefaultPacket.h +++ b/include/sp/default/DefaultPacket.h @@ -13,4 +13,10 @@ using PacketMessage = Message< option::Handler // add dispatch() operation >; +#define DeclarePacket(packetName) class packetName##Packet : public sp::MessageBase, \ + sp::option::DispatchImpl, \ + sp::option::FieldsImpl \ + > {} + } // namespace sp diff --git a/include/sp/protocol/Field.h b/include/sp/protocol/Field.h index fd8309a..ca385fe 100644 --- a/include/sp/protocol/Field.h +++ b/include/sp/protocol/Field.h @@ -1,6 +1,7 @@ #pragma once #include +#include namespace sp { @@ -62,4 +63,18 @@ class FieldWriter { DataBuffer& m_Buffer; }; +template +struct FieldsBuilder {}; + +template <> +struct FieldsBuilder<> { + using Type = std::tuple<>; +}; + +template +struct FieldsBuilder { + using Type = sp::tuple_cat_t>, typename FieldsBuilder::Type>; +}; + + } // namespace sp diff --git a/src/main.cpp b/src/main.cpp index 9c0f3e3..5b42916 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,6 +16,7 @@ int main() { KeepAliveHandler handler; msg->Dispatch(handler); + //TODO: constructor keepAlive->GetField() = 69; sp::DataBuffer buffer; msg->Write(buffer);