SFMLHelper: Add stream insertion overload helpers for enum types

Will be used in future changes to eliminate the need to cast when
inserting various message IDs
This commit is contained in:
Lioncash 2021-09-22 13:40:46 -04:00
parent cc84799c7f
commit 07af775afa
2 changed files with 25 additions and 6 deletions

View File

@ -3,18 +3,37 @@
#pragma once
#include <type_traits>
#include <SFML/Network/Packet.hpp>
#include "Common/CommonTypes.h"
#include "Common/Swap.h"
namespace sf
{
class Packet;
}
sf::Packet& operator>>(sf::Packet& packet, Common::BigEndianValue<u16>& data);
sf::Packet& operator>>(sf::Packet& packet, Common::BigEndianValue<u32>& data);
sf::Packet& operator>>(sf::Packet& packet, Common::BigEndianValue<u64>& data);
template <typename Enum, std::enable_if_t<std::is_enum_v<Enum>>* = nullptr>
sf::Packet& operator<<(sf::Packet& packet, Enum e)
{
using Underlying = std::underlying_type_t<Enum>;
packet << static_cast<Underlying>(e);
return packet;
}
template <typename Enum, std::enable_if_t<std::is_enum_v<Enum>>* = nullptr>
sf::Packet& operator>>(sf::Packet& packet, Enum& e)
{
using Underlying = std::underlying_type_t<Enum>;
Underlying value{};
packet >> value;
e = static_cast<Enum>(value);
return packet;
}
namespace Common
{
u64 PacketReadU64(sf::Packet& packet);

View File

@ -121,7 +121,7 @@ struct NetTraversalConfig
};
// messages
enum
enum MessageID : u8
{
NP_MSG_PLAYER_JOIN = 0x10,
NP_MSG_PLAYER_LEAVE = 0x11,