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

Update time-format-localization-server.cpp for somewhat simpler code #30556

Merged
Merged
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
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
andy31415 marked this conversation as resolved.
Show resolved Hide resolved
{
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
Loading