refactor(packet): replace if-constexpr chain with explicit template specializations

Refactor `get_packet_id()` by removing the if-constexpr chain and instead
providing explicit specializations for each packet type. This simplifies the
primary template (which now only contains a static assertion) and improves
compile-time dispatch clarity. The `always_false` helper is no longer needed
in the primary template.
This commit is contained in:
2026-06-27 10:51:57 +08:00
parent e3f973284c
commit aace0b4888

View File

@@ -59,43 +59,53 @@ enum class PacketEnum : uint16_t {
}; };
template <typename> struct always_false : std::false_type {}; // NOLINT template <typename> struct always_false : std::false_type {}; // NOLINT
template <typename T> constexpr uint16_t get_packet_id() {
using enum PacketEnum;
using std::is_same_v;
using U = std::decay_t<T>; template <typename T> constexpr uint16_t get_packet_id() {
constexpr auto& to_num = std::to_underlying<PacketEnum>; static_assert(always_false<T>::value, "Unknown Type");
if constexpr (is_same_v<U, LoginReq>) { return 0;
return to_num(LOGIN_REQ); }
} else if constexpr (is_same_v<U, LoginRsp>) {
return to_num(LOGIN_RSP); template <> constexpr uint16_t get_packet_id<LoginReq>() {
} else if constexpr (is_same_v<U, LogoutReq>) { return std::to_underlying(PacketEnum::LOGIN_REQ);
return to_num(LOGOUT_REQ); }
} else if constexpr (is_same_v<U, LogoutRsp>) { template <> constexpr uint16_t get_packet_id<LoginRsp>() {
return to_num(LOGOUT_RSP); return std::to_underlying(PacketEnum::LOGIN_RSP);
} else if constexpr (is_same_v<U, PlayerInfo>) { }
return to_num(PLAYER_INFO); template <> constexpr uint16_t get_packet_id<LogoutReq>() {
} else if constexpr (is_same_v<U, PlayerPos>) { return std::to_underlying(PacketEnum::LOGOUT_REQ);
return to_num(PLAYER_POS); }
} else if constexpr (is_same_v<U, PlayerInfoRsp>) { template <> constexpr uint16_t get_packet_id<LogoutRsp>() {
return to_num(PLAYER_INFO_RSP); return std::to_underlying(PacketEnum::LOGOUT_RSP);
} else if constexpr (is_same_v<U, ChunkDataReq>) { }
return to_num(CHUNK_DATA_REQ); template <> constexpr uint16_t get_packet_id<PlayerInfo>() {
} else if constexpr (is_same_v<U, ChunkDataRsp>) { return std::to_underlying(PacketEnum::PLAYER_INFO);
return to_num(CHUNK_DATA_RSP); }
} else if constexpr (is_same_v<U, BlockChangeReq>) { template <> constexpr uint16_t get_packet_id<PlayerPos>() {
return to_num(BLOCK_CHANGE_REQ); return std::to_underlying(PacketEnum::PLAYER_POS);
} else if constexpr (is_same_v<U, BlockChangeRsp>) { }
return to_num(BLOCK_CHANGE_RSP); template <> constexpr uint16_t get_packet_id<PlayerInfoRsp>() {
} else if constexpr (is_same_v<U, UpdateTime>) { return std::to_underlying(PacketEnum::PLAYER_INFO_RSP);
return to_num(UPDATE_TIME); }
} else if constexpr (is_same_v<U, Ping>) { template <> constexpr uint16_t get_packet_id<ChunkDataReq>() {
return to_num(PING); return std::to_underlying(PacketEnum::CHUNK_DATA_REQ);
} else if constexpr (is_same_v<U, Pong>) { }
return to_num(PONG); template <> constexpr uint16_t get_packet_id<ChunkDataRsp>() {
} else { return std::to_underlying(PacketEnum::CHUNK_DATA_RSP);
static_assert(always_false<U>::value, "Unknown Type"); }
} template <> constexpr uint16_t get_packet_id<BlockChangeReq>() {
return std::to_underlying(PacketEnum::BLOCK_CHANGE_REQ);
}
template <> constexpr uint16_t get_packet_id<BlockChangeRsp>() {
return std::to_underlying(PacketEnum::BLOCK_CHANGE_RSP);
}
template <> constexpr uint16_t get_packet_id<UpdateTime>() {
return std::to_underlying(PacketEnum::UPDATE_TIME);
}
template <> constexpr uint16_t get_packet_id<Ping>() {
return std::to_underlying(PacketEnum::PING);
}
template <> constexpr uint16_t get_packet_id<Pong>() {
return std::to_underlying(PacketEnum::PONG);
} }
template <typename T> template <typename T>