Skip to content

Commit 34b5079

Browse files
committed
Replacing custom list implementation with std::list
1 parent ce1df18 commit 34b5079

File tree

4 files changed

+118
-82
lines changed

4 files changed

+118
-82
lines changed

Diff for: src/cbor/ArduinoCloudProperty.cpp

+33-23
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
#include "ArduinoCloudProperty.h"
1919

20+
#undef max
21+
#undef min
22+
#include <algorithm>
23+
2024
#ifndef ARDUINO_ARCH_SAMD
2125
#pragma message "No RTC available on this architecture - ArduinoIoTCloud will not keep track of local change timestamps ."
2226
#endif
@@ -38,7 +42,6 @@ ArduinoCloudProperty::ArduinoCloudProperty()
3842
_update_interval_millis(0),
3943
_last_local_change_timestamp(0),
4044
_last_cloud_change_timestamp(0),
41-
_map_data_list(nullptr),
4245
_identifier(0),
4346
_attributeIdentifier(0),
4447
_lightPayload(false) {
@@ -174,7 +177,7 @@ void ArduinoCloudProperty::appendAttributeName(String attributeName, std::functi
174177
cbor_encoder_close_container(encoder, &mapEncoder);
175178
}
176179

177-
void ArduinoCloudProperty::setAttributesFromCloud(LinkedList<CborMapData *> *map_data_list) {
180+
void ArduinoCloudProperty::setAttributesFromCloud(std::list<CborMapData *> * map_data_list) {
178181
_map_data_list = map_data_list;
179182
_attributeIdentifier = 0;
180183
setAttributesFromCloud();
@@ -215,31 +218,38 @@ void ArduinoCloudProperty::setAttributeReal(String& value, String attributeName)
215218
});
216219
}
217220

