Replace some usages of chip::Optional with std::optional (#33050)

* Convert some chip::Optional to std::optional

* Use operator*/-> for optional value access after has_value has been called

* Slightly more compact code

* Restyle

---------

Co-authored-by: Andrei Litvin <andreilitvin@google.com>
diff --git a/src/app/CommandHandler.cpp b/src/app/CommandHandler.cpp
index 901cc65..005067c 100644
--- a/src/app/CommandHandler.cpp
+++ b/src/app/CommandHandler.cpp
@@ -158,7 +158,7 @@
 
         // Grab the CommandRef if there is one, and validate that it's there when it
         // has to be.
-        Optional<uint16_t> commandRef;
+        std::optional<uint16_t> commandRef;
         uint16_t ref;
         err = commandData.GetRef(&ref);
         VerifyOrReturnError(err == CHIP_NO_ERROR || err == CHIP_END_OF_TLV, err);
@@ -168,7 +168,7 @@
         }
         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 @@
                                                         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 @@
                        "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 @@
         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 @@
     }
 
     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 @@
     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 e31edfd..2eed73d 100644
--- a/src/app/CommandHandler.h
+++ b/src/app/CommandHandler.h
@@ -686,7 +686,7 @@
     // TODO Allow flexibility in registration.
     BasicCommandPathRegistry<CHIP_CONFIG_MAX_PATHS_PER_INVOKE> mBasicCommandPathRegistry;
     CommandPathRegistry * mCommandPathRegistry = &mBasicCommandPathRegistry;
-    Optional<uint16_t> mRefForResponse;
+    std::optional<uint16_t> mRefForResponse;
 
     CommandHandlerExchangeInterface * mpResponder = nullptr;
 
diff --git a/src/app/CommandPathRegistry.h b/src/app/CommandPathRegistry.h
index 0d914ef..da0d16e 100644
--- a/src/app/CommandPathRegistry.h
+++ b/src/app/CommandPathRegistry.h
@@ -24,13 +24,15 @@
 #include <lib/core/CHIPError.h>
 #include <lib/core/Optional.h>
 
+#include <optional>
+
 namespace chip {
 namespace app {
 
 struct CommandPathRegistryEntry
 {
     ConcreteCommandPath requestPath = ConcreteCommandPath(0, 0, 0);
-    Optional<uint16_t> ref;
+    std::optional<uint16_t> ref;
 };
 
 class CommandPathRegistry
@@ -38,11 +40,11 @@
 public:
     virtual ~CommandPathRegistry() = default;
 
-    virtual Optional<CommandPathRegistryEntry> Find(const ConcreteCommandPath & requestPath) const  = 0;
-    virtual Optional<CommandPathRegistryEntry> GetFirstEntry() const                                = 0;
-    virtual CHIP_ERROR Add(const ConcreteCommandPath & requestPath, const Optional<uint16_t> & ref) = 0;
-    virtual size_t Count() const                                                                    = 0;
-    virtual size_t MaxSize() const                                                                  = 0;
+    virtual std::optional<CommandPathRegistryEntry> Find(const ConcreteCommandPath & requestPath) const  = 0;
+    virtual std::optional<CommandPathRegistryEntry> GetFirstEntry() const                                = 0;
+    virtual CHIP_ERROR Add(const ConcreteCommandPath & requestPath, const std::optional<uint16_t> & ref) = 0;
+    virtual size_t Count() const                                                                         = 0;
+    virtual size_t MaxSize() const                                                                       = 0;
 };
 
 /**
@@ -59,28 +61,28 @@
 class BasicCommandPathRegistry : public CommandPathRegistry
 {
 public:
-    Optional<CommandPathRegistryEntry> Find(const ConcreteCommandPath & requestPath) const override
+    std::optional<CommandPathRegistryEntry> 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<CommandPathRegistryEntry> GetFirstEntry() const override
+    std::optional<CommandPathRegistryEntry> 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<uint16_t> & ref) override
+    CHIP_ERROR Add(const ConcreteCommandPath & requestPath, const std::optional<uint16_t> & ref) override
     {
         if (mCount >= N)
         {
diff --git a/src/app/tests/TestBasicCommandPathRegistry.cpp b/src/app/tests/TestBasicCommandPathRegistry.cpp
index 61e859b..28d7621 100644
--- a/src/app/tests/TestBasicCommandPathRegistry.cpp
+++ b/src/app/tests/TestBasicCommandPathRegistry.cpp
@@ -36,13 +36,13 @@
     BasicCommandPathRegistry<kQuickTestSize> basicCommandPathRegistry;
 
     ConcreteCommandPath concretePath(0, 0, 0);
-    Optional<uint16_t> commandRef;
+    std::optional<uint16_t> 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 @@
     CHIP_ERROR err = CHIP_NO_ERROR;
     BasicCommandPathRegistry<kQuickTestSize> basicCommandPathRegistry;
 
-    Optional<uint16_t> commandRef;
-    commandRef.SetValue(0);
+    std::optional<uint16_t> commandRef;
+    commandRef.emplace(0);
 
     uint16_t endpointValue = 0;
 
@@ -78,14 +78,14 @@
     CHIP_ERROR err = CHIP_NO_ERROR;
     BasicCommandPathRegistry<kQuickTestSize> basicCommandPathRegistry;
 
-    Optional<uint16_t> commandRef;
+    std::optional<uint16_t> 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 @@
     BasicCommandPathRegistry<kQuickTestSize> basicCommandPathRegistry;
     size_t maxPlusOne = kQuickTestSize + 1;
 
-    Optional<uint16_t> commandRef;
+    std::optional<uint16_t> 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 9837818..8729658 100644
--- a/src/app/tests/TestCommandInteraction.cpp
+++ b/src/app/tests/TestCommandInteraction.cpp
@@ -40,6 +40,7 @@
 #include <messaging/ExchangeContext.h>
 #include <messaging/ExchangeMgr.h>
 #include <messaging/Flags.h>
+#include <optional>
 #include <platform/CHIPDeviceLayer.h>
 #include <protocols/interaction_model/Constants.h>
 #include <system/SystemPacketBuffer.h>
@@ -392,7 +393,7 @@
                                              const Optional<uint16_t> & aRef) :
             CommandHandler(apCallback)
         {
-            GetCommandPathRegistry().Add(aRequestCommandPath, aRef);
+            GetCommandPathRegistry().Add(aRequestCommandPath, aRef.std_optional());
             SetExchangeInterface(&mMockCommandResponder);
         }
         MockCommandResponder mMockCommandResponder;
@@ -407,7 +408,8 @@
     // 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<uint16_t> aCommandRef = NullOptional);
+                                       EndpointId aEndpointId              = kTestEndpointId,
+                                       std::optional<uint16_t> 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::GenerateInvokeResponse(nlTestSuite * apSuite, void * apContext, System::PacketBufferHandle & aPayload,
                                                     CommandId aCommandId, ClusterId aClusterId, EndpointId aEndpointId,
-                                                    Optional<uint16_t> aCommandRef)
+                                                    std::optional<uint16_t> aCommandRef)
 
 {
     CHIP_ERROR err = CHIP_NO_ERROR;
@@ -536,9 +538,9 @@
         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 @@
     ConcreteCommandPath requestCommandPath1          = { kTestEndpointId, kTestClusterId, kTestCommandIdFillResponseMessage };
     ConcreteCommandPath requestCommandPath2          = { kTestEndpointId, kTestClusterId, kTestCommandIdCommandSpecificResponse };
 
-    CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, MakeOptional<uint16_t>(static_cast<uint16_t>(1)));
+    CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, std::make_optional<uint16_t>(static_cast<uint16_t>(1)));
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-    err = basicCommandPathRegistry.Add(requestCommandPath2, MakeOptional<uint16_t>(static_cast<uint16_t>(2)));
+    err = basicCommandPathRegistry.Add(requestCommandPath2, std::make_optional<uint16_t>(static_cast<uint16_t>(2)));
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
 
     err = commandHandler.AllocateBuffer();
@@ -823,7 +825,7 @@
 
     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 @@
     ConcreteCommandPath requestCommandPath1          = { kTestEndpointId, kTestClusterId, kTestCommandIdFillResponseMessage };
     ConcreteCommandPath requestCommandPath2          = { kTestEndpointId, kTestClusterId, kTestCommandIdCommandSpecificResponse };
 
-    CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, MakeOptional<uint16_t>(static_cast<uint16_t>(1)));
+    CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, std::make_optional<uint16_t>(static_cast<uint16_t>(1)));
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-    err = basicCommandPathRegistry.Add(requestCommandPath2, MakeOptional<uint16_t>(static_cast<uint16_t>(2)));
+    err = basicCommandPathRegistry.Add(requestCommandPath2, std::make_optional<uint16_t>(static_cast<uint16_t>(2)));
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
 
     uint32_t sizeToLeave = 0;
@@ -1977,9 +1979,9 @@
     ConcreteCommandPath requestCommandPath1          = { kTestEndpointId, kTestClusterId, kTestCommandIdFillResponseMessage };
     ConcreteCommandPath requestCommandPath2          = { kTestEndpointId, kTestClusterId, kTestCommandIdCommandSpecificResponse };
 
-    CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, MakeOptional<uint16_t>(static_cast<uint16_t>(1)));
+    CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, std::make_optional<uint16_t>(static_cast<uint16_t>(1)));
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-    err = basicCommandPathRegistry.Add(requestCommandPath2, MakeOptional<uint16_t>(static_cast<uint16_t>(2)));
+    err = basicCommandPathRegistry.Add(requestCommandPath2, std::make_optional<uint16_t>(static_cast<uint16_t>(2)));
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
 
     uint32_t sizeToLeave = 0;
@@ -2005,9 +2007,9 @@
     ConcreteCommandPath requestCommandPath1          = { kTestEndpointId, kTestClusterId, kTestCommandIdFillResponseMessage };
     ConcreteCommandPath requestCommandPath2          = { kTestEndpointId, kTestClusterId, kTestCommandIdCommandSpecificResponse };
 
-    CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, MakeOptional<uint16_t>(static_cast<uint16_t>(1)));
+    CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, std::make_optional<uint16_t>(static_cast<uint16_t>(1)));
     NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
-    err = basicCommandPathRegistry.Add(requestCommandPath2, MakeOptional<uint16_t>(static_cast<uint16_t>(2)));
+    err = basicCommandPathRegistry.Add(requestCommandPath2, std::make_optional<uint16_t>(static_cast<uint16_t>(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 3d509f5..8da7054 100644
--- a/src/lib/core/Optional.h
+++ b/src/lib/core/Optional.h
@@ -24,6 +24,7 @@
 #pragma once
 
 #include <new>
+#include <optional>
 #include <type_traits>
 #include <utility>
 
@@ -211,6 +212,12 @@
     bool operator==(const T & other) const { return HasValue() && Value() == other; }
     bool operator!=(const T & other) const { return !(*this == other); }
 
+    std::optional<T> std_optional() const
+    {
+        VerifyOrReturnValue(HasValue(), std::nullopt);
+        return std::make_optional(Value());
+    }
+
     /** Convenience method to create an optional without a valid value. */
     static Optional<T> Missing() { return Optional<T>(); }