Skip to content

Commit

Permalink
Adding parameter array support
Browse files Browse the repository at this point in the history
  • Loading branch information
ayrton04 committed Feb 20, 2018
1 parent f88ade7 commit f70c7ef
Show file tree
Hide file tree
Showing 4 changed files with 845 additions and 47 deletions.
10 changes: 10 additions & 0 deletions rclcpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ if(BUILD_TESTING)
target_link_libraries(test_node ${PROJECT_NAME})
endif()
ament_add_gtest(test_parameter_events_filter test/test_parameter_events_filter.cpp)
ament_add_gtest(test_parameter test/test_parameter.cpp)
if(TARGET test_parameter)
target_include_directories(test_parameter PUBLIC
${rcl_interfaces_INCLUDE_DIRS}
${rmw_INCLUDE_DIRS}
${rosidl_generator_cpp_INCLUDE_DIRS}
${rosidl_typesupport_cpp_INCLUDE_DIRS}
)
target_link_libraries(test_parameter ${PROJECT_NAME})
endif()
if(TARGET test_parameter_events_filter)
target_include_directories(test_parameter_events_filter PUBLIC
${rcl_interfaces_INCLUDE_DIRS}
Expand Down
191 changes: 173 additions & 18 deletions rclcpp/include/rclcpp/parameter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef RCLCPP__PARAMETER_HPP_
#define RCLCPP__PARAMETER_HPP_

#include <iostream>
#include <ostream>
#include <sstream>
#include <string>
Expand All @@ -39,6 +40,10 @@ enum ParameterType
PARAMETER_DOUBLE = rcl_interfaces::msg::ParameterType::PARAMETER_DOUBLE,
PARAMETER_STRING = rcl_interfaces::msg::ParameterType::PARAMETER_STRING,
PARAMETER_BYTE_ARRAY = rcl_interfaces::msg::ParameterType::PARAMETER_BYTE_ARRAY,
PARAMETER_BOOL_ARRAY = rcl_interfaces::msg::ParameterType::PARAMETER_BOOL_ARRAY,
PARAMETER_INTEGER_ARRAY = rcl_interfaces::msg::ParameterType::PARAMETER_INTEGER_ARRAY,
PARAMETER_DOUBLE_ARRAY = rcl_interfaces::msg::ParameterType::PARAMETER_DOUBLE_ARRAY,
PARAMETER_STRING_ARRAY = rcl_interfaces::msg::ParameterType::PARAMETER_STRING_ARRAY,
};

// Structure to store an arbitrary parameter with templated get/set methods
Expand All @@ -65,6 +70,30 @@ class ParameterVariant
explicit ParameterVariant(
const std::string & name,
const std::vector<uint8_t> & byte_array_value);
RCLCPP_PUBLIC
explicit ParameterVariant(
const std::string & name,
const std::vector<bool> & bool_array_value);
RCLCPP_PUBLIC
explicit ParameterVariant(
const std::string & name,
const std::vector<int> & int_array_value);
RCLCPP_PUBLIC
explicit ParameterVariant(
const std::string & name,
const std::vector<int64_t> & int_array_value);
RCLCPP_PUBLIC
explicit ParameterVariant(
const std::string & name,
const std::vector<float> & double_array_value);
RCLCPP_PUBLIC
explicit ParameterVariant(
const std::string & name,
const std::vector<double> & double_array_value);
RCLCPP_PUBLIC
explicit ParameterVariant(
const std::string & name,
const std::vector<std::string> & string_array_value);

RCLCPP_PUBLIC
ParameterType
Expand All @@ -84,6 +113,17 @@ class ParameterVariant

// The following get_value() variants require the use of ParameterType

template<ParameterType type>
typename std::enable_if<type == ParameterType::PARAMETER_BOOL, bool>::type
get_value() const
{
if (value_.type != rcl_interfaces::msg::ParameterType::PARAMETER_BOOL) {
// TODO(wjwwood): use custom exception
throw std::runtime_error("Invalid type");
}
return value_.bool_value;
}

template<ParameterType type>
typename std::enable_if<type == ParameterType::PARAMETER_INTEGER, int64_t>::type
get_value() const
Expand Down Expand Up @@ -118,30 +158,74 @@ class ParameterVariant
}

template<ParameterType type>
typename std::enable_if<type == ParameterType::PARAMETER_BOOL, bool>::type
typename std::enable_if<
type == ParameterType::PARAMETER_BYTE_ARRAY, const std::vector<uint8_t> &>::type
get_value() const
{
if (value_.type != rcl_interfaces::msg::ParameterType::PARAMETER_BOOL) {
if (value_.type != rcl_interfaces::msg::ParameterType::PARAMETER_BYTE_ARRAY) {
// TODO(wjwwood): use custom exception
throw std::runtime_error("Invalid type");
}
return value_.bool_value;
return value_.byte_array_value;
}

template<ParameterType type>
typename std::enable_if<
type == ParameterType::PARAMETER_BYTE_ARRAY, const std::vector<uint8_t> &>::type
type == ParameterType::PARAMETER_BOOL_ARRAY, const std::vector<bool> &>::type
get_value() const
{
if (value_.type != rcl_interfaces::msg::ParameterType::PARAMETER_BYTE_ARRAY) {
if (value_.type != rcl_interfaces::msg::ParameterType::PARAMETER_BOOL_ARRAY) {
// TODO(wjwwood): use custom exception
throw std::runtime_error("Invalid type");
}
return value_.byte_array_value;
return value_.bool_array_value;
}

template<ParameterType type>
typename std::enable_if<
type == ParameterType::PARAMETER_INTEGER_ARRAY, const std::vector<int64_t> &>::type
get_value() const
{
if (value_.type != rcl_interfaces::msg::ParameterType::PARAMETER_INTEGER_ARRAY) {
// TODO(wjwwood): use custom exception
throw std::runtime_error("Invalid type");
}
return value_.integer_array_value;
}

template<ParameterType type>
typename std::enable_if<
type == ParameterType::PARAMETER_DOUBLE_ARRAY, const std::vector<double> &>::type
get_value() const
{
if (value_.type != rcl_interfaces::msg::ParameterType::PARAMETER_DOUBLE_ARRAY) {
// TODO(wjwwood): use custom exception
throw std::runtime_error("Invalid type");
}
return value_.double_array_value;
}

template<ParameterType type>
typename std::enable_if<
type == ParameterType::PARAMETER_STRING_ARRAY, const std::vector<std::string> &>::type
get_value() const
{
if (value_.type != rcl_interfaces::msg::ParameterType::PARAMETER_STRING_ARRAY) {
// TODO(wjwwood): use custom exception
throw std::runtime_error("Invalid type");
}
return value_.string_array_value;
}

// The following get_value() variants allow the use of primitive types

template<typename type>
typename std::enable_if<std::is_same<type, bool>::value, bool>::type
get_value() const
{
return get_value<ParameterType::PARAMETER_BOOL>();
}

template<typename type>
typename std::enable_if<
std::is_integral<type>::value && !std::is_same<type, bool>::value, int64_t>::type
Expand All @@ -165,21 +249,54 @@ class ParameterVariant
}

template<typename type>
typename std::enable_if<std::is_same<type, bool>::value, bool>::type
typename std::enable_if<
std::is_convertible<
type, const std::vector<uint8_t> &>::value, const std::vector<uint8_t> &>::type
get_value() const
{
return get_value<ParameterType::PARAMETER_BOOL>();
return get_value<ParameterType::PARAMETER_BYTE_ARRAY>();
}

template<typename type>
typename std::enable_if<
std::is_convertible<
type, const std::vector<uint8_t> &>::value, const std::vector<uint8_t> &>::type
type, const std::vector<bool> &>::value, const std::vector<bool> &>::type
get_value() const
{
return get_value<ParameterType::PARAMETER_BYTE_ARRAY>();
return get_value<ParameterType::PARAMETER_BOOL_ARRAY>();
}

template<typename type>
typename std::enable_if<
std::is_convertible<
type, const std::vector<int64_t> &>::value, const std::vector<int64_t> &>::type
get_value() const
{
return get_value<ParameterType::PARAMETER_INTEGER_ARRAY>();
}

template<typename type>
typename std::enable_if<
std::is_convertible<
type, const std::vector<double> &>::value, const std::vector<double> &>::type
get_value() const
{
return get_value<ParameterType::PARAMETER_DOUBLE_ARRAY>();
}

template<typename type>
typename std::enable_if<
std::is_convertible<
type, const std::vector<std::string> &>::value, const std::vector<std::string> &>::type
get_value() const
{
return get_value<ParameterType::PARAMETER_STRING_ARRAY>();
}

RCLCPP_PUBLIC
bool
as_bool() const;

RCLCPP_PUBLIC
int64_t
as_int() const;
Expand All @@ -193,12 +310,24 @@ class ParameterVariant
as_string() const;

RCLCPP_PUBLIC
bool
as_bool() const;
const std::vector<uint8_t> &
as_byte_array() const;

RCLCPP_PUBLIC
const std::vector<uint8_t> &
as_bytes() const;
const std::vector<bool> &
as_bool_array() const;

RCLCPP_PUBLIC
const std::vector<int64_t> &
as_integer_array() const;

RCLCPP_PUBLIC
const std::vector<double> &
as_double_array() const;

RCLCPP_PUBLIC
const std::vector<std::string> &
as_string_array() const;

RCLCPP_PUBLIC
static ParameterVariant
Expand All @@ -208,16 +337,42 @@ class ParameterVariant
rcl_interfaces::msg::Parameter
to_parameter();

RCLCPP_PUBLIC
std::string
value_to_string() const;
std::string value_to_string() const;

private:
template<typename ValType, typename PrintType = ValType>
std::string
array_to_string(
const std::vector<ValType> & array,
const std::ios::fmtflags format_flags = std::ios::dec) const
{
std::stringstream type_array;
bool first_item = true;
type_array << "[";
type_array.setf(format_flags, std::ios_base::basefield | std::ios::boolalpha);
type_array << std::showbase;
for (const ValType value : array) {
if (!first_item) {
type_array << ", ";
} else {
first_item = false;
}
type_array << static_cast<PrintType>(value);
}
type_array << "]";
return type_array.str();
}

template<typename OutputType, typename InputType>
void vector_assign(OutputType & output, const InputType & input)
{
output.assign(input.begin(), input.end());
}

std::string name_;
rcl_interfaces::msg::ParameterValue value_;
};


/// Return a json encoded version of the parameter intended for a dict.
RCLCPP_PUBLIC
std::string
Expand Down
Loading

0 comments on commit f70c7ef

Please sign in to comment.