add msg.ToString()
All checks were successful
Linux arm64 / Build (push) Successful in 15s

This commit is contained in:
2025-03-04 20:26:42 +01:00
parent 81c9dbadd6
commit 7f8d9e3f96
14 changed files with 105 additions and 40 deletions

View File

@@ -7,12 +7,13 @@ namespace sp {
class PacketHandler;
using PacketMessage = Message<
option::MsgIdType<std::uint8_t>, // add id() operation
option::ReadOperations, // add read() operation
option::WriteOperations, // add write() operation
option::WriteId, // write id before data
option::Handler<PacketHandler> // add dispatch() operation
>;
option::MsgIdType<std::uint8_t>, // add id() operation
option::ReadOperations, // add read() operation
option::WriteOperations, // add write() operation
option::WriteId, // write id before data
option::Handler<PacketHandler>, // 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::PacketMessage, sp::option::StaticNumIdImpl<packetName>, \
sp::option::DispatchImpl<packetName##Packet>, sp::option::FieldsImpl<packetName##Fields>>
sp::option::DispatchImpl<packetName##Packet>, sp::option::FieldsImpl<packetName##Fields>>, \
sp::option::ToStringImpl<packetName##Packet>

View File

@@ -13,4 +13,6 @@ namespace sp {
template <typename TBase, typename... TOptions>
class MessageBase : public details::MessageImplBuilder<TBase, TOptions...>::Type {};
} // namespace sp
} // namespace sp
#include <sp/protocol/message/MessagePrinterImpl.h>

View File

@@ -1,23 +0,0 @@
#pragma once
#include <ostream>
#include <sp/common/Reflection.h>
#include <sp/protocol/message/MessagePrinterImpl.h>
namespace sp {
/**
* \brief Prints a message in a human readable string
*/
template <typename TBase, typename... TOptions>
std::ostream& operator<<(std::ostream& a_Stream, const sp::MessageBase<TBase, TOptions...>& a_Message) {
a_Stream
<< sp::GetClassName(a_Message)
<< sp::details::IdPrinter<TOptions...>::PrintMessageId()
<< "["
<< sp::details::PrintFields(a_Message.GetFields())
<< "]";
return a_Stream;
}
} // namespace sp

View File

@@ -37,8 +37,12 @@ struct MessageImplBuilder {
static const bool HasValidImpl = InterfaceOptions::HasValid && ImplOptions::HasFieldsImpl;
using Base6 = typename MessageImplProcessValidFields<Base5, HasValidImpl>::Type;
// Provide ToStringImpl() if possible
static const bool HasToStringImpl = InterfaceOptions::HasToString;
using Base7 = typename MessageImplProcessToString<Base6, ImplOptions, HasToStringImpl>::Type;
// The last BaseN must be taken as final type.
using Type = Base6;
using Type = Base7;
};
} // namespace details

View File

@@ -16,6 +16,11 @@ struct DispatchImpl {};
template <typename TFields>
struct FieldsImpl {};
// Print fields of the message, facilitate implementation of
// ToStringImpl()
template <typename TActual>
struct ToStringImpl {};
} // namespace option

View File

@@ -4,6 +4,20 @@ namespace sp {
namespace details {
// ToString impl
template <typename TBase, typename ParsedImplOptions, bool TImplement>
struct MessageImplProcessToString;
template <typename TBase, typename ParsedImplOptions>
struct MessageImplProcessToString<TBase, ParsedImplOptions, true> {
using Type = MessageImplToStringBase<TBase, typename ParsedImplOptions::ActualMessage>;
};
template <typename TBase, typename ParsedImplOptions>
struct MessageImplProcessToString<TBase, ParsedImplOptions, false> {
using Type = TBase;
};
// id impl
template <typename TBase, typename ParsedImplOptions, bool TImplement>

View File

@@ -33,8 +33,11 @@ struct MessageInterfaceBuilder {
// add write id functionality if write id and write was provided
using Base7 = typename MessageInterfaceProcessWriteId<Base6, ParsedOptions::HasWriteId && ParsedOptions::HasWriteOperations>::Type;
// The last Base7 must be taken as final type.
using Type = Base7;
// add ToString() if HasToString was provided
using Base8 = typename MessageInterfaceProcessToString<Base7, ParsedOptions::HasToString>::Type;
// The last Base8 must be taken as final type.
using Type = Base8;
};
} // namespace details

View File

@@ -101,5 +101,19 @@ struct MessageInterfaceProcessWriteId<TBase, false> {
using Type = TBase;
};
// Build to string
template <typename TBase, bool THasToString>
struct MessageInterfaceProcessToString;
template <typename TBase>
struct MessageInterfaceProcessToString<TBase, true> {
using Type = MessageInterfaceToStringBase<TBase>;
};
template <typename TBase>
struct MessageInterfaceProcessToString<TBase, false> {
using Type = TBase;
};
} // namespace details
} // namespace sp

View File

@@ -129,5 +129,17 @@ class MessageInterfaceWriteIdBase : public TBase {
}
};
// Debug print functionality chunk
template <typename TBase>
class MessageInterfaceToStringBase : public TBase {
public:
std::string ToString() const {
return ToStringImpl();
}
protected:
virtual std::string ToStringImpl() const = 0;
};
} // namespace details
} // namespace sp

View File

@@ -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<option::ValidCheckInterface, TOptions...> :
static const bool HasValid = true;
};
template <typename... TOptions>
struct MessageInterfaceParsedOptions<option::DebugPrint, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
static const bool HasToString = true;
};
} // namespace details
} // namespace sp

View File

@@ -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 <typename T>
struct Handler {

View File

@@ -1,8 +1,8 @@
#pragma once
#include <sstream>
#include <sp/protocol/MessageBase.h>
#include <sp/common/Templates.h>
#include <sp/common/Reflection.h>
namespace sp {
namespace details {
@@ -59,8 +59,8 @@ struct FieldPrinter {
};
template <unsigned int IAlignment, typename TContainer, typename... TFields>
struct FieldPrinter<sp::BitField<TContainer, TFields...>, IAlignment> {
static std::string PrintField(const sp::Field<sp::BitField<TContainer, TFields...>, IAlignment>& a_Field) {
struct FieldPrinter<BitField<TContainer, TFields...>, IAlignment> {
static std::string PrintField(const Field<BitField<TContainer, TFields...>, IAlignment>& a_Field) {
return "BitField<" + GetClassName<TContainer>() + ">[" + PrintFields(a_Field.GetValue().GetFields()) + "]";
}
};
@@ -78,5 +78,11 @@ std::string PrintFields(const std::tuple<TFields...>& a_Fields) {
return concat.substr(0, concat.size() - 2);
}
template <typename TBase, typename... TOptions>
std::string PrintMessage(const MessageBase<TBase, TOptions...>& a_Message) {
return sp::GetClassName(a_Message) + sp::details::IdPrinter<TOptions...>::PrintMessageId() + "[" +
sp::details::PrintFields(a_Message.GetFields()) + "]";
}
} // namespace details
} // namespace sp

View File

@@ -1,8 +1,24 @@
#pragma once
namespace sp {
template <typename TBase, typename... TOptions>
class MessageBase;
namespace details {
template <typename TBase, typename... TOptions>
std::string PrintMessage(const MessageBase<TBase, TOptions...>& a_Message);
// ID information chunk
template <typename TBase, typename TActual>
class MessageImplToStringBase : public TBase {
protected:
virtual std::string ToStringImpl() const override {
return PrintMessage(static_cast<const TActual&>(*this));
}
};
// ID information chunk
@@ -18,6 +34,8 @@ class MessageImplStaticNumIdBase : public TBase {
}
};
// Dispatch implementation chunk
template <typename TBase, typename TActual>
class MessageImplDispatchBase : public TBase {

View File

@@ -5,12 +5,11 @@
#include <sp/default/DefaultPacketDispatcher.h>
#include <sp/extensions/Extensions.h>
#include <sp/protocol/MessagePrinter.h>
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<UpgradeTowerPacket>();
upgradeTower2->Read(buffer);
std::cout << "Test : " << *upgradeTower2 << "\n";
std::cout << "Test : " << msg->ToString() << "\n";
sp::PacketFactory factory;
auto packet = factory.CreateMessage(msgId);