From a7054444ea08836b668a35d9bd577bc088adbe87 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Fri, 9 Jul 2021 11:40:03 -0400 Subject: [PATCH] Use to_underlying instead of manually writing it out or static asserts. Cleans up some code now that we don't have to choose between cumbersome template bits with decltype and std::underlying_type_t and cassting to a fixed type which might be wrong. --- .../commands/payload/SetupPayloadParseCommand.cpp | 4 ++-- src/app/util/message.cpp | 3 ++- src/controller/CHIPDevice.cpp | 5 ++--- src/controller/CHIPDevice.h | 5 ++--- src/lib/core/CHIPTLV.h | 3 ++- src/messaging/ExchangeContext.h | 6 +++--- src/messaging/ExchangeMgr.h | 7 +++---- src/protocols/bdx/BdxTransferSession.cpp | 5 ++--- src/protocols/secure_channel/CASESession.cpp | 3 ++- src/protocols/secure_channel/PASESession.cpp | 3 ++- src/protocols/secure_channel/StatusReport.cpp | 4 ++-- src/transport/raw/MessageHeader.cpp | 2 +- src/transport/raw/MessageHeader.h | 7 +++---- 13 files changed, 28 insertions(+), 29 deletions(-) diff --git a/examples/chip-tool/commands/payload/SetupPayloadParseCommand.cpp b/examples/chip-tool/commands/payload/SetupPayloadParseCommand.cpp index 41d745b07515e2..88c118e70e2949 100644 --- a/examples/chip-tool/commands/payload/SetupPayloadParseCommand.cpp +++ b/examples/chip-tool/commands/payload/SetupPayloadParseCommand.cpp @@ -17,6 +17,7 @@ */ #include "SetupPayloadParseCommand.h" +#include #include #include #include @@ -48,8 +49,7 @@ CHIP_ERROR SetupPayloadParseCommand::Print(chip::SetupPayload payload) std::vector optionalVendorData; CHIP_ERROR err = CHIP_NO_ERROR; - ChipLogProgress(SetupPayload, "CommissioningFlow: %" PRIu8, - static_cast>(payload.commissioningFlow)); + ChipLogProgress(SetupPayload, "CommissioningFlow: %" PRIu8, to_underlying(payload.commissioningFlow)); ChipLogProgress(SetupPayload, "VendorID: %u", payload.vendorID); ChipLogProgress(SetupPayload, "Version: %u", payload.version); ChipLogProgress(SetupPayload, "ProductID: %u", payload.productID); diff --git a/src/app/util/message.cpp b/src/app/util/message.cpp index a5ecf4c9ab24da..cd59151cecbf5d 100644 --- a/src/app/util/message.cpp +++ b/src/app/util/message.cpp @@ -42,6 +42,7 @@ #include #include #include +#include using namespace chip; @@ -184,7 +185,7 @@ void emberAfPutInt16sInResp(int16_t value) void emberAfPutStatusInResp(EmberAfStatus value) { - emberAfPutInt8uInResp(static_cast>(value)); + emberAfPutInt8uInResp(to_underlying(value)); } // ------------------------------------ diff --git a/src/controller/CHIPDevice.cpp b/src/controller/CHIPDevice.cpp index d04ba07ae9f71b..5304bb37a74047 100644 --- a/src/controller/CHIPDevice.cpp +++ b/src/controller/CHIPDevice.cpp @@ -48,6 +48,7 @@ #include #include #include +#include #include #include #include @@ -184,9 +185,7 @@ CHIP_ERROR Device::Serialize(SerializedDevice & output) serializable.mDeviceOperationalCertProvisioned = (mDeviceOperationalCertProvisioned) ? 1 : 0; - static_assert(std::is_same::type, uint8_t>::value, - "The underlying type of Transport::Type is not uint8_t."); - serializable.mDeviceTransport = static_cast(mDeviceAddress.GetTransportType()); + serializable.mDeviceTransport = to_underlying(mDeviceAddress.GetTransportType()); ReturnErrorOnFailure(Inet::GetInterfaceName(mDeviceAddress.GetInterface(), Uint8::to_char(serializable.mInterfaceName), sizeof(serializable.mInterfaceName))); diff --git a/src/controller/CHIPDevice.h b/src/controller/CHIPDevice.h index 3fa1da6a016544..1c07f693e12806 100644 --- a/src/controller/CHIPDevice.h +++ b/src/controller/CHIPDevice.h @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -135,9 +136,7 @@ class DLL_EXPORT Device : public Messaging::ExchangeDelegate, public SessionEsta template ::value>> CHIP_ERROR SendMessage(MessageType msgType, System::PacketBufferHandle && message) { - static_assert(std::is_same, uint8_t>::value, "Enum is wrong size; cast is not safe"); - return SendMessage(Protocols::MessageTypeTraits::ProtocolId(), static_cast(msgType), - std::move(message)); + return SendMessage(Protocols::MessageTypeTraits::ProtocolId(), to_underlying(msgType), std::move(message)); } CHIP_ERROR SendReadAttributeRequest(app::AttributePathParams aPath, Callback::Cancelable * onSuccessCallback, diff --git a/src/lib/core/CHIPTLV.h b/src/lib/core/CHIPTLV.h index cfcf68a4d077c4..71cdfbbc776fb4 100644 --- a/src/lib/core/CHIPTLV.h +++ b/src/lib/core/CHIPTLV.h @@ -33,6 +33,7 @@ #include #include +#include #include #include @@ -1115,7 +1116,7 @@ class DLL_EXPORT TLVWriter template CHIP_ERROR Put(uint64_t tag, T data) { - return Put(tag, static_cast>(data)); + return Put(tag, to_underlying(data)); } /** diff --git a/src/messaging/ExchangeContext.h b/src/messaging/ExchangeContext.h index c766af817a0538..45ec26a08cafa6 100644 --- a/src/messaging/ExchangeContext.h +++ b/src/messaging/ExchangeContext.h @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -104,9 +105,8 @@ class DLL_EXPORT ExchangeContext : public ReliableMessageContext, public Referen CHIP_ERROR SendMessage(MessageType msgType, System::PacketBufferHandle && msgPayload, const SendFlags & sendFlags = SendFlags(SendMessageFlags::kNone)) { - static_assert(std::is_same, uint8_t>::value, "Enum is wrong size; cast is not safe"); - return SendMessage(Protocols::MessageTypeTraits::ProtocolId(), static_cast(msgType), - std::move(msgPayload), sendFlags); + return SendMessage(Protocols::MessageTypeTraits::ProtocolId(), to_underlying(msgType), std::move(msgPayload), + sendFlags); } /** diff --git a/src/messaging/ExchangeMgr.h b/src/messaging/ExchangeMgr.h index 6a83ff5afacb20..9b7dbbd4656e81 100644 --- a/src/messaging/ExchangeMgr.h +++ b/src/messaging/ExchangeMgr.h @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -138,9 +139,8 @@ class DLL_EXPORT ExchangeManager : public SecureSessionMgrDelegate template ::value>> CHIP_ERROR RegisterUnsolicitedMessageHandlerForType(MessageType msgType, ExchangeDelegate * delegate) { - static_assert(std::is_same, uint8_t>::value, "Enum is wrong size; cast is not safe"); return RegisterUnsolicitedMessageHandlerForType(Protocols::MessageTypeTraits::ProtocolId(), - static_cast(msgType), delegate); + to_underlying(msgType), delegate); } /** @@ -173,9 +173,8 @@ class DLL_EXPORT ExchangeManager : public SecureSessionMgrDelegate template ::value>> CHIP_ERROR UnregisterUnsolicitedMessageHandlerForType(MessageType msgType) { - static_assert(std::is_same, uint8_t>::value, "Enum is wrong size; cast is not safe"); return UnregisterUnsolicitedMessageHandlerForType(Protocols::MessageTypeTraits::ProtocolId(), - static_cast(msgType)); + to_underlying(msgType)); } /** diff --git a/src/protocols/bdx/BdxTransferSession.cpp b/src/protocols/bdx/BdxTransferSession.cpp index 4cfcde276a4292..a9d3280640062b 100644 --- a/src/protocols/bdx/BdxTransferSession.cpp +++ b/src/protocols/bdx/BdxTransferSession.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -754,12 +755,10 @@ CHIP_ERROR TransferSession::VerifyProposedMode(const BitFlags, uint16_t>::value, "Cast is not safe"); - mStatusReportData.statusCode = code; Protocols::SecureChannel::StatusReport report(Protocols::SecureChannel::GeneralStatusCode::kFailure, - Protocols::BDX::Id.ToFullyQualifiedSpecForm(), static_cast(code)); + Protocols::BDX::Id.ToFullyQualifiedSpecForm(), to_underlying(code)); size_t msgSize = report.Size(); Encoding::LittleEndian::PacketBufferWriter bbuf(chip::MessagePacketBuffer::New(msgSize), msgSize); VerifyOrExit(!bbuf.IsNull(), mPendingOutput = OutputEventType::kInternalError); diff --git a/src/protocols/secure_channel/CASESession.cpp b/src/protocols/secure_channel/CASESession.cpp index ff18f1b4be259e..55dbfed81295cf 100644 --- a/src/protocols/secure_channel/CASESession.cpp +++ b/src/protocols/secure_channel/CASESession.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include namespace chip { @@ -300,7 +301,7 @@ void CASESession::OnResponseTimeout(ExchangeContext * ec) VerifyOrReturn(mExchangeCtxt == ec, ChipLogError(SecureChannel, "CASESession::OnResponseTimeout exchange doesn't match")); ChipLogError(SecureChannel, "CASESession timed out while waiting for a response from the peer. Expected message type was %" PRIu8, - static_cast>(mNextExpectedMsg)); + to_underlying(mNextExpectedMsg)); mDelegate->OnSessionEstablishmentError(CHIP_ERROR_TIMEOUT); Clear(); } diff --git a/src/protocols/secure_channel/PASESession.cpp b/src/protocols/secure_channel/PASESession.cpp index 8259524a4e9e33..b98b79fe7aa13d 100644 --- a/src/protocols/secure_channel/PASESession.cpp +++ b/src/protocols/secure_channel/PASESession.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include namespace chip { @@ -330,7 +331,7 @@ void PASESession::OnResponseTimeout(ExchangeContext * ec) ChipLogError(SecureChannel, "PASESession::OnResponseTimeout exchange doesn't match")); ChipLogError(SecureChannel, "PASESession timed out while waiting for a response from the peer. Expected message type was %" PRIu8, - static_cast>(mNextExpectedMsg)); + to_underlying(mNextExpectedMsg)); mDelegate->OnSessionEstablishmentError(CHIP_ERROR_TIMEOUT); Clear(); } diff --git a/src/protocols/secure_channel/StatusReport.cpp b/src/protocols/secure_channel/StatusReport.cpp index 73ade99c920c54..e24f12752898b7 100644 --- a/src/protocols/secure_channel/StatusReport.cpp +++ b/src/protocols/secure_channel/StatusReport.cpp @@ -21,6 +21,7 @@ #include #include +#include #include @@ -77,8 +78,7 @@ CHIP_ERROR StatusReport::Parse(System::PacketBufferHandle buf) Encoding::LittleEndian::BufferWriter & StatusReport::WriteToBuffer(Encoding::LittleEndian::BufferWriter & buf) const { - static_assert(std::is_same, uint16_t>::value, "Cast is not safe"); - buf.Put16(static_cast(mGeneralCode)).Put32(mProtocolId).Put16(mProtocolCode); + buf.Put16(to_underlying(mGeneralCode)).Put32(mProtocolId).Put16(mProtocolCode); if (!mProtocolData.IsNull()) { buf.Put(mProtocolData->Start(), mProtocolData->DataLength()); diff --git a/src/transport/raw/MessageHeader.cpp b/src/transport/raw/MessageHeader.cpp index fc3e312715c444..46d0c7f1276158 100644 --- a/src/transport/raw/MessageHeader.cpp +++ b/src/transport/raw/MessageHeader.cpp @@ -319,7 +319,7 @@ CHIP_ERROR PayloadHeader::Encode(uint8_t * data, uint16_t size, uint16_t * encod LittleEndian::Write16(p, mExchangeID); if (HaveVendorId()) { - LittleEndian::Write16(p, static_cast>(mProtocolID.GetVendorId())); + LittleEndian::Write16(p, to_underlying(mProtocolID.GetVendorId())); } LittleEndian::Write16(p, mProtocolID.GetProtocolId()); if (mAckId.HasValue()) diff --git a/src/transport/raw/MessageHeader.h b/src/transport/raw/MessageHeader.h index f62f00ccb95be8..20e1d67ad0f5b2 100644 --- a/src/transport/raw/MessageHeader.h +++ b/src/transport/raw/MessageHeader.h @@ -33,6 +33,7 @@ #include #include #include +#include #include namespace chip { @@ -341,8 +342,7 @@ class PayloadHeader template ::value>> bool HasMessageType(MessageType type) const { - static_assert(std::is_same, uint8_t>::value, "Enum is wrong size; cast is not safe"); - return HasProtocol(Protocols::MessageTypeTraits::ProtocolId()) && HasMessageType(static_cast(type)); + return HasProtocol(Protocols::MessageTypeTraits::ProtocolId()) && HasMessageType(to_underlying(type)); } /** @@ -373,8 +373,7 @@ class PayloadHeader template ::value>> PayloadHeader & SetMessageType(MessageType type) { - static_assert(std::is_same, uint8_t>::value, "Enum is wrong size; cast is not safe"); - SetMessageType(Protocols::MessageTypeTraits::ProtocolId(), static_cast(type)); + SetMessageType(Protocols::MessageTypeTraits::ProtocolId(), to_underlying(type)); return *this; }