Skip to content

Commit

Permalink
Update time-format-localization-server.cpp for somewhat simpler code (
Browse files Browse the repository at this point in the history
#30556)

* Refactor

* Undo the UseActiveLocale change
  • Loading branch information
andy31415 authored and pull[bot] committed Jan 16, 2024
1 parent b487e7b commit 99260b0
Showing 1 changed file with 39 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,44 +52,47 @@ class TimeFormatLocalizationAttrAccess : public AttributeAccessInterface
CHIP_ERROR ReadSupportedCalendarTypes(AttributeValueEncoder & aEncoder);
};

class AutoReleaseIterator
{
public:
using Iterator = DeviceLayer::DeviceInfoProvider::SupportedCalendarTypesIterator;

AutoReleaseIterator(Iterator * value) : mIterator(value) {}
~AutoReleaseIterator()
{
if (mIterator != nullptr)
{
mIterator->Release();
}
}

bool IsValid() const { return mIterator != nullptr; }
bool Next(CalendarTypeEnum & value) { return (mIterator == nullptr) ? false : mIterator->Next(value); }

private:
Iterator * mIterator;
};

TimeFormatLocalizationAttrAccess gAttrAccess;

CHIP_ERROR TimeFormatLocalizationAttrAccess::ReadSupportedCalendarTypes(AttributeValueEncoder & aEncoder)
{
CHIP_ERROR err = CHIP_NO_ERROR;

DeviceLayer::DeviceInfoProvider * provider = DeviceLayer::GetDeviceInfoProvider();
VerifyOrReturnValue(provider != nullptr, aEncoder.EncodeEmptyList());

if (provider)
{
DeviceLayer::DeviceInfoProvider::SupportedCalendarTypesIterator * it = provider->IterateSupportedCalendarTypes();

if (it)
{
err = aEncoder.EncodeList([&it](const auto & encoder) -> CHIP_ERROR {
CalendarTypeEnum type;

while (it->Next(type))
{
ReturnErrorOnFailure(encoder.Encode(type));
}
AutoReleaseIterator it(provider->IterateSupportedCalendarTypes());
VerifyOrReturnValue(it.IsValid(), aEncoder.EncodeEmptyList());

return CHIP_NO_ERROR;
});
return aEncoder.EncodeList([&it](const auto & encoder) -> CHIP_ERROR {
CalendarTypeEnum type;

it->Release();
}
else
while (it.Next(type))
{
err = aEncoder.EncodeEmptyList();
ReturnErrorOnFailure(encoder.Encode(type));
}
}
else
{
err = aEncoder.EncodeEmptyList();
}

return err;
return CHIP_NO_ERROR;
});
}

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

DeviceLayer::DeviceInfoProvider * provider = DeviceLayer::GetDeviceInfoProvider();
VerifyOrReturnValue(provider != nullptr, false);

if (provider)
{
DeviceLayer::DeviceInfoProvider::SupportedCalendarTypesIterator * it = provider->IterateSupportedCalendarTypes();
AutoReleaseIterator it(provider->IterateSupportedCalendarTypes());
VerifyOrReturnValue(it.IsValid(), false);

if (it)
CalendarTypeEnum type;
while (it.Next(type))
{
validType = type;
if (validType == newType)
{
CalendarTypeEnum type;

while (it->Next(type))
{
validType = type;

if (validType == newType)
{
it->Release();
return true;
}
}

it->Release();
return true;
}
}

Expand Down

0 comments on commit 99260b0

Please sign in to comment.