Skip to content

Commit 1236448

Browse files
andy31415pull[bot]
authored andcommitted
Update time-format-localization-server.cpp for somewhat simpler code (#30556)
* Refactor * Undo the UseActiveLocale change
1 parent 2025a86 commit 1236448

File tree

1 file changed

+39
-45
lines changed

1 file changed

+39
-45
lines changed

src/app/clusters/time-format-localization-server/time-format-localization-server.cpp

+39-45
Original file line numberDiff line numberDiff line change
@@ -52,44 +52,47 @@ class TimeFormatLocalizationAttrAccess : public AttributeAccessInterface
5252
CHIP_ERROR ReadSupportedCalendarTypes(AttributeValueEncoder & aEncoder);
5353
};
5454

55+
class AutoReleaseIterator
56+
{
57+
public:
58+
using Iterator = DeviceLayer::DeviceInfoProvider::SupportedCalendarTypesIterator;
59+
60+
AutoReleaseIterator(Iterator * value) : mIterator(value) {}
61+
~AutoReleaseIterator()
62+
{
63+
if (mIterator != nullptr)
64+
{
65+
mIterator->Release();
66+
}
67+
}
68+
69+
bool IsValid() const { return mIterator != nullptr; }
70+
bool Next(CalendarTypeEnum & value) { return (mIterator == nullptr) ? false : mIterator->Next(value); }
71+
72+
private:
73+
Iterator * mIterator;
74+
};
75+
5576
TimeFormatLocalizationAttrAccess gAttrAccess;
5677

5778
CHIP_ERROR TimeFormatLocalizationAttrAccess::ReadSupportedCalendarTypes(AttributeValueEncoder & aEncoder)
5879
{
59-
CHIP_ERROR err = CHIP_NO_ERROR;
60-
6180
DeviceLayer::DeviceInfoProvider * provider = DeviceLayer::GetDeviceInfoProvider();
81+
VerifyOrReturnValue(provider != nullptr, aEncoder.EncodeEmptyList());
6282

63-
if (provider)
64-
{
65-
DeviceLayer::DeviceInfoProvider::SupportedCalendarTypesIterator * it = provider->IterateSupportedCalendarTypes();
66-
67-
if (it)
68-
{
69-
err = aEncoder.EncodeList([&it](const auto & encoder) -> CHIP_ERROR {
70-
CalendarTypeEnum type;
71-
72-
while (it->Next(type))
73-
{
74-
ReturnErrorOnFailure(encoder.Encode(type));
75-
}
83+
AutoReleaseIterator it(provider->IterateSupportedCalendarTypes());
84+
VerifyOrReturnValue(it.IsValid(), aEncoder.EncodeEmptyList());
7685

77-
return CHIP_NO_ERROR;
78-
});
86+
return aEncoder.EncodeList([&it](const auto & encoder) -> CHIP_ERROR {
87+
CalendarTypeEnum type;
7988

80-
it->Release();
81-
}
82-
else
89+
while (it.Next(type))
8390
{
84-
err = aEncoder.EncodeEmptyList();
91+
ReturnErrorOnFailure(encoder.Encode(type));
8592
}
86-
}
87-
else
88-
{
89-
err = aEncoder.EncodeEmptyList();
90-
}
9193

92-
return err;
94+
return CHIP_NO_ERROR;
95+
});
9396
}
9497

9598
CHIP_ERROR TimeFormatLocalizationAttrAccess::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
@@ -114,27 +117,18 @@ bool IsSupportedCalendarType(CalendarTypeEnum newType, CalendarTypeEnum & validT
114117
validType = CalendarTypeEnum::kBuddhist;
115118

116119
DeviceLayer::DeviceInfoProvider * provider = DeviceLayer::GetDeviceInfoProvider();
120+
VerifyOrReturnValue(provider != nullptr, false);
117121

118-
if (provider)
119-
{
120-
DeviceLayer::DeviceInfoProvider::SupportedCalendarTypesIterator * it = provider->IterateSupportedCalendarTypes();
122+
AutoReleaseIterator it(provider->IterateSupportedCalendarTypes());
123+
VerifyOrReturnValue(it.IsValid(), false);
121124

122-
if (it)
125+
CalendarTypeEnum type;
126+
while (it.Next(type))
127+
{
128+
validType = type;
129+
if (validType == newType)
123130
{
124-
CalendarTypeEnum type;
125-
126-
while (it->Next(type))
127-
{
128-
validType = type;
129-
130-
if (validType == newType)
131-
{
132-
it->Release();
133-
return true;
134-
}
135-
}
136-
137-
it->Release();
131+
return true;
138132
}
139133
}
140134

0 commit comments

Comments
 (0)