diff --git a/src/lib/core/Optional.h b/src/lib/core/Optional.h index 6e14e00f506bc5..b3861a9ea43052 100644 --- a/src/lib/core/Optional.h +++ b/src/lib/core/Optional.h @@ -29,6 +29,13 @@ namespace chip { +/// An empty class type used to indicate optional type with uninitialized state. +struct NullOptionalType +{ + explicit NullOptionalType() = default; +}; +constexpr NullOptionalType NullOptional{}; + /** * Pairs an object with a boolean value to determine if the object value * is actually valid or not. @@ -38,6 +45,8 @@ class Optional { public: constexpr Optional() : mHasValue(false) {} + constexpr Optional(NullOptionalType) : mHasValue(false){}; + ~Optional() { if (mHasValue) diff --git a/src/transport/SessionManager.h b/src/transport/SessionManager.h index 0f34c262e21a6f..43f6e0adbf1a12 100644 --- a/src/transport/SessionManager.h +++ b/src/transport/SessionManager.h @@ -266,8 +266,7 @@ class DLL_EXPORT SessionManager : public TransportMgrDelegate Optional CreateUnauthenticatedSession(const Transport::PeerAddress & peerAddress) { Optional session = mUnauthenticatedSessions.FindOrAllocateEntry(peerAddress); - return session.HasValue() ? Optional::Value(SessionHandle(session.Value())) - : Optional::Missing(); + return session.HasValue() ? MakeOptional(session.Value()) : NullOptional; } private: diff --git a/src/transport/UnauthenticatedSessionTable.h b/src/transport/UnauthenticatedSessionTable.h index 7658889ed821d2..70b46f26bf56b2 100644 --- a/src/transport/UnauthenticatedSessionTable.h +++ b/src/transport/UnauthenticatedSessionTable.h @@ -83,9 +83,9 @@ class UnauthenticatedSessionTable { public: /** - * Get a peer given the peer id. If the peer doesn't exist in the cache, allocate a new entry for it. + * Get a session given the peer address. If the session doesn't exist in the cache, allocate a new entry for it. * - * @return the peer found or allocated, nullptr if not found and allocate failed. + * @return the session found or allocated, nullptr if not found and allocation failed. */ CHECK_RETURN_VALUE Optional FindOrAllocateEntry(const PeerAddress & address)