Reviewed-on: #1 Co-authored-by: Persson-dev <sim16.prib@gmail.com> Co-committed-by: Persson-dev <sim16.prib@gmail.com>
120 lines
2.4 KiB
C++
120 lines
2.4 KiB
C++
#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
|