Skip to content

Commit 0302f19

Browse files
committed
Convert some chip::Optional to std::optional
1 parent 8a4dffc commit 0302f19

6 files changed

+62
-48
lines changed

src/app/CommandHandler.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ CHIP_ERROR CommandHandler::ValidateInvokeRequestMessageAndBuildRegistry(InvokeRe
158158

159159
// Grab the CommandRef if there is one, and validate that it's there when it
160160
// has to be.
161-
Optional<uint16_t> commandRef;
161+
std::optional<uint16_t> commandRef;
162162
uint16_t ref;
163163
err = commandData.GetRef(&ref);
164164
VerifyOrReturnError(err == CHIP_NO_ERROR || err == CHIP_END_OF_TLV, err);
@@ -168,7 +168,7 @@ CHIP_ERROR CommandHandler::ValidateInvokeRequestMessageAndBuildRegistry(InvokeRe
168168
}
169169
if (err == CHIP_NO_ERROR)
170170
{
171-
commandRef.SetValue(ref);
171+
commandRef.emplace(ref);
172172
}
173173

174174
// Adding can fail if concretePath is not unique, or if commandRef is a value
@@ -590,9 +590,9 @@ CHIP_ERROR CommandHandler::PrepareInvokeResponseCommand(const ConcreteCommandPat
590590
const CommandHandler::InvokeResponseParameters & aPrepareParameters)
591591
{
592592
auto commandPathRegistryEntry = GetCommandPathRegistry().Find(aPrepareParameters.mRequestCommandPath);
593-
VerifyOrReturnValue(commandPathRegistryEntry.HasValue(), CHIP_ERROR_INCORRECT_STATE);
593+
VerifyOrReturnValue(commandPathRegistryEntry.has_value(), CHIP_ERROR_INCORRECT_STATE);
594594

595-
return PrepareInvokeResponseCommand(commandPathRegistryEntry.Value(), aResponseCommandPath,
595+
return PrepareInvokeResponseCommand(commandPathRegistryEntry.value(), aResponseCommandPath,
596596
aPrepareParameters.mStartOrEndDataStruct);
597597
}
598598

@@ -610,9 +610,9 @@ CHIP_ERROR CommandHandler::PrepareCommand(const ConcreteCommandPath & aResponseC
610610
"Seemingly device supports batch commands, but is calling the deprecated PrepareCommand API");
611611

612612
auto commandPathRegistryEntry = GetCommandPathRegistry().GetFirstEntry();
613-
VerifyOrReturnValue(commandPathRegistryEntry.HasValue(), CHIP_ERROR_INCORRECT_STATE);
613+
VerifyOrReturnValue(commandPathRegistryEntry.has_value(), CHIP_ERROR_INCORRECT_STATE);
614614

615-
return PrepareInvokeResponseCommand(commandPathRegistryEntry.Value(), aResponseCommandPath, aStartDataStruct);
615+
return PrepareInvokeResponseCommand(commandPathRegistryEntry.value(), aResponseCommandPath, aStartDataStruct);
616616
}
617617

618618
CHIP_ERROR CommandHandler::PrepareInvokeResponseCommand(const CommandPathRegistryEntry & apCommandPathRegistryEntry,
@@ -675,9 +675,9 @@ CHIP_ERROR CommandHandler::FinishCommand(bool aStartDataStruct)
675675
ReturnErrorOnFailure(commandData.GetWriter()->EndContainer(mDataElementContainerType));
676676
}
677677

678-
if (mRefForResponse.HasValue())
678+
if (mRefForResponse.has_value())
679679
{
680-
ReturnErrorOnFailure(commandData.Ref(mRefForResponse.Value()));
680+
ReturnErrorOnFailure(commandData.Ref(mRefForResponse.value()));
681681
}
682682

683683
ReturnErrorOnFailure(commandData.EndOfCommandDataIB());
@@ -699,8 +699,8 @@ CHIP_ERROR CommandHandler::PrepareStatus(const ConcreteCommandPath & aCommandPat
699699
}
700700

701701
auto commandPathRegistryEntry = GetCommandPathRegistry().Find(aCommandPath);
702-
VerifyOrReturnError(commandPathRegistryEntry.HasValue(), CHIP_ERROR_INCORRECT_STATE);
703-
mRefForResponse = commandPathRegistryEntry.Value().ref;
702+
VerifyOrReturnError(commandPathRegistryEntry.has_value(), CHIP_ERROR_INCORRECT_STATE);
703+
mRefForResponse = commandPathRegistryEntry.value().ref;
704704

705705
MoveToState(State::Preparing);
706706
InvokeResponseIBs::Builder & invokeResponses = mInvokeResponseBuilder.GetInvokeResponses();
@@ -720,9 +720,9 @@ CHIP_ERROR CommandHandler::FinishStatus()
720720
VerifyOrReturnError(mState == State::AddingCommand, CHIP_ERROR_INCORRECT_STATE);
721721

722722
CommandStatusIB::Builder & commandStatus = mInvokeResponseBuilder.GetInvokeResponses().GetInvokeResponse().GetStatus();
723-
if (mRefForResponse.HasValue())
723+
if (mRefForResponse.has_value())
724724
{
725-
ReturnErrorOnFailure(commandStatus.Ref(mRefForResponse.Value()));
725+
ReturnErrorOnFailure(commandStatus.Ref(mRefForResponse.value()));
726726
}
727727

728728
ReturnErrorOnFailure(mInvokeResponseBuilder.GetInvokeResponses().GetInvokeResponse().GetStatus().EndOfCommandStatusIB());

src/app/CommandHandler.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ class CommandHandler
686686
// TODO Allow flexibility in registration.
687687
BasicCommandPathRegistry<CHIP_CONFIG_MAX_PATHS_PER_INVOKE> mBasicCommandPathRegistry;
688688
CommandPathRegistry * mCommandPathRegistry = &mBasicCommandPathRegistry;
689-
Optional<uint16_t> mRefForResponse;
689+
std::optional<uint16_t> mRefForResponse;
690690

691691
CommandHandlerExchangeInterface * mpResponder = nullptr;
692692

src/app/CommandPathRegistry.h

+15-13
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,27 @@
2424
#include <lib/core/CHIPError.h>
2525
#include <lib/core/Optional.h>
2626

27+
#include <optional>
28+
2729
namespace chip {
2830
namespace app {
2931

3032
struct CommandPathRegistryEntry
3133
{
3234
ConcreteCommandPath requestPath = ConcreteCommandPath(0, 0, 0);
33-
Optional<uint16_t> ref;
35+
std::optional<uint16_t> ref;
3436
};
3537

3638
class CommandPathRegistry
3739
{
3840
public:
3941
virtual ~CommandPathRegistry() = default;
4042

41-
virtual Optional<CommandPathRegistryEntry> Find(const ConcreteCommandPath & requestPath) const = 0;
42-
virtual Optional<CommandPathRegistryEntry> GetFirstEntry() const = 0;
43-
virtual CHIP_ERROR Add(const ConcreteCommandPath & requestPath, const Optional<uint16_t> & ref) = 0;
44-
virtual size_t Count() const = 0;
45-
virtual size_t MaxSize() const = 0;
43+
virtual std::optional<CommandPathRegistryEntry> Find(const ConcreteCommandPath & requestPath) const = 0;
44+
virtual std::optional<CommandPathRegistryEntry> GetFirstEntry() const = 0;
45+
virtual CHIP_ERROR Add(const ConcreteCommandPath & requestPath, const std::optional<uint16_t> & ref) = 0;
46+
virtual size_t Count() const = 0;
47+
virtual size_t MaxSize() const = 0;
4648
};
4749

4850
/**
@@ -59,28 +61,28 @@ template <size_t N>
5961
class BasicCommandPathRegistry : public CommandPathRegistry
6062
{
6163
public:
62-
Optional<CommandPathRegistryEntry> Find(const ConcreteCommandPath & requestPath) const override
64+
std::optional<CommandPathRegistryEntry> Find(const ConcreteCommandPath & requestPath) const override
6365
{
6466
for (size_t i = 0; i < mCount; i++)
6567
{
6668
if (mTable[i].requestPath == requestPath)
6769
{
68-
return MakeOptional(mTable[i]);
70+
return std::make_optional(mTable[i]);
6971
}
7072
}
71-
return NullOptional;
73+
return std::nullopt;
7274
}
7375

74-
Optional<CommandPathRegistryEntry> GetFirstEntry() const override
76+
std::optional<CommandPathRegistryEntry> GetFirstEntry() const override
7577
{
7678
if (mCount > 0)
7779
{
78-
return MakeOptional(mTable[0]);
80+
return std::make_optional(mTable[0]);
7981
}
80-
return NullOptional;
82+
return std::nullopt;
8183
}
8284

83-
CHIP_ERROR Add(const ConcreteCommandPath & requestPath, const Optional<uint16_t> & ref) override
85+
CHIP_ERROR Add(const ConcreteCommandPath & requestPath, const std::optional<uint16_t> & ref) override
8486
{
8587
if (mCount >= N)
8688
{

src/app/tests/TestBasicCommandPathRegistry.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ void TestAddingSameConcretePath(nlTestSuite * apSuite, void * apContext)
3636
BasicCommandPathRegistry<kQuickTestSize> basicCommandPathRegistry;
3737

3838
ConcreteCommandPath concretePath(0, 0, 0);
39-
Optional<uint16_t> commandRef;
39+
std::optional<uint16_t> commandRef;
4040
uint16_t commandRefValue = 0;
4141

4242
size_t idx = 0;
4343
for (idx = 0; idx < kQuickTestSize && err == CHIP_NO_ERROR; idx++)
4444
{
45-
commandRef.SetValue(commandRefValue);
45+
commandRef.emplace(commandRefValue);
4646
commandRefValue++;
4747
err = basicCommandPathRegistry.Add(concretePath, commandRef);
4848
}
@@ -56,8 +56,8 @@ void TestAddingSameCommandRef(nlTestSuite * apSuite, void * apContext)
5656
CHIP_ERROR err = CHIP_NO_ERROR;
5757
BasicCommandPathRegistry<kQuickTestSize> basicCommandPathRegistry;
5858

59-
Optional<uint16_t> commandRef;
60-
commandRef.SetValue(0);
59+
std::optional<uint16_t> commandRef;
60+
commandRef.emplace(0);
6161

6262
uint16_t endpointValue = 0;
6363

@@ -78,14 +78,14 @@ void TestAddingMaxNumberOfEntries(nlTestSuite * apSuite, void * apContext)
7878
CHIP_ERROR err = CHIP_NO_ERROR;
7979
BasicCommandPathRegistry<kQuickTestSize> basicCommandPathRegistry;
8080

81-
Optional<uint16_t> commandRef;
81+
std::optional<uint16_t> commandRef;
8282
uint16_t commandRefAndEndpointValue = 0;
8383

8484
size_t idx = 0;
8585
for (idx = 0; idx < kQuickTestSize && err == CHIP_NO_ERROR; idx++)
8686
{
8787
ConcreteCommandPath concretePath(commandRefAndEndpointValue, 0, 0);
88-
commandRef.SetValue(commandRefAndEndpointValue);
88+
commandRef.emplace(commandRefAndEndpointValue);
8989
commandRefAndEndpointValue++;
9090
err = basicCommandPathRegistry.Add(concretePath, commandRef);
9191
}
@@ -100,14 +100,14 @@ void TestAddingTooManyEntries(nlTestSuite * apSuite, void * apContext)
100100
BasicCommandPathRegistry<kQuickTestSize> basicCommandPathRegistry;
101101
size_t maxPlusOne = kQuickTestSize + 1;
102102

103-
Optional<uint16_t> commandRef;
103+
std::optional<uint16_t> commandRef;
104104
uint16_t commandRefAndEndpointValue = 0;
105105

106106
size_t idx = 0;
107107
for (idx = 0; idx < maxPlusOne && err == CHIP_NO_ERROR; idx++)
108108
{
109109
ConcreteCommandPath concretePath(commandRefAndEndpointValue, 0, 0);
110-
commandRef.SetValue(commandRefAndEndpointValue);
110+
commandRef.emplace(commandRefAndEndpointValue);
111111
commandRefAndEndpointValue++;
112112
err = basicCommandPathRegistry.Add(concretePath, commandRef);
113113
}

src/app/tests/TestCommandInteraction.cpp

+16-14
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <messaging/ExchangeContext.h>
4141
#include <messaging/ExchangeMgr.h>
4242
#include <messaging/Flags.h>
43+
#include <optional>
4344
#include <platform/CHIPDeviceLayer.h>
4445
#include <protocols/interaction_model/Constants.h>
4546
#include <system/SystemPacketBuffer.h>
@@ -392,7 +393,7 @@ class TestCommandInteraction
392393
const Optional<uint16_t> & aRef) :
393394
CommandHandler(apCallback)
394395
{
395-
GetCommandPathRegistry().Add(aRequestCommandPath, aRef);
396+
GetCommandPathRegistry().Add(aRequestCommandPath, aRef.std_optional());
396397
SetExchangeInterface(&mMockCommandResponder);
397398
}
398399
MockCommandResponder mMockCommandResponder;
@@ -407,7 +408,8 @@ class TestCommandInteraction
407408
// payload will be included. Otherwise no payload will be included.
408409
static void GenerateInvokeResponse(nlTestSuite * apSuite, void * apContext, System::PacketBufferHandle & aPayload,
409410
CommandId aCommandId, ClusterId aClusterId = kTestClusterId,
410-
EndpointId aEndpointId = kTestEndpointId, Optional<uint16_t> aCommandRef = NullOptional);
411+
EndpointId aEndpointId = kTestEndpointId,
412+
std::optional<uint16_t> aCommandRef = std::nullopt);
411413
static void AddInvokeRequestData(nlTestSuite * apSuite, void * apContext, CommandSender * apCommandSender,
412414
CommandId aCommandId = kTestCommandIdWithData);
413415
static void AddInvalidInvokeRequestData(nlTestSuite * apSuite, void * apContext, CommandSender * apCommandSender,
@@ -494,7 +496,7 @@ void TestCommandInteraction::GenerateInvokeRequest(nlTestSuite * apSuite, void *
494496

495497
void TestCommandInteraction::GenerateInvokeResponse(nlTestSuite * apSuite, void * apContext, System::PacketBufferHandle & aPayload,
496498
CommandId aCommandId, ClusterId aClusterId, EndpointId aEndpointId,
497-
Optional<uint16_t> aCommandRef)
499+
std::optional<uint16_t> aCommandRef)
498500

499501
{
500502
CHIP_ERROR err = CHIP_NO_ERROR;
@@ -536,9 +538,9 @@ void TestCommandInteraction::GenerateInvokeResponse(nlTestSuite * apSuite, void
536538
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
537539
}
538540

539-
if (aCommandRef.HasValue())
541+
if (aCommandRef.has_value())
540542
{
541-
NL_TEST_ASSERT(apSuite, commandDataIBBuilder.Ref(aCommandRef.Value()) == CHIP_NO_ERROR);
543+
NL_TEST_ASSERT(apSuite, commandDataIBBuilder.Ref(aCommandRef.value()) == CHIP_NO_ERROR);
542544
}
543545

544546
commandDataIBBuilder.EndOfCommandDataIB();
@@ -633,9 +635,9 @@ uint32_t TestCommandInteraction::GetAddResponseDataOverheadSizeForPath(nlTestSui
633635
ConcreteCommandPath requestCommandPath1 = { kTestEndpointId, kTestClusterId, kTestCommandIdFillResponseMessage };
634636
ConcreteCommandPath requestCommandPath2 = { kTestEndpointId, kTestClusterId, kTestCommandIdCommandSpecificResponse };
635637

636-
CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, MakeOptional<uint16_t>(static_cast<uint16_t>(1)));
638+
CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, std::make_optional<uint16_t>(static_cast<uint16_t>(1)));
637639
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
638-
err = basicCommandPathRegistry.Add(requestCommandPath2, MakeOptional<uint16_t>(static_cast<uint16_t>(2)));
640+
err = basicCommandPathRegistry.Add(requestCommandPath2, std::make_optional<uint16_t>(static_cast<uint16_t>(2)));
639641
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
640642

641643
err = commandHandler.AllocateBuffer();
@@ -823,7 +825,7 @@ void TestCommandInteraction::TestCommandSenderExtendableApiWithProcessReceivedMs
823825

824826
uint16_t invalidResponseCommandRef = 2;
825827
GenerateInvokeResponse(apSuite, apContext, buf, kTestCommandIdWithData, kTestClusterId, kTestEndpointId,
826-
MakeOptional(invalidResponseCommandRef));
828+
std::make_optional(invalidResponseCommandRef));
827829
bool moreChunkedMessages = false;
828830
err = commandSender.ProcessInvokeResponse(std::move(buf), moreChunkedMessages);
829831
NL_TEST_ASSERT(apSuite, err == CHIP_ERROR_KEY_NOT_FOUND);
@@ -1948,9 +1950,9 @@ void TestCommandInteraction::TestCommandHandler_FillUpInvokeResponseMessageWhere
19481950
ConcreteCommandPath requestCommandPath1 = { kTestEndpointId, kTestClusterId, kTestCommandIdFillResponseMessage };
19491951
ConcreteCommandPath requestCommandPath2 = { kTestEndpointId, kTestClusterId, kTestCommandIdCommandSpecificResponse };
19501952

1951-
CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, MakeOptional<uint16_t>(static_cast<uint16_t>(1)));
1953+
CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, std::make_optional<uint16_t>(static_cast<uint16_t>(1)));
19521954
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
1953-
err = basicCommandPathRegistry.Add(requestCommandPath2, MakeOptional<uint16_t>(static_cast<uint16_t>(2)));
1955+
err = basicCommandPathRegistry.Add(requestCommandPath2, std::make_optional<uint16_t>(static_cast<uint16_t>(2)));
19541956
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
19551957

19561958
uint32_t sizeToLeave = 0;
@@ -1977,9 +1979,9 @@ void TestCommandInteraction::TestCommandHandler_FillUpInvokeResponseMessageWhere
19771979
ConcreteCommandPath requestCommandPath1 = { kTestEndpointId, kTestClusterId, kTestCommandIdFillResponseMessage };
19781980
ConcreteCommandPath requestCommandPath2 = { kTestEndpointId, kTestClusterId, kTestCommandIdCommandSpecificResponse };
19791981

1980-
CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, MakeOptional<uint16_t>(static_cast<uint16_t>(1)));
1982+
CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, std::make_optional<uint16_t>(static_cast<uint16_t>(1)));
19811983
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
1982-
err = basicCommandPathRegistry.Add(requestCommandPath2, MakeOptional<uint16_t>(static_cast<uint16_t>(2)));
1984+
err = basicCommandPathRegistry.Add(requestCommandPath2, std::make_optional<uint16_t>(static_cast<uint16_t>(2)));
19831985
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
19841986

19851987
uint32_t sizeToLeave = 0;
@@ -2005,9 +2007,9 @@ void TestCommandInteraction::TestCommandHandler_FillUpInvokeResponseMessageWhere
20052007
ConcreteCommandPath requestCommandPath1 = { kTestEndpointId, kTestClusterId, kTestCommandIdFillResponseMessage };
20062008
ConcreteCommandPath requestCommandPath2 = { kTestEndpointId, kTestClusterId, kTestCommandIdCommandSpecificResponse };
20072009

2008-
CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, MakeOptional<uint16_t>(static_cast<uint16_t>(1)));
2010+
CHIP_ERROR err = basicCommandPathRegistry.Add(requestCommandPath1, std::make_optional<uint16_t>(static_cast<uint16_t>(1)));
20092011
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
2010-
err = basicCommandPathRegistry.Add(requestCommandPath2, MakeOptional<uint16_t>(static_cast<uint16_t>(2)));
2012+
err = basicCommandPathRegistry.Add(requestCommandPath2, std::make_optional<uint16_t>(static_cast<uint16_t>(2)));
20112013
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
20122014

20132015
uint32_t sizeToLeave = 0;

src/lib/core/Optional.h

+10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#pragma once
2525

2626
#include <new>
27+
#include <optional>
2728
#include <type_traits>
2829
#include <utility>
2930

@@ -211,6 +212,15 @@ class Optional
211212
bool operator==(const T & other) const { return HasValue() && Value() == other; }
212213
bool operator!=(const T & other) const { return !(*this == other); }
213214

215+
std::optional<T> std_optional() const
216+
{
217+
if (!HasValue())
218+
{
219+
return std::optional<T>();
220+
}
221+
return std::make_optional(Value());
222+
}
223+
214224
/** Convenience method to create an optional without a valid value. */
215225
static Optional<T> Missing() { return Optional<T>(); }
216226

0 commit comments

Comments
 (0)