From 7f8d9e3f9685b8c9070c574c9b917ab04d9ace3b Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Tue, 4 Mar 2025 20:26:42 +0100 Subject: [PATCH] add msg.ToString() --- include/sp/default/DefaultPacket.h | 16 +++++++------ include/sp/protocol/MessageBase.h | 4 +++- include/sp/protocol/MessagePrinter.h | 23 ------------------- .../sp/protocol/message/MessageImplBuilder.h | 6 ++++- .../sp/protocol/message/MessageImplOptions.h | 5 ++++ .../sp/protocol/message/MessageImplProcess.h | 14 +++++++++++ .../message/MessageInterfaceBuilder.h | 7 ++++-- .../message/MessageInterfaceProcess.h | 14 +++++++++++ .../protocol/message/MessageInterfacesImpl.h | 12 ++++++++++ .../message/MessageInterfacesOptions.h | 6 +++++ include/sp/protocol/message/MessageOptions.h | 3 +++ .../sp/protocol/message/MessagePrinterImpl.h | 12 +++++++--- include/sp/protocol/message/MessagesImpl.h | 18 +++++++++++++++ test/test_packets.cpp | 5 ++-- 14 files changed, 105 insertions(+), 40 deletions(-) delete mode 100644 include/sp/protocol/MessagePrinter.h diff --git a/include/sp/default/DefaultPacket.h b/include/sp/default/DefaultPacket.h index 24e8156..107a9ec 100644 --- a/include/sp/default/DefaultPacket.h +++ b/include/sp/default/DefaultPacket.h @@ -7,12 +7,13 @@ namespace sp { class PacketHandler; using PacketMessage = Message< - option::MsgIdType, // add id() operation - option::ReadOperations, // add read() operation - option::WriteOperations, // add write() operation - option::WriteId, // write id before data - option::Handler // add dispatch() operation ->; + option::MsgIdType, // add id() operation + option::ReadOperations, // add read() operation + option::WriteOperations, // add write() operation + option::WriteId, // write id before data + option::Handler, // add dispatch() operation + option::DebugPrint // add ToString() operator + >; #define PacketConstructor(packetName) \ packetName##Packet() {} \ @@ -23,7 +24,8 @@ using PacketMessage = Message< #define DeclarePacket(packetName) \ class packetName##Packet : public sp::MessageBase, \ - sp::option::DispatchImpl, sp::option::FieldsImpl> + sp::option::DispatchImpl, sp::option::FieldsImpl>, \ + sp::option::ToStringImpl diff --git a/include/sp/protocol/MessageBase.h b/include/sp/protocol/MessageBase.h index 628233b..80d0b8a 100644 --- a/include/sp/protocol/MessageBase.h +++ b/include/sp/protocol/MessageBase.h @@ -13,4 +13,6 @@ namespace sp { template class MessageBase : public details::MessageImplBuilder::Type {}; -} // namespace sp \ No newline at end of file +} // namespace sp + +#include \ No newline at end of file diff --git a/include/sp/protocol/MessagePrinter.h b/include/sp/protocol/MessagePrinter.h deleted file mode 100644 index 8bdb5bc..0000000 --- a/include/sp/protocol/MessagePrinter.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace sp { - -/** - * \brief Prints a message in a human readable string - */ -template -std::ostream& operator<<(std::ostream& a_Stream, const sp::MessageBase& a_Message) { - a_Stream - << sp::GetClassName(a_Message) - << sp::details::IdPrinter::PrintMessageId() - << "[" - << sp::details::PrintFields(a_Message.GetFields()) - << "]"; - return a_Stream; -} - -} // namespace sp \ No newline at end of file diff --git a/include/sp/protocol/message/MessageImplBuilder.h b/include/sp/protocol/message/MessageImplBuilder.h index 2ae61fd..b01219e 100644 --- a/include/sp/protocol/message/MessageImplBuilder.h +++ b/include/sp/protocol/message/MessageImplBuilder.h @@ -37,8 +37,12 @@ struct MessageImplBuilder { static const bool HasValidImpl = InterfaceOptions::HasValid && ImplOptions::HasFieldsImpl; using Base6 = typename MessageImplProcessValidFields::Type; + // Provide ToStringImpl() if possible + static const bool HasToStringImpl = InterfaceOptions::HasToString; + using Base7 = typename MessageImplProcessToString::Type; + // The last BaseN must be taken as final type. - using Type = Base6; + using Type = Base7; }; } // namespace details diff --git a/include/sp/protocol/message/MessageImplOptions.h b/include/sp/protocol/message/MessageImplOptions.h index 5a23f50..5944a8d 100644 --- a/include/sp/protocol/message/MessageImplOptions.h +++ b/include/sp/protocol/message/MessageImplOptions.h @@ -16,6 +16,11 @@ struct DispatchImpl {}; template struct FieldsImpl {}; +// Print fields of the message, facilitate implementation of +// ToStringImpl() +template +struct ToStringImpl {}; + } // namespace option diff --git a/include/sp/protocol/message/MessageImplProcess.h b/include/sp/protocol/message/MessageImplProcess.h index c377df1..31706f6 100644 --- a/include/sp/protocol/message/MessageImplProcess.h +++ b/include/sp/protocol/message/MessageImplProcess.h @@ -4,6 +4,20 @@ namespace sp { namespace details { +// ToString impl +template +struct MessageImplProcessToString; + +template +struct MessageImplProcessToString { + using Type = MessageImplToStringBase; +}; + +template +struct MessageImplProcessToString { + using Type = TBase; +}; + // id impl template diff --git a/include/sp/protocol/message/MessageInterfaceBuilder.h b/include/sp/protocol/message/MessageInterfaceBuilder.h index 310e2cd..7496a18 100644 --- a/include/sp/protocol/message/MessageInterfaceBuilder.h +++ b/include/sp/protocol/message/MessageInterfaceBuilder.h @@ -33,8 +33,11 @@ struct MessageInterfaceBuilder { // add write id functionality if write id and write was provided using Base7 = typename MessageInterfaceProcessWriteId::Type; - // The last Base7 must be taken as final type. - using Type = Base7; + // add ToString() if HasToString was provided + using Base8 = typename MessageInterfaceProcessToString::Type; + + // The last Base8 must be taken as final type. + using Type = Base8; }; } // namespace details diff --git a/include/sp/protocol/message/MessageInterfaceProcess.h b/include/sp/protocol/message/MessageInterfaceProcess.h index b6d7da2..9b4fb97 100644 --- a/include/sp/protocol/message/MessageInterfaceProcess.h +++ b/include/sp/protocol/message/MessageInterfaceProcess.h @@ -101,5 +101,19 @@ struct MessageInterfaceProcessWriteId { using Type = TBase; }; +// Build to string +template +struct MessageInterfaceProcessToString; + +template +struct MessageInterfaceProcessToString { + using Type = MessageInterfaceToStringBase; +}; + +template +struct MessageInterfaceProcessToString { + using Type = TBase; +}; + } // namespace details } // namespace sp \ No newline at end of file diff --git a/include/sp/protocol/message/MessageInterfacesImpl.h b/include/sp/protocol/message/MessageInterfacesImpl.h index f3f5ecb..72b813d 100644 --- a/include/sp/protocol/message/MessageInterfacesImpl.h +++ b/include/sp/protocol/message/MessageInterfacesImpl.h @@ -129,5 +129,17 @@ class MessageInterfaceWriteIdBase : public TBase { } }; +// Debug print functionality chunk +template +class MessageInterfaceToStringBase : public TBase { + public: + std::string ToString() const { + return ToStringImpl(); + } + + protected: + virtual std::string ToStringImpl() const = 0; +}; + } // namespace details } // namespace sp diff --git a/include/sp/protocol/message/MessageInterfacesOptions.h b/include/sp/protocol/message/MessageInterfacesOptions.h index 15ecb06..a311c16 100644 --- a/include/sp/protocol/message/MessageInterfacesOptions.h +++ b/include/sp/protocol/message/MessageInterfacesOptions.h @@ -16,6 +16,7 @@ struct MessageInterfaceParsedOptions<> { static const bool HasWriteId = false; static const bool HasHandler = false; static const bool HasValid = false; + static const bool HasToString = false; }; @@ -59,5 +60,10 @@ struct MessageInterfaceParsedOptions : static const bool HasValid = true; }; +template +struct MessageInterfaceParsedOptions : public MessageInterfaceParsedOptions { + static const bool HasToString = true; +}; + } // namespace details } // namespace sp diff --git a/include/sp/protocol/message/MessageOptions.h b/include/sp/protocol/message/MessageOptions.h index ff784cb..dadd1e0 100644 --- a/include/sp/protocol/message/MessageOptions.h +++ b/include/sp/protocol/message/MessageOptions.h @@ -24,6 +24,9 @@ struct LittleEndian {}; // Include validity check in public interface struct ValidCheckInterface {}; +// Add a ToString() method containing fields +struct DebugPrint {}; + // Define handler class template struct Handler { diff --git a/include/sp/protocol/message/MessagePrinterImpl.h b/include/sp/protocol/message/MessagePrinterImpl.h index 5955326..2637735 100644 --- a/include/sp/protocol/message/MessagePrinterImpl.h +++ b/include/sp/protocol/message/MessagePrinterImpl.h @@ -1,8 +1,8 @@ #pragma once #include -#include #include +#include namespace sp { namespace details { @@ -59,8 +59,8 @@ struct FieldPrinter { }; template -struct FieldPrinter, IAlignment> { - static std::string PrintField(const sp::Field, IAlignment>& a_Field) { +struct FieldPrinter, IAlignment> { + static std::string PrintField(const Field, IAlignment>& a_Field) { return "BitField<" + GetClassName() + ">[" + PrintFields(a_Field.GetValue().GetFields()) + "]"; } }; @@ -78,5 +78,11 @@ std::string PrintFields(const std::tuple& a_Fields) { return concat.substr(0, concat.size() - 2); } +template +std::string PrintMessage(const MessageBase& a_Message) { + return sp::GetClassName(a_Message) + sp::details::IdPrinter::PrintMessageId() + "[" + + sp::details::PrintFields(a_Message.GetFields()) + "]"; +} + } // namespace details } // namespace sp diff --git a/include/sp/protocol/message/MessagesImpl.h b/include/sp/protocol/message/MessagesImpl.h index 91875f2..5c4c947 100644 --- a/include/sp/protocol/message/MessagesImpl.h +++ b/include/sp/protocol/message/MessagesImpl.h @@ -1,8 +1,24 @@ #pragma once namespace sp { + +template +class MessageBase; + namespace details { +template +std::string PrintMessage(const MessageBase& a_Message); + +// ID information chunk +template +class MessageImplToStringBase : public TBase { + protected: + virtual std::string ToStringImpl() const override { + return PrintMessage(static_cast(*this)); + } +}; + // ID information chunk @@ -18,6 +34,8 @@ class MessageImplStaticNumIdBase : public TBase { } }; + + // Dispatch implementation chunk template class MessageImplDispatchBase : public TBase { diff --git a/test/test_packets.cpp b/test/test_packets.cpp index c22baad..59302b6 100644 --- a/test/test_packets.cpp +++ b/test/test_packets.cpp @@ -5,12 +5,11 @@ #include #include -#include class KeepAliveHandler : public sp::PacketHandler { void Handle(const KeepAlivePacket& packet) { std::cout << "KeepAlive handled !!\n"; - std::cout << packet << std::endl; + std::cout << packet.ToString() << std::endl; } void Handle(const DisconnectPacket& packet) { @@ -40,7 +39,7 @@ int main() { auto upgradeTower2 = std::make_unique(); upgradeTower2->Read(buffer); - std::cout << "Test : " << *upgradeTower2 << "\n"; + std::cout << "Test : " << msg->ToString() << "\n"; sp::PacketFactory factory; auto packet = factory.CreateMessage(msgId);