Use enum value to return appropriate values for get_value<>

This commit is contained in:
Esteve Fernandez 2015-05-04 14:34:38 -07:00
parent db0e117c72
commit d82aa219d3

View file

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