Skip to content

Commit f5829bd

Browse files
committed
Use for loop instead of for_each to be able to break loop when condition is met
1 parent 31cd667 commit f5829bd

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

src/cbor/CBOREncoder.cpp

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,23 @@ CborError CBOREncoder::encode(PropertyContainer & property_container, uint8_t *
5555
PropertyContainer::iterator iter = property_container.begin();
5656
std::advance(iter, current_property_index);
5757

58-
std::for_each(iter,
59-
property_container.end(),
60-
[lightPayload, &arrayEncoder, &error, &num_encoded_properties, &num_checked_properties](Property * p)
61-
{
62-
bool maximum_number_of_properties_reached = (num_encoded_properties >= encoded_properties_message_limit) && (encoded_properties_message_limit != CBOR_ENCODER_NO_PROPERTIES_LIMIT);
63-
bool cbor_encoder_error = (error != CborNoError);
64-
if((!cbor_encoder_error) && (!maximum_number_of_properties_reached))
65-
{
66-
if (p->shouldBeUpdated() && p->isReadableByCloud())
67-
{
68-
error = p->append(&arrayEncoder, lightPayload);
69-
if(error == CborNoError)
70-
num_encoded_properties++;
71-
else
72-
return;
73-
}
74-
num_checked_properties++;
75-
}
76-
});
58+
for(; iter != property_container.end(); iter++)
59+
{
60+
Property * p = * iter;
61+
bool maximum_number_of_properties_reached = (num_encoded_properties >= encoded_properties_message_limit) && (encoded_properties_message_limit != -1);
62+
bool cbor_encoder_error = (error != CborNoError);
63+
64+
if (maximum_number_of_properties_reached || cbor_encoder_error)
65+
break;
66+
67+
if (p->shouldBeUpdated() && p->isReadableByCloud())
68+
{
69+
error = p->append(&arrayEncoder, lightPayload);
70+
if(error == CborNoError)
71+
num_encoded_properties++;
72+
}
73+
num_checked_properties++;
74+
}
7775

7876
if ((CborNoError != error) && (CborErrorOutOfMemory != error))
7977
{
@@ -97,16 +95,16 @@ CborError CBOREncoder::encode(PropertyContainer & property_container, uint8_t *
9795
iter = property_container.begin();
9896
std::advance(iter, current_property_index);
9997
int num_appended_properties = 0;
100-
std::for_each(iter,
101-
property_container.end(),
102-
[&num_appended_properties, &num_checked_properties](Property * p)
103-
{
104-
if (num_appended_properties < num_checked_properties)
105-
{
106-
p->appendCompleted();
107-
num_appended_properties++;
108-
}
109-
});
98+
99+
for(; iter != property_container.end(); iter++)
100+
{
101+
Property * p = * iter;
102+
if (num_appended_properties >= num_checked_properties)
103+
break;
104+
105+
p->appendCompleted();
106+
num_appended_properties++;
107+
}
110108

111109
/* Advance property index for the nex message */
112110
current_property_index += num_checked_properties;

0 commit comments

Comments
 (0)