diff --git a/src/app/CommandHandler.cpp b/src/app/CommandHandler.cpp index 901cc658580749..005067c96bd7c4 100644 --- a/src/app/CommandHandler.cpp +++ b/src/app/CommandHandler.cpp @@ -158,7 +158,7 @@ CHIP_ERROR CommandHandler::ValidateInvokeRequestMessageAndBuildRegistry(InvokeRe // Grab the CommandRef if there is one, and validate that it's there when it // has to be. - Optional commandRef; + std::optional commandRef; uint16_t ref; err = commandData.GetRef(&ref); VerifyOrReturnError(err == CHIP_NO_ERROR || err == CHIP_END_OF_TLV, err); @@ -168,7 +168,7 @@ CHIP_ERROR CommandHandler::ValidateInvokeRequestMessageAndBuildRegistry(InvokeRe } if (err == CHIP_NO_ERROR) { - commandRef.SetValue(ref); + commandRef.emplace(ref); } // Adding can fail if concretePath is not unique, or if commandRef is a value @@ -590,10 +590,9 @@ CHIP_ERROR CommandHandler::PrepareInvokeResponseCommand(const ConcreteCommandPat const CommandHandler::InvokeResponseParameters & aPrepareParameters) { auto commandPathRegistryEntry = GetCommandPathRegistry().Find(aPrepareParameters.mRequestCommandPath); - VerifyOrReturnValue(commandPathRegistryEntry.HasValue(), CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnValue(commandPathRegistryEntry.has_value(), CHIP_ERROR_INCORRECT_STATE); - return PrepareInvokeResponseCommand(commandPathRegistryEntry.Value(), aResponseCommandPath, - aPrepareParameters.mStartOrEndDataStruct); + return PrepareInvokeResponseCommand(*commandPathRegistryEntry, aResponseCommandPath, aPrepareParameters.mStartOrEndDataStruct); } CHIP_ERROR CommandHandler::PrepareCommand(const ConcreteCommandPath & aResponseCommandPath, bool aStartDataStruct) @@ -610,9 +609,9 @@ CHIP_ERROR CommandHandler::PrepareCommand(const ConcreteCommandPath & aResponseC "Seemingly device supports batch commands, but is calling the deprecated PrepareCommand API"); auto commandPathRegistryEntry = GetCommandPathRegistry().GetFirstEntry(); - VerifyOrReturnValue(commandPathRegistryEntry.HasValue(), CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnValue(commandPathRegistryEntry.has_value(), CHIP_ERROR_INCORRECT_STATE); - return PrepareInvokeResponseCommand(commandPathRegistryEntry.Value(), aResponseCommandPath, aStartDataStruct); + return PrepareInvokeResponseCommand(*commandPathRegistryEntry, aResponseCommandPath, aStartDataStruct); } CHIP_ERROR CommandHandler::PrepareInvokeResponseCommand(const CommandPathRegistryEntry & apCommandPathRegistryEntry, @@ -675,9 +674,9 @@ CHIP_ERROR CommandHandler::FinishCommand(bool aStartDataStruct) ReturnErrorOnFailure(commandData.GetWriter()->EndContainer(mDataElementContainerType)); } - if (mRefForResponse.HasValue()) + if (mRefForResponse.has_value()) { - ReturnErrorOnFailure(commandData.Ref(mRefForResponse.Value())); + ReturnErrorOnFailure(commandData.Ref(*mRefForResponse)); } ReturnErrorOnFailure(commandData.EndOfCommandDataIB()); @@ -699,8 +698,8 @@ CHIP_ERROR CommandHandler::PrepareStatus(const ConcreteCommandPath & aCommandPat } auto commandPathRegistryEntry = GetCommandPathRegistry().Find(aCommandPath); - VerifyOrReturnError(commandPathRegistryEntry.HasValue(), CHIP_ERROR_INCORRECT_STATE); - mRefForResponse = commandPathRegistryEntry.Value().ref; + VerifyOrReturnError(commandPathRegistryEntry.has_value(), CHIP_ERROR_INCORRECT_STATE); + mRefForResponse = commandPathRegistryEntry->ref; MoveToState(State::Preparing); InvokeResponseIBs::Builder & invokeResponses = mInvokeResponseBuilder.GetInvokeResponses(); @@ -720,9 +719,9 @@ CHIP_ERROR CommandHandler::FinishStatus() VerifyOrReturnError(mState == State::AddingCommand, CHIP_ERROR_INCORRECT_STATE); CommandStatusIB::Builder & commandStatus = mInvokeResponseBuilder.GetInvokeResponses().GetInvokeResponse().GetStatus(); - if (mRefForResponse.HasValue()) + if (mRefForResponse.has_value()) { - ReturnErrorOnFailure(commandStatus.Ref(mRefForResponse.Value())); + ReturnErrorOnFailure(commandStatus.Ref(*mRefForResponse)); } ReturnErrorOnFailure(mInvokeResponseBuilder.GetInvokeResponses().GetInvokeResponse().GetStatus().EndOfCommandStatusIB()); diff --git a/src/app/CommandHandler.h b/src/app/CommandHandler.h index e31edfd444c629..2eed73daccdda9 100644 --- a/src/app/CommandHandler.h +++ b/src/app/CommandHandler.h @@ -686,7 +686,7 @@ class CommandHandler // TODO Allow flexibility in registration. BasicCommandPathRegistry mBasicCommandPathRegistry; CommandPathRegistry * mCommandPathRegistry = &mBasicCommandPathRegistry; - Optional mRefForResponse; + std::optional mRefForResponse; CommandHandlerExchangeInterface * mpResponder = nullptr; diff --git a/src/app/CommandPathRegistry.h b/src/app/CommandPathRegistry.h index 0d914ef1de3348..da0d16e1f69109 100644 --- a/src/app/CommandPathRegistry.h +++ b/src/app/CommandPathRegistry.h @@ -24,13 +24,15 @@ #include #include +#include + namespace chip { namespace app { struct CommandPathRegistryEntry { ConcreteCommandPath requestPath = ConcreteCommandPath(0, 0, 0); - Optional ref; + std::optional ref; }; class CommandPathRegistry @@ -38,11 +40,11 @@ class CommandPathRegistry public: virtual ~CommandPathRegistry() = default; - virtual Optional Find(const ConcreteCommandPath & requestPath) const = 0; - virtual Optional GetFirstEntry() const = 0; - virtual CHIP_ERROR Add(const ConcreteCommandPath & requestPath, const Optional & ref) = 0; - virtual size_t Count() const = 0; - virtual size_t MaxSize() const = 0; + virtual std::optional Find(const ConcreteCommandPath & requestPath) const = 0; + virtual std::optional GetFirstEntry() const = 0; + virtual CHIP_ERROR Add(const ConcreteCommandPath & requestPath, const std::optional & ref) = 0; + virtual size_t Count() const = 0; + virtual size_t MaxSize() const = 0; }; /** @@ -59,28 +61,28 @@ template class BasicCommandPathRegistry : public CommandPathRegistry { public: - Optional Find(const ConcreteCommandPath & requestPath) const override + std::optional Find(const ConcreteCommandPath & requestPath) const override { for (size_t i = 0; i < mCount; i++) { if (mTable[i].requestPath == requestPath) { - return MakeOptional(mTable[i]); + return std::make_optional(mTable[i]); } } - return NullOptional; + return std::nullopt; } - Optional GetFirstEntry() const override + std::optional GetFirstEntry() const override { if (mCount > 0) { - return MakeOptional(mTable[0]); + return std::make_optional(mTable[0]); } - return NullOptional; + return std::nullopt; } - CHIP_ERROR Add(const ConcreteCommandPath & requestPath, const Optional & ref) override + CHIP_ERROR Add(const ConcreteCommandPath & requestPath, const std::optional & ref) override { if (mCount >= N) { diff --git a/src/app/tests/TestBasicCommandPathRegistry.cpp b/src/app/tests/TestBasicCommandPathRegistry.cpp index 61e859b95498f0..28d7621ccbe7d1 100644 --- a/src/app/tests/TestBasicCommandPathRegistry.cpp +++ b/src/app/tests/TestBasicCommandPathRegistry.cpp @@ -36,13 +36,13 @@ void TestAddingSameConcretePath(nlTestSuite * apSuite, void * apContext) BasicCommandPathRegistry basicCommandPathRegistry; ConcreteCommandPath concretePath(0, 0, 0); - Optional commandRef; + std::optional commandRef; uint16_t commandRefValue = 0; size_t idx = 0; for (idx = 0; idx < kQuickTestSize && err == CHIP_NO_ERROR; idx++) { - commandRef.SetValue(commandRefValue); + commandRef.emplace(commandRefValue); commandRefValue++; err = basicCommandPathRegistry.Add(concretePath, commandRef); } @@ -56,8 +56,8 @@ void TestAddingSameCommandRef(nlTestSuite * apSuite, void * apContext) CHIP_ERROR err = CHIP_NO_ERROR; BasicCommandPathRegistry basicCommandPathRegistry; - Optional commandRef; - commandRef.SetValue(0); + std::optional commandRef; + commandRef.emplace(0); uint16_t endpointValue = 0; @@ -78,14 +78,14 @@ void TestAddingMaxNumberOfEntries(nlTestSuite * apSuite, void * apContext) CHIP_ERROR err = CHIP_NO_ERROR; BasicCommandPathRegistry basicCommandPathRegistry; - Optional commandRef; + std::optional commandRef; uint16_t commandRefAndEndpointValue = 0; size_t idx = 0; for (idx = 0; idx < kQuickTestSize && err == CHIP_NO_ERROR; idx++) { ConcreteCommandPath concretePath(commandRefAndEndpointValue, 0, 0); - commandRef.SetValue(commandRefAndEndpointValue); + commandRef.emplace(commandRefAndEndpointValue); commandRefAndEndpointValue++; err = basicCommandPathRegistry.Add(concretePath, commandRef); } @@ -100,14 +100,14 @@ void TestAddingTooManyEntries(nlTestSuite * apSuite, void * apContext) BasicCommandPathRegistry basicCommandPathRegistry; size_t maxPlusOne = kQuickTestSize + 1; - Optional commandRef; + std::optional commandRef; uint16_t commandRefAndEndpointValue = 0; size_t idx = 0; for (idx = 0; idx < maxPlusOne && err == CHIP_NO_ERROR; idx++) { ConcreteCommandPath concretePath(commandRefAndEndpointValue, 0, 0); - commandRef.SetValue(commandRefAndEndpointValue); + commandRef.emplace(commandRefAndEndpointValue); commandRefAndEndpointValue++; err = basicCommandPathRegistry.Add(concretePath, commandRef); } diff --git a/src/app/tests/TestCommandInteraction.cpp b/src/app/tests/TestCommandInteraction.cpp index 9837818862c21b..87296580fa695f 100644 --- a/src/app/tests/TestCommandInteraction.cpp +++ b/src/app/tests/TestCommandInteraction.cpp @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -392,7 +393,7 @@ class TestCommandInteraction const Optional & aRef) : CommandHandler(apCallback) { - GetCommandPathRegistry().Add(aRequestCommandPath, aRef); + GetCommandPathRegistry().Add(aRequestCommandPath, aRef.std_optional()); SetExchangeInterface(&mMockCommandResponder); } MockCommandResponder mMockCommandResponder; @@ -407,7 +408,8 @@ class TestCommandInteraction // payload will be included. Otherwise no payload will be included. static void GenerateInvokeResponse(nlTestSuite * apSuite, void * apContext, System::PacketBufferHandle & aPayload, CommandId aCommandId, ClusterId aClusterId = kTestClusterId, - EndpointId aEndpointId = kTestEndpointId, Optional aCommandRef = NullOptional); + EndpointId aEndpointId = kTestEndpointId, + std::optional aCommandRef = std::nullopt); static void AddInvokeRequestData(nlTestSuite * apSuite, void * apContext, CommandSender * apCommandSender, CommandId aCommandId = kTestCommandIdWithData); static void AddInvalidInvokeRequestData(nlTestSuite * apSuite, void * apContext, CommandSender * apCommandSender, @@ -494,7 +496,7 @@ void TestCommandInteraction::GenerateInvokeRequest(nlTestSuite * apSuite, void * void TestCommandInteraction::GenerateInvokeResponse(nlTestSuite * apSuite, void * apContext, System::PacketBufferHandle & aPayload, CommandId aCommandId, ClusterId aClusterId, EndpointId aEndpointId, - Optional aCommandRef) + std::optional aCommandRef) { CHIP_ERROR err = CHIP_NO_ERROR; @@ -536,9 +538,9 @@ void TestCommandInteraction::GenerateInvokeResponse(nlTestSuite * apSuite, void NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); } - if (aCommandRef.HasValue()) + if (aCommandRef.has_value()) { - NL_TEST_ASSERT(apSuite, commandDataIBBuilder.Ref(aCommandRef.Value()) == CHIP_NO_ERROR); + NL_TEST_ASSERT(apSuite, commandDataIBBuilder.Ref(*aCommandRef) == CHIP_NO_ERROR); } commandDataIBBuilder.EndOfCommandDataIB(); @@ -633,9 +635,9 @@ uint32_t TestCommandInteraction::GetAddResponseDataOverheadSizeForPath(nlTestSui ConcreteCommandPath requestCommandPath1 = { kTestEndpointId, kTestClusterId, kTestCommandIdFillResponseMessage }; ConcreteCommandPath requestCommandPath2 = { kTestEndpointId, kTestClusterId, kTestCommandIdCommandSpecificResponse }; - CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, MakeOptional(static_cast(1))); + CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, std::make_optional(static_cast(1))); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); - err = basicCommandPathRegistry.Add(requestCommandPath2, MakeOptional(static_cast(2))); + err = basicCommandPathRegistry.Add(requestCommandPath2, std::make_optional(static_cast(2))); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); err = commandHandler.AllocateBuffer(); @@ -823,7 +825,7 @@ void TestCommandInteraction::TestCommandSenderExtendableApiWithProcessReceivedMs uint16_t invalidResponseCommandRef = 2; GenerateInvokeResponse(apSuite, apContext, buf, kTestCommandIdWithData, kTestClusterId, kTestEndpointId, - MakeOptional(invalidResponseCommandRef)); + std::make_optional(invalidResponseCommandRef)); bool moreChunkedMessages = false; err = commandSender.ProcessInvokeResponse(std::move(buf), moreChunkedMessages); NL_TEST_ASSERT(apSuite, err == CHIP_ERROR_KEY_NOT_FOUND); @@ -1948,9 +1950,9 @@ void TestCommandInteraction::TestCommandHandler_FillUpInvokeResponseMessageWhere ConcreteCommandPath requestCommandPath1 = { kTestEndpointId, kTestClusterId, kTestCommandIdFillResponseMessage }; ConcreteCommandPath requestCommandPath2 = { kTestEndpointId, kTestClusterId, kTestCommandIdCommandSpecificResponse }; - CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, MakeOptional(static_cast(1))); + CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, std::make_optional(static_cast(1))); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); - err = basicCommandPathRegistry.Add(requestCommandPath2, MakeOptional(static_cast(2))); + err = basicCommandPathRegistry.Add(requestCommandPath2, std::make_optional(static_cast(2))); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); uint32_t sizeToLeave = 0; @@ -1977,9 +1979,9 @@ void TestCommandInteraction::TestCommandHandler_FillUpInvokeResponseMessageWhere ConcreteCommandPath requestCommandPath1 = { kTestEndpointId, kTestClusterId, kTestCommandIdFillResponseMessage }; ConcreteCommandPath requestCommandPath2 = { kTestEndpointId, kTestClusterId, kTestCommandIdCommandSpecificResponse }; - CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, MakeOptional(static_cast(1))); + CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, std::make_optional(static_cast(1))); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); - err = basicCommandPathRegistry.Add(requestCommandPath2, MakeOptional(static_cast(2))); + err = basicCommandPathRegistry.Add(requestCommandPath2, std::make_optional(static_cast(2))); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); uint32_t sizeToLeave = 0; @@ -2005,9 +2007,9 @@ void TestCommandInteraction::TestCommandHandler_FillUpInvokeResponseMessageWhere ConcreteCommandPath requestCommandPath1 = { kTestEndpointId, kTestClusterId, kTestCommandIdFillResponseMessage }; ConcreteCommandPath requestCommandPath2 = { kTestEndpointId, kTestClusterId, kTestCommandIdCommandSpecificResponse }; - CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, MakeOptional(static_cast(1))); + CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, std::make_optional(static_cast(1))); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); - err = basicCommandPathRegistry.Add(requestCommandPath2, MakeOptional(static_cast(2))); + err = basicCommandPathRegistry.Add(requestCommandPath2, std::make_optional(static_cast(2))); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); uint32_t sizeToLeave = 0; diff --git a/src/lib/core/Optional.h b/src/lib/core/Optional.h index 3d509f544c97cd..8da70544967d9e 100644 --- a/src/lib/core/Optional.h +++ b/src/lib/core/Optional.h @@ -24,6 +24,7 @@ #pragma once #include +#include #include #include @@ -211,6 +212,12 @@ class Optional bool operator==(const T & other) const { return HasValue() && Value() == other; } bool operator!=(const T & other) const { return !(*this == other); } + std::optional std_optional() const + { + VerifyOrReturnValue(HasValue(), std::nullopt); + return std::make_optional(Value()); + } + /** Convenience method to create an optional without a valid value. */ static Optional Missing() { return Optional(); }