diff --git a/rclcpp/include/rclcpp/parameter.hpp b/rclcpp/include/rclcpp/parameter.hpp index 8dc9ac6..afb7ce1 100644 --- a/rclcpp/include/rclcpp/parameter.hpp +++ b/rclcpp/include/rclcpp/parameter.hpp @@ -100,11 +100,6 @@ public: value_.parameter_type = rcl_interfaces::ParameterType::PARAMETER_BYTES; } - /* Templated getter */ - template - T - get_value() const; - inline ParameterType get_type() const {return static_cast(value_.parameter_type); } inline std::string get_name() const & {return name_; } @@ -114,57 +109,62 @@ public: return value_; } + template + typename std::enable_if::type + get_value() const + { + if (value_.parameter_type != rcl_interfaces::ParameterType::PARAMETER_INTEGER) { + // TODO: use custom exception + throw std::runtime_error("Invalid type"); + } + return value_.integer_value; + } + template + typename std::enable_if::type + get_value() const + { + if (value_.parameter_type != rcl_interfaces::ParameterType::PARAMETER_DOUBLE) { + // TODO: use custom exception + throw std::runtime_error("Invalid type"); + } + return value_.double_value; + } + template + typename std::enable_if::type + get_value() const + { + if (value_.parameter_type != rcl_interfaces::ParameterType::PARAMETER_STRING) { + // TODO: use custom exception + throw std::runtime_error("Invalid type"); + } + return value_.string_value; + } + template + typename std::enable_if::type + get_value() const + { + if (value_.parameter_type != rcl_interfaces::ParameterType::PARAMETER_BOOL) { + // TODO: use custom exception + throw std::runtime_error("Invalid type"); + } + return value_.bool_value; + } + template + typename std::enable_if>::type + get_value() const + { + if (value_.parameter_type != rcl_interfaces::ParameterType::PARAMETER_BYTES) { + // TODO: use custom exception + throw std::runtime_error("Invalid type"); + } + return value_.bytes_value; + } + private: std::string name_; rcl_interfaces::ParameterValue value_; }; -template<> -inline int64_t ParameterVariant::get_value() const -{ - if (value_.parameter_type != rcl_interfaces::ParameterType::PARAMETER_INTEGER) { - // TODO: use custom exception - throw std::runtime_error("Invalid type"); - } - return value_.integer_value; -} -template<> -inline double ParameterVariant::get_value() const -{ - if (value_.parameter_type != rcl_interfaces::ParameterType::PARAMETER_DOUBLE) { - // TODO: use custom exception - throw std::runtime_error("Invalid type"); - } - return value_.double_value; -} -template<> -inline std::string ParameterVariant::get_value() const -{ - if (value_.parameter_type != rcl_interfaces::ParameterType::PARAMETER_STRING) { - // TODO: use custom exception - throw std::runtime_error("Invalid type"); - } - return value_.string_value; -} -template<> -inline bool ParameterVariant::get_value() const -{ - if (value_.parameter_type != rcl_interfaces::ParameterType::PARAMETER_BOOL) { - // TODO: use custom exception - throw std::runtime_error("Invalid type"); - } - return value_.bool_value; -} -template<> -inline std::vector ParameterVariant::get_value() const -{ - if (value_.parameter_type != rcl_interfaces::ParameterType::PARAMETER_BYTES) { - // TODO: use custom exception - throw std::runtime_error("Invalid type"); - } - return value_.bytes_value; -} - class AsyncParametersClient {