MessagePrinter (Fixes #2)
All checks were successful
Linux arm64 / Build (push) Successful in 15s

This commit is contained in:
2025-03-04 12:01:15 +01:00
parent fa6ba74068
commit e8367cd91c
8 changed files with 149 additions and 15 deletions

View File

@@ -4,7 +4,7 @@ namespace sp {
namespace option {
// Provide static numeric ID, to facilitate implementation of GetIdImpl()
template <std::intmax_t TId>
template <std::uintmax_t TId>
struct StaticNumIdImpl {};
// Facilitate implementation of DispatchImpl()
@@ -39,10 +39,10 @@ struct MessageImplParsedOptions<> {
template <std::intmax_t TId, typename... TOptions>
template <std::uintmax_t TId, typename... TOptions>
struct MessageImplParsedOptions<option::StaticNumIdImpl<TId>, TOptions...> : public MessageImplParsedOptions<TOptions...> {
static const bool HasStaticNumIdImpl = true;
static const std::intmax_t MsgId = TId;
static const std::uintmax_t MsgId = TId;
};
template <typename TActual, typename... TOptions>

View File

@@ -0,0 +1,81 @@
#pragma once
#include <sstream>
#include <sp/protocol/MessageBase.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 T>
std::string PrintData(const T& a_Data) {
std::stringstream sStream;
sStream << a_Data;
return sStream.str();
}
template <>
std::string PrintData<char>(const char& a_Data) {
return std::to_string(a_Data);
}
template <>
std::string PrintData<unsigned char>(const unsigned char& a_Data) {
return std::to_string(a_Data);
}
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 GetClassName<TField>() + "=" + PrintData(a_Field.GetValue());
}
};
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) {
return "BitField<" + GetClassName<TContainer>() + ">[" + 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);
}
} // namespace details
} // namespace sp

View File

@@ -81,7 +81,7 @@ class MessageImplFieldsReadBase : public TBase {
}
// reading field in bitfield
template <typename TFieldType, typename TField, int IAlignment>
template <typename TFieldType, typename TField, std::size_t IAlignment>
void ReadField(Field<TField, IAlignment>& field, TFieldType data, std::size_t offset) {
static constexpr std::size_t TotalBitCount = sizeof(TFieldType) * 8;
// we suppose that the first element is at the highest bits
@@ -118,7 +118,7 @@ class MessageImplFieldsWriteBase : public TBase {
}
// writing field in bitfield
template <typename TFieldType, typename TField, int IAlignment>
template <typename TFieldType, typename TField, std::size_t IAlignment>
void WriteField(const Field<TField, IAlignment>& field, TFieldType& data, std::size_t offset) const {
static constexpr std::size_t TotalBitCount = sizeof(TFieldType) * 8;
// we suppose that the first element is at the highest bits