Files
Simple-Protocol-Lib/include/sp/protocol/message/MessagePrinterImpl.h
Persson-dev a04dc67213
Some checks failed
Linux arm64 / Build (push) Failing after 12s
only use stream for serialisation
2025-03-13 16:08:53 +01:00

79 lines
2.4 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) + ")";
}
};
} // namespace details
template <typename... TFields>
std::ostream& operator<<(std::ostream& a_Stream, const std::tuple<TFields...>& a_Fields);
template <typename TField, unsigned int IAlignment>
struct FieldPrinter {
static std::ostream& PrintField(std::ostream& a_Stream, const sp::Field<TField, IAlignment>& a_Field) {
return a_Stream << sp::Reflector<TField>::GetClassName() << "=" << PrintableField<TField>(a_Field.GetValue());
}
};
template <unsigned int IAlignment, typename TContainer, typename... TFields>
struct FieldPrinter<sp::BitField<TContainer, TFields...>, IAlignment> {
static std::ostream& PrintField(
std::ostream& a_Stream, const sp::Field<sp::BitField<TContainer, TFields...>, IAlignment>& a_Field) {
a_Stream << "BitField<" << sp::Reflector<TContainer>::GetClassName() << ">[";
a_Stream << a_Field.GetValue().GetFields() << "]";
return a_Stream;
}
};
template <typename... TFields>
std::ostream& operator<<(std::ostream& a_Stream, const std::tuple<TFields...>& a_Fields) {
bool first = true;
TupleForEach(
[&a_Stream, &first](const auto& a_Field) {
if (!first)
a_Stream << ", ";
using TField = typename std::decay<decltype(a_Field)>::type;
constexpr std::size_t alignment = TField::AlignmentValue;
FieldPrinter<typename TField::StorageType, alignment>::PrintField(a_Stream, a_Field);
first = false;
},
a_Fields);
return a_Stream;
}
template <typename TBase, typename... TOptions>
std::ostream& operator<<(std::ostream& a_Stream, const sp::MessageBase<TBase, TOptions...>& a_Message) {
a_Stream << sp::GetBasicClassName(a_Message) << sp::details::IdPrinter<TOptions...>::PrintMessageId() << "["
<< a_Message.GetFields() << "]";
return a_Stream;
}
} // namespace sp