Skip to content

Commit 7743c98

Browse files
committed
Extract CHECK_CBOR macro to also use it within CBOREncoder::encode
1 parent 13dda3d commit 7743c98

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

src/cbor/CBOREncoder.cpp

+4-8
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,20 @@
3030
/******************************************************************************
3131
* PUBLIC MEMBER FUNCTIONS
3232
******************************************************************************/
33-
#include <iostream>
33+
3434
CborError CBOREncoder::encode(PropertyContainer & property_container, uint8_t * data, size_t const size, int & bytes_encoded, bool lightPayload)
3535
{
36-
CborError error = CborNoError;
3736
CborEncoder encoder, arrayEncoder;
3837

3938
cbor_encoder_init(&encoder, data, size, 0);
4039

41-
error = cbor_encoder_create_array(&encoder, &arrayEncoder, CborIndefiniteLength);
42-
if (CborNoError != error)
43-
return error;
40+
CHECK_CBOR(cbor_encoder_create_array(&encoder, &arrayEncoder, CborIndefiniteLength));
4441

4542
/* Check if backing storage and cloud has diverged
4643
* time interval may be elapsed or property may be changed
4744
* and if that's the case encode the property into the CBOR.
4845
*/
46+
CborError error = CborNoError;
4947
int num_encoded_properties = 0;
5048
std::for_each(property_container.begin(),
5149
property_container.end(),
@@ -64,9 +62,7 @@ CborError CBOREncoder::encode(PropertyContainer & property_container, uint8_t *
6462
(CborErrorOutOfMemory != error))
6563
return error;
6664

67-
error = cbor_encoder_close_container(&encoder, &arrayEncoder);
68-
if (CborNoError != error)
69-
return error;
65+
CHECK_CBOR(cbor_encoder_close_container(&encoder, &arrayEncoder));
7066

7167
if (num_encoded_properties > 0)
7268
bytes_encoded = cbor_encoder_get_buffer_size(&encoder, data);

src/cbor/lib/tinycbor/cbor-lib.h

+18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
#ifndef CBOR_LIB_H
22
#define CBOR_LIB_H
33

4+
/******************************************************************************
5+
INCLUDE
6+
******************************************************************************/
7+
48
#include "src/cbor.h"
59

10+
/******************************************************************************
11+
* DEFINE
12+
******************************************************************************/
13+
14+
#ifndef CHECK_CBOR
15+
#define CHECK_CBOR(expr) \
16+
do { \
17+
CborError error = CborNoError; \
18+
error = (expr); \
19+
if (CborNoError != error) \
20+
return error; \
21+
} while(0);
22+
#endif /* CHECK_CBOR */
23+
624
#endif

src/property/Property.h

-8
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,6 @@
4040
DEFINE
4141
******************************************************************************/
4242

43-
#define CHECK_CBOR(expr) \
44-
do { \
45-
CborError error = CborNoError; \
46-
error = (expr); \
47-
if (CborNoError != error) \
48-
return error; \
49-
} while(0);
50-
5143
#define appendAttributesToCloud() appendAttributesToCloudReal(CborEncoder *encoder)
5244
#define appendAttribute(x) appendAttributeReal(x, getAttributeName(#x, '.'), encoder)
5345
#define setAttribute(x) setAttributeReal(x, getAttributeName(#x, '.'))

0 commit comments

Comments
 (0)