From 1095684e9071cf354d414f467a00f41697517ed7 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 20 Nov 2023 08:56:16 -0500 Subject: [PATCH] Update `time-format-localization-server.cpp` for somewhat simpler code (#30556) * Refactor * Undo the UseActiveLocale change --- .../time-format-localization-server.cpp | 84 +++++++++---------- 1 file changed, 39 insertions(+), 45 deletions(-) diff --git a/src/app/clusters/time-format-localization-server/time-format-localization-server.cpp b/src/app/clusters/time-format-localization-server/time-format-localization-server.cpp index f4eb458a0a8313..8bc0b2a6416d03 100644 --- a/src/app/clusters/time-format-localization-server/time-format-localization-server.cpp +++ b/src/app/clusters/time-format-localization-server/time-format-localization-server.cpp @@ -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) @@ -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; } }