218-
void ArduinoCloudProperty::setAttributeReal(String attributeName, std::function<void (CborMapData *md)>setValue) {
221+
void ArduinoCloudProperty::setAttributeReal(String attributeName, std::function<void (CborMapData *md)>setValue)
222+
{
219223
if (attributeName != "") {
220224
_attributeIdentifier++;
221225
}
222-
for (int i = 0; i < _map_data_list->size(); i++) {
223-
CborMapData *map = _map_data_list->get(i);
224-
if (map != nullptr) {
225-
if (map->light_payload.isSet() && map->light_payload.get()) {
226-
// if a light payload is detected, the attribute identifier is retrieved from the cbor map and the corresponding attribute is updated
227-
int attid = map->attribute_identifier.get();
228-
if (attid == _attributeIdentifier) {
229-
setValue(map);
230-
break;
231-
}
232-
} else {
233-
// if a normal payload is detected, the name of the attribute to be updated is extracted directly from the cbor map
234-
String an = map->attribute_name.get();
235-
if (an == attributeName) {
236-
setValue(map);
237-
break;
238-
}
239-
}
240-
}
241-
}
242226

227+
std::for_each(_map_data_list->begin(),
228+
_map_data_list->end(),
229+
[this, attributeName, setValue](CborMapData * map)
230+
{
231+
if (map != nullptr)
232+
{
233+
if (map->light_payload.isSet() && map->light_payload.get())
234+
{
235+
// if a light payload is detected, the attribute identifier is retrieved from the cbor map and the corresponding attribute is updated
236+
int attid = map->attribute_identifier.get();
237+
if (attid == _attributeIdentifier) {
238+
setValue(map);
239+
return;
240+
}
241+
}
242+
else
243+
{
244+
// if a normal payload is detected, the name of the attribute to be updated is extracted directly from the cbor map
245+
String an = map->attribute_name.get();
246+
if (an == attributeName) {
247+
setValue(map);
248+
return;
249+
}
250+
}
251+
}
252+
});
243253
}
244254

245255
String ArduinoCloudProperty::getAttributeName(String propertyName, char separator) {

Diff for: src/cbor/ArduinoCloudProperty.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@
2727
INCLUDE
2828
******************************************************************************/
2929

30-
3130
#include <Arduino.h>
3231
// in order to allow <functional> to define its own max and min functions
3332
#undef max
3433
#undef min
34+
#include <list>
3535
#include <functional>
3636

3737
#include "lib/tinycbor/cbor-lib.h"
38-
#include "lib/LinkedList/LinkedList.h"
3938

4039
#define appendAttributesToCloud() appendAttributesToCloudReal(CborEncoder *encoder)
4140
#define appendAttribute(x) appendAttributeReal(x, getAttributeName(#x, '.'), encoder)
@@ -169,7 +168,7 @@ class ArduinoCloudProperty {
169168
void appendAttributeReal(float value, String attributeName = "", CborEncoder *encoder = nullptr);
170169
void appendAttributeReal(String value, String attributeName = "", CborEncoder *encoder = nullptr);
171170
void appendAttributeName(String attributeName, std::function<void (CborEncoder& mapEncoder)>f, CborEncoder *encoder);
172-
void setAttributesFromCloud(LinkedList<CborMapData *> *map_data_list);
171+
void setAttributesFromCloud(std::list<CborMapData *> * map_data_list);
173172
void setAttributeReal(bool& value, String attributeName = "");
174173
void setAttributeReal(int& value, String attributeName = "");
175174
void setAttributeReal(float& value, String attributeName = "");
@@ -206,7 +205,7 @@ class ArduinoCloudProperty {
206205
/* Variables used for reconnection sync*/
207206
unsigned long _last_local_change_timestamp;
208207
unsigned long _last_cloud_change_timestamp;
209-
LinkedList<CborMapData *> * _map_data_list;
208+
std::list<CborMapData *> * _map_data_list;
210209
/* Store the identifier of the property in the array list */
211210
int _identifier;
212211
int _attributeIdentifier;

Diff for: src/cbor/ArduinoCloudThing.cpp

+73-50
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
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

490513
void ArduinoCloudThing::updateProperty(String propertyName, unsigned long cloudChangeEventTime) {

Diff for: src/cbor/ArduinoCloudThing.h

+9-5
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@
2222
INCLUDE
2323
******************************************************************************/
2424

25+
#undef max
26+
#undef min
27+
#include <list>
28+
2529
#include "ArduinoCloudProperty.h"
26-
#include "lib/LinkedList/LinkedList.h"
30+
2731
#include "types/CloudBool.h"
2832
#include "types/CloudFloat.h"
2933
#include "types/CloudInt.h"
@@ -95,14 +99,14 @@ class ArduinoCloudThing {
9599

96100
private:
97101
GetTimeCallbackFunc _get_time_func;
98-
LinkedList<ArduinoCloudProperty *> _property_list;
102+
std::list<ArduinoCloudProperty *> _property_list;
99103
/* Keep track of the number of primitive properties in the Thing. If 0 it allows the early exit in updateTimestampOnLocallyChangedProperties() */
100104
int _numPrimitivesProperties;
101105
int _numProperties;
102106
/* Indicates the if the message received to be decoded is a response to the getLastValues inquiry */
103107
bool _isSyncMessage;
104108
/* List of map data that will hold all the attributes of a property */
105-
LinkedList<CborMapData *> _map_data_list;
109+
std::list<CborMapData *> _map_data_list;
106110
/* Current property name during decoding: use to look for a new property in the senml value array */
107111
String _currentPropertyName;
108112
unsigned long _currentPropertyBaseTime,
@@ -140,15 +144,15 @@ class ArduinoCloudThing {
140144

141145
static bool ifNumericConvertToDouble(CborValue * value_iter, double * numeric_val);
142146
static double convertCborHalfFloatToDouble(uint16_t const half_val);
143-
void freeMapDataList(LinkedList<CborMapData *> * map_data_list);
147+
void freeMapDataList(std::list<CborMapData *> * map_data_list);
144148
inline void addProperty(ArduinoCloudProperty * property_obj, int propertyIdentifier) {
145149
if (propertyIdentifier != -1) {
146150
property_obj->setIdentifier(propertyIdentifier);
147151
} else {
148152
// if property identifier is -1, an incremental value will be assigned as identifier.
149153
property_obj->setIdentifier(_numProperties);
150154
}
151-
_property_list.add(property_obj);
155+
_property_list.push_back(property_obj);
152156
}
153157
ArduinoCloudProperty * getProperty(String const & name);
154158
ArduinoCloudProperty * getProperty(int const & identifier);

0 commit comments

Comments
 (0)