enum class field access

This commit is contained in:
2025-02-18 19:33:02 +01:00
parent e16ad84865
commit 66835554f1
5 changed files with 20 additions and 8 deletions

View File

@@ -4,7 +4,7 @@
#include <sp/protocol/Field.h> #include <sp/protocol/Field.h>
#include <sp/protocol/MessageBase.h> #include <sp/protocol/MessageBase.h>
enum DisconnectPacketFields { enum class DisconnectFieldsE {
Reason = 0 Reason = 0
}; };
@@ -15,6 +15,6 @@ DeclarePacket(Disconnect){
PacketConstructor(Disconnect) PacketConstructor(Disconnect)
const std::string& GetReason() const { const std::string& GetReason() const {
return GetField<Reason>(); return GetField<DisconnectFieldsE, DisconnectFieldsE::Reason>();
} }
}; };

View File

@@ -4,9 +4,8 @@
#include <sp/protocol/Field.h> #include <sp/protocol/Field.h>
#include <sp/protocol/MessageBase.h> #include <sp/protocol/MessageBase.h>
enum KeepAlivePacketFields { enum class KeepAliveFieldsE {
KeepAliveId = 0, KeepAliveId = 0,
TestAlignField = 1,
}; };
using KeepAliveFields = std::tuple< using KeepAliveFields = std::tuple<
@@ -18,6 +17,6 @@ DeclarePacket(KeepAlive){
PacketConstructor(KeepAlive) PacketConstructor(KeepAlive)
std::uint64_t GetKeepAliveId() const { std::uint64_t GetKeepAliveId() const {
return GetField<KeepAlive>(); return GetField<KeepAliveFieldsE, KeepAliveFieldsE::KeepAliveId>();
} }
}; };

View File

@@ -4,7 +4,8 @@
#include <sp/protocol/Field.h> #include <sp/protocol/Field.h>
#include <sp/protocol/MessageBase.h> #include <sp/protocol/MessageBase.h>
enum UpgradeTowerPacketFields {
enum class UpgradeTowerFieldsE {
m_Tower = 0, m_Tower = 0,
m_Upgrade, m_Upgrade,
}; };
@@ -21,10 +22,10 @@ DeclarePacket(UpgradeTower){
PacketConstructor(UpgradeTower) PacketConstructor(UpgradeTower)
std::uint16_t GetTowerId() const { std::uint16_t GetTowerId() const {
return GetField<0>().GetField<m_Tower>(); return GetField<0>().GetField<UpgradeTowerFieldsE, UpgradeTowerFieldsE::m_Tower>();
} }
std::uint8_t GetTowerUpgrade() const { std::uint8_t GetTowerUpgrade() const {
return GetField<0>().GetField<m_Upgrade>(); return GetField<0>().GetField<UpgradeTowerFieldsE, UpgradeTowerFieldsE::m_Upgrade>();
} }
}; };

View File

@@ -45,6 +45,12 @@ class BitField {
return std::get<FIndex>(this->GetFields()).GetValue(); return std::get<FIndex>(this->GetFields()).GetValue();
} }
// allow use of enums
template <typename E, E FIndex>
const auto& GetField() const {
return std::get<static_cast<std::size_t>(FIndex)>(this->GetFields()).GetValue();
}
private: private:
template <int IOffset, typename... T, std::enable_if_t<IOffset >= sizeof...(T), bool> = true> template <int IOffset, typename... T, std::enable_if_t<IOffset >= sizeof...(T), bool> = true>
void Apply(const std::tuple<T...>& args) {} void Apply(const std::tuple<T...>& args) {}

View File

@@ -119,6 +119,12 @@ class MessageImplFieldsBase : public TBase {
return std::get<FIndex>(GetFields()).GetValue(); return std::get<FIndex>(GetFields()).GetValue();
} }
// allow use of enums
template <typename E, E FIndex>
const auto& GetField() const {
return std::get<static_cast<std::size_t>(FIndex)>(this->GetFields()).GetValue();
}
private: private:
AllFields m_Fields; AllFields m_Fields;
}; };