Files
Simple-Protocol-Lib/include/sp/protocol/message/MessagePrinterImpl.h
Persson-dev 77356ce749
All checks were successful
Linux arm64 / Build (push) Successful in 15s
better print override
2025-03-04 22:45:50 +01:00

72 lines
2.2 KiB
C++

#pragma once
#include <sp/common/Reflection.h>
#include <sp/common/Templates.h>
#include <sp/protocol/MessagePrinter.h>
namespace sp {
namespace details {
template <typename... TOptions>
struct IdPrinter {};
template <>
struct IdPrinter<> {
static std::string PrintMessageId() {
return "";
}
};
template <typename TOption, typename... TOptions>
struct IdPrinter<TOption, TOptions...> {
static std::string PrintMessageId() {
return IdPrinter<TOptions...>::PrintMessageId();
}
};
template <typename... TOptions, std::uintmax_t TId>
struct IdPrinter<option::StaticNumIdImpl<TId>, TOptions...> {
static std::string PrintMessageId() {
return "(Id: " + std::to_string(TId) + ")";
}
};
template <typename... TFields>
std::string PrintFields(const std::tuple<TFields...>& a_Fields);
template <typename TField, unsigned int IAlignment>
struct FieldPrinter {
static std::string PrintField(const sp::Field<TField, IAlignment>& a_Field) {
return Reflector<TField>::GetClassName() + "=" + PrintData(a_Field.GetValue());
}
};
template <unsigned int IAlignment, typename TContainer, typename... TFields>
struct FieldPrinter<BitField<TContainer, TFields...>, IAlignment> {
static std::string PrintField(const Field<BitField<TContainer, TFields...>, IAlignment>& a_Field) {
return "BitField<" + Reflector<TContainer>::GetClassName() + ">[" + PrintFields(a_Field.GetValue().GetFields()) + "]";
}
};
template <typename... TFields>
std::string PrintFields(const std::tuple<TFields...>& a_Fields) {
std::string concat;
TupleForEach(
[&concat](const auto& a_Field) {
using TField = typename std::decay<decltype(a_Field)>::type;
constexpr std::size_t alignment = TField::AlignmentValue;
concat += FieldPrinter<typename TField::StorageType, alignment>::PrintField(a_Field) + ", ";
},
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::GetBasicClassName(a_Message) + sp::details::IdPrinter<TOptions...>::PrintMessageId() + "[" +
sp::details::PrintFields(a_Message.GetFields()) + "]";
}
} // namespace details
} // namespace sp