2121
2222#include < Arduino.h>
2323
24+ #undef max
25+ #undef min
26+ #include < algorithm>
27+
2428#include " ArduinoCloudThing.h"
2529
2630/* *****************************************************************************
@@ -157,61 +161,78 @@ void ArduinoCloudThing::decode(uint8_t const * const payload, size_t const lengt
157161 }
158162}
159163
160- bool ArduinoCloudThing::isPropertyInContainer (String const & name) {
161- for (int i = 0 ; i < _property_list.size (); i++) {
162- ArduinoCloudProperty * p = _property_list.get (i);
163- if (p->name () == name) {
164- return true ;
165- }
166- }
167- return false ;
164+ bool ArduinoCloudThing::isPropertyInContainer (String const & name)
165+ {
166+ return (getProperty (name) != nullptr );
168167}
169168
170- int ArduinoCloudThing::appendChangedProperties (CborEncoder * arrayEncoder, bool lightPayload) {
169+ int ArduinoCloudThing::appendChangedProperties (CborEncoder * arrayEncoder, bool lightPayload)
170+ {
171171 int appendedProperties = 0 ;
172- for (int i = 0 ; i < _property_list.size (); i++) {
173- ArduinoCloudProperty * p = _property_list.get (i);
174- if (p->shouldBeUpdated () && p->isReadableByCloud ()) {
175- p->append (arrayEncoder, lightPayload);
176- appendedProperties++;
177- }
178- }
172+ std::for_each (_property_list.begin (),
173+ _property_list.end (),
174+ [arrayEncoder, lightPayload, &appendedProperties](ArduinoCloudProperty * p)
175+ {
176+ if (p->shouldBeUpdated () && p->isReadableByCloud ())
177+ {
178+ p->append (arrayEncoder, lightPayload);
179+ appendedProperties++;
180+ }
181+ });
179182 return appendedProperties;
180183}
181184
182185// retrieve property by name
183- ArduinoCloudProperty * ArduinoCloudThing::getProperty (String const & name) {
184- for (int i = 0 ; i < _property_list.size (); i++) {
185- ArduinoCloudProperty * p = _property_list.get (i);
186- if (p->name () == name) {
187- return p;
188- }
189- }
190- return NULL ;
186+ ArduinoCloudProperty * ArduinoCloudThing::getProperty (String const & name)
187+ {
188+ std::list<ArduinoCloudProperty *>::iterator iter;
189+
190+ iter = std::find_if (_property_list.begin (),
191+ _property_list.end (),
192+ [name](ArduinoCloudProperty * p) -> bool
193+ {
194+ return (p->name () == name);
195+ });
196+
197+ if (iter == _property_list.end ())
198+ return nullptr ;
199+ else
200+ return (*iter);
191201}
192202
193203// retrieve property by identifier
194- ArduinoCloudProperty * ArduinoCloudThing::getProperty (int const & pos) {
195- for (int i = 0 ; i < _property_list.size (); i++) {
196- ArduinoCloudProperty * p = _property_list.get (i);
197- if (p->identifier () == pos) {
198- return p;
199- }
200- }
201- return NULL ;
204+ ArduinoCloudProperty * ArduinoCloudThing::getProperty (int const & pos)
205+ {
206+ std::list<ArduinoCloudProperty *>::iterator iter;
207+
208+ iter = std::find_if (_property_list.begin (),
209+ _property_list.end (),
210+ [pos](ArduinoCloudProperty * p) -> bool
211+ {
212+ return (p->identifier () == pos);
213+ });
214+
215+ if (iter == _property_list.end ())
216+ return nullptr ;
217+ else
218+ return (*iter);
202219}
203220
204221// this function updates the timestamps on the primitive properties that have been modified locally since last cloud synchronization
205- void ArduinoCloudThing::updateTimestampOnLocallyChangedProperties () {
206- if (_numPrimitivesProperties == 0 ) {
207- return ;
208- } else {
209- for (int i = 0 ; i < _property_list.size (); i++) {
210- CloudWrapperBase * p = (CloudWrapperBase *)_property_list.get (i);
211- if (p->isPrimitive () && p->isChangedLocally () && p->isReadableByCloud ()) {
212- p->updateLocalTimestamp ();
213- }
214- }
222+ void ArduinoCloudThing::updateTimestampOnLocallyChangedProperties ()
223+ {
224+ if (_numPrimitivesProperties > 0 )
225+ {
226+ std::for_each (_property_list.begin (),
227+ _property_list.end (),
228+ [](ArduinoCloudProperty * p)
229+ {
230+ CloudWrapperBase * pbase = reinterpret_cast <CloudWrapperBase *>(p);
231+ if (pbase->isPrimitive () && pbase->isChangedLocally () && pbase->isReadableByCloud ())
232+ {
233+ p->updateLocalTimestamp ();
234+ }
235+ });
215236 }
216237}
217238
@@ -448,7 +469,6 @@ ArduinoCloudThing::MapParserState ArduinoCloudThing::handle_LeaveMap(CborValue *
448469 updateProperty (_currentPropertyName, _currentPropertyBaseTime + _currentPropertyTime);
449470 /* Reset current property data */
450471 freeMapDataList (&_map_data_list);
451- _map_data_list.clear ();
452472 _currentPropertyBaseTime = 0 ;
453473 _currentPropertyTime = 0 ;
454474 }
@@ -459,7 +479,7 @@ ArduinoCloudThing::MapParserState ArduinoCloudThing::handle_LeaveMap(CborValue *
459479 if (map_data->time .isSet () && (map_data->time .get () > _currentPropertyTime)) {
460480 _currentPropertyTime = (unsigned long )map_data->time .get ();
461481 }
462- _map_data_list.add (map_data);
482+ _map_data_list.push_back (map_data);
463483 _currentPropertyName = propertyName;
464484 }
465485
@@ -472,19 +492,22 @@ ArduinoCloudThing::MapParserState ArduinoCloudThing::handle_LeaveMap(CborValue *
472492 updateProperty (_currentPropertyName, _currentPropertyBaseTime + _currentPropertyTime);
473493 /* Reset last property data */
474494 freeMapDataList (&_map_data_list);
475- _map_data_list.clear ();
476495 next_state = MapParserState::Complete;
477496 }
478497 }
479498
480499 return next_state;
481500}
482501
483- void ArduinoCloudThing::freeMapDataList (LinkedList<CborMapData *> *map_data_list) {
484- while (map_data_list->size () > 0 ) {
485- CborMapData const * mapData = map_data_list->pop ();
486- delete mapData;
487- }
502+ void ArduinoCloudThing::freeMapDataList (std::list<CborMapData *> * map_data_list)
503+ {
504+ std::for_each (map_data_list->begin (),
505+ map_data_list->end (),
506+ [](CborMapData * map_data)
507+ {
508+ delete map_data;
509+ });
510+ map_data_list->clear ();
488511}
489512
490513void ArduinoCloudThing::updateProperty (String propertyName, unsigned long cloudChangeEventTime) {
0 commit comments