1414
1515#include "rcl_yaml_param_parser/impl/add_to_arrays.h"
1616
17- ///
18- /// Add a value to a bool array. Create the array if it does not exist
19- ///
17+ #define ADD_VALUE_TO_SIMPLE_ARRAY (val_array , value , value_type , allocator ) \
18+ do { \
19+ if (NULL == val_array->values) { \
20+ val_array->values = value; \
21+ val_array->size = 1; \
22+ } else { \
23+ /* Increase the array size by one and add the new value */ \
24+ value_type * tmp_arr = val_array -> values ; \
25+ val_array -> values = allocator .zero_allocate ( \
26+ val_array -> size + 1U , sizeof (value_type ), allocator .state ); \
27+ if (NULL == val_array -> values ) { \
28+ val_array -> values = tmp_arr ; \
29+ RCUTILS_SAFE_FWRITE_TO_STDERR ("Error allocating mem\n" ); \
30+ return RCUTILS_RET_BAD_ALLOC ; \
31+ } \
32+ memmove (val_array -> values , tmp_arr , (val_array -> size * sizeof (value_type ))); \
33+ val_array -> values [val_array -> size ] = * value ; \
34+ val_array -> size ++ ; \
35+ allocator .deallocate (value , allocator .state ); \
36+ allocator .deallocate (tmp_arr , allocator .state ); \
37+ } \
38+ return RCUTILS_RET_OK ; \
39+ } while (0 )
40+
2041rcutils_ret_t add_val_to_bool_arr (
2142 rcl_bool_array_t * const val_array ,
2243 bool * value ,
@@ -27,26 +48,7 @@ rcutils_ret_t add_val_to_bool_arr(
2748 RCUTILS_CHECK_ALLOCATOR_WITH_MSG (
2849 & allocator , "invalid allocator" , return RCUTILS_RET_INVALID_ARGUMENT );
2950
30- if (NULL == val_array -> values ) {
31- val_array -> values = value ;
32- val_array -> size = 1 ;
33- } else {
34- /// Increase the array size by one and add the new value
35- bool * tmp_arr = val_array -> values ;
36- val_array -> values = allocator .zero_allocate (
37- val_array -> size + 1U , sizeof (bool ), allocator .state );
38- if (NULL == val_array -> values ) {
39- val_array -> values = tmp_arr ;
40- RCUTILS_SAFE_FWRITE_TO_STDERR ("Error allocating mem\n" );
41- return RCUTILS_RET_BAD_ALLOC ;
42- }
43- memmove (val_array -> values , tmp_arr , (val_array -> size * sizeof (bool )));
44- val_array -> values [val_array -> size ] = * value ;
45- val_array -> size ++ ;
46- allocator .deallocate (value , allocator .state );
47- allocator .deallocate (tmp_arr , allocator .state );
48- }
49- return RCUTILS_RET_OK ;
51+ ADD_VALUE_TO_SIMPLE_ARRAY (val_array , value , bool , allocator );
5052}
5153
5254///
@@ -62,26 +64,7 @@ rcutils_ret_t add_val_to_int_arr(
6264 RCUTILS_CHECK_ALLOCATOR_WITH_MSG (
6365 & allocator , "invalid allocator" , return RCUTILS_RET_INVALID_ARGUMENT );
6466
65- if (NULL == val_array -> values ) {
66- val_array -> values = value ;
67- val_array -> size ++ ;
68- } else {
69- /// Increase the array size by one and add the new value
70- int64_t * tmp_arr = val_array -> values ;
71- val_array -> values = allocator .zero_allocate (
72- val_array -> size + 1U , sizeof (int64_t ), allocator .state );
73- if (NULL == val_array -> values ) {
74- val_array -> values = tmp_arr ;
75- RCUTILS_SAFE_FWRITE_TO_STDERR ("Error allocating mem\n" );
76- return RCUTILS_RET_BAD_ALLOC ;
77- }
78- memmove (val_array -> values , tmp_arr , (val_array -> size * sizeof (int64_t )));
79- val_array -> values [val_array -> size ] = * value ;
80- val_array -> size ++ ;
81- allocator .deallocate (value , allocator .state );
82- allocator .deallocate (tmp_arr , allocator .state );
83- }
84- return RCUTILS_RET_OK ;
67+ ADD_VALUE_TO_SIMPLE_ARRAY (val_array , value , int64_t , allocator );
8568}
8669
8770///
@@ -97,26 +80,7 @@ rcutils_ret_t add_val_to_double_arr(
9780 RCUTILS_CHECK_ALLOCATOR_WITH_MSG (
9881 & allocator , "invalid allocator" , return RCUTILS_RET_INVALID_ARGUMENT );
9982
100- if (NULL == val_array -> values ) {
101- val_array -> values = value ;
102- val_array -> size ++ ;
103- } else {
104- /// Increase the array size by one and add the new value
105- double * tmp_arr = val_array -> values ;
106- val_array -> values = allocator .zero_allocate (
107- val_array -> size + 1U , sizeof (double ), allocator .state );
108- if (NULL == val_array -> values ) {
109- val_array -> values = tmp_arr ;
110- RCUTILS_SAFE_FWRITE_TO_STDERR ("Error allocating mem\n" );
111- return RCUTILS_RET_BAD_ALLOC ;
112- }
113- memmove (val_array -> values , tmp_arr , (val_array -> size * sizeof (double )));
114- val_array -> values [val_array -> size ] = * value ;
115- val_array -> size ++ ;
116- allocator .deallocate (value , allocator .state );
117- allocator .deallocate (tmp_arr , allocator .state );
118- }
119- return RCUTILS_RET_OK ;
83+ ADD_VALUE_TO_SIMPLE_ARRAY (val_array , value , double , allocator );
12084}
12185
12286///
0 commit comments