@@ -52,44 +52,47 @@ class TimeFormatLocalizationAttrAccess : public AttributeAccessInterface
52
52
CHIP_ERROR ReadSupportedCalendarTypes (AttributeValueEncoder & aEncoder);
53
53
};
54
54
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
+
55
76
TimeFormatLocalizationAttrAccess gAttrAccess ;
56
77
57
78
CHIP_ERROR TimeFormatLocalizationAttrAccess::ReadSupportedCalendarTypes (AttributeValueEncoder & aEncoder)
58
79
{
59
- CHIP_ERROR err = CHIP_NO_ERROR;
60
-
61
80
DeviceLayer::DeviceInfoProvider * provider = DeviceLayer::GetDeviceInfoProvider ();
81
+ VerifyOrReturnValue (provider != nullptr , aEncoder.EncodeEmptyList ());
62
82
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 ());
76
85
77
- return CHIP_NO_ERROR;
78
- }) ;
86
+ return aEncoder. EncodeList ([&it]( const auto & encoder) -> CHIP_ERROR {
87
+ CalendarTypeEnum type ;
79
88
80
- it->Release ();
81
- }
82
- else
89
+ while (it.Next (type))
83
90
{
84
- err = aEncoder. EncodeEmptyList ( );
91
+ ReturnErrorOnFailure (encoder. Encode (type) );
85
92
}
86
- }
87
- else
88
- {
89
- err = aEncoder.EncodeEmptyList ();
90
- }
91
93
92
- return err;
94
+ return CHIP_NO_ERROR;
95
+ });
93
96
}
94
97
95
98
CHIP_ERROR TimeFormatLocalizationAttrAccess::Read (const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
@@ -114,27 +117,18 @@ bool IsSupportedCalendarType(CalendarTypeEnum newType, CalendarTypeEnum & validT
114
117
validType = CalendarTypeEnum::kBuddhist ;
115
118
116
119
DeviceLayer::DeviceInfoProvider * provider = DeviceLayer::GetDeviceInfoProvider ();
120
+ VerifyOrReturnValue (provider != nullptr , false );
117
121
118
- if (provider)
119
- {
120
- DeviceLayer::DeviceInfoProvider::SupportedCalendarTypesIterator * it = provider->IterateSupportedCalendarTypes ();
122
+ AutoReleaseIterator it (provider->IterateSupportedCalendarTypes ());
123
+ VerifyOrReturnValue (it.IsValid (), false );
121
124
122
- if (it)
125
+ CalendarTypeEnum type;
126
+ while (it.Next (type))
127
+ {
128
+ validType = type;
129
+ if (validType == newType)
123
130
{
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 ;
138
132
}
139
133
}
140
134
0 commit comments