add operator<< for packets (#14)
All checks were successful
Linux arm64 / Build (push) Successful in 16s
All checks were successful
Linux arm64 / Build (push) Successful in 16s
Reviewed-on: #14 Co-authored-by: Persson-dev <sim16.prib@gmail.com> Co-committed-by: Persson-dev <sim16.prib@gmail.com>
This commit was merged in pull request #14.
This commit is contained in:
@@ -133,12 +133,12 @@ class MessageInterfaceWriteIdBase : public TBase {
|
||||
template <typename TBase>
|
||||
class MessageInterfaceToStringBase : public TBase {
|
||||
public:
|
||||
std::string ToString() const {
|
||||
return ToStringImpl();
|
||||
friend std::ostream& operator<<(std::ostream& a_Stream, const MessageInterfaceToStringBase& a_Message) {
|
||||
return a_Message.OpOutImpl(a_Stream);
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual std::string ToStringImpl() const = 0;
|
||||
virtual std::ostream& OpOutImpl(std::ostream& a_Stream) const = 0;
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
|
||||
@@ -31,41 +31,49 @@ struct IdPrinter<option::StaticNumIdImpl<TId>, TOptions...> {
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
|
||||
template <typename... TFields>
|
||||
std::string PrintFields(const std::tuple<TFields...>& a_Fields);
|
||||
std::ostream& operator<<(std::ostream& a_Stream, 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());
|
||||
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<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()) + "]";
|
||||
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::string PrintFields(const std::tuple<TFields...>& a_Fields) {
|
||||
std::string concat;
|
||||
std::ostream& operator<<(std::ostream& a_Stream, const std::tuple<TFields...>& a_Fields) {
|
||||
bool first = true;
|
||||
TupleForEach(
|
||||
[&concat](const auto& a_Field) {
|
||||
[&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;
|
||||
concat += FieldPrinter<typename TField::StorageType, alignment>::PrintField(a_Field) + ", ";
|
||||
FieldPrinter<typename TField::StorageType, alignment>::PrintField(a_Stream, a_Field);
|
||||
first = false;
|
||||
},
|
||||
a_Fields);
|
||||
return concat.substr(0, concat.size() - 2);
|
||||
return a_Stream;
|
||||
}
|
||||
|
||||
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()) + "]";
|
||||
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 details
|
||||
} // namespace sp
|
||||
} // namespace sp
|
||||
@@ -14,8 +14,8 @@ std::string PrintMessage(const MessageBase<TBase, TOptions...>& a_Message);
|
||||
template <typename TBase, typename TActual>
|
||||
class MessageImplToStringBase : public TBase {
|
||||
protected:
|
||||
virtual std::string ToStringImpl() const override {
|
||||
return PrintMessage(static_cast<const TActual&>(*this));
|
||||
virtual std::ostream& OpOutImpl(std::ostream& a_Stream) const override{
|
||||
return a_Stream << static_cast<const TActual&>(*this);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
56
include/sp/protocol/message/OstreamFieldIterator.h
Normal file
56
include/sp/protocol/message/OstreamFieldIterator.h
Normal file
@@ -0,0 +1,56 @@
|
||||
// infix_iterator.h
|
||||
//
|
||||
// Lifted from Jerry Coffin's 's prefix_ostream_iterator
|
||||
#pragma once
|
||||
|
||||
#include <ostream>
|
||||
#include <sp/protocol/Field.h>
|
||||
|
||||
namespace sp {
|
||||
|
||||
template <class T, class charT = char, class traits = std::char_traits<charT>>
|
||||
class OstreamFieldIterator {
|
||||
private:
|
||||
std::basic_ostream<charT, traits>* m_Os;
|
||||
std::string m_Delimiter;
|
||||
bool m_FirstElem;
|
||||
|
||||
public:
|
||||
using iterator_category = std::output_iterator_tag;
|
||||
using value_type = void;
|
||||
using difference_type = void;
|
||||
using pointer = void;
|
||||
using reference = void;
|
||||
|
||||
using char_type = charT;
|
||||
using traits_type = traits;
|
||||
using ostream_type = std::basic_ostream<charT, traits>;
|
||||
|
||||
OstreamFieldIterator(ostream_type& a_Stream) : m_Os(&a_Stream), m_Delimiter(0), m_FirstElem(true) {}
|
||||
OstreamFieldIterator(ostream_type& a_Stream, std::string&& a_Delimiter) :
|
||||
m_Os(&a_Stream), m_Delimiter(std::move(a_Delimiter)), m_FirstElem(true) {}
|
||||
|
||||
auto& operator=(const T& item) {
|
||||
// Here's the only real change from ostream_iterator:
|
||||
// Normally, the '*m_Os << item;' would come before the 'if'.
|
||||
if (!m_FirstElem && !m_Delimiter.empty())
|
||||
*m_Os << m_Delimiter;
|
||||
*m_Os << sp::PrintableField<T>(item);
|
||||
m_FirstElem = false;
|
||||
return *this;
|
||||
}
|
||||
|
||||
auto& operator*() {
|
||||
return *this;
|
||||
}
|
||||
|
||||
auto& operator++() {
|
||||
return *this;
|
||||
}
|
||||
|
||||
auto& operator++(int) {
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace sp
|
||||
Reference in New Issue
Block a user