Skip to content

Commit

Permalink
Refactor parser.c for better testability (ros2#754)
Browse files Browse the repository at this point in the history
* Refactor rcl_yaml_param_parser for better testability

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* Reorder parser.c to match parser.h

Signed-off-by: Stephen Brawner <brawner@gmail.com>

squash! Reorder parser.c to match parser.h

* Refactor yaml_variant.c for deduplication

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* ADD_VALUE_TO_SIMPLE_ARRAY for deduplication

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* Remove fprintf

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* PR Fixup

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* Move headers to src directory

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* PR Fixup

Signed-off-by: Stephen Brawner <brawner@gmail.com>

* Rebase ros2#780

Signed-off-by: Stephen Brawner <brawner@gmail.com>
  • Loading branch information
brawner committed Sep 1, 2020
1 parent 1d94ed7 commit 5663f24
Show file tree
Hide file tree
Showing 13 changed files with 1,900 additions and 1,582 deletions.
5 changes: 5 additions & 0 deletions rcl_yaml_param_parser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
endif()

set(rcl_yaml_parser_sources
src/add_to_arrays.c
src/namespace.c
src/node_params.c
src/parse.c
src/parser.c
src/yaml_variant.c
)

add_library(
Expand Down
119 changes: 119 additions & 0 deletions rcl_yaml_param_parser/src/add_to_arrays.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright 2018 Apex.AI, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "./impl/add_to_arrays.h"

#define ADD_VALUE_TO_SIMPLE_ARRAY(val_array, value, value_type, allocator) \
do { \
if (NULL == val_array->values) { \
val_array->values = value; \
val_array->size = 1; \
} else { \
/* Increase the array size by one and add the new value */ \
value_type * tmp_arr = val_array->values; \
val_array->values = allocator.zero_allocate( \
val_array->size + 1U, sizeof(value_type), allocator.state); \
if (NULL == val_array->values) { \
val_array->values = tmp_arr; \
RCUTILS_SAFE_FWRITE_TO_STDERR("Error allocating mem\n"); \
return RCUTILS_RET_BAD_ALLOC; \
} \
memcpy(val_array->values, tmp_arr, (val_array->size * sizeof(value_type))); \
val_array->values[val_array->size] = *value; \
val_array->size++; \
allocator.deallocate(value, allocator.state); \
allocator.deallocate(tmp_arr, allocator.state); \
} \
return RCUTILS_RET_OK; \
} while (0)

rcutils_ret_t add_val_to_bool_arr(
rcl_bool_array_t * const val_array,
bool * value,
const rcutils_allocator_t allocator)
{
RCUTILS_CHECK_ARGUMENT_FOR_NULL(val_array, RCUTILS_RET_INVALID_ARGUMENT);
RCUTILS_CHECK_ARGUMENT_FOR_NULL(value, RCUTILS_RET_INVALID_ARGUMENT);
RCUTILS_CHECK_ALLOCATOR_WITH_MSG(
&allocator, "invalid allocator", return RCUTILS_RET_INVALID_ARGUMENT);

ADD_VALUE_TO_SIMPLE_ARRAY(val_array, value, bool, allocator);
}

///
/// Add a value to an integer array. Create the array if it does not exist
///
rcutils_ret_t add_val_to_int_arr(
rcl_int64_array_t * const val_array,
int64_t * value,
const rcutils_allocator_t allocator)
{
RCUTILS_CHECK_ARGUMENT_FOR_NULL(val_array, RCUTILS_RET_INVALID_ARGUMENT);
RCUTILS_CHECK_ARGUMENT_FOR_NULL(value, RCUTILS_RET_INVALID_ARGUMENT);
RCUTILS_CHECK_ALLOCATOR_WITH_MSG(
&allocator, "invalid allocator", return RCUTILS_RET_INVALID_ARGUMENT);

ADD_VALUE_TO_SIMPLE_ARRAY(val_array, value, int64_t, allocator);
}

///
/// Add a value to a double array. Create the array if it does not exist
///
rcutils_ret_t add_val_to_double_arr(
rcl_double_array_t * const val_array,
double * value,
const rcutils_allocator_t allocator)
{
RCUTILS_CHECK_ARGUMENT_FOR_NULL(val_array, RCUTILS_RET_INVALID_ARGUMENT);
RCUTILS_CHECK_ARGUMENT_FOR_NULL(value, RCUTILS_RET_INVALID_ARGUMENT);
RCUTILS_CHECK_ALLOCATOR_WITH_MSG(
&allocator, "invalid allocator", return RCUTILS_RET_INVALID_ARGUMENT);

ADD_VALUE_TO_SIMPLE_ARRAY(val_array, value, double, allocator);
}

///
/// Add a value to a string array. Create the array if it does not exist
///
rcutils_ret_t add_val_to_string_arr(
rcutils_string_array_t * const val_array,
char * value,
const rcutils_allocator_t allocator)
{
RCUTILS_CHECK_ARGUMENT_FOR_NULL(val_array, RCUTILS_RET_INVALID_ARGUMENT);
RCUTILS_CHECK_ARGUMENT_FOR_NULL(value, RCUTILS_RET_INVALID_ARGUMENT);
RCUTILS_CHECK_ALLOCATOR_WITH_MSG(
&allocator, "invalid allocator", return RCUTILS_RET_INVALID_ARGUMENT);

if (NULL == val_array->data) {
rcutils_ret_t ret = rcutils_string_array_init(val_array, 1, &allocator);
if (RCUTILS_RET_OK != ret) {
return ret;
}
val_array->data[0U] = value;
} else {
/// Increase the array size by one and add the new value
char ** new_string_arr_ptr = allocator.reallocate(
val_array->data,
((val_array->size + 1U) * sizeof(char *)), allocator.state);
if (NULL == new_string_arr_ptr) {
RCUTILS_SAFE_FWRITE_TO_STDERR("Error allocating mem\n");
return RCUTILS_RET_BAD_ALLOC;
}
val_array->data = new_string_arr_ptr;
val_array->data[val_array->size] = value;
val_array->size++;
}
return RCUTILS_RET_OK;
}
79 changes: 79 additions & 0 deletions rcl_yaml_param_parser/src/impl/add_to_arrays.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2018 Apex.AI, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef IMPL__ADD_TO_ARRAYS_H_
#define IMPL__ADD_TO_ARRAYS_H_

#include <yaml.h>

#include "rcutils/types.h"

#include "./types.h"
#include "rcl_yaml_param_parser/types.h"
#include "rcl_yaml_param_parser/visibility_control.h"

#ifdef __cplusplus
extern "C"
{
#endif

///
/// Add a value to a bool array. Create the array if it does not exist
///
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t add_val_to_bool_arr(
rcl_bool_array_t * const val_array,
bool * value,
const rcutils_allocator_t allocator);

///
/// Add a value to an integer array. Create the array if it does not exist
///
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t add_val_to_int_arr(
rcl_int64_array_t * const val_array,
int64_t * value,
const rcutils_allocator_t allocator);

///
/// Add a value to a double array. Create the array if it does not exist
///
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t add_val_to_double_arr(
rcl_double_array_t * const val_array,
double * value,
const rcutils_allocator_t allocator);

///
/// Add a value to a string array. Create the array if it does not exist
///
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t add_val_to_string_arr(
rcutils_string_array_t * const val_array,
char * value,
const rcutils_allocator_t allocator);

///
/// TODO (anup.pemmaiah): Support byte array
///

#ifdef __cplusplus
}
#endif

#endif // IMPL__ADD_TO_ARRAYS_H_
68 changes: 68 additions & 0 deletions rcl_yaml_param_parser/src/impl/namespace.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2018 Apex.AI, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef IMPL__NAMESPACE_H_
#define IMPL__NAMESPACE_H_

#include <yaml.h>

#include "rcutils/types.h"

#include "./types.h"
#include "rcl_yaml_param_parser/types.h"
#include "rcl_yaml_param_parser/visibility_control.h"

#ifdef __cplusplus
extern "C"
{
#endif

///
/// Add name to namespace tracker
///
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t add_name_to_ns(
namespace_tracker_t * ns_tracker,
const char * name,
const namespace_type_t namespace_type,
rcutils_allocator_t allocator);

///
/// Remove name from namespace tracker
///
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t rem_name_from_ns(
namespace_tracker_t * ns_tracker,
const namespace_type_t namespace_type,
rcutils_allocator_t allocator);

///
/// Replace namespace in namespace tracker
///
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t replace_ns(
namespace_tracker_t * ns_tracker,
char * const new_ns,
const uint32_t new_ns_count,
const namespace_type_t namespace_type,
rcutils_allocator_t allocator);

#ifdef __cplusplus
}
#endif

#endif // IMPL__NAMESPACE_H_
47 changes: 47 additions & 0 deletions rcl_yaml_param_parser/src/impl/node_params.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2018 Apex.AI, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef IMPL__NODE_PARAMS_H_
#define IMPL__NODE_PARAMS_H_

#include "rcl_yaml_param_parser/types.h"
#include "rcl_yaml_param_parser/visibility_control.h"

#ifdef __cplusplus
extern "C"
{
#endif

///
/// Create rcl_node_params_t structure
///
RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t node_params_init(
rcl_node_params_t * node_params,
const rcutils_allocator_t allocator);

///
/// Finalize rcl_node_params_t structure
///
RCL_YAML_PARAM_PARSER_PUBLIC
void rcl_yaml_node_params_fini(
rcl_node_params_t * node_params,
const rcutils_allocator_t allocator);

#ifdef __cplusplus
}
#endif

#endif // IMPL__NODE_PARAMS_H_
Loading

0 comments on commit 5663f24

Please sign in to comment.