This commit is contained in:
2024-07-20 00:45:22 +02:00
parent 159867e747
commit 3eb3eadd4b
5 changed files with 73 additions and 27 deletions

View File

@@ -1,5 +1,10 @@
#pragma once
/**
* \file Packets.h
* \brief File containing the definitions of the packets
*/
#include <blitz/protocol/PacketData.h>
#include <blitz/protocol/PacketDeclare.h>
#include <string>
@@ -9,14 +14,20 @@ namespace protocol {
class PacketVisitor;
/** A Packet id is 8 bits wide */
using PacketID = std::uint8_t;
#define DeclarePacket(PacketName, ...) PacketName,
#define DeclarePacket(PacketName, ...) /** PacketName */ PacketName,
/**
* \enum PacketType
* \brief Map a Packet to an id
*/
enum class PacketType : PacketID {
DeclareAllPacket()
/** The number of packets */
PACKET_COUNT
};
@@ -26,9 +37,16 @@ enum class PacketType : PacketID {
class Packet {
public:
/**
* \return The real type of the packet
*/
virtual PacketType GetType() const = 0;
private:
/** Use a PacketVisitor to make double-dispatch possible */
virtual void Accept(PacketVisitor& a_Visitor) const = 0;
friend class PacketVisitor;
};
@@ -38,16 +56,21 @@ class Packet {
namespace packets {
/**
* \class ConcretePacket
* \brief A Packet associated with an id and holding data
* \tparam PT The packet type
* \tparam Data The structure holding the data of the packet (in blitz::protocol::data namespace)
*/
template <PacketType PT, typename Data>
class ConcretePacket : public Packet {
public:
/** The type of the struct holding the data */
using PacketDataType = Data;
/** The structure holding the actual data */
PacketDataType m_Data;
/** Construct the packet with data of type PacketDataType */
ConcretePacket(const PacketDataType& a_Data = {});
constexpr PacketType GetType() const override {
@@ -56,8 +79,6 @@ class ConcretePacket : public Packet {
private:
void Accept(PacketVisitor& a_Visitor) const override;
friend class PacketVisitor;
};
@@ -72,7 +93,8 @@ class ConcretePacket : public Packet {
using PacketName = ConcretePacket<PacketType::PacketName, data::PacketName>; \
template class ConcretePacket<PacketType::PacketName, data::PacketName>;
#else
#define DeclarePacket(PacketName, ...) using PacketName = ConcretePacket<PacketType::PacketName, data::PacketName>;
#define DeclarePacket(PacketName, ...) /** Defines the PacketName packet */ \
using PacketName = ConcretePacket<PacketType::PacketName, data::PacketName>;
#endif
DeclareAllPacket()