Skip to content

Commit 4dc866e

Browse files
committed
Address suggestion re: optionality conversion
1 parent e8d0e29 commit 4dc866e

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

examples/all-clusters-app/all-clusters-common/src/energy-preference-delegate.cpp

+20-6
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ struct EPrefDelegate : public Delegate
3030
virtual ~EPrefDelegate();
3131

3232
CHIP_ERROR GetEnergyBalanceAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep,
33-
chip::MutableCharSpan & aOutLabel) override;
33+
chip::Optional<chip::MutableCharSpan> & aOutLabel) override;
3434
CHIP_ERROR GetEnergyPriorityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, EnergyPriorityEnum & priority) override;
3535
CHIP_ERROR GetLowPowerModeSensitivityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep,
36-
chip::MutableCharSpan & aOutLabel) override;
36+
chip::Optional<chip::MutableCharSpan> & aOutLabel) override;
3737

3838
size_t GetNumEnergyBalances(chip::EndpointId aEndpoint) override;
3939
size_t GetNumLowPowerModeSensitivities(chip::EndpointId aEndpoint) override;
@@ -63,12 +63,19 @@ size_t EPrefDelegate::GetNumLowPowerModeSensitivities(chip::EndpointId aEndpoint
6363

6464
CHIP_ERROR
6565
EPrefDelegate::GetEnergyBalanceAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep,
66-
chip::MutableCharSpan & aOutLabel)
66+
chip::Optional<chip::MutableCharSpan> & aOutLabel)
6767
{
6868
if (aIndex < GetNumEnergyBalances(aEndpoint))
6969
{
7070
aOutStep = gsEnergyBalances[aIndex].step;
71-
chip::CopyCharSpanToMutableCharSpan(gsEnergyBalances[aIndex].label.ValueOr(""_span), aOutLabel);
71+
if (gsEnergyBalances[aIndex].HasValue())
72+
{
73+
chip::CopyCharSpanToMutableCharSpan(gsEnergyBalances[aIndex].label.Value(), aOutLabel.Value());
74+
}
75+
else
76+
{
77+
aOutLabel.ClearValue();
78+
}
7279
return CHIP_NO_ERROR;
7380
}
7481
return CHIP_ERROR_NOT_FOUND;
@@ -90,12 +97,19 @@ EPrefDelegate::GetEnergyPriorityAtIndex(chip::EndpointId aEndpoint, size_t aInde
9097

9198
CHIP_ERROR
9299
EPrefDelegate::GetLowPowerModeSensitivityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep,
93-
chip::MutableCharSpan & aOutLabel)
100+
chip::Optional<chip::MutableCharSpan> & aOutLabel)
94101
{
95102
if (aIndex < GetNumLowPowerModeSensitivities(aEndpoint))
96103
{
97104
aOutStep = gsPowerBalances[aIndex].step;
98-
chip::CopyCharSpanToMutableCharSpan(gsPowerBalances[aIndex].label.ValueOr(""_span), aOutLabel);
105+
if (gsPowerBalances[aIndex])
106+
{
107+
chip::CopyCharSpanToMutableCharSpan(gsPowerBalances[aIndex].label.Value(), aOutLabel.Value());
108+
}
109+
else
110+
{
111+
aOutLabel.ClearValue();
112+
}
99113
return CHIP_NO_ERROR;
100114
}
101115

src/app/clusters/energy-preference-server/energy-preference-server.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ CHIP_ERROR EnergyPrefAttrAccess::Read(const ConcreteReadAttributePath & aPath, A
7171
return aEncoder.EncodeList([endpoint](const auto & encoder) -> CHIP_ERROR {
7272
chip::Percent step;
7373
char buffer[64];
74-
chip::MutableCharSpan label(buffer);
74+
chip::Optional<chip::MutableCharSpan> label{chip::MutableCharSpan(buffer)};
7575
size_t index = 0;
7676
CHIP_ERROR err = CHIP_NO_ERROR;
7777
while ((err = gsDelegate->GetEnergyBalanceAtIndex(endpoint, index, step, label)) == CHIP_NO_ERROR)
7878
{
79-
BalanceStruct::Type balance = { step, Optional<CharSpan>(label) };
79+
BalanceStruct::Type balance = { step, label.HasValue() ? Optional<CharSpan>(label.Value()) : Optional<CharSpan>() };
8080
ReturnErrorOnFailure(encoder.Encode(balance));
8181
index++;
8282
}
@@ -124,12 +124,12 @@ CHIP_ERROR EnergyPrefAttrAccess::Read(const ConcreteReadAttributePath & aPath, A
124124
return aEncoder.EncodeList([endpoint](const auto & encoder) -> CHIP_ERROR {
125125
chip::Percent step;
126126
char buffer[64];
127-
chip::MutableCharSpan label(buffer);
127+
chip::Optional<chip::MutableCharSpan> label{chip::MutableCharSpan(buffer)};
128128
size_t index = 0;
129129
CHIP_ERROR err = CHIP_NO_ERROR;
130130
while ((err = gsDelegate->GetLowPowerModeSensitivityAtIndex(endpoint, index, step, label)) == CHIP_NO_ERROR)
131131
{
132-
BalanceStruct::Type balance = { step, Optional<CharSpan>(label) };
132+
BalanceStruct::Type balance = { step, label.HasValue() ? Optional<CharSpan>(label.Value()) : Optional<CharSpan>() };
133133
ReturnErrorOnFailure(encoder.Encode(balance));
134134
index++;
135135
}

src/app/clusters/energy-preference-server/energy-preference-server.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct Delegate
5353
* @return CHIP_ERROR_NOT_FOUND if the index is out of range.
5454
*/
5555
virtual CHIP_ERROR GetEnergyBalanceAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep,
56-
chip::MutableCharSpan & aOutLabel) = 0;
56+
chip::Optional<chip::MutableCharSpan> & aOutLabel) = 0;
5757

5858
/**
5959
* Get an Energy Priority.
@@ -84,7 +84,7 @@ struct Delegate
8484
* @return CHIP_ERROR_NOT_FOUND if the index is out of range.
8585
*/
8686
virtual CHIP_ERROR GetLowPowerModeSensitivityAtIndex(chip::EndpointId aEndpoint, size_t aIndex, chip::Percent & aOutStep,
87-
chip::MutableCharSpan & aOutLabel) = 0;
87+
chip::Optional<chip::MutableCharSpan> & aOutLabel) = 0;
8888

8989
/**
9090
* Get the number of energy balances this endpoint has.

0 commit comments

Comments
 (0)