Merge pull request #42 from ros2/refactor_examples_and_interfaces
added get_type_name and to_string to ParameterVariant
This commit is contained in:
commit
cab273ccc0
1 changed files with 64 additions and 0 deletions
|
@ -15,6 +15,8 @@
|
||||||
#ifndef RCLCPP_RCLCPP_PARAMETER_HPP_
|
#ifndef RCLCPP_RCLCPP_PARAMETER_HPP_
|
||||||
#define RCLCPP_RCLCPP_PARAMETER_HPP_
|
#define RCLCPP_RCLCPP_PARAMETER_HPP_
|
||||||
|
|
||||||
|
#include <ostream>
|
||||||
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include <rmw/rmw.h>
|
#include <rmw/rmw.h>
|
||||||
|
@ -93,6 +95,27 @@ public:
|
||||||
|
|
||||||
inline ParameterType get_type() const {return static_cast<ParameterType>(value_.type); }
|
inline ParameterType get_type() const {return static_cast<ParameterType>(value_.type); }
|
||||||
|
|
||||||
|
inline std::string get_type_name() const
|
||||||
|
{
|
||||||
|
switch (get_type()) {
|
||||||
|
case rclcpp::parameter::ParameterType::PARAMETER_BOOL:
|
||||||
|
return "bool";
|
||||||
|
case rclcpp::parameter::ParameterType::PARAMETER_INTEGER:
|
||||||
|
return "integer";
|
||||||
|
case rclcpp::parameter::ParameterType::PARAMETER_DOUBLE:
|
||||||
|
return "double";
|
||||||
|
case rclcpp::parameter::ParameterType::PARAMETER_STRING:
|
||||||
|
return "string";
|
||||||
|
case rclcpp::parameter::ParameterType::PARAMETER_BYTES:
|
||||||
|
return "bytes";
|
||||||
|
case rclcpp::parameter::ParameterType::PARAMETER_NOT_SET:
|
||||||
|
return "not set";
|
||||||
|
default:
|
||||||
|
throw std::runtime_error(
|
||||||
|
"Unexpected type from ParameterVariant: " + std::to_string(get_type()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
inline std::string get_name() const & {return name_; }
|
inline std::string get_name() const & {return name_; }
|
||||||
|
|
||||||
inline rcl_interfaces::msg::ParameterValue get_parameter_value() const
|
inline rcl_interfaces::msg::ParameterValue get_parameter_value() const
|
||||||
|
@ -193,11 +216,52 @@ public:
|
||||||
return parameter;
|
return parameter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string to_string() const
|
||||||
|
{
|
||||||
|
switch (get_type()) {
|
||||||
|
case rclcpp::parameter::ParameterType::PARAMETER_BOOL:
|
||||||
|
return as_bool() ? "true" : "false";
|
||||||
|
case rclcpp::parameter::ParameterType::PARAMETER_INTEGER:
|
||||||
|
return std::to_string(as_int());
|
||||||
|
case rclcpp::parameter::ParameterType::PARAMETER_DOUBLE:
|
||||||
|
return std::to_string(as_double());
|
||||||
|
case rclcpp::parameter::ParameterType::PARAMETER_STRING:
|
||||||
|
return as_string();
|
||||||
|
case rclcpp::parameter::ParameterType::PARAMETER_BYTES:
|
||||||
|
{
|
||||||
|
std::stringstream bytes;
|
||||||
|
bool first_byte = true;
|
||||||
|
bytes << "[" << std::hex;
|
||||||
|
for (auto & byte : as_bytes())
|
||||||
|
{
|
||||||
|
bytes << "0x" << byte;
|
||||||
|
if (!first_byte) {
|
||||||
|
bytes << ", ";
|
||||||
|
} else {
|
||||||
|
first_byte = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bytes.str();
|
||||||
|
}
|
||||||
|
case rclcpp::parameter::ParameterType::PARAMETER_NOT_SET:
|
||||||
|
return "not set";
|
||||||
|
default:
|
||||||
|
throw std::runtime_error(
|
||||||
|
"Unexpected type from ParameterVariant: " + std::to_string(get_type()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string name_;
|
std::string name_;
|
||||||
rcl_interfaces::msg::ParameterValue value_;
|
rcl_interfaces::msg::ParameterValue value_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::ostream & operator<<(std::ostream & os, const ParameterVariant & pv)
|
||||||
|
{
|
||||||
|
os << pv.to_string();
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
} /* namespace parameter */
|
} /* namespace parameter */
|
||||||
|
|
||||||
} /* namespace rclcpp */
|
} /* namespace rclcpp */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue