From e39f8de89836899a12a8e06108a91c1eb7058871 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sat, 15 Feb 2025 12:39:53 +0100 Subject: [PATCH] better fields access --- include/examples/DisconnectPacket.h | 13 +++++++++---- include/examples/KeepAlivePacket.h | 27 +++++++++++++++++++++------ include/sp/default/DefaultPacket.h | 20 +++++++++++--------- include/sp/protocol/Field.h | 5 +++++ include/sp/protocol/MessageBase.h | 5 +++++ src/main.cpp | 5 ++++- 6 files changed, 55 insertions(+), 20 deletions(-) diff --git a/include/examples/DisconnectPacket.h b/include/examples/DisconnectPacket.h index 6a69371..c6a51bb 100644 --- a/include/examples/DisconnectPacket.h +++ b/include/examples/DisconnectPacket.h @@ -4,10 +4,15 @@ #include #include -enum DisconnectPacketFields { - Reason = 0 -}; +enum DisconnectPacketFields { Reason = 0 }; using DisconnectFields = std::tuple; -DeclarePacket(Disconnect); \ No newline at end of file +DeclarePacket(Disconnect){ + public: + PacketConstructor(Disconnect) + + const std::string& GetReason() const { + return GetField(); + } +}; diff --git a/include/examples/KeepAlivePacket.h b/include/examples/KeepAlivePacket.h index 7cfe3f6..02b63e2 100644 --- a/include/examples/KeepAlivePacket.h +++ b/include/examples/KeepAlivePacket.h @@ -10,11 +10,26 @@ enum KeepAlivePacketFields { }; using KeepAliveFields = std::tuple< - std::uint64_t, //<- KeepAliveId + std::uint64_t, //<- KeepAliveId sp::BitField, //<- m_Tower - sp::Field //<- m_Upgrade - > ->; + sp::Field, //<- m_Tower + sp::Field //<- m_Upgrade + > + >; -DeclarePacket(KeepAlive); \ No newline at end of file +DeclarePacket(KeepAlive){ + public: + PacketConstructor(KeepAlive) + + std::uint64_t GetKeepAliveId() const { + return GetField(); + } + + std::uint16_t GetTowerId() const { + return GetField().GetField<0>(); + } + + std::uint8_t GetTowerUpgrade() const { + return GetField().GetField<1>(); + } +}; \ No newline at end of file diff --git a/include/sp/default/DefaultPacket.h b/include/sp/default/DefaultPacket.h index 43a0770..63b7ae4 100644 --- a/include/sp/default/DefaultPacket.h +++ b/include/sp/default/DefaultPacket.h @@ -13,15 +13,17 @@ using PacketMessage = Message, // add id() opera option::Handler // add dispatch() operation >; -#define DeclarePacket(packetName) \ - class packetName##Packet : public sp::MessageBase, \ - sp::option::DispatchImpl, sp::option::FieldsImpl> { \ - public: \ - packetName##Packet() {} \ - template \ - packetName##Packet(Args... args) { \ - Construct(args...); \ - } \ +#define PacketConstructor(packetName) \ + packetName##Packet() {} \ + template \ + packetName##Packet(Args... args) { \ + Construct(args...); \ } +#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 6bafa85..dc1f702 100644 --- a/include/sp/protocol/Field.h +++ b/include/sp/protocol/Field.h @@ -40,6 +40,11 @@ class BitField { return std::get(this->GetFields()).GetValue(); } + template + const auto& GetField() const { + return std::get(this->GetFields()).GetValue(); + } + private: template = sizeof...(T), bool> = true> void Apply(const std::tuple& args) {} diff --git a/include/sp/protocol/MessageBase.h b/include/sp/protocol/MessageBase.h index a489b7b..ce9a798 100644 --- a/include/sp/protocol/MessageBase.h +++ b/include/sp/protocol/MessageBase.h @@ -114,6 +114,11 @@ class MessageImplFieldsBase : public TBase { return std::get(GetFields()).GetValue(); } + template + const auto& GetField() const { + return std::get(GetFields()).GetValue(); + } + private: AllFields m_Fields; }; diff --git a/src/main.cpp b/src/main.cpp index 9eb8d25..d6d7347 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -30,7 +30,10 @@ int main() { auto keepAlive2 = std::make_unique(); keepAlive2->Read(buffer); - std::cout << "KeepAlive2 : " << keepAlive2->GetField() << "\n"; + std::cout << "KeepAlive2 : " << keepAlive2->GetKeepAliveId() << "\n"; + + keepAlive2->GetField().GetField<0>(); + std::cout << "Test : " << (unsigned) keepAlive2->GetTowerId() << "\n"; sp::PacketFactory factory; auto packet = factory.CreateMessage(msgId);