fix alignment + refactor
Reviewed-on: #1 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 #1.
This commit is contained in:
@@ -40,6 +40,17 @@ class BitField {
|
||||
return std::get<FIndex>(this->GetFields()).GetValue();
|
||||
}
|
||||
|
||||
template <std::size_t FIndex>
|
||||
const auto& GetField() const {
|
||||
return std::get<FIndex>(this->GetFields()).GetValue();
|
||||
}
|
||||
|
||||
// allow use of enums
|
||||
template <typename E, E FIndex>
|
||||
const auto& GetField() const {
|
||||
return std::get<static_cast<std::size_t>(FIndex)>(this->GetFields()).GetValue();
|
||||
}
|
||||
|
||||
private:
|
||||
template <int IOffset, typename... T, std::enable_if_t<IOffset >= sizeof...(T), bool> = true>
|
||||
void Apply(const std::tuple<T...>& args) {}
|
||||
@@ -103,6 +114,6 @@ template <typename TField, typename... TFields>
|
||||
struct FieldsBuilder<TField, TFields...> {
|
||||
using Type = sp::tuple_cat_t<std::tuple<Field<TField, 0>>, typename FieldsBuilder<TFields...>::Type>;
|
||||
};
|
||||
} // namespace details
|
||||
|
||||
} // namespace details
|
||||
} // namespace sp
|
||||
|
||||
@@ -23,26 +23,26 @@ namespace sp
|
||||
using Base = GenericHandler<TCommon, std::tuple<TRest...> >;
|
||||
public:
|
||||
using Base::Handle; // Don't hide all Handle() functions from base classes
|
||||
virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T3& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T4& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T5& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T6& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T7& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T8& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T9& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T10& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T11& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T12& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T13& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T14& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T15& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T16& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T17& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T18& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T19& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T20& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(const T1& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T2& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T3& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T4& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T5& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T6& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T7& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T8& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T9& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T10& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T11& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T12& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T13& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T14& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T15& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T16& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T17& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T18& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T19& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T20& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
};
|
||||
|
||||
// 10 by 10
|
||||
@@ -55,16 +55,16 @@ namespace sp
|
||||
using Base = GenericHandler<TCommon, std::tuple<TRest...> >;
|
||||
public:
|
||||
using Base::Handle; // Don't hide all Handle() functions from base classes
|
||||
virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T3& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T4& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T5& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T6& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T7& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T8& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T9& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T10& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(const T1& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T2& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T3& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T4& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T5& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T6& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T7& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T8& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T9& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T10& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
};
|
||||
|
||||
// 5 by 5
|
||||
@@ -76,11 +76,11 @@ namespace sp
|
||||
using Base = GenericHandler<TCommon, std::tuple<TRest...> >;
|
||||
public:
|
||||
using Base::Handle; // Don't hide all Handle() functions from base classes
|
||||
virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T3& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T4& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T5& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(const T1& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T2& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T3& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T4& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T5& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
};
|
||||
|
||||
// Deal with rest with 4 types
|
||||
@@ -89,11 +89,11 @@ namespace sp
|
||||
{
|
||||
public:
|
||||
virtual ~GenericHandler() {}
|
||||
virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T3& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T4& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(TCommon&) { } //Nothing to do
|
||||
virtual void Handle(const T1& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T2& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T3& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T4& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const TCommon&) { } //Nothing to do
|
||||
};
|
||||
|
||||
// Deal with rest with 3 types
|
||||
@@ -102,10 +102,10 @@ namespace sp
|
||||
{
|
||||
public:
|
||||
virtual ~GenericHandler() {}
|
||||
virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T3& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(TCommon&) { } //Nothing to do
|
||||
virtual void Handle(const T1& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T2& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T3& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const TCommon&) { } //Nothing to do
|
||||
};
|
||||
|
||||
// Deal with rest with 2 types
|
||||
@@ -114,9 +114,9 @@ namespace sp
|
||||
{
|
||||
public:
|
||||
virtual ~GenericHandler() {}
|
||||
virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(TCommon&) { } //Nothing to do
|
||||
virtual void Handle(const T1& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const T2& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const TCommon&) { } //Nothing to do
|
||||
};
|
||||
|
||||
// Deal with rest with 1 type
|
||||
@@ -125,8 +125,8 @@ namespace sp
|
||||
{
|
||||
public:
|
||||
virtual ~GenericHandler() {}
|
||||
virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(TCommon&) { } //Nothing to do
|
||||
virtual void Handle(const T1& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||
virtual void Handle(const TCommon&) { } //Nothing to do
|
||||
};
|
||||
|
||||
// Deal with rest with 0 type
|
||||
@@ -135,7 +135,7 @@ namespace sp
|
||||
{
|
||||
public:
|
||||
virtual ~GenericHandler() {}
|
||||
virtual void Handle(TCommon&) { } //Nothing to do
|
||||
virtual void Handle(const TCommon&) { } //Nothing to do
|
||||
};
|
||||
|
||||
} // sp
|
||||
@@ -3,7 +3,7 @@
|
||||
// Inspired by
|
||||
// https://alex-robenko.gitbook.io/comms-protocols-cpp
|
||||
|
||||
#include <sp/protocol/MessageInterfaceBuilder.h>
|
||||
#include <sp/protocol/message/MessageInterfaceBuilder.h>
|
||||
|
||||
namespace sp {
|
||||
|
||||
|
||||
@@ -2,339 +2,15 @@
|
||||
|
||||
#include <sp/protocol/Message.h>
|
||||
|
||||
#include <sp/protocol/message/MessageImplOptions.h>
|
||||
|
||||
#include <sp/protocol/message/MessagesImpl.h>
|
||||
#include <sp/protocol/message/MessageImplProcess.h>
|
||||
#include <sp/protocol/message/MessageImplBuilder.h>
|
||||
|
||||
namespace sp {
|
||||
namespace option {
|
||||
|
||||
// Provide static numeric ID, to facilitate implementation of GetIdImpl()
|
||||
template <std::intmax_t TId>
|
||||
struct StaticNumIdImpl {};
|
||||
|
||||
// Facilitate implementation of DispatchImpl()
|
||||
template <typename TActual>
|
||||
struct DispatchImpl {};
|
||||
|
||||
// Provide fields of the message, facilitate implementation of
|
||||
// ReadImpl(), WriteImpl(), ValidImpl(), etc...
|
||||
template <typename TFields>
|
||||
struct FieldsImpl {};
|
||||
|
||||
} // namespace option
|
||||
|
||||
|
||||
namespace details {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template <typename... TOptions>
|
||||
class MessageImplParsedOptions;
|
||||
|
||||
template <>
|
||||
struct MessageImplParsedOptions<> {
|
||||
static const bool HasStaticNumIdImpl = false;
|
||||
static const bool HasDispatchImpl = false;
|
||||
static const bool HasFieldsImpl = false;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template <std::intmax_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;
|
||||
};
|
||||
|
||||
template <typename TActual, typename... TOptions>
|
||||
struct MessageImplParsedOptions<option::DispatchImpl<TActual>, TOptions...> : public MessageImplParsedOptions<TOptions...> {
|
||||
static const bool HasDispatchImpl = true;
|
||||
using ActualMessage = TActual;
|
||||
};
|
||||
|
||||
template <typename TFields, typename... TOptions>
|
||||
struct MessageImplParsedOptions<option::FieldsImpl<TFields>, TOptions...> : public MessageImplParsedOptions<TOptions...> {
|
||||
static const bool HasFieldsImpl = true;
|
||||
using Fields = TFields;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ID information chunk
|
||||
template <typename TBase, std::intmax_t TId>
|
||||
class MessageImplStaticNumIdBase : public TBase {
|
||||
public:
|
||||
// Reuse the message ID type defined in the interface
|
||||
using MsgIdType = typename TBase::MsgIdType;
|
||||
|
||||
protected:
|
||||
virtual MsgIdType GetIdImpl() const override {
|
||||
return static_cast<MsgIdType>(TId);
|
||||
}
|
||||
};
|
||||
|
||||
// Dispatch implementation chunk
|
||||
template <typename TBase, typename TActual>
|
||||
class MessageImplDispatchBase : public TBase {
|
||||
public:
|
||||
// Reuse the Handler type defined in the interface class
|
||||
using Handler = typename TBase::HandlerType;
|
||||
|
||||
protected:
|
||||
virtual void DispatchImpl(Handler& handler) override {
|
||||
handler.Handle(static_cast<TActual&>(*this));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
template <typename TBase, typename TFields>
|
||||
class MessageImplFieldsBase : public TBase {
|
||||
public:
|
||||
using AllFields = typename details::FieldsBuilder<TFields>::Type;
|
||||
|
||||
template <typename... Args>
|
||||
void Construct(Args... args) {
|
||||
m_Fields = std::make_tuple(args...);
|
||||
}
|
||||
|
||||
AllFields& GetFields() {
|
||||
return m_Fields;
|
||||
}
|
||||
|
||||
const AllFields& GetFields() const {
|
||||
return m_Fields;
|
||||
}
|
||||
|
||||
template <std::size_t FIndex>
|
||||
auto& GetField() {
|
||||
return std::get<FIndex>(GetFields()).GetValue();
|
||||
}
|
||||
|
||||
private:
|
||||
AllFields m_Fields;
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
class MessageImplFieldsReadBase : public TBase {
|
||||
private:
|
||||
// normal reading
|
||||
template <typename TField>
|
||||
void ReadField(Field<TField, 0>& field, DataBuffer& buffer) {
|
||||
this->ReadData(field.GetValue(), buffer);
|
||||
}
|
||||
|
||||
// reading field in bitfield
|
||||
template <typename TFieldType, typename TField, int 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
|
||||
field.GetValue() = (data >> TotalBitCount - IAlignment - offset) & ((1 << IAlignment) - 1);
|
||||
}
|
||||
|
||||
// reading bitfield
|
||||
template <typename TContainer, typename TFirst, typename... TFields>
|
||||
void ReadField(Field<BitField<TContainer, TFirst, TFields...>, 0>& field, DataBuffer& buffer) {
|
||||
TContainer data;
|
||||
this->ReadData(data, buffer);
|
||||
std::size_t offset = 0;
|
||||
TupleForEach(
|
||||
[data, this, &offset](auto& field) {
|
||||
this->ReadField(field, data, offset);
|
||||
offset += field.GetAlignment();
|
||||
},
|
||||
field.GetValue().GetFields());
|
||||
}
|
||||
|
||||
void ReadImpl(DataBuffer& buffer) override {
|
||||
auto& allFields = this->GetFields();
|
||||
TupleForEach([&buffer, this](auto& field) { this->ReadField(field, buffer); }, allFields);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
class MessageImplFieldsWriteBase : public TBase {
|
||||
private:
|
||||
// normal writing
|
||||
template <typename TField>
|
||||
void WriteField(Field<TField, 0>& field, DataBuffer& buffer) {
|
||||
this->WriteData(field.GetValue(), buffer);
|
||||
}
|
||||
|
||||
// writing field in bitfield
|
||||
template <typename TFieldType, typename TField, int IAlignment>
|
||||
void WriteField(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
|
||||
data |= (field.GetValue() & ((1 << IAlignment) - 1)) << TotalBitCount - IAlignment - offset;
|
||||
}
|
||||
|
||||
// writing bitfield
|
||||
template <typename TContainer, typename TFirst, typename... TFields>
|
||||
void WriteField(Field<BitField<TContainer, TFirst, TFields...>, 0>& field, DataBuffer& buffer) {
|
||||
TContainer data = 0;
|
||||
std::size_t offset = 0;
|
||||
TupleForEach(
|
||||
[&data, this, &offset](auto& field) {
|
||||
this->WriteField(field, data, offset);
|
||||
offset += field.GetAlignment();
|
||||
},
|
||||
field.GetValue().GetFields());
|
||||
this->WriteData(data, buffer);
|
||||
}
|
||||
|
||||
void WriteImpl(DataBuffer& buffer) override {
|
||||
auto& allFields = this->GetFields();
|
||||
TupleForEach([&buffer, this](auto& field) { this->WriteField(field, buffer); }, allFields);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
class MessageImplFieldsValidBase : public TBase {
|
||||
protected:
|
||||
bool ValidImpl() const override {
|
||||
// Access fields via interface provided in previous chunk
|
||||
// auto& allFields = TBase::GetFields();
|
||||
//... // validate all the fields
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
// id impl
|
||||
template <typename TBase, typename ParsedImplOptions, bool TImplement>
|
||||
struct MessageImplProcessStaticNumId;
|
||||
|
||||
template <typename TBase, typename ParsedImplOptions>
|
||||
struct MessageImplProcessStaticNumId<TBase, ParsedImplOptions, true> {
|
||||
using Type = MessageImplStaticNumIdBase<TBase, ParsedImplOptions::MsgId>;
|
||||
};
|
||||
|
||||
template <typename TBase, typename ParsedImplOptions>
|
||||
struct MessageImplProcessStaticNumId<TBase, ParsedImplOptions, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
// dispatch impl
|
||||
template <typename TBase, typename ParsedImplOptions, bool TImplement>
|
||||
struct MessageImplProcessDispatch;
|
||||
|
||||
template <typename TBase, typename ParsedImplOptions>
|
||||
struct MessageImplProcessDispatch<TBase, ParsedImplOptions, true> {
|
||||
using Type = MessageImplDispatchBase<TBase, typename ParsedImplOptions::ActualMessage>;
|
||||
};
|
||||
|
||||
template <typename TBase, typename ParsedImplOptions>
|
||||
struct MessageImplProcessDispatch<TBase, ParsedImplOptions, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
// fields impl
|
||||
template <typename TBase, typename ParsedImplOptions, bool TImplement>
|
||||
struct MessageImplProcessFields;
|
||||
|
||||
template <typename TBase, typename ParsedImplOptions>
|
||||
struct MessageImplProcessFields<TBase, ParsedImplOptions, true> {
|
||||
using Type = MessageImplFieldsBase<TBase, typename ParsedImplOptions::Fields>;
|
||||
};
|
||||
|
||||
template <typename TBase, typename ParsedImplOptions>
|
||||
struct MessageImplProcessFields<TBase, ParsedImplOptions, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
// read impl
|
||||
template <typename TBase, bool TImplement>
|
||||
struct MessageImplProcessReadFields;
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageImplProcessReadFields<TBase, true> {
|
||||
using Type = MessageImplFieldsReadBase<TBase>;
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageImplProcessReadFields<TBase, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
// write impl
|
||||
template <typename TBase, bool TImplement>
|
||||
struct MessageImplProcessWriteFields;
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageImplProcessWriteFields<TBase, true> {
|
||||
using Type = MessageImplFieldsWriteBase<TBase>;
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageImplProcessWriteFields<TBase, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
// valid impl
|
||||
template <typename TBase, bool TImplement>
|
||||
struct MessageImplProcessValidFields;
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageImplProcessValidFields<TBase, true> {
|
||||
using Type = MessageImplFieldsValidBase<TBase>;
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageImplProcessValidFields<TBase, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// TBase is interface class
|
||||
// TOptions... are the implementation options
|
||||
template <typename TBase, typename... TOptions>
|
||||
struct MessageImplBuilder {
|
||||
// ParsedOptions class is supposed to be defined in comms::Message class
|
||||
using InterfaceOptions = typename TBase::ParsedOptions;
|
||||
|
||||
// Parse implementation options
|
||||
using ImplOptions = MessageImplParsedOptions<TOptions...>;
|
||||
|
||||
// Provide GetIdImpl() if possible
|
||||
static const bool HasStaticNumIdImpl = InterfaceOptions::HasMsgIdType && ImplOptions::HasStaticNumIdImpl;
|
||||
using Base1 = typename MessageImplProcessStaticNumId<TBase, ImplOptions, HasStaticNumIdImpl>::Type;
|
||||
|
||||
// Provide DispatchImpl() if possible
|
||||
static const bool HasDispatchImpl = InterfaceOptions::HasHandler && ImplOptions::HasDispatchImpl;
|
||||
using Base2 = typename MessageImplProcessDispatch<Base1, ImplOptions, HasDispatchImpl>::Type;
|
||||
|
||||
// Provide access to fields if possible
|
||||
using Base3 = typename MessageImplProcessFields<Base2, ImplOptions, ImplOptions::HasFieldsImpl>::Type;
|
||||
|
||||
// Provide ReadImpl() if possible
|
||||
static const bool HasReadImpl = InterfaceOptions::HasReadOperations && ImplOptions::HasFieldsImpl;
|
||||
using Base4 = typename MessageImplProcessReadFields<Base3, HasReadImpl>::Type;
|
||||
|
||||
// Provide WriteImpl() if possible
|
||||
static const bool HasWriteImpl = InterfaceOptions::HasWriteOperations && ImplOptions::HasFieldsImpl;
|
||||
using Base5 = typename MessageImplProcessWriteFields<Base4, HasWriteImpl>::Type;
|
||||
|
||||
// Provide ValidImpl() if possible
|
||||
static const bool HasValidImpl = InterfaceOptions::HasValid && ImplOptions::HasFieldsImpl;
|
||||
using Base6 = typename MessageImplProcessValidFields<Base5, HasValidImpl>::Type;
|
||||
|
||||
// The last BaseN must be taken as final type.
|
||||
using Type = Base6;
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
|
||||
template <typename TBase, typename... TOptions>
|
||||
class MessageBase : public details::MessageImplBuilder<TBase, TOptions...>::Type {};
|
||||
|
||||
|
||||
} // namespace sp
|
||||
@@ -1,278 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/protocol/Options.h>
|
||||
#include <sp/common/ByteSwapping.h>
|
||||
|
||||
namespace sp {
|
||||
namespace details {
|
||||
|
||||
template <typename... TOptions>
|
||||
struct MessageInterfaceParsedOptions {};
|
||||
|
||||
template <>
|
||||
struct MessageInterfaceParsedOptions<> {
|
||||
static const bool HasMsgIdType = false;
|
||||
static const bool HasLittleEndian = false;
|
||||
static const bool HasReadOperations = false;
|
||||
static const bool HasWriteOperations = false;
|
||||
static const bool HasWriteId = false;
|
||||
static const bool HasHandler = false;
|
||||
static const bool HasValid = false;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template <typename T, typename... TOptions>
|
||||
struct MessageInterfaceParsedOptions<option::MsgIdType<T>, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||
static const bool HasMsgIdType = true;
|
||||
using MsgIdType = T;
|
||||
};
|
||||
|
||||
template <typename... TOptions>
|
||||
struct MessageInterfaceParsedOptions<option::LittleEndian, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||
static const bool HasLittleEndian = true;
|
||||
};
|
||||
|
||||
template <typename... TOptions>
|
||||
struct MessageInterfaceParsedOptions<option::ReadOperations, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||
static const bool HasReadOperations = true;
|
||||
};
|
||||
|
||||
template <typename... TOptions>
|
||||
struct MessageInterfaceParsedOptions<option::WriteOperations, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||
static const bool HasWriteOperations = true;
|
||||
};
|
||||
|
||||
template <typename... TOptions>
|
||||
struct MessageInterfaceParsedOptions<option::WriteId, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||
static const bool HasWriteId = true;
|
||||
};
|
||||
|
||||
template <typename T, typename... TOptions>
|
||||
struct MessageInterfaceParsedOptions<option::Handler<T>, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||
static const bool HasHandler = true;
|
||||
using HandlerType = option::Handler<T>;
|
||||
};
|
||||
|
||||
template <typename... TOptions>
|
||||
struct MessageInterfaceParsedOptions<option::ValidCheckInterface, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||
static const bool HasValid = true;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ID retrieval chunk
|
||||
template <typename TBase, typename TId>
|
||||
class MessageInterfaceIdTypeBase : public TBase {
|
||||
public:
|
||||
using MsgIdType = TId;
|
||||
MsgIdType GetId() const {
|
||||
return GetIdImpl();
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual MsgIdType GetIdImpl() const = 0;
|
||||
};
|
||||
|
||||
// Big endian serialisation chunk
|
||||
template <typename TBase>
|
||||
class MessageInterfaceBigEndian : public TBase {
|
||||
protected:
|
||||
template <typename T>
|
||||
void ReadData(T& value, DataBuffer& buffer) {
|
||||
buffer >> value;
|
||||
FromNetwork(value);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void WriteData(T value, DataBuffer& buffer) {
|
||||
ToNetwork(value);
|
||||
buffer << value;
|
||||
}
|
||||
};
|
||||
|
||||
// Little endian serialisation chunk
|
||||
template <typename TBase>
|
||||
class MessageInterfaceLittleEndian : public TBase {
|
||||
protected:
|
||||
template <typename T>
|
||||
void ReadData(T& value, DataBuffer& buffer) {
|
||||
buffer >> value;
|
||||
TrySwapBytes(value);
|
||||
FromNetwork(value);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void WriteData(const T& value, DataBuffer& buffer) {
|
||||
ToNetwork(value);
|
||||
TrySwapBytes(value);
|
||||
buffer << value;
|
||||
}
|
||||
};
|
||||
|
||||
// Read functionality chunk
|
||||
template <typename TBase>
|
||||
class MessageInterfaceReadBase : public TBase {
|
||||
public:
|
||||
void Read(DataBuffer& buffer) {
|
||||
return ReadImpl(buffer);
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void ReadImpl(DataBuffer& buffer) = 0;
|
||||
};
|
||||
|
||||
// Write functionality chunk
|
||||
template <typename TBase>
|
||||
class MessageInterfaceWriteBase : public TBase {
|
||||
public:
|
||||
void Write(DataBuffer& buffer) {
|
||||
WriteImpl(buffer);
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void WriteImpl(DataBuffer& buffer) = 0;
|
||||
};
|
||||
|
||||
// Handler functionality chunk
|
||||
template <typename TBase, typename THandler>
|
||||
class MessageInterfaceHandlerBase : public TBase {
|
||||
public:
|
||||
using HandlerType = typename THandler::HandlerT;
|
||||
|
||||
void Dispatch(HandlerType& handler) {
|
||||
DispatchImpl(handler);
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void DispatchImpl(HandlerType& handler) = 0;
|
||||
};
|
||||
|
||||
// Validity functionality chunk
|
||||
template <typename TBase>
|
||||
class MessageInterfaceValidityBase : public TBase {
|
||||
public:
|
||||
bool Valid() const {
|
||||
return ValidImpl();
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual bool ValidImpl() const = 0;
|
||||
};
|
||||
|
||||
// Writing id functionality chunk
|
||||
template <typename TBase>
|
||||
class MessageInterfaceWriteIdBase : public TBase {
|
||||
public:
|
||||
void Write(DataBuffer& buffer) {
|
||||
this->WriteData(this->GetId(), buffer);
|
||||
this->WriteImpl(buffer);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
// Build message Id
|
||||
template <typename TBase, typename TParsedOptions, bool THasMsgIdType>
|
||||
struct MessageInterfaceProcessMsgId;
|
||||
|
||||
template <typename TBase, typename TParsedOptions>
|
||||
struct MessageInterfaceProcessMsgId<TBase, TParsedOptions, true> {
|
||||
using Type = MessageInterfaceIdTypeBase<TBase, typename TParsedOptions::MsgIdType>;
|
||||
};
|
||||
|
||||
template <typename TBase, typename TParsedOptions>
|
||||
struct MessageInterfaceProcessMsgId<TBase, TParsedOptions, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
// Build endianess
|
||||
template <typename TBase, bool THasLittleEndian>
|
||||
struct MessageInterfaceProcessEndian;
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageInterfaceProcessEndian<TBase, true> {
|
||||
using Type = MessageInterfaceLittleEndian<TBase>;
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageInterfaceProcessEndian<TBase, false> {
|
||||
using Type = MessageInterfaceBigEndian<TBase>;
|
||||
};
|
||||
|
||||
// Build read
|
||||
template <typename TBase, bool THasRead>
|
||||
struct MessageInterfaceProcessRead;
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageInterfaceProcessRead<TBase, true> {
|
||||
using Type = MessageInterfaceReadBase<TBase>;
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageInterfaceProcessRead<TBase, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
// Build write
|
||||
template <typename TBase, bool THasWrite>
|
||||
struct MessageInterfaceProcessWrite;
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageInterfaceProcessWrite<TBase, true> {
|
||||
using Type = MessageInterfaceWriteBase<TBase>;
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageInterfaceProcessWrite<TBase, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
// Build handler
|
||||
template <typename TBase, typename TParsedOptions, bool THasHandler>
|
||||
struct MessageInterfaceProcessHandler;
|
||||
|
||||
template <typename TBase, typename TParsedOptions>
|
||||
struct MessageInterfaceProcessHandler<TBase, TParsedOptions, true> {
|
||||
using Type = MessageInterfaceHandlerBase<TBase, typename TParsedOptions::HandlerType>;
|
||||
};
|
||||
|
||||
template <typename TBase, typename TParsedOptions>
|
||||
struct MessageInterfaceProcessHandler<TBase, TParsedOptions, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
// Build valid
|
||||
template <typename TBase, bool THasValid>
|
||||
struct MessageInterfaceProcessValid;
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageInterfaceProcessValid<TBase, true> {
|
||||
using Type = MessageInterfaceValidityBase<TBase>;
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageInterfaceProcessValid<TBase, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
// Build id writing
|
||||
template <typename TBase, bool THasValid>
|
||||
struct MessageInterfaceProcessWriteId;
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageInterfaceProcessWriteId<TBase, true> {
|
||||
using Type = MessageInterfaceWriteIdBase<TBase>;
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageInterfaceProcessWriteId<TBase, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
} // namespace details
|
||||
} // namespace sp
|
||||
45
include/sp/protocol/message/MessageImplBuilder.h
Normal file
45
include/sp/protocol/message/MessageImplBuilder.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
namespace sp {
|
||||
namespace details {
|
||||
|
||||
|
||||
// TBase is interface class
|
||||
// TOptions... are the implementation options
|
||||
template <typename TBase, typename... TOptions>
|
||||
struct MessageImplBuilder {
|
||||
// ParsedOptions class is supposed to be defined in comms::Message class
|
||||
using InterfaceOptions = typename TBase::ParsedOptions;
|
||||
|
||||
// Parse implementation options
|
||||
using ImplOptions = MessageImplParsedOptions<TOptions...>;
|
||||
|
||||
// Provide GetIdImpl() if possible
|
||||
static const bool HasStaticNumIdImpl = InterfaceOptions::HasMsgIdType && ImplOptions::HasStaticNumIdImpl;
|
||||
using Base1 = typename MessageImplProcessStaticNumId<TBase, ImplOptions, HasStaticNumIdImpl>::Type;
|
||||
|
||||
// Provide DispatchImpl() if possible
|
||||
static const bool HasDispatchImpl = InterfaceOptions::HasHandler && ImplOptions::HasDispatchImpl;
|
||||
using Base2 = typename MessageImplProcessDispatch<Base1, ImplOptions, HasDispatchImpl>::Type;
|
||||
|
||||
// Provide access to fields if possible
|
||||
using Base3 = typename MessageImplProcessFields<Base2, ImplOptions, ImplOptions::HasFieldsImpl>::Type;
|
||||
|
||||
// Provide ReadImpl() if possible
|
||||
static const bool HasReadImpl = InterfaceOptions::HasReadOperations && ImplOptions::HasFieldsImpl;
|
||||
using Base4 = typename MessageImplProcessReadFields<Base3, HasReadImpl>::Type;
|
||||
|
||||
// Provide WriteImpl() if possible
|
||||
static const bool HasWriteImpl = InterfaceOptions::HasWriteOperations && ImplOptions::HasFieldsImpl;
|
||||
using Base5 = typename MessageImplProcessWriteFields<Base4, HasWriteImpl>::Type;
|
||||
|
||||
// Provide ValidImpl() if possible
|
||||
static const bool HasValidImpl = InterfaceOptions::HasValid && ImplOptions::HasFieldsImpl;
|
||||
using Base6 = typename MessageImplProcessValidFields<Base5, HasValidImpl>::Type;
|
||||
|
||||
// The last BaseN must be taken as final type.
|
||||
using Type = Base6;
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
} // namespace sp
|
||||
61
include/sp/protocol/message/MessageImplOptions.h
Normal file
61
include/sp/protocol/message/MessageImplOptions.h
Normal file
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
namespace sp {
|
||||
namespace option {
|
||||
|
||||
// Provide static numeric ID, to facilitate implementation of GetIdImpl()
|
||||
template <std::intmax_t TId>
|
||||
struct StaticNumIdImpl {};
|
||||
|
||||
// Facilitate implementation of DispatchImpl()
|
||||
template <typename TActual>
|
||||
struct DispatchImpl {};
|
||||
|
||||
// Provide fields of the message, facilitate implementation of
|
||||
// ReadImpl(), WriteImpl(), ValidImpl(), etc...
|
||||
template <typename TFields>
|
||||
struct FieldsImpl {};
|
||||
|
||||
} // namespace option
|
||||
|
||||
|
||||
namespace details {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template <typename... TOptions>
|
||||
class MessageImplParsedOptions;
|
||||
|
||||
template <>
|
||||
struct MessageImplParsedOptions<> {
|
||||
static const bool HasStaticNumIdImpl = false;
|
||||
static const bool HasDispatchImpl = false;
|
||||
static const bool HasFieldsImpl = false;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template <std::intmax_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;
|
||||
};
|
||||
|
||||
template <typename TActual, typename... TOptions>
|
||||
struct MessageImplParsedOptions<option::DispatchImpl<TActual>, TOptions...> : public MessageImplParsedOptions<TOptions...> {
|
||||
static const bool HasDispatchImpl = true;
|
||||
using ActualMessage = TActual;
|
||||
};
|
||||
|
||||
template <typename TFields, typename... TOptions>
|
||||
struct MessageImplParsedOptions<option::FieldsImpl<TFields>, TOptions...> : public MessageImplParsedOptions<TOptions...> {
|
||||
static const bool HasFieldsImpl = true;
|
||||
using Fields = TFields;
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
} // namespace sp
|
||||
93
include/sp/protocol/message/MessageImplProcess.h
Normal file
93
include/sp/protocol/message/MessageImplProcess.h
Normal file
@@ -0,0 +1,93 @@
|
||||
#pragma once
|
||||
|
||||
namespace sp {
|
||||
namespace details {
|
||||
|
||||
|
||||
|
||||
// id impl
|
||||
template <typename TBase, typename ParsedImplOptions, bool TImplement>
|
||||
struct MessageImplProcessStaticNumId;
|
||||
|
||||
template <typename TBase, typename ParsedImplOptions>
|
||||
struct MessageImplProcessStaticNumId<TBase, ParsedImplOptions, true> {
|
||||
using Type = MessageImplStaticNumIdBase<TBase, ParsedImplOptions::MsgId>;
|
||||
};
|
||||
|
||||
template <typename TBase, typename ParsedImplOptions>
|
||||
struct MessageImplProcessStaticNumId<TBase, ParsedImplOptions, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
// dispatch impl
|
||||
template <typename TBase, typename ParsedImplOptions, bool TImplement>
|
||||
struct MessageImplProcessDispatch;
|
||||
|
||||
template <typename TBase, typename ParsedImplOptions>
|
||||
struct MessageImplProcessDispatch<TBase, ParsedImplOptions, true> {
|
||||
using Type = MessageImplDispatchBase<TBase, typename ParsedImplOptions::ActualMessage>;
|
||||
};
|
||||
|
||||
template <typename TBase, typename ParsedImplOptions>
|
||||
struct MessageImplProcessDispatch<TBase, ParsedImplOptions, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
// fields impl
|
||||
template <typename TBase, typename ParsedImplOptions, bool TImplement>
|
||||
struct MessageImplProcessFields;
|
||||
|
||||
template <typename TBase, typename ParsedImplOptions>
|
||||
struct MessageImplProcessFields<TBase, ParsedImplOptions, true> {
|
||||
using Type = MessageImplFieldsBase<TBase, typename ParsedImplOptions::Fields>;
|
||||
};
|
||||
|
||||
template <typename TBase, typename ParsedImplOptions>
|
||||
struct MessageImplProcessFields<TBase, ParsedImplOptions, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
// read impl
|
||||
template <typename TBase, bool TImplement>
|
||||
struct MessageImplProcessReadFields;
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageImplProcessReadFields<TBase, true> {
|
||||
using Type = MessageImplFieldsReadBase<TBase>;
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageImplProcessReadFields<TBase, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
// write impl
|
||||
template <typename TBase, bool TImplement>
|
||||
struct MessageImplProcessWriteFields;
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageImplProcessWriteFields<TBase, true> {
|
||||
using Type = MessageImplFieldsWriteBase<TBase>;
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageImplProcessWriteFields<TBase, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
// valid impl
|
||||
template <typename TBase, bool TImplement>
|
||||
struct MessageImplProcessValidFields;
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageImplProcessValidFields<TBase, true> {
|
||||
using Type = MessageImplFieldsValidBase<TBase>;
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageImplProcessValidFields<TBase, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
} // namespace sp
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/protocol/MessageInterfaces.h>
|
||||
#include <sp/protocol/message/MessageInterfaces.h>
|
||||
|
||||
namespace sp {
|
||||
namespace details {
|
||||
@@ -33,7 +33,7 @@ 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 Base6 must be taken as final type.
|
||||
// The last Base7 must be taken as final type.
|
||||
using Type = Base7;
|
||||
};
|
||||
|
||||
105
include/sp/protocol/message/MessageInterfaceProcess.h
Normal file
105
include/sp/protocol/message/MessageInterfaceProcess.h
Normal file
@@ -0,0 +1,105 @@
|
||||
#pragma once
|
||||
|
||||
namespace sp {
|
||||
namespace details {
|
||||
|
||||
// Build message Id
|
||||
template <typename TBase, typename TParsedOptions, bool THasMsgIdType>
|
||||
struct MessageInterfaceProcessMsgId;
|
||||
|
||||
template <typename TBase, typename TParsedOptions>
|
||||
struct MessageInterfaceProcessMsgId<TBase, TParsedOptions, true> {
|
||||
using Type = MessageInterfaceIdTypeBase<TBase, typename TParsedOptions::MsgIdType>;
|
||||
};
|
||||
|
||||
template <typename TBase, typename TParsedOptions>
|
||||
struct MessageInterfaceProcessMsgId<TBase, TParsedOptions, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
// Build endianess
|
||||
template <typename TBase, bool THasLittleEndian>
|
||||
struct MessageInterfaceProcessEndian;
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageInterfaceProcessEndian<TBase, true> {
|
||||
using Type = MessageInterfaceLittleEndian<TBase>;
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageInterfaceProcessEndian<TBase, false> {
|
||||
using Type = MessageInterfaceBigEndian<TBase>;
|
||||
};
|
||||
|
||||
// Build read
|
||||
template <typename TBase, bool THasRead>
|
||||
struct MessageInterfaceProcessRead;
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageInterfaceProcessRead<TBase, true> {
|
||||
using Type = MessageInterfaceReadBase<TBase>;
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageInterfaceProcessRead<TBase, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
// Build write
|
||||
template <typename TBase, bool THasWrite>
|
||||
struct MessageInterfaceProcessWrite;
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageInterfaceProcessWrite<TBase, true> {
|
||||
using Type = MessageInterfaceWriteBase<TBase>;
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageInterfaceProcessWrite<TBase, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
// Build handler
|
||||
template <typename TBase, typename TParsedOptions, bool THasHandler>
|
||||
struct MessageInterfaceProcessHandler;
|
||||
|
||||
template <typename TBase, typename TParsedOptions>
|
||||
struct MessageInterfaceProcessHandler<TBase, TParsedOptions, true> {
|
||||
using Type = MessageInterfaceHandlerBase<TBase, typename TParsedOptions::HandlerType>;
|
||||
};
|
||||
|
||||
template <typename TBase, typename TParsedOptions>
|
||||
struct MessageInterfaceProcessHandler<TBase, TParsedOptions, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
// Build valid
|
||||
template <typename TBase, bool THasValid>
|
||||
struct MessageInterfaceProcessValid;
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageInterfaceProcessValid<TBase, true> {
|
||||
using Type = MessageInterfaceValidityBase<TBase>;
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageInterfaceProcessValid<TBase, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
// Build id writing
|
||||
template <typename TBase, bool THasValid>
|
||||
struct MessageInterfaceProcessWriteId;
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageInterfaceProcessWriteId<TBase, true> {
|
||||
using Type = MessageInterfaceWriteIdBase<TBase>;
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageInterfaceProcessWriteId<TBase, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
} // namespace sp
|
||||
7
include/sp/protocol/message/MessageInterfaces.h
Normal file
7
include/sp/protocol/message/MessageInterfaces.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/protocol/message/MessageOptions.h>
|
||||
|
||||
#include <sp/protocol/message/MessageInterfacesOptions.h>
|
||||
#include <sp/protocol/message/MessageInterfacesImpl.h>
|
||||
#include <sp/protocol/message/MessageInterfaceProcess.h>
|
||||
119
include/sp/protocol/message/MessageInterfacesImpl.h
Normal file
119
include/sp/protocol/message/MessageInterfacesImpl.h
Normal file
@@ -0,0 +1,119 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/common/ByteSwapping.h>
|
||||
|
||||
namespace sp {
|
||||
namespace details {
|
||||
|
||||
|
||||
// ID retrieval chunk
|
||||
template <typename TBase, typename TId>
|
||||
class MessageInterfaceIdTypeBase : public TBase {
|
||||
public:
|
||||
using MsgIdType = TId;
|
||||
MsgIdType GetId() const {
|
||||
return GetIdImpl();
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual MsgIdType GetIdImpl() const = 0;
|
||||
};
|
||||
|
||||
// Big endian serialisation chunk
|
||||
template <typename TBase>
|
||||
class MessageInterfaceBigEndian : public TBase {
|
||||
protected:
|
||||
template <typename T>
|
||||
void ReadData(T& value, DataBuffer& buffer) {
|
||||
buffer >> value;
|
||||
FromNetwork(value);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void WriteData(T value, DataBuffer& buffer) {
|
||||
ToNetwork(value);
|
||||
buffer << value;
|
||||
}
|
||||
};
|
||||
|
||||
// Little endian serialisation chunk
|
||||
template <typename TBase>
|
||||
class MessageInterfaceLittleEndian : public TBase {
|
||||
protected:
|
||||
template <typename T>
|
||||
void ReadData(T& value, DataBuffer& buffer) {
|
||||
buffer >> value;
|
||||
TrySwapBytes(value);
|
||||
FromNetwork(value);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void WriteData(const T& value, DataBuffer& buffer) {
|
||||
ToNetwork(value);
|
||||
TrySwapBytes(value);
|
||||
buffer << value;
|
||||
}
|
||||
};
|
||||
|
||||
// Read functionality chunk
|
||||
template <typename TBase>
|
||||
class MessageInterfaceReadBase : public TBase {
|
||||
public:
|
||||
void Read(DataBuffer& buffer) {
|
||||
return ReadImpl(buffer);
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void ReadImpl(DataBuffer& buffer) = 0;
|
||||
};
|
||||
|
||||
// Write functionality chunk
|
||||
template <typename TBase>
|
||||
class MessageInterfaceWriteBase : public TBase {
|
||||
public:
|
||||
void Write(DataBuffer& buffer) {
|
||||
WriteImpl(buffer);
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void WriteImpl(DataBuffer& buffer) = 0;
|
||||
};
|
||||
|
||||
// Handler functionality chunk
|
||||
template <typename TBase, typename THandler>
|
||||
class MessageInterfaceHandlerBase : public TBase {
|
||||
public:
|
||||
using HandlerType = typename THandler::HandlerT;
|
||||
|
||||
void Dispatch(HandlerType& handler) {
|
||||
DispatchImpl(handler);
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void DispatchImpl(HandlerType& handler) = 0;
|
||||
};
|
||||
|
||||
// Validity functionality chunk
|
||||
template <typename TBase>
|
||||
class MessageInterfaceValidityBase : public TBase {
|
||||
public:
|
||||
bool Valid() const {
|
||||
return ValidImpl();
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual bool ValidImpl() const = 0;
|
||||
};
|
||||
|
||||
// Writing id functionality chunk
|
||||
template <typename TBase>
|
||||
class MessageInterfaceWriteIdBase : public TBase {
|
||||
public:
|
||||
void Write(DataBuffer& buffer) {
|
||||
this->WriteData(this->GetId(), buffer);
|
||||
this->WriteImpl(buffer);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
} // namespace sp
|
||||
63
include/sp/protocol/message/MessageInterfacesOptions.h
Normal file
63
include/sp/protocol/message/MessageInterfacesOptions.h
Normal file
@@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
|
||||
namespace sp {
|
||||
namespace details {
|
||||
|
||||
|
||||
template <typename... TOptions>
|
||||
struct MessageInterfaceParsedOptions {};
|
||||
|
||||
template <>
|
||||
struct MessageInterfaceParsedOptions<> {
|
||||
static const bool HasMsgIdType = false;
|
||||
static const bool HasLittleEndian = false;
|
||||
static const bool HasReadOperations = false;
|
||||
static const bool HasWriteOperations = false;
|
||||
static const bool HasWriteId = false;
|
||||
static const bool HasHandler = false;
|
||||
static const bool HasValid = false;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template <typename T, typename... TOptions>
|
||||
struct MessageInterfaceParsedOptions<option::MsgIdType<T>, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||
static const bool HasMsgIdType = true;
|
||||
using MsgIdType = T;
|
||||
};
|
||||
|
||||
template <typename... TOptions>
|
||||
struct MessageInterfaceParsedOptions<option::LittleEndian, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||
static const bool HasLittleEndian = true;
|
||||
};
|
||||
|
||||
template <typename... TOptions>
|
||||
struct MessageInterfaceParsedOptions<option::ReadOperations, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||
static const bool HasReadOperations = true;
|
||||
};
|
||||
|
||||
template <typename... TOptions>
|
||||
struct MessageInterfaceParsedOptions<option::WriteOperations, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||
static const bool HasWriteOperations = true;
|
||||
};
|
||||
|
||||
template <typename... TOptions>
|
||||
struct MessageInterfaceParsedOptions<option::WriteId, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||
static const bool HasWriteId = true;
|
||||
};
|
||||
|
||||
template <typename T, typename... TOptions>
|
||||
struct MessageInterfaceParsedOptions<option::Handler<T>, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||
static const bool HasHandler = true;
|
||||
using HandlerType = option::Handler<T>;
|
||||
};
|
||||
|
||||
template <typename... TOptions>
|
||||
struct MessageInterfaceParsedOptions<option::ValidCheckInterface, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||
static const bool HasValid = true;
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
} // namespace sp
|
||||
160
include/sp/protocol/message/MessagesImpl.h
Normal file
160
include/sp/protocol/message/MessagesImpl.h
Normal file
@@ -0,0 +1,160 @@
|
||||
#pragma once
|
||||
|
||||
namespace sp {
|
||||
namespace details {
|
||||
|
||||
|
||||
|
||||
// ID information chunk
|
||||
template <typename TBase, std::intmax_t TId>
|
||||
class MessageImplStaticNumIdBase : public TBase {
|
||||
public:
|
||||
// Reuse the message ID type defined in the interface
|
||||
using MsgIdType = typename TBase::MsgIdType;
|
||||
|
||||
protected:
|
||||
virtual MsgIdType GetIdImpl() const override {
|
||||
return static_cast<MsgIdType>(TId);
|
||||
}
|
||||
};
|
||||
|
||||
// Dispatch implementation chunk
|
||||
template <typename TBase, typename TActual>
|
||||
class MessageImplDispatchBase : public TBase {
|
||||
public:
|
||||
// Reuse the Handler type defined in the interface class
|
||||
using Handler = typename TBase::HandlerType;
|
||||
|
||||
protected:
|
||||
virtual void DispatchImpl(Handler& handler) override {
|
||||
handler.Handle(static_cast<TActual&>(*this));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
template <typename TBase, typename TFields>
|
||||
class MessageImplFieldsBase : public TBase {
|
||||
public:
|
||||
using AllFields = typename details::FieldsBuilder<TFields>::Type;
|
||||
|
||||
template <typename... Args>
|
||||
void Construct(Args... args) {
|
||||
m_Fields = std::make_tuple(args...);
|
||||
}
|
||||
|
||||
AllFields& GetFields() {
|
||||
return m_Fields;
|
||||
}
|
||||
|
||||
const AllFields& GetFields() const {
|
||||
return m_Fields;
|
||||
}
|
||||
|
||||
template <std::size_t FIndex>
|
||||
auto& GetField() {
|
||||
return std::get<FIndex>(GetFields()).GetValue();
|
||||
}
|
||||
|
||||
template <std::size_t FIndex>
|
||||
const auto& GetField() const {
|
||||
return std::get<FIndex>(GetFields()).GetValue();
|
||||
}
|
||||
|
||||
// allow use of enums
|
||||
template <typename E, E FIndex>
|
||||
const auto& GetField() const {
|
||||
return std::get<static_cast<std::size_t>(FIndex)>(this->GetFields()).GetValue();
|
||||
}
|
||||
|
||||
private:
|
||||
AllFields m_Fields;
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
class MessageImplFieldsReadBase : public TBase {
|
||||
private:
|
||||
// normal reading
|
||||
template <typename TField>
|
||||
void ReadField(Field<TField, 0>& field, DataBuffer& buffer) {
|
||||
this->ReadData(field.GetValue(), buffer);
|
||||
}
|
||||
|
||||
// reading field in bitfield
|
||||
template <typename TFieldType, typename TField, int 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
|
||||
field.GetValue() = (data >> TotalBitCount - IAlignment - offset) & ((1 << IAlignment) - 1);
|
||||
}
|
||||
|
||||
// reading bitfield
|
||||
template <typename TContainer, typename TFirst, typename... TFields>
|
||||
void ReadField(Field<BitField<TContainer, TFirst, TFields...>, 0>& field, DataBuffer& buffer) {
|
||||
TContainer data;
|
||||
this->ReadData(data, buffer);
|
||||
std::size_t offset = 0;
|
||||
TupleForEach(
|
||||
[data, this, &offset](auto& field) {
|
||||
this->ReadField(field, data, offset);
|
||||
offset += field.GetAlignment();
|
||||
},
|
||||
field.GetValue().GetFields());
|
||||
}
|
||||
|
||||
void ReadImpl(DataBuffer& buffer) override {
|
||||
auto& allFields = this->GetFields();
|
||||
TupleForEach([&buffer, this](auto& field) { this->ReadField(field, buffer); }, allFields);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
class MessageImplFieldsWriteBase : public TBase {
|
||||
private:
|
||||
// normal writing
|
||||
template <typename TField>
|
||||
void WriteField(Field<TField, 0>& field, DataBuffer& buffer) {
|
||||
this->WriteData(field.GetValue(), buffer);
|
||||
}
|
||||
|
||||
// writing field in bitfield
|
||||
template <typename TFieldType, typename TField, int IAlignment>
|
||||
void WriteField(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
|
||||
data |= (field.GetValue() & ((1 << IAlignment) - 1)) << TotalBitCount - IAlignment - offset;
|
||||
}
|
||||
|
||||
// writing bitfield
|
||||
template <typename TContainer, typename TFirst, typename... TFields>
|
||||
void WriteField(Field<BitField<TContainer, TFirst, TFields...>, 0>& field, DataBuffer& buffer) {
|
||||
TContainer data = 0;
|
||||
std::size_t offset = 0;
|
||||
TupleForEach(
|
||||
[&data, this, &offset](auto& field) {
|
||||
this->WriteField(field, data, offset);
|
||||
offset += field.GetAlignment();
|
||||
},
|
||||
field.GetValue().GetFields());
|
||||
this->WriteData(data, buffer);
|
||||
}
|
||||
|
||||
void WriteImpl(DataBuffer& buffer) override {
|
||||
auto& allFields = this->GetFields();
|
||||
TupleForEach([&buffer, this](auto& field) { this->WriteField(field, buffer); }, allFields);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
class MessageImplFieldsValidBase : public TBase {
|
||||
protected:
|
||||
bool ValidImpl() const override {
|
||||
// Access fields via interface provided in previous chunk
|
||||
// auto& allFields = TBase::GetFields();
|
||||
//... // validate all the fields
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
} // namespace sp
|
||||
Reference in New Issue
Block a user