Browse Source

Don't use inheritance with tl::expected

pull/6644/head
staphen 3 years ago committed by Anders Jenbo
parent
commit
98c0e8f2da
  1. 23
      Source/dvlnet/packet.cpp
  2. 37
      Source/dvlnet/packet.h
  3. 11
      Source/dvlnet/tcp_server.h

23
Source/dvlnet/packet.cpp

@ -84,35 +84,36 @@ const char *packet_type_to_string(uint8_t packetType)
}
}
PacketTypeError::PacketTypeError(std::uint8_t unknownPacketType)
: message_(StrCat("Unknown packet type ", unknownPacketType))
PacketError PacketTypeError(std::uint8_t unknownPacketType)
{
return PacketError(StrCat("Unknown packet type ", unknownPacketType));
}
PacketTypeError::PacketTypeError(std::initializer_list<packet_type> expectedTypes, std::uint8_t actual)
PacketError PacketTypeError(std::initializer_list<packet_type> expectedTypes, std::uint8_t actual)
{
message_ = "Expected packet of type ";
const auto appendPacketType = [this](std::uint8_t t) {
std::string message = "Expected packet of type ";
const auto appendPacketType = [&](std::uint8_t t) {
const char *typeStr = packet_type_to_string(t);
if (typeStr != nullptr)
message_.append(typeStr);
message.append(typeStr);
else
StrAppend(message_, t);
StrAppend(message, t);
};
constexpr char KJoinTypes[] = " or ";
for (const packet_type t : expectedTypes) {
appendPacketType(t);
message_.append(KJoinTypes);
message.append(KJoinTypes);
}
message_.resize(message_.size() - (sizeof(KJoinTypes) - 1));
message_.append(", got");
message.resize(message.size() - (sizeof(KJoinTypes) - 1));
message.append(", got");
appendPacketType(actual);
return PacketError(std::move(message));
}
namespace {
tl::expected<void, PacketTypeError> CheckPacketTypeOneOf(std::initializer_list<packet_type> expectedTypes, std::uint8_t actualType)
tl::expected<void, PacketError> CheckPacketTypeOneOf(std::initializer_list<packet_type> expectedTypes, std::uint8_t actualType)
{
if (c_none_of(expectedTypes,
[actualType](uint8_t type) { return type == actualType; })) {

37
Source/dvlnet/packet.h

@ -61,26 +61,43 @@ static constexpr plr_t PLR_BROADCAST = 0xFF;
class PacketError {
public:
virtual const char *what() const
PacketError()
: message_(std::string_view("Incorrect package size"))
{
return "Incorrect package size";
}
};
class PacketTypeError : public PacketError {
public:
explicit PacketTypeError(std::uint8_t unknownPacketType);
PacketTypeError(std::initializer_list<packet_type> expectedTypes, std::uint8_t actual);
PacketError(const char message[])
: message_(std::string_view(message))
{
}
PacketError(std::string &&message)
: message_(std::move(message))
{
}
const char *what() const override
PacketError(const PacketError &error)
: message_(std::string(error.message_))
{
return message_.c_str();
}
PacketError(PacketError &&error)
: message_(std::move(error.message_))
{
}
std::string_view what() const
{
return message_;
}
private:
std::string message_;
StringOrView message_;
};
PacketError PacketTypeError(std::uint8_t unknownPacketType);
PacketError PacketTypeError(std::initializer_list<packet_type> expectedTypes, std::uint8_t actual);
class packet {
protected:
packet_type m_type;

11
Source/dvlnet/tcp_server.h

@ -27,13 +27,10 @@
namespace devilution::net {
class ServerError : public PacketError {
public:
const char *what() const override
{
return "Invalid player ID";
}
};
inline PacketError ServerError()
{
return PacketError("Invalid player ID");
}
class tcp_server {
public:

Loading…
Cancel
Save