From b415df7ed6f52c35569d973e9afaad32f05773c7 Mon Sep 17 00:00:00 2001 From: Shane Loretz Date: Fri, 1 Jun 2018 14:03:43 -0700 Subject: [PATCH] Move exceptions to exceptions.hpp --- rclcpp/include/rclcpp/exceptions.hpp | 15 ++++++++++++ rclcpp/include/rclcpp/parameter_map.hpp | 32 +++---------------------- rclcpp/src/rclcpp/parameter_map.cpp | 19 ++------------- rclcpp/test/test_parameter_map.cpp | 17 ++++++++----- 4 files changed, 31 insertions(+), 52 deletions(-) diff --git a/rclcpp/include/rclcpp/exceptions.hpp b/rclcpp/include/rclcpp/exceptions.hpp index 23da40e972..7328ea4e83 100644 --- a/rclcpp/include/rclcpp/exceptions.hpp +++ b/rclcpp/include/rclcpp/exceptions.hpp @@ -181,6 +181,21 @@ class EventNotRegisteredError : public std::runtime_error : std::runtime_error("event already registered") {} }; +/// Thrown if passed parameters are inconsistent or invalid +class InvalidParametersException : public std::runtime_error +{ +public: + // Inherit constructors from runtime_error; + using std::runtime_error::runtime_error; +}; + +/// Throwing if passed parameter value is invalid. +class InvalidParameterValueException : public std::runtime_error +{ + // Inherit constructors from runtime_error; + using std::runtime_error::runtime_error; +}; + } // namespace exceptions } // namespace rclcpp diff --git a/rclcpp/include/rclcpp/parameter_map.hpp b/rclcpp/include/rclcpp/parameter_map.hpp index 748bbe5c01..09b59776a6 100644 --- a/rclcpp/include/rclcpp/parameter_map.hpp +++ b/rclcpp/include/rclcpp/parameter_map.hpp @@ -15,46 +15,20 @@ #ifndef RCLCPP__PARAMETER_MAP_HPP_ #define RCLCPP__PARAMETER_MAP_HPP_ -#include +#include + #include #include #include +#include "rclcpp/exceptions.hpp" #include "rclcpp/parameter.hpp" #include "rclcpp/parameter_value.hpp" #include "rclcpp/visibility_control.hpp" -#include "rcl_yaml_param_parser/types.h" namespace rclcpp { -/// Indicate `rcl_params_t` is or invalid. -class InvalidParametersException : public std::exception -{ -public: - /// Instantiate exception with a message. - /// \param[in] message A message to be included in the output of what(). - RCLCPP_PUBLIC - explicit InvalidParametersException(std::string message); - - RCLCPP_PUBLIC - ~InvalidParametersException(); - - RCLCPP_PUBLIC - const char * - what() const noexcept override; - -private: - std::string msg_; -}; - -/// Indicate `rcl_variant_t` is or invalid. -class InvalidParameterValueException : public InvalidParametersException -{ - // inherit constructor - using InvalidParametersException::InvalidParametersException; -}; - /// A map of fully qualified node names to a list of parameters using ParameterMap = std::unordered_map>; diff --git a/rclcpp/src/rclcpp/parameter_map.cpp b/rclcpp/src/rclcpp/parameter_map.cpp index 6f88f3cc4a..29da1f1c88 100644 --- a/rclcpp/src/rclcpp/parameter_map.cpp +++ b/rclcpp/src/rclcpp/parameter_map.cpp @@ -17,26 +17,11 @@ #include "rclcpp/parameter_map.hpp" -using rclcpp::InvalidParametersException; -using rclcpp::InvalidParameterValueException; +using rclcpp::exceptions::InvalidParametersException; +using rclcpp::exceptions::InvalidParameterValueException; using rclcpp::ParameterMap; using rclcpp::ParameterValue; -InvalidParametersException::InvalidParametersException(std::string message) -: msg_(message) -{ -} - -InvalidParametersException::~InvalidParametersException() -{ -} - -const char * -InvalidParametersException::what() const noexcept -{ - return msg_.c_str(); -} - ParameterMap rclcpp::parameter_map_from(const rcl_params_t * const c_params) { diff --git a/rclcpp/test/test_parameter_map.cpp b/rclcpp/test/test_parameter_map.cpp index ad74870476..1d56d93d9e 100644 --- a/rclcpp/test/test_parameter_map.cpp +++ b/rclcpp/test/test_parameter_map.cpp @@ -84,7 +84,7 @@ make_node_params(rcl_params_t * c_params, size_t node_idx, std::vectornum_nodes = 1; - EXPECT_THROW(rclcpp::parameter_map_from(c_params), rclcpp::InvalidParametersException); + EXPECT_THROW( + rclcpp::parameter_map_from(c_params), rclcpp::exceptions::InvalidParametersException); c_params->num_nodes = 0; rcl_yaml_node_struct_fini(c_params); @@ -105,7 +106,8 @@ TEST(Test_parameter_map_from, null_node_params) auto allocated_params = c_params->params; c_params->params = NULL; - EXPECT_THROW(rclcpp::parameter_map_from(c_params), rclcpp::InvalidParametersException); + EXPECT_THROW( + rclcpp::parameter_map_from(c_params), rclcpp::exceptions::InvalidParametersException); c_params->params = allocated_params; rcl_yaml_node_struct_fini(c_params); @@ -117,7 +119,8 @@ TEST(Test_parameter_map_from, null_node_name_in_node_names) auto allocated_name = c_params->node_names[0]; c_params->node_names[0] = NULL; - EXPECT_THROW(rclcpp::parameter_map_from(c_params), rclcpp::InvalidParametersException); + EXPECT_THROW( + rclcpp::parameter_map_from(c_params), rclcpp::exceptions::InvalidParametersException); c_params->node_names[0] = allocated_name; rcl_yaml_node_struct_fini(c_params); @@ -128,7 +131,8 @@ TEST(Test_parameter_map_from, null_node_param_value) rcl_params_t * c_params = make_params({"foo"}); make_node_params(c_params, 0, {"bar"}); - EXPECT_THROW(rclcpp::parameter_map_from(c_params), rclcpp::InvalidParameterValueException); + EXPECT_THROW( + rclcpp::parameter_map_from(c_params), rclcpp::exceptions::InvalidParameterValueException); rcl_yaml_node_struct_fini(c_params); } @@ -140,7 +144,8 @@ TEST(Test_parameter_map_from, null_node_param_name) auto allocated_name = c_params->params[0].parameter_names[0]; c_params->params[0].parameter_names[0] = NULL; - EXPECT_THROW(rclcpp::parameter_map_from(c_params), rclcpp::InvalidParametersException); + EXPECT_THROW( + rclcpp::parameter_map_from(c_params), rclcpp::exceptions::InvalidParametersException); c_params->params[0].parameter_names[0] = allocated_name; rcl_yaml_node_struct_fini(c_params);