7#include <capabilities2_msgs/msg/capability.hpp>
8#include <capabilities2_msgs/msg/capability_parameter.hpp>
15 using std::runtime_error::runtime_error;
21 virtual const char*
what() const noexcept
override
23 return std::runtime_error::what();
54 Parameter(
const capabilities2_msgs::msg::CapabilityParameter& msg)
66 return value[0] ==
"true";
68 return std::stod(
value[0]);
70 return std::stoi(
value[0]);
74 std::vector<bool> vec;
75 for (
const auto& v :
value)
76 vec.push_back(v ==
"true");
80 std::vector<double> vec;
81 for (
const auto& v :
value)
82 vec.push_back(std::stod(v));
87 for (
const auto& v :
value)
88 vec.push_back(std::stoi(v));
105 value.push_back(std::any_cast<bool>(new_value) ?
"true" :
"false");
109 value.push_back(std::to_string(std::any_cast<double>(new_value)));
113 value.push_back(std::to_string(std::any_cast<int>(new_value)));
117 value.push_back(std::any_cast<std::string>(new_value));
120 const auto& vec = std::any_cast<std::vector<bool>>(new_value);
122 for (
const auto& v : vec)
123 value.push_back(v ?
"true" :
"false");
127 const auto& vec = std::any_cast<std::vector<double>>(new_value);
129 for (
const auto& v : vec)
130 value.push_back(std::to_string(v));
134 const auto& vec = std::any_cast<std::vector<int>>(new_value);
136 for (
const auto& v : vec)
137 value.push_back(std::to_string(v));
141 value = std::any_cast<std::vector<std::string>>(new_value);
149 capabilities2_msgs::msg::CapabilityParameter
toMsg()
const
151 capabilities2_msgs::msg::CapabilityParameter msg;
154 msg.type =
static_cast<int>(
type);
175 for (
const auto& option_msg : msg.parameters)
195 for (
const auto& option :
options)
196 if (option.key == key)
208 std::any
get_value(
const std::string& key, std::any default_value)
213 if (option.key == key)
217 return option.get_value();
219 catch (
const std::exception& e)
221 throw options_exception(
"Failed to convert option '" + option.key +
"': " + e.what());
226 return default_value;
240 if (option.key == key)
244 option.set_value(key, value, type);
247 catch (
const std::exception& e)
249 throw options_exception(
"Failed to set option '" + option.key +
"': " + e.what());
259 capabilities2_msgs::msg::Capability
toMsg()
const
261 capabilities2_msgs::msg::Capability msg;
262 for (
const auto& option :
options)
263 msg.parameters.push_back(option.toMsg());
Definition event_base.hpp:14
OptionType
Definition event_parameters.hpp:28
capability options for a capability runner given by interface and provider
Definition event_parameters.hpp:167
std::any get_value(const std::string &key, std::any default_value)
Get the value of an option by key.
Definition event_parameters.hpp:208
EventParameters(const capabilities2_msgs::msg::Capability &msg)
Definition event_parameters.hpp:172
EventParameters()=default
void set_value(const std::string &key, const std::any &value, const OptionType &type)
Set the value of an option by key, if the option does not exist it will be created.
Definition event_parameters.hpp:237
bool is_empty() const
Definition event_parameters.hpp:182
std::vector< Parameter > options
Definition event_parameters.hpp:168
bool has_value(const std::string &key) const
Check if an option with the given key exists.
Definition event_parameters.hpp:193
capabilities2_msgs::msg::Capability toMsg() const
Definition event_parameters.hpp:259
Key value pair for capability options.
Definition event_parameters.hpp:47
Parameter(const capabilities2_msgs::msg::CapabilityParameter &msg)
Definition event_parameters.hpp:54
std::any get_value()
Definition event_parameters.hpp:61
capabilities2_msgs::msg::CapabilityParameter toMsg() const
Definition event_parameters.hpp:149
std::string key
Definition event_parameters.hpp:48
std::vector< std::string > value
Definition event_parameters.hpp:49
OptionType type
Definition event_parameters.hpp:50
void set_value(std::string new_key, std::any new_value, OptionType new_type)
Definition event_parameters.hpp:96
Definition event_parameters.hpp:14
virtual const char * what() const noexcept override
Definition event_parameters.hpp:21
options_exception(const std::string &what)
Definition event_parameters.hpp:17