Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a ValueOr method to Optional #3767

Merged
merged 1 commit into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/lib/core/Optional.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ class Optional
return mValue;
}

/** Gets the current value of the optional if the optional has a value;
otherwise returns the provided default value. */
const T & ValueOr(const T & defaultValue) const
{
if (HasValue())
{
return mValue;
}
return defaultValue;
}

/** Checks if the optional contains a value or not */
bool HasValue() const { return mHasValue; }

Expand Down
3 changes: 1 addition & 2 deletions src/protocols/echo/EchoClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ void EchoClient::OnMessageReceived(ExchangeContext * ec, const PacketHeader & pa
// Call the registered OnEchoResponseReceived handler, if any.
if (echoApp->OnEchoResponseReceived != nullptr)
{
echoApp->OnEchoResponseReceived(packetHeader.GetSourceNodeId().HasValue() ? packetHeader.GetSourceNodeId().Value() : 0,
payload);
echoApp->OnEchoResponseReceived(packetHeader.GetSourceNodeId().ValueOr(0), payload);
}

exit:
Expand Down
4 changes: 2 additions & 2 deletions src/transport/SecurePairingSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ CHIP_ERROR SecurePairingSession::ToSerializable(SecurePairingSessionSerializable
{
CHIP_ERROR error = CHIP_NO_ERROR;

const NodeId localNodeId = (mLocalNodeId.HasValue()) ? mLocalNodeId.Value() : kUndefinedNodeId;
const NodeId peerNodeId = (mPeerNodeId.HasValue()) ? mPeerNodeId.Value() : kUndefinedNodeId;
const NodeId localNodeId = mLocalNodeId.ValueOr(kUndefinedNodeId);
const NodeId peerNodeId = mPeerNodeId.ValueOr(kUndefinedNodeId);
VerifyOrExit(CanCastTo<uint16_t>(mKeLen), error = CHIP_ERROR_INTERNAL);
VerifyOrExit(CanCastTo<uint64_t>(localNodeId), error = CHIP_ERROR_INTERNAL);
VerifyOrExit(CanCastTo<uint64_t>(peerNodeId), error = CHIP_ERROR_INTERNAL);
Expand Down