24 template <IsPacket PacketType>
30 std::unique_ptr<Packet> (*
const *ptr)(
const ByteView) =
31 it->second.target<std::unique_ptr<Packet> (*)(
const ByteView)>();
32 if (ptr && ptr == &(&PacketType::deserialize))
36 std::string exception_msg =
37 "An error occured while trying to register packet deserializer: it is already initialized for this unique packet id with different function!";
39 auto t =
instance().type_names.at(PacketType::static_unique_id);
40 auto hex = [](
int value) -> std::string
42 std::stringstream stream;
43 stream << std::hex << value;
47 exception_msg +=
"\n It is already initialized for type with name: " + std::string(t) +
48 " with an ID of 0x" + hex(PacketType::static_unique_id);
49 exception_msg +=
".\n You are passing " + std::string(
typeid(PacketType).name()) +
50 " with an ID of 0x" + hex(PacketType::static_unique_id);
51 exception_msg +=
".\n Please check static unique IDs for these packets.";
53 spdlog::critical(exception_msg);
54 throw std::invalid_argument(exception_msg.c_str());
58 const char *type_name =
typeid(PacketType).name();
60 spdlog::info(
"Registered deserializer for {} with id {}", type_name, PacketType::static_unique_id);
62 instance().type_names[PacketType::static_unique_id] = type_name;
80 std::unique_ptr<Packet> (*
const *ptr)(
const ByteView) =
81 factory.target<std::unique_ptr<Packet> (*)(
const ByteView)>();
82 if (!ptr || ptr == it->second.target<std::unique_ptr<Packet> (*)(
const ByteView)>())
84 throw std::invalid_argument(
"Packet deserializer already initialized with different function!");
107 return it->second(bytearray);
132 std::unordered_map<UniquePacketID, const char *> type_names;
145 template <
typename T>
A class responsible for registering and creating packet deserializers.
static void RegisterDeserializer(UniquePacketID packet_id, PacketDeserializeFunc factory)
Register a packet deserializer function for a specific packet ID.
static void RegisterDeserializer()
Register a packet deserializer function for a specific PacketType.
std::unordered_map< UniquePacketID, PacketDeserializeFunc > packet_deserializers_
Map storing registered packet deserializer functions.
static PacketFactory & instance()
Instance of the Packet Factory.
static std::unique_ptr< PacketFactory > instance_
static std::unique_ptr< Packet > Deserialize(const ByteView &bytearray, UniquePacketID packet_type)
Deserialize a byte view into a unique pointer of the specified packet type.
This is the main namespace for the Mal Packet Weaver library.
std::function< std::unique_ptr< Packet >(const ByteView)> PacketDeserializeFunc
Type alias for packet deserialization function.
uint32_t UniquePacketID
Unique identifier for a packet, combining subsystem and packet IDs.
Helper class for registering a packet type with the PacketFactory.
PacketTypeRegistrationHelper()
Constructor. Registers the packet type with the PacketFactory